2020logger = logging .getLogger (name = "biothings-cli" )
2121
2222
23- def operation_mode (operation_method : Callable ):
23+ def operation_mode (operation : Callable ):
2424 """
2525 Based off the directory structure for where the biothings-cli
2626 was invoked we set the "mode" to one of two states:
@@ -47,64 +47,90 @@ def operation_mode(operation_method: Callable):
4747 the mode is hub
4848 """
4949
50- @functools .wraps (operation_method )
51- async def determine_operation_mode (* args , ** kwargs ):
52- working_directory = pathlib .Path .cwd ()
53- working_directory_files = {file .name for file in working_directory .iterdir ()}
54-
55- mode = None
56- if "manifest.json" in working_directory_files or "manifest.yaml" in working_directory_files :
57- logger .debug ("Inferring singular manifest plugin from directory structure" )
58- mode = "SINGULAR"
59- elif "__init__.py" in working_directory_files :
60- logger .debug ("Inferring singular advanced plugin from directory structure" )
61- mode = "SINGULAR"
50+ @functools .wraps (operation )
51+ def determine_operation_mode (* args , ** kwargs ):
52+
53+ def determine_hub_mode ():
54+ working_directory = pathlib .Path .cwd ()
55+ working_directory_files = {file .name for file in working_directory .iterdir ()}
56+
57+ mode = None
58+ if "manifest.json" in working_directory_files or "manifest.yaml" in working_directory_files :
59+ logger .debug ("Inferring singular manifest plugin from directory structure" )
60+ mode = "SINGULAR"
61+ elif "__init__.py" in working_directory_files :
62+ logger .debug ("Inferring singular advanced plugin from directory structure" )
63+ mode = "SINGULAR"
64+ else :
65+ logger .debug ("Inferring multiple plugins from directory structure" )
66+ mode = "HUB"
67+
68+ if mode == "SINGULAR" :
69+ if kwargs .get ("plugin_name" , None ) is not None :
70+ kwargs ["plugin_name" ] = None
71+ elif mode == "HUB" :
72+ if kwargs .get ("plugin_name" , None ) is None :
73+ raise MissingPluginName (working_directory )
74+
75+ @functools .wraps (operation )
76+ def handle_function (* args , ** kwargs ):
77+ operation_result = operation (* args , ** kwargs )
78+ return operation_result
79+
80+ @functools .wraps (operation )
81+ async def handle_corountine (* args , ** kwargs ):
82+ operation_result = await operation (* args , ** kwargs )
83+ return operation_result
84+
85+ determine_hub_mode ()
86+
87+ if inspect .iscoroutinefunction (operation ):
88+ return handle_corountine (* args , ** kwargs )
6289 else :
63- logger .debug ("Inferring multiple plugins from directory structure" )
64- mode = "HUB"
65-
66- if mode == "SINGULAR" :
67- if kwargs .get ("plugin_name" , None ) is not None :
68- kwargs ["plugin_name" ] = None
69- elif mode == "HUB" :
70- if kwargs .get ("plugin_name" , None ) is None :
71- raise MissingPluginName (working_directory )
72-
73- if inspect .iscoroutinefunction (operation_method ):
74- operation_result = await operation_method (* args , ** kwargs )
75- else :
76- operation_result = operation_method (* args , ** kwargs )
77- return operation_result
90+ return handle_function (* args , ** kwargs )
7891
7992 return determine_operation_mode
8093
8194
82- def cli_system_path (func : Callable ): # pylint: disable=unused-argument
95+ def cli_system_path (operation : Callable ): # pylint: disable=unused-argument
8396 """
8497 Used for ensuring that if we've appended files to biothings-cli
8598 path file (stored under config.BIOTHINGS_CLI_PATH), then we need to update
8699 the system path so we can discover the modules at runtime
87100 """
88101
89- @functools .wraps (func )
90- async def update_system_path (* args , ** kwargs ):
91- from biothings import config
102+ @functools .wraps (operation )
103+ def update_system_path (* args , ** kwargs ):
104+
105+ def update_system_path_from_file ():
106+ from biothings import config
107+
108+ discovery_path = pathlib .Path (config .BIOTHINGS_CLI_PATH ).resolve ().absolute ()
109+ path_file = discovery_path .joinpath (".biothings_cli.pth" )
110+
111+ if path_file .exists ():
112+ with open (path_file , "r" , encoding = "utf-8" ) as handle :
113+ path_entries = handle .readlines ()
114+ path_entries = [entry .strip ("\n " ) for entry in path_entries ]
115+ sys .path .extend (path_entries )
116+ for path in path_entries :
117+ logger .debug ("Adding %s to system path" , path )
118+
119+ @functools .wraps (operation )
120+ def handle_function (* args , ** kwargs ):
121+ operation_result = operation (* args , ** kwargs )
122+ return operation_result
92123
93- discovery_path = pathlib .Path (config .BIOTHINGS_CLI_PATH ).resolve ().absolute ()
94- path_file = discovery_path .joinpath (".biothings_cli.pth" )
124+ @functools .wraps (operation )
125+ async def handle_corountine (* args , ** kwargs ):
126+ operation_result = await operation (* args , ** kwargs )
127+ return operation_result
95128
96- if path_file .exists ():
97- with open (path_file , "r" , encoding = "utf-8" ) as handle :
98- path_entries = handle .readlines ()
99- path_entries = [entry .strip ("\n " ) for entry in path_entries ]
100- sys .path .extend (path_entries )
101- for path in path_entries :
102- logger .debug ("Adding %s to system path" , path )
129+ update_system_path_from_file ()
103130
104- if inspect .iscoroutinefunction (func ):
105- func_result = await func (* args , ** kwargs )
131+ if inspect .iscoroutinefunction (operation ):
132+ return handle_corountine (* args , ** kwargs )
106133 else :
107- func_result = func (* args , ** kwargs )
108- return func_result
134+ return handle_function (* args , ** kwargs )
109135
110136 return update_system_path
0 commit comments