5656import shutil
5757import sys
5858import uuid
59+ from importlib import import_module
5960from typing import Optional , Union
6061
6162import jsonschema
6667from rich .console import Console
6768from rich .panel import Panel
6869
69- from biothings .cli .commands .decorators import cli_system_path , operation_mode
70- from biothings .cli .structure import TEMPLATE_DIRECTORY
70+ from biothings .cli .commands .decorators import cli_system_path , get_biothings_config , operation_mode
7171from biothings .cli .exceptions import UnknownUploaderSource
72+ from biothings .cli .structure import TEMPLATE_DIRECTORY
7273from biothings .cli .utils import (
7374 clean_dumped_files ,
7475 clean_uploaded_sources ,
8788logger = logging .getLogger (name = "biothings-cli" )
8889
8990
91+ def _load_attr (module_path : str , attr_name : str ):
92+ return getattr (import_module (module_path ), attr_name )
93+
94+
9095# do not apply operation_mode decorator since this operation means to create a new plugin
9196# regardless what the current working directory has
9297def do_create (plugin_name : str , multi_uploaders : bool = False , parallelizer : bool = False ):
@@ -127,9 +132,9 @@ async def do_dump(plugin_name: Optional[str] = None, show_dumped: bool = True, m
127132 """
128133 Perform dump for the given plugin
129134 """
130- from biothings import config
131- from biothings .cli .assistant import CLIAssistant
132- from biothings .utils import hub_db
135+ config = get_biothings_config ()
136+ CLIAssistant = _load_attr ( " biothings.cli.assistant" , " CLIAssistant" )
137+ hub_db = import_module ( " biothings.utils. hub_db" )
133138
134139 hub_db .setup (config )
135140 assistant_instance = CLIAssistant (plugin_name )
@@ -184,8 +189,7 @@ async def do_upload(plugin_name: Optional[str] = None, batch_limit: int = 10000,
184189 >>> self.commands["upload_all"] = self.managers["upload_manager"].upload_all
185190 >>> self.commands["update_source_meta"] = self.managers["upload_manager"].update_source_meta
186191 """
187- from biothings .cli .assistant import CLIAssistant
188-
192+ CLIAssistant = _load_attr ("biothings.cli.assistant" , "CLIAssistant" )
189193 assistant_instance = CLIAssistant (plugin_name )
190194 uploader_classes = assistant_instance .get_uploader_class ()
191195 for uploader_class in uploader_classes :
@@ -243,8 +247,7 @@ async def do_parallel_upload(
243247
244248 This is a modified version of the ParallelUploader `update_data` source call
245249 """
246- from biothings .cli .assistant import CLIAssistant
247-
250+ CLIAssistant = _load_attr ("biothings.cli.assistant" , "CLIAssistant" )
248251 assistant_instance = CLIAssistant (plugin_name )
249252 uploader_classes = assistant_instance .get_uploader_class ()
250253 for uploader_class in uploader_classes :
@@ -368,10 +371,10 @@ async def do_index(plugin_name: Optional[str] = None, sub_source_name: Optional[
368371 The default location is localhost:9200. If successful a couple frames detailing the build and
369372 index information will be displayed to the enduser
370373 """
371- from biothings import config
372- from biothings .cli .assistant import CLIAssistant
373- from biothings .hub .databuild .builder import BuilderException
374- from biothings .utils .manager import JobManager
374+ config = get_biothings_config ()
375+ CLIAssistant = _load_attr ( " biothings.cli.assistant" , " CLIAssistant" )
376+ BuilderException = _load_attr ( " biothings.hub.databuild.builder" , " BuilderException" )
377+ JobManager = _load_attr ( " biothings.utils.manager" , " JobManager" )
375378
376379 if platform .system () == "Windows" :
377380 logger .warning ("The `biothings-cli dataplugin index` command isn't supported on windows" )
@@ -500,8 +503,7 @@ async def do_list(
500503 """
501504 List the dumped files, uploaded sources, or hubdb content.
502505 """
503- from biothings .cli .assistant import CLIAssistant
504-
506+ CLIAssistant = _load_attr ("biothings.cli.assistant" , "CLIAssistant" )
505507 assistant_instance = CLIAssistant (plugin_name )
506508 if dump :
507509 dumper_instance = assistant_instance .get_dumper_class ()
@@ -524,7 +526,7 @@ async def do_list(
524526
525527@cli_system_path
526528@operation_mode
527- async def do_inspect (
529+ async def do_inspect ( # pylint: disable=too-many-arguments,too-many-positional-arguments
528530 plugin_name : Optional [str ] = None ,
529531 sub_source_name : Optional [str ] = None ,
530532 mode : str = "type,stats" ,
@@ -535,8 +537,7 @@ async def do_inspect(
535537 """
536538 Perform inspection on a data plugin.
537539 """
538- from biothings .cli .assistant import CLIAssistant
539-
540+ CLIAssistant = _load_attr ("biothings.cli.assistant" , "CLIAssistant" )
540541 assistant_instance = CLIAssistant (plugin_name )
541542 uploader_classes = assistant_instance .get_uploader_class ()
542543
@@ -593,9 +594,9 @@ async def do_serve(plugin_name: Optional[str] = None, host: str = "localhost", p
593594 """
594595 Handles creation of a basic web server for hosting files using for a dataplugin
595596 """
596- from biothings .cli .assistant import CLIAssistant
597- from biothings .cli .web_app import main
598- from biothings .utils import hub_db
597+ CLIAssistant = _load_attr ( " biothings.cli.assistant" , " CLIAssistant" )
598+ main = _load_attr ( " biothings.cli.web_app" , " main" )
599+ hub_db = import_module ( " biothings.utils. hub_db" )
599600
600601 assistant_instance = CLIAssistant (plugin_name )
601602 uploader_classes = assistant_instance .get_uploader_class ()
@@ -614,8 +615,7 @@ async def do_clean(
614615 """
615616 Clean the dumped files, uploaded sources, or both.
616617 """
617- from biothings .cli .assistant import CLIAssistant
618-
618+ CLIAssistant = _load_attr ("biothings.cli.assistant" , "CLIAssistant" )
619619 if clean_all :
620620 dump = True
621621 upload = True
@@ -644,8 +644,7 @@ async def display_schema():
644644 Loads the jsonschema definition file and displays it to the
645645 console
646646 """
647- from biothings .hub .dataplugin .loaders .schema import load_manifest_schema
648-
647+ load_manifest_schema = _load_attr ("biothings.hub.dataplugin.loaders.schema" , "load_manifest_schema" )
649648 manifest_schema = load_manifest_schema ()
650649 schema_validator = jsonschema .validators .validator_for (manifest_schema )
651650 valid_schema = False
@@ -677,8 +676,7 @@ async def validate_manifest(plugin_name: Optional[str] = None):
677676 Loads the manifest file and validates it against the schema file
678677 If an error exists it will display the error to the enduser
679678 """
680- from biothings .hub .dataplugin .loaders .loader import ManifestBasedPluginLoader
681-
679+ ManifestBasedPluginLoader = _load_attr ("biothings.hub.dataplugin.loaders.loader" , "ManifestBasedPluginLoader" )
682680 if plugin_name is None :
683681 plugin_directory = pathlib .Path .cwd ().resolve ().absolute ()
684682 plugin_name = plugin_directory .name
0 commit comments