Break up importing logic in methods
This commit is contained in:
parent
fdf3b2e764
commit
c00e2aef19
@ -64,12 +64,23 @@ def get_ref_type(
|
|||||||
imports.add(f"import {string_import} as {string_alias}")
|
imports.add(f"import {string_import} as {string_alias}")
|
||||||
return f"{string_alias}.{py_type}"
|
return f"{string_alias}.{py_type}"
|
||||||
|
|
||||||
|
current_package: List[str] = package.split(".") if package else []
|
||||||
py_package: List[str] = source_package.split(".") if source_package else []
|
py_package: List[str] = source_package.split(".") if source_package else []
|
||||||
py_type: str = pythonize_class_name(source_type)
|
py_type: str = pythonize_class_name(source_type)
|
||||||
|
|
||||||
current_package: List[str] = package.split(".") if package else []
|
if py_package == current_package:
|
||||||
|
return import_sibling(py_type)
|
||||||
|
|
||||||
# importing sibling
|
if py_package[0 : len(current_package)] == current_package:
|
||||||
|
return import_descendent(current_package, imports, py_package, py_type)
|
||||||
|
|
||||||
|
if current_package[0 : len(py_package)] == py_package:
|
||||||
|
return import_ancestor(current_package, imports, py_package, py_type)
|
||||||
|
|
||||||
|
return import_cousin(current_package, imports, py_package, py_type)
|
||||||
|
|
||||||
|
|
||||||
|
def import_sibling(py_type):
|
||||||
"""
|
"""
|
||||||
package =
|
package =
|
||||||
name = Foo
|
name = Foo
|
||||||
@ -80,10 +91,10 @@ def get_ref_type(
|
|||||||
package = foo.bar
|
package = foo.bar
|
||||||
name = foo.bar.Baz
|
name = foo.bar.Baz
|
||||||
"""
|
"""
|
||||||
if py_package == current_package:
|
|
||||||
return f'"{py_type}"'
|
return f'"{py_type}"'
|
||||||
|
|
||||||
# importing child & descendent:
|
|
||||||
|
def import_descendent(current_package, imports, py_package, py_type):
|
||||||
"""
|
"""
|
||||||
package =
|
package =
|
||||||
name = foo.Bar
|
name = foo.Bar
|
||||||
@ -91,11 +102,9 @@ def get_ref_type(
|
|||||||
package =
|
package =
|
||||||
name = foo.bar.Baz
|
name = foo.bar.Baz
|
||||||
"""
|
"""
|
||||||
if py_package[0 : len(current_package)] == current_package:
|
|
||||||
importing_descendent = py_package[len(current_package) :]
|
importing_descendent = py_package[len(current_package) :]
|
||||||
string_from = ".".join(importing_descendent[0:-1])
|
string_from = ".".join(importing_descendent[0:-1])
|
||||||
string_import = importing_descendent[-1]
|
string_import = importing_descendent[-1]
|
||||||
|
|
||||||
if string_from:
|
if string_from:
|
||||||
string_alias = "_".join(importing_descendent)
|
string_alias = "_".join(importing_descendent)
|
||||||
imports.add(f"from .{string_from} import {string_import} as {string_alias}")
|
imports.add(f"from .{string_from} import {string_import} as {string_alias}")
|
||||||
@ -104,7 +113,8 @@ def get_ref_type(
|
|||||||
imports.add(f"from . import {string_import}")
|
imports.add(f"from . import {string_import}")
|
||||||
return f"{string_import}.{py_type}"
|
return f"{string_import}.{py_type}"
|
||||||
|
|
||||||
# importing parent & ancestor
|
|
||||||
|
def import_ancestor(current_package, imports, py_package, py_type):
|
||||||
"""
|
"""
|
||||||
package = foo.bar
|
package = foo.bar
|
||||||
name = foo.Foo
|
name = foo.Foo
|
||||||
@ -115,12 +125,12 @@ def get_ref_type(
|
|||||||
package = foo.bar.baz
|
package = foo.bar.baz
|
||||||
name = Bar
|
name = Bar
|
||||||
"""
|
"""
|
||||||
if current_package[0 : len(py_package)] == py_package:
|
|
||||||
distance_up = len(current_package) - len(py_package)
|
distance_up = len(current_package) - len(py_package)
|
||||||
imports.add(f"from .{'.' * distance_up} import {py_type}")
|
imports.add(f"from .{'.' * distance_up} import {py_type}")
|
||||||
return py_type
|
return py_type
|
||||||
|
|
||||||
# importing unrelated or cousin
|
|
||||||
|
def import_cousin(current_package, imports, py_package, py_type):
|
||||||
"""
|
"""
|
||||||
package = foo.bar
|
package = foo.bar
|
||||||
name = baz.Foo
|
name = baz.Foo
|
||||||
|
Loading…
x
Reference in New Issue
Block a user