added in ability for admins to import in code (#523)
Thanks for the contribution 👍 . I'm sure that many users of CVAT will benefit from the enhanced version of "automatic annotation".
main
parent
a82546210c
commit
418cdbe146
@ -0,0 +1,36 @@
|
|||||||
|
import ast
|
||||||
|
from collections import namedtuple
|
||||||
|
import importlib
|
||||||
|
|
||||||
|
Import = namedtuple("Import", ["module", "name", "alias"])
|
||||||
|
|
||||||
|
def import_modules(source_code: str):
|
||||||
|
results = {}
|
||||||
|
imports = parse_imports(source_code)
|
||||||
|
for import_ in imports:
|
||||||
|
module = import_.module if import_.module else import_.name
|
||||||
|
loaded_module = importlib.import_module(module)
|
||||||
|
|
||||||
|
if not import_.name == module:
|
||||||
|
loaded_module = getattr(loaded_module, import_.name)
|
||||||
|
|
||||||
|
if import_.alias:
|
||||||
|
results[import_.alias] = loaded_module
|
||||||
|
else:
|
||||||
|
results[import_.name] = loaded_module
|
||||||
|
|
||||||
|
return results
|
||||||
|
|
||||||
|
def parse_imports(source_code: str):
|
||||||
|
root = ast.parse(source_code)
|
||||||
|
|
||||||
|
for node in ast.iter_child_nodes(root):
|
||||||
|
if isinstance(node, ast.Import):
|
||||||
|
module = []
|
||||||
|
elif isinstance(node, ast.ImportFrom):
|
||||||
|
module = node.module
|
||||||
|
else:
|
||||||
|
continue
|
||||||
|
|
||||||
|
for n in node.names:
|
||||||
|
yield Import(module, n.name, n.asname)
|
||||||
Loading…
Reference in New Issue