Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
mod2 = ub.touch(join(sub2, 'mod2.py'))
root_main = ub.touch(join(root, '__main__.py'))
sub2_main = ub.touch(join(sub2, '__main__.py'))
bad1 = ub.ensuredir((root, 'bad1'))
bad2 = ub.ensuredir((sub1, 'bad2'))
b0 = ub.touch(join(bad1, 'b0.py'))
b1 = ub.touch(join(bad2, 'b1.py'))
import os
ub.modpath_to_modname(root, relativeto=os.path.dirname(dpath)) # TODO: assert correct output
with PythonPathContext(dpath):
assert ub.modpath_to_modname(root) == '_tmproot927'
assert ub.modpath_to_modname(sub1) == '_tmproot927.sub1'
assert ub.modpath_to_modname(sub2) == '_tmproot927.sub1.sub2'
assert ub.modpath_to_modname(mod0) == '_tmproot927.mod0'
assert ub.modpath_to_modname(mod1) == '_tmproot927.sub1.mod1'
assert ub.modpath_to_modname(mod2) == '_tmproot927.sub1.sub2.mod2'
assert ub.modpath_to_modname(root_init) == '_tmproot927'
assert ub.modpath_to_modname(sub1_init) == '_tmproot927.sub1'
assert ub.modpath_to_modname(sub2_init) == '_tmproot927.sub1.sub2'
assert ub.modpath_to_modname(root_init, hide_init=False) == '_tmproot927.__init__'
assert ub.modpath_to_modname(sub1_init, hide_init=False) == '_tmproot927.sub1.__init__'
assert ub.modpath_to_modname(sub2_init, hide_init=False) == '_tmproot927.sub1.sub2.__init__'
assert ub.modpath_to_modname(root, hide_main=True, hide_init=False) == '_tmproot927.__init__'
assert ub.modpath_to_modname(sub2_init) == '_tmproot927.sub1.sub2'
assert ub.modpath_to_modname(root_init, hide_init=False) == '_tmproot927.__init__'
assert ub.modpath_to_modname(sub1_init, hide_init=False) == '_tmproot927.sub1.__init__'
assert ub.modpath_to_modname(sub2_init, hide_init=False) == '_tmproot927.sub1.sub2.__init__'
assert ub.modpath_to_modname(root, hide_main=True, hide_init=False) == '_tmproot927.__init__'
assert ub.modpath_to_modname(sub1, hide_main=True, hide_init=False) == '_tmproot927.sub1.__init__'
assert ub.modpath_to_modname(sub2, hide_main=True, hide_init=False) == '_tmproot927.sub1.sub2.__init__'
assert ub.modpath_to_modname(root, hide_main=False, hide_init=False) == '_tmproot927.__init__'
assert ub.modpath_to_modname(sub1, hide_main=False, hide_init=False) == '_tmproot927.sub1.__init__'
assert ub.modpath_to_modname(sub2, hide_main=False, hide_init=False) == '_tmproot927.sub1.sub2.__init__'
assert ub.modpath_to_modname(root, hide_main=False, hide_init=True) == '_tmproot927'
assert ub.modpath_to_modname(sub1, hide_main=False, hide_init=True) == '_tmproot927.sub1'
assert ub.modpath_to_modname(sub2, hide_main=False, hide_init=True) == '_tmproot927.sub1.sub2'
assert ub.modpath_to_modname(root_main, hide_main=False, hide_init=True) == '_tmproot927.__main__'
assert ub.modpath_to_modname(sub2_main, hide_main=False, hide_init=True) == '_tmproot927.sub1.sub2.__main__'
assert ub.modpath_to_modname(root_main, hide_main=False, hide_init=True) == '_tmproot927.__main__'
assert ub.modpath_to_modname(sub2_main, hide_main=False, hide_init=True) == '_tmproot927.sub1.sub2.__main__'
assert ub.modpath_to_modname(root_main, hide_main=True, hide_init=True) == '_tmproot927'
assert ub.modpath_to_modname(sub2_main, hide_main=True, hide_init=True) == '_tmproot927.sub1.sub2'
assert ub.modpath_to_modname(root_main, hide_main=True, hide_init=False) == '_tmproot927'
assert ub.modpath_to_modname(sub2_main, hide_main=True, hide_init=False) == '_tmproot927.sub1.sub2'
# Non-existant / invalid modules should always be None
for a, b in it.product([True, False], [True, False]):
import os
ub.modpath_to_modname(root, relativeto=os.path.dirname(dpath)) # TODO: assert correct output
with PythonPathContext(dpath):
assert ub.modpath_to_modname(root) == '_tmproot927'
assert ub.modpath_to_modname(sub1) == '_tmproot927.sub1'
assert ub.modpath_to_modname(sub2) == '_tmproot927.sub1.sub2'
assert ub.modpath_to_modname(mod0) == '_tmproot927.mod0'
assert ub.modpath_to_modname(mod1) == '_tmproot927.sub1.mod1'
assert ub.modpath_to_modname(mod2) == '_tmproot927.sub1.sub2.mod2'
assert ub.modpath_to_modname(root_init) == '_tmproot927'
assert ub.modpath_to_modname(sub1_init) == '_tmproot927.sub1'
assert ub.modpath_to_modname(sub2_init) == '_tmproot927.sub1.sub2'
assert ub.modpath_to_modname(root_init, hide_init=False) == '_tmproot927.__init__'
assert ub.modpath_to_modname(sub1_init, hide_init=False) == '_tmproot927.sub1.__init__'
assert ub.modpath_to_modname(sub2_init, hide_init=False) == '_tmproot927.sub1.sub2.__init__'
assert ub.modpath_to_modname(root, hide_main=True, hide_init=False) == '_tmproot927.__init__'
assert ub.modpath_to_modname(sub1, hide_main=True, hide_init=False) == '_tmproot927.sub1.__init__'
assert ub.modpath_to_modname(sub2, hide_main=True, hide_init=False) == '_tmproot927.sub1.sub2.__init__'
assert ub.modpath_to_modname(root, hide_main=False, hide_init=False) == '_tmproot927.__init__'
assert ub.modpath_to_modname(sub1, hide_main=False, hide_init=False) == '_tmproot927.sub1.__init__'
assert ub.modpath_to_modname(sub2, hide_main=False, hide_init=False) == '_tmproot927.sub1.sub2.__init__'
assert ub.modpath_to_modname(root, hide_main=False, hide_init=True) == '_tmproot927'
assert ub.modpath_to_modname(sub1, hide_main=False, hide_init=True) == '_tmproot927.sub1'
assert ub.modpath_to_modname(root, hide_main=False, hide_init=False) == '_tmproot927.__init__'
assert ub.modpath_to_modname(sub1, hide_main=False, hide_init=False) == '_tmproot927.sub1.__init__'
assert ub.modpath_to_modname(sub2, hide_main=False, hide_init=False) == '_tmproot927.sub1.sub2.__init__'
assert ub.modpath_to_modname(root, hide_main=False, hide_init=True) == '_tmproot927'
assert ub.modpath_to_modname(sub1, hide_main=False, hide_init=True) == '_tmproot927.sub1'
assert ub.modpath_to_modname(sub2, hide_main=False, hide_init=True) == '_tmproot927.sub1.sub2'
assert ub.modpath_to_modname(root_main, hide_main=False, hide_init=True) == '_tmproot927.__main__'
assert ub.modpath_to_modname(sub2_main, hide_main=False, hide_init=True) == '_tmproot927.sub1.sub2.__main__'
assert ub.modpath_to_modname(root_main, hide_main=False, hide_init=True) == '_tmproot927.__main__'
assert ub.modpath_to_modname(sub2_main, hide_main=False, hide_init=True) == '_tmproot927.sub1.sub2.__main__'
assert ub.modpath_to_modname(root_main, hide_main=True, hide_init=True) == '_tmproot927'
assert ub.modpath_to_modname(sub2_main, hide_main=True, hide_init=True) == '_tmproot927.sub1.sub2'
assert ub.modpath_to_modname(root_main, hide_main=True, hide_init=False) == '_tmproot927'
assert ub.modpath_to_modname(sub2_main, hide_main=True, hide_init=False) == '_tmproot927.sub1.sub2'
# Non-existant / invalid modules should always be None
for a, b in it.product([True, False], [True, False]):
with pytest.raises(ValueError):
ub.modpath_to_modname(join(sub1, '__main__.py'), hide_main=a, hide_init=b)
assert ub.modpath_to_modname(b0, hide_main=a, hide_init=b) == 'b0'
assert ub.modpath_to_modname(b1, hide_main=a, hide_init=b) == 'b1'
with pytest.raises(ValueError):
ub.modpath_to_modname(bad1, hide_main=a, hide_init=b)
with pytest.raises(ValueError):
ub.modpath_to_modname(bad2, hide_main=a, hide_init=b)
assert '_tmproot927' not in sys.modules
bad1 = ub.ensuredir((root, 'bad1'))
bad2 = ub.ensuredir((sub1, 'bad2'))
b0 = ub.touch(join(bad1, 'b0.py'))
b1 = ub.touch(join(bad2, 'b1.py'))
import os
ub.modpath_to_modname(root, relativeto=os.path.dirname(dpath)) # TODO: assert correct output
with PythonPathContext(dpath):
assert ub.modpath_to_modname(root) == '_tmproot927'
assert ub.modpath_to_modname(sub1) == '_tmproot927.sub1'
assert ub.modpath_to_modname(sub2) == '_tmproot927.sub1.sub2'
assert ub.modpath_to_modname(mod0) == '_tmproot927.mod0'
assert ub.modpath_to_modname(mod1) == '_tmproot927.sub1.mod1'
assert ub.modpath_to_modname(mod2) == '_tmproot927.sub1.sub2.mod2'
assert ub.modpath_to_modname(root_init) == '_tmproot927'
assert ub.modpath_to_modname(sub1_init) == '_tmproot927.sub1'
assert ub.modpath_to_modname(sub2_init) == '_tmproot927.sub1.sub2'
assert ub.modpath_to_modname(root_init, hide_init=False) == '_tmproot927.__init__'
assert ub.modpath_to_modname(sub1_init, hide_init=False) == '_tmproot927.sub1.__init__'
assert ub.modpath_to_modname(sub2_init, hide_init=False) == '_tmproot927.sub1.sub2.__init__'
assert ub.modpath_to_modname(root, hide_main=True, hide_init=False) == '_tmproot927.__init__'
assert ub.modpath_to_modname(sub1, hide_main=True, hide_init=False) == '_tmproot927.sub1.__init__'
assert ub.modpath_to_modname(sub2, hide_main=True, hide_init=False) == '_tmproot927.sub1.sub2.__init__'
assert ub.modpath_to_modname(root, hide_main=False, hide_init=False) == '_tmproot927.__init__'
root_main = ub.touch(join(root, '__main__.py'))
sub2_main = ub.touch(join(sub2, '__main__.py'))
bad1 = ub.ensuredir((root, 'bad1'))
bad2 = ub.ensuredir((sub1, 'bad2'))
b0 = ub.touch(join(bad1, 'b0.py'))
b1 = ub.touch(join(bad2, 'b1.py'))
import os
ub.modpath_to_modname(root, relativeto=os.path.dirname(dpath)) # TODO: assert correct output
with PythonPathContext(dpath):
assert ub.modpath_to_modname(root) == '_tmproot927'
assert ub.modpath_to_modname(sub1) == '_tmproot927.sub1'
assert ub.modpath_to_modname(sub2) == '_tmproot927.sub1.sub2'
assert ub.modpath_to_modname(mod0) == '_tmproot927.mod0'
assert ub.modpath_to_modname(mod1) == '_tmproot927.sub1.mod1'
assert ub.modpath_to_modname(mod2) == '_tmproot927.sub1.sub2.mod2'
assert ub.modpath_to_modname(root_init) == '_tmproot927'
assert ub.modpath_to_modname(sub1_init) == '_tmproot927.sub1'
assert ub.modpath_to_modname(sub2_init) == '_tmproot927.sub1.sub2'
assert ub.modpath_to_modname(root_init, hide_init=False) == '_tmproot927.__init__'
assert ub.modpath_to_modname(sub1_init, hide_init=False) == '_tmproot927.sub1.__init__'
assert ub.modpath_to_modname(sub2_init, hide_init=False) == '_tmproot927.sub1.sub2.__init__'
assert ub.modpath_to_modname(root, hide_main=True, hide_init=False) == '_tmproot927.__init__'
assert ub.modpath_to_modname(sub1, hide_main=True, hide_init=False) == '_tmproot927.sub1.__init__'
bad1 = ub.ensuredir((root, 'bad1'))
bad2 = ub.ensuredir((sub1, 'bad2'))
b0 = ub.touch(join(bad1, 'b0.py'))
b1 = ub.touch(join(bad2, 'b1.py'))
import os
ub.modpath_to_modname(root, relativeto=os.path.dirname(dpath)) # TODO: assert correct output
with PythonPathContext(dpath):
assert ub.modpath_to_modname(root) == '_tmproot927'
assert ub.modpath_to_modname(sub1) == '_tmproot927.sub1'
assert ub.modpath_to_modname(sub2) == '_tmproot927.sub1.sub2'
assert ub.modpath_to_modname(mod0) == '_tmproot927.mod0'
assert ub.modpath_to_modname(mod1) == '_tmproot927.sub1.mod1'
assert ub.modpath_to_modname(mod2) == '_tmproot927.sub1.sub2.mod2'
assert ub.modpath_to_modname(root_init) == '_tmproot927'
assert ub.modpath_to_modname(sub1_init) == '_tmproot927.sub1'
assert ub.modpath_to_modname(sub2_init) == '_tmproot927.sub1.sub2'
assert ub.modpath_to_modname(root_init, hide_init=False) == '_tmproot927.__init__'
assert ub.modpath_to_modname(sub1_init, hide_init=False) == '_tmproot927.sub1.__init__'
assert ub.modpath_to_modname(sub2_init, hide_init=False) == '_tmproot927.sub1.sub2.__init__'
assert ub.modpath_to_modname(root, hide_main=True, hide_init=False) == '_tmproot927.__init__'
assert ub.modpath_to_modname(sub1, hide_main=True, hide_init=False) == '_tmproot927.sub1.__init__'
assert ub.modpath_to_modname(sub2, hide_main=True, hide_init=False) == '_tmproot927.sub1.sub2.__init__'
assert ub.modpath_to_modname(root, hide_main=False, hide_init=False) == '_tmproot927.__init__'
assert ub.modpath_to_modname(sub1, hide_main=False, hide_init=False) == '_tmproot927.sub1.__init__'
b1 = ub.touch(join(bad2, 'b1.py'))
import os
ub.modpath_to_modname(root, relativeto=os.path.dirname(dpath)) # TODO: assert correct output
with PythonPathContext(dpath):
assert ub.modpath_to_modname(root) == '_tmproot927'
assert ub.modpath_to_modname(sub1) == '_tmproot927.sub1'
assert ub.modpath_to_modname(sub2) == '_tmproot927.sub1.sub2'
assert ub.modpath_to_modname(mod0) == '_tmproot927.mod0'
assert ub.modpath_to_modname(mod1) == '_tmproot927.sub1.mod1'
assert ub.modpath_to_modname(mod2) == '_tmproot927.sub1.sub2.mod2'
assert ub.modpath_to_modname(root_init) == '_tmproot927'
assert ub.modpath_to_modname(sub1_init) == '_tmproot927.sub1'
assert ub.modpath_to_modname(sub2_init) == '_tmproot927.sub1.sub2'
assert ub.modpath_to_modname(root_init, hide_init=False) == '_tmproot927.__init__'
assert ub.modpath_to_modname(sub1_init, hide_init=False) == '_tmproot927.sub1.__init__'
assert ub.modpath_to_modname(sub2_init, hide_init=False) == '_tmproot927.sub1.sub2.__init__'
assert ub.modpath_to_modname(root, hide_main=True, hide_init=False) == '_tmproot927.__init__'
assert ub.modpath_to_modname(sub1, hide_main=True, hide_init=False) == '_tmproot927.sub1.__init__'
assert ub.modpath_to_modname(sub2, hide_main=True, hide_init=False) == '_tmproot927.sub1.sub2.__init__'
assert ub.modpath_to_modname(root, hide_main=False, hide_init=False) == '_tmproot927.__init__'
assert ub.modpath_to_modname(sub1, hide_main=False, hide_init=False) == '_tmproot927.sub1.__init__'
assert ub.modpath_to_modname(sub2, hide_main=False, hide_init=False) == '_tmproot927.sub1.sub2.__init__'
assert ub.modpath_to_modname(root, hide_main=False, hide_init=True) == '_tmproot927'
assert ub.modpath_to_modname(sub2_init, hide_init=False) == '_tmproot927.sub1.sub2.__init__'
assert ub.modpath_to_modname(root, hide_main=True, hide_init=False) == '_tmproot927.__init__'
assert ub.modpath_to_modname(sub1, hide_main=True, hide_init=False) == '_tmproot927.sub1.__init__'
assert ub.modpath_to_modname(sub2, hide_main=True, hide_init=False) == '_tmproot927.sub1.sub2.__init__'
assert ub.modpath_to_modname(root, hide_main=False, hide_init=False) == '_tmproot927.__init__'
assert ub.modpath_to_modname(sub1, hide_main=False, hide_init=False) == '_tmproot927.sub1.__init__'
assert ub.modpath_to_modname(sub2, hide_main=False, hide_init=False) == '_tmproot927.sub1.sub2.__init__'
assert ub.modpath_to_modname(root, hide_main=False, hide_init=True) == '_tmproot927'
assert ub.modpath_to_modname(sub1, hide_main=False, hide_init=True) == '_tmproot927.sub1'
assert ub.modpath_to_modname(sub2, hide_main=False, hide_init=True) == '_tmproot927.sub1.sub2'
assert ub.modpath_to_modname(root_main, hide_main=False, hide_init=True) == '_tmproot927.__main__'
assert ub.modpath_to_modname(sub2_main, hide_main=False, hide_init=True) == '_tmproot927.sub1.sub2.__main__'
assert ub.modpath_to_modname(root_main, hide_main=False, hide_init=True) == '_tmproot927.__main__'
assert ub.modpath_to_modname(sub2_main, hide_main=False, hide_init=True) == '_tmproot927.sub1.sub2.__main__'
assert ub.modpath_to_modname(root_main, hide_main=True, hide_init=True) == '_tmproot927'
assert ub.modpath_to_modname(sub2_main, hide_main=True, hide_init=True) == '_tmproot927.sub1.sub2'
assert ub.modpath_to_modname(root_main, hide_main=True, hide_init=False) == '_tmproot927'
assert ub.modpath_to_modname(sub2_main, hide_main=True, hide_init=False) == '_tmproot927.sub1.sub2'
# Non-existant / invalid modules should always be None
for a, b in it.product([True, False], [True, False]):
with pytest.raises(ValueError):
ub.modpath_to_modname(join(sub1, '__main__.py'), hide_main=a, hide_init=b)
assert ub.modpath_to_modname(b0, hide_main=a, hide_init=b) == 'b0'
assert ub.modpath_to_modname(b1, hide_main=a, hide_init=b) == 'b1'
assert ub.modpath_to_modname(sub2_main, hide_main=False, hide_init=True) == '_tmproot927.sub1.sub2.__main__'
assert ub.modpath_to_modname(root_main, hide_main=False, hide_init=True) == '_tmproot927.__main__'
assert ub.modpath_to_modname(sub2_main, hide_main=False, hide_init=True) == '_tmproot927.sub1.sub2.__main__'
assert ub.modpath_to_modname(root_main, hide_main=True, hide_init=True) == '_tmproot927'
assert ub.modpath_to_modname(sub2_main, hide_main=True, hide_init=True) == '_tmproot927.sub1.sub2'
assert ub.modpath_to_modname(root_main, hide_main=True, hide_init=False) == '_tmproot927'
assert ub.modpath_to_modname(sub2_main, hide_main=True, hide_init=False) == '_tmproot927.sub1.sub2'
# Non-existant / invalid modules should always be None
for a, b in it.product([True, False], [True, False]):
with pytest.raises(ValueError):
ub.modpath_to_modname(join(sub1, '__main__.py'), hide_main=a, hide_init=b)
assert ub.modpath_to_modname(b0, hide_main=a, hide_init=b) == 'b0'
assert ub.modpath_to_modname(b1, hide_main=a, hide_init=b) == 'b1'
with pytest.raises(ValueError):
ub.modpath_to_modname(bad1, hide_main=a, hide_init=b)
with pytest.raises(ValueError):
ub.modpath_to_modname(bad2, hide_main=a, hide_init=b)
assert '_tmproot927' not in sys.modules
assert '_tmproot927.mod0' not in sys.modules
assert '_tmproot927.sub1' not in sys.modules
assert '_tmproot927.sub1.mod1' not in sys.modules
assert '_tmproot927.sub1.sub2' not in sys.modules
assert '_tmproot927.sub1.mod2.mod2' not in sys.modules