Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
assert static.modpath_to_modname(root, hide_main=False, hide_init=True) == '_tmproot'
assert static.modpath_to_modname(sub1, hide_main=False, hide_init=True) == '_tmproot.sub1'
assert static.modpath_to_modname(sub2, hide_main=False, hide_init=True) == '_tmproot.sub1.sub2'
assert static.modpath_to_modname(root_main, hide_main=False, hide_init=True) == '_tmproot.__main__'
assert static.modpath_to_modname(sub2_main, hide_main=False, hide_init=True) == '_tmproot.sub1.sub2.__main__'
assert static.modpath_to_modname(root_main, hide_main=False, hide_init=True) == '_tmproot.__main__'
assert static.modpath_to_modname(sub2_main, hide_main=False, hide_init=True) == '_tmproot.sub1.sub2.__main__'
assert static.modpath_to_modname(root_main, hide_main=True, hide_init=True) == '_tmproot'
assert static.modpath_to_modname(sub2_main, hide_main=True, hide_init=True) == '_tmproot.sub1.sub2'
assert static.modpath_to_modname(root_main, hide_main=True, hide_init=False) == '_tmproot'
assert static.modpath_to_modname(sub2_main, hide_main=True, hide_init=False) == '_tmproot.sub1.sub2'
# Non-existant / invalid modules should always be None
for a, b in it.product([True, False], [True, False]):
with pytest.raises(ValueError):
static.modpath_to_modname(join(sub1, '__main__.py'), hide_main=a, hide_init=b)
assert static.modpath_to_modname(b0, hide_main=a, hide_init=b) == 'b0'
assert static.modpath_to_modname(b1, hide_main=a, hide_init=b) == 'b1'
with pytest.raises(ValueError):
static.modpath_to_modname(bad1, hide_main=a, hide_init=b)
with pytest.raises(ValueError):
static.modpath_to_modname(bad2, hide_main=a, hide_init=b)
assert '_tmproot' not in sys.modules
assert '_tmproot.mod0' not in sys.modules
assert '_tmproot.sub1' not in sys.modules
assert '_tmproot.sub1.mod1' not in sys.modules
with utils.PythonPathContext(dpath):
assert static.modpath_to_modname(root) == '_tmproot'
assert static.modpath_to_modname(sub1) == '_tmproot.sub1'
assert static.modpath_to_modname(sub2) == '_tmproot.sub1.sub2'
assert static.modpath_to_modname(mod0) == '_tmproot.mod0'
assert static.modpath_to_modname(mod1) == '_tmproot.sub1.mod1'
assert static.modpath_to_modname(mod2) == '_tmproot.sub1.sub2.mod2'
assert static.modpath_to_modname(root_init) == '_tmproot'
assert static.modpath_to_modname(sub1_init) == '_tmproot.sub1'
assert static.modpath_to_modname(sub2_init) == '_tmproot.sub1.sub2'
assert static.modpath_to_modname(root_init, hide_init=False) == '_tmproot.__init__'
assert static.modpath_to_modname(sub1_init, hide_init=False) == '_tmproot.sub1.__init__'
assert static.modpath_to_modname(sub2_init, hide_init=False) == '_tmproot.sub1.sub2.__init__'
assert static.modpath_to_modname(root, hide_main=True, hide_init=False) == '_tmproot.__init__'
assert static.modpath_to_modname(sub1, hide_main=True, hide_init=False) == '_tmproot.sub1.__init__'
assert static.modpath_to_modname(sub2, hide_main=True, hide_init=False) == '_tmproot.sub1.sub2.__init__'
assert static.modpath_to_modname(root, hide_main=False, hide_init=False) == '_tmproot.__init__'
assert static.modpath_to_modname(sub1, hide_main=False, hide_init=False) == '_tmproot.sub1.__init__'
assert static.modpath_to_modname(sub2, hide_main=False, hide_init=False) == '_tmproot.sub1.sub2.__init__'
assert static.modpath_to_modname(root, hide_main=False, hide_init=True) == '_tmproot'
assert static.modpath_to_modname(sub1, hide_main=False, hide_init=True) == '_tmproot.sub1'
assert static.modpath_to_modname(sub2, hide_main=False, hide_init=True) == '_tmproot.sub1.sub2'
assert static.modpath_to_modname(root_main, hide_main=False, hide_init=True) == '_tmproot.__main__'
assert static.modpath_to_modname(sub2_main, hide_main=False, hide_init=True) == '_tmproot.sub1.sub2.__main__'
assert static.modpath_to_modname(root_main, hide_main=False, hide_init=True) == '_tmproot.__main__'
assert static.modpath_to_modname(sub2_main, hide_main=False, hide_init=True) == '_tmproot.sub1.sub2.__main__'
assert static.modpath_to_modname(root_main, hide_main=True, hide_init=True) == '_tmproot'
assert static.modpath_to_modname(sub2_main, hide_main=True, hide_init=True) == '_tmproot.sub1.sub2'
assert static.modpath_to_modname(root_main, hide_main=True, hide_init=False) == '_tmproot'
assert static.modpath_to_modname(sub2_main, hide_main=True, hide_init=False) == '_tmproot.sub1.sub2'
# Non-existant / invalid modules should always be None
for a, b in it.product([True, False], [True, False]):
with pytest.raises(ValueError):
static.modpath_to_modname(join(sub1, '__main__.py'), hide_main=a, hide_init=b)
assert static.modpath_to_modname(b0, hide_main=a, hide_init=b) == 'b0'
assert static.modpath_to_modname(b1, hide_main=a, hide_init=b) == 'b1'
with pytest.raises(ValueError):
static.modpath_to_modname(bad1, hide_main=a, hide_init=b)
with pytest.raises(ValueError):
static.modpath_to_modname(bad2, hide_main=a, hide_init=b)
assert '_tmproot' not in sys.modules
assert '_tmproot.mod0' not in sys.modules
assert '_tmproot.sub1' not in sys.modules
assert '_tmproot.sub1.mod1' not in sys.modules
assert '_tmproot.sub1.sub2' not in sys.modules
assert '_tmproot.sub1.mod2.mod2' not in sys.modules
root_main = touch((root, '__main__.py'))
sub2_main = touch((sub2, '__main__.py'))
bad1 = utils.ensuredir((root, 'bad1'))
bad2 = utils.ensuredir((sub1, 'bad2'))
b0 = touch((bad1, 'b0.py'))
b1 = touch((bad2, 'b1.py'))
with utils.PythonPathContext(dpath):
assert static.modpath_to_modname(root) == '_tmproot'
assert static.modpath_to_modname(sub1) == '_tmproot.sub1'
assert static.modpath_to_modname(sub2) == '_tmproot.sub1.sub2'
assert static.modpath_to_modname(mod0) == '_tmproot.mod0'
assert static.modpath_to_modname(mod1) == '_tmproot.sub1.mod1'
assert static.modpath_to_modname(mod2) == '_tmproot.sub1.sub2.mod2'
assert static.modpath_to_modname(root_init) == '_tmproot'
assert static.modpath_to_modname(sub1_init) == '_tmproot.sub1'
assert static.modpath_to_modname(sub2_init) == '_tmproot.sub1.sub2'
assert static.modpath_to_modname(root_init, hide_init=False) == '_tmproot.__init__'
assert static.modpath_to_modname(sub1_init, hide_init=False) == '_tmproot.sub1.__init__'
assert static.modpath_to_modname(sub2_init, hide_init=False) == '_tmproot.sub1.sub2.__init__'
assert static.modpath_to_modname(root, hide_main=True, hide_init=False) == '_tmproot.__init__'
assert static.modpath_to_modname(sub1, hide_main=True, hide_init=False) == '_tmproot.sub1.__init__'
assert static.modpath_to_modname(sub2, hide_main=True, hide_init=False) == '_tmproot.sub1.sub2.__init__'
assert static.modpath_to_modname(root, hide_main=False, hide_init=False) == '_tmproot.__init__'
assert static.modpath_to_modname(sub1, hide_main=False, hide_init=False) == '_tmproot.sub1.__init__'
assert static.modpath_to_modname(sub2_main, hide_main=False, hide_init=True) == '_tmproot.sub1.sub2.__main__'
assert static.modpath_to_modname(root_main, hide_main=True, hide_init=True) == '_tmproot'
assert static.modpath_to_modname(sub2_main, hide_main=True, hide_init=True) == '_tmproot.sub1.sub2'
assert static.modpath_to_modname(root_main, hide_main=True, hide_init=False) == '_tmproot'
assert static.modpath_to_modname(sub2_main, hide_main=True, hide_init=False) == '_tmproot.sub1.sub2'
# Non-existant / invalid modules should always be None
for a, b in it.product([True, False], [True, False]):
with pytest.raises(ValueError):
static.modpath_to_modname(join(sub1, '__main__.py'), hide_main=a, hide_init=b)
assert static.modpath_to_modname(b0, hide_main=a, hide_init=b) == 'b0'
assert static.modpath_to_modname(b1, hide_main=a, hide_init=b) == 'b1'
with pytest.raises(ValueError):
static.modpath_to_modname(bad1, hide_main=a, hide_init=b)
with pytest.raises(ValueError):
static.modpath_to_modname(bad2, hide_main=a, hide_init=b)
assert '_tmproot' not in sys.modules
assert '_tmproot.mod0' not in sys.modules
assert '_tmproot.sub1' not in sys.modules
assert '_tmproot.sub1.mod1' not in sys.modules
assert '_tmproot.sub1.sub2' not in sys.modules
assert '_tmproot.sub1.mod2.mod2' not in sys.modules
Args:
modpath (str): module filepath
hide_init (bool): removes the __init__ suffix (default True)
hide_init (bool): removes the __main__ suffix (default False)
Returns:
str: modname
Example:
>>> import ubelt.meta.static_analysis
>>> modpath = ubelt.meta.static_analysis.__file__
>>> print(modpath_to_modname(modpath))
ubelt.meta.static_analysis
"""
from xdoctest import static_analysis as static
return static.modpath_to_modname(modpath, hide_init, hide_main)
"""
Args:
pkgpath (str): path to a package with an __init__.py file
Example:
>>> import ubelt as ub
>>> pkgpath = ub.modname_to_modpath('ubelt')
>>> import_paths = dict(_find_local_submodules(pkgpath))
>>> assert 'util_dict' in import_paths
"""
# Find all the children modules in this package (non recursive)
from xdoctest import static_analysis as static
pkgname = static.modpath_to_modname(pkgpath)
for sub_modpath in static.package_modpaths(pkgpath, with_pkg=True,
recursive=False):
sub_modname = static.modpath_to_modname(sub_modpath)
rel_modname = sub_modname[len(pkgname) + 1:]
if rel_modname.startswith('_'):
# Skip private modules
pass
elif not rel_modname:
pass
else:
yield rel_modname, sub_modpath
modpath (str): base path to a package (with an __init__)
imports (list): list of submodules to look at in the base package
CommandLine:
python -m ubelt._internal.static_autogen _static_parse_imports
Example:
>>> import ubelt as ub
>>> modpath = ub.modname_to_modpath('ubelt')
>>> tup = _static_parse_imports(modpath, None, True)
>>> modname, imports, from_imports = tup
>>> assert 'util_arg' in imports
>>> assert 'util_dict' in imports
"""
from xdoctest import static_analysis as static
modname = static.modpath_to_modname(modpath)
if imports is not None:
import_paths = {
m: static.modname_to_modpath(modname + '.' + m, hide_init=False)
for m in imports
}
else:
import_paths = dict(_find_local_submodules(modpath))
imports = sorted(import_paths.keys())
from_imports = []
for rel_modname in imports:
sub_modpath = import_paths[rel_modname]
try:
if six.PY2:
with open(sub_modpath, 'r') as file:
source = file.read()