Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
CommandLine:
pytest testing/test_static.py::test_modpath_to_modname -s
python testing/test_static.py test_modpath_to_modname
"""
with ub.TempDir() as temp:
dpath = temp.dpath
# Create a dummy package heirachy
root = ub.ensuredir((dpath, '_tmproot927'))
sub1 = ub.ensuredir((root, 'sub1'))
sub2 = ub.ensuredir((sub1, 'sub2'))
root_init = ub.touch(join(root, '__init__.py'))
sub1_init = ub.touch(join(sub1, '__init__.py'))
sub2_init = ub.touch(join(sub2, '__init__.py'))
mod0 = ub.touch(join(root, 'mod0.py'))
mod1 = ub.touch(join(sub1, 'mod1.py'))
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
"""
Creates a dummy package structure with or without __init__ files
"""
root = ub.ensuredir(join(dpath, pkgname))
ub.delete(root)
ub.ensuredir(root)
paths = {
'root': root,
'submod1': ub.touch(join(root, 'submod1.py')),
'submod2': ub.touch(join(root, 'submod2.py')),
'subdir1': ub.ensuredir(join(root, 'subdir1')),
'subdir2': ub.ensuredir(join(root, 'subdir2')),
}
paths['subdir1_init'] = ub.touch(join(paths['subdir1'], '__init__.py'))
paths['subdir2_init'] = ub.touch(join(paths['subdir2'], '__init__.py'))
paths['root_init'] = ub.touch(join(paths['root'], '__init__.py'))
ub.writeto(paths['subdir1_init'], ub.codeblock(
'''
simple_subattr1 = "hello world"
simple_subattr2 = "hello world"
_private_attr = "hello world"
'''))
ub.writeto(paths['subdir2_init'], ub.codeblock(
'''
__all__ = ['public_attr']
public_attr = "hello world"
private_attr = "hello world"
'''))
root_init = ub.touch(join(root, '__init__.py'))
sub1_init = ub.touch(join(sub1, '__init__.py'))
sub2_init = ub.touch(join(sub2, '__init__.py'))
mod0 = ub.touch(join(root, 'mod0.py'))
mod1 = ub.touch(join(sub1, 'mod1.py'))
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'))
with PythonPathContext(dpath):
subpaths = sorted(static.package_modpaths(root, with_pkg=True))
# should only return files not directories
assert root_init in subpaths
assert sub1_init in subpaths
assert sub2_init in subpaths
assert root not in subpaths
assert sub1 not in subpaths
assert sub2 not in subpaths
assert root_main in subpaths
assert sub2_main in subpaths
assert mod0 in subpaths
temp.__exit__(None, None, None)
%timeit _syspath_modname_to_modpath('xdoctest.static_analysis')
%timeit _pkgutil_modname_to_modpath('xdoctest.static_analysis')
"""
with ub.TempDir() as temp:
dpath = temp.dpath
# Some "bad" non-module directories
tmpbad = ub.ensuredir((dpath, '_tmpbad'))
# Make a submodule of a bad directory, look good.
sub_bad = ub.ensuredir((tmpbad, 'sub_bad'))
ub.touch(join(tmpbad, '_inbad.py'))
subbad = ub.touch(join(sub_bad, '__init__.py')) # NOQA
b0 = ub.touch(join(sub_bad, 'b0.py')) # NOQA
with PythonPathContext(dpath):
assert _static_modname_to_modpath('_tmpbad') is None
# Tricky case, these modules look good outside of _tmpbad WOW, you
# can actually import this and it works, but pkgloader still
# returns None so we should too.
assert _static_modname_to_modpath('_tmpbad.sub_bad') is None
assert _static_modname_to_modpath('_tmpbad.sub_bad.b0') is None
# We should be able to statically find all of the good module
# directories.
# this should all be static
import sys
assert '_tmpsingle' not in sys.modules
sys.path.append(temp.dpath)
temp.__exit__(None, None, None)
"""
from xdoctest import static_analysis as static
with ub.TempDir() as temp:
dpath = temp.dpath
# Create a dummy package heirachy
root = ub.ensuredir((dpath, '_tmproot927'))
sub1 = ub.ensuredir((root, 'sub1'))
sub2 = ub.ensuredir((sub1, 'sub2'))
root_init = ub.touch(join(root, '__init__.py'))
sub1_init = ub.touch(join(sub1, '__init__.py'))
sub2_init = ub.touch(join(sub2, '__init__.py'))
mod0 = ub.touch(join(root, 'mod0.py'))
mod1 = ub.touch(join(sub1, 'mod1.py'))
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'))
with PythonPathContext(dpath):
subpaths = sorted(static.package_modpaths(root, with_pkg=True))
def test_modname_to_modpath_single():
with ub.TempDir() as temp:
dpath = temp.dpath
# Single module
single = ub.touch(join(dpath, '_tmpsingle.py'))
single_main = ub.touch(join(dpath, '__main__.py'))
with PythonPathContext(dpath):
assert single == _static_modname_to_modpath('_tmpsingle')
assert single == _static_modname_to_modpath('_tmpsingle', hide_init=True, hide_main=False)
assert single == _static_modname_to_modpath('_tmpsingle', hide_init=False, hide_main=False)
assert single == _static_modname_to_modpath('_tmpsingle', hide_init=False, hide_main=True)
# Weird module named main not in a package
assert _static_modname_to_modpath('__main__') == single_main
assert _static_modname_to_modpath('__main__', hide_init=True, hide_main=False) == single_main
assert _static_modname_to_modpath('__main__', hide_init=False, hide_main=False) == single_main
assert _static_modname_to_modpath('__main__', hide_init=False, hide_main=True) == single_main
def test_modpath_to_modname():
"""
CommandLine:
pytest testing/test_static.py::test_modpath_to_modname -s
python testing/test_static.py test_modpath_to_modname
"""
with ub.TempDir() as temp:
dpath = temp.dpath
# Create a dummy package heirachy
root = ub.ensuredir((dpath, '_tmproot927'))
sub1 = ub.ensuredir((root, 'sub1'))
sub2 = ub.ensuredir((sub1, 'sub2'))
root_init = ub.touch(join(root, '__init__.py'))
sub1_init = ub.touch(join(sub1, '__init__.py'))
sub2_init = ub.touch(join(sub2, '__init__.py'))
mod0 = ub.touch(join(root, 'mod0.py'))
mod1 = ub.touch(join(sub1, 'mod1.py'))
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
root_init = ub.touch(join(root, '__init__.py'))
sub1_init = ub.touch(join(sub1, '__init__.py'))
sub2_init = ub.touch(join(sub2, '__init__.py'))
mod0 = ub.touch(join(root, 'mod0.py'))
mod1 = ub.touch(join(sub1, 'mod1.py'))
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'
def make_dummy_package(dpath, pkgname='mkinit_dummy_module'):
"""
Creates a dummy package structure with or without __init__ files
"""
root = ub.ensuredir(join(dpath, pkgname))
ub.delete(root)
ub.ensuredir(root)
paths = {
'root': root,
'submod1': ub.touch(join(root, 'submod1.py')),
'submod2': ub.touch(join(root, 'submod2.py')),
'subdir1': ub.ensuredir(join(root, 'subdir1')),
'subdir2': ub.ensuredir(join(root, 'subdir2')),
}
paths['subdir1_init'] = ub.touch(join(paths['subdir1'], '__init__.py'))
paths['subdir2_init'] = ub.touch(join(paths['subdir2'], '__init__.py'))
paths['root_init'] = ub.touch(join(paths['root'], '__init__.py'))
ub.writeto(paths['subdir1_init'], ub.codeblock(
'''
simple_subattr1 = "hello world"
simple_subattr2 = "hello world"
_private_attr = "hello world"
'''))
ub.writeto(paths['subdir2_init'], ub.codeblock(
"""
from xdoctest import static_analysis as static
with ub.TempDir() as temp:
dpath = temp.dpath
# Create a dummy package heirachy
root = ub.ensuredir((dpath, '_tmproot927'))
sub1 = ub.ensuredir((root, 'sub1'))
sub2 = ub.ensuredir((sub1, 'sub2'))
root_init = ub.touch(join(root, '__init__.py'))
sub1_init = ub.touch(join(sub1, '__init__.py'))
sub2_init = ub.touch(join(sub2, '__init__.py'))
mod0 = ub.touch(join(root, 'mod0.py'))
mod1 = ub.touch(join(sub1, 'mod1.py'))
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'))
with PythonPathContext(dpath):
subpaths = sorted(static.package_modpaths(root, with_pkg=True))
# should only return files not directories
assert root_init in subpaths
assert sub1_init in subpaths