Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _rectify_to_modpath(modpath_or_name):
""" if modpath_or_name is a name, statically converts it to a path """
modpath = static.modname_to_modpath(modpath_or_name)
if modpath is None:
if six.PY2:
if modpath_or_name.endswith('.pyc'):
modpath_or_name = modpath_or_name[:-1]
if exists(modpath_or_name):
modpath = modpath_or_name
else:
raise ValueError('Cannot find module={}'.format(modpath_or_name))
return modpath
def _static_modname_to_modpath(modname, **kwargs):
# Calls static.modname_to_modpath with checks
had = modname in sys.modules
try:
modpath = static.modname_to_modpath(modname, **kwargs)
except ValueError:
modpath = None
if not had:
assert modname not in sys.modules, (
'{} should not be imported'.format(modname))
return modpath
def _module_exists(modname):
if modname not in _MODNAME_EXISTS_CACHE:
from xdoctest import static_analysis as static
modpath = static.modname_to_modpath(modname)
exists_flag = modpath is not None
_MODNAME_EXISTS_CACHE[modname] = exists_flag
exists_flag = _MODNAME_EXISTS_CACHE[modname]
return exists_flag
python -c "import ubelt._internal as a; a.autogen_init('')"
CommandLine:
python -m ubelt._internal.static_autogen autogen_init
Example:
>>> init_fpath, new_text = autogen_init('ubelt', imports=['util_arg'],
>>> attrs=True, use_all=True,
>>> dry=True)
>>> assert 'argflag' in new_text
"""
from xdoctest import static_analysis as static
if exists(modpath_or_name):
modpath = modpath_or_name
else:
modpath = static.modname_to_modpath(modpath_or_name)
if modpath is None:
raise ValueError('Invalid module {}'.format(modpath_or_name))
if imports is None:
# the __init__ file may have a variable describing the correct imports
# should imports specify the name of this variable or should it always
# be GLOBAL_MODULES?
with open(join(modpath, '__init__.py'), 'r') as file:
source = file.read()
varname = 'GLOBAL_MODULES'
try:
imports = static.parse_static_value(varname, source)
except NameError:
pass
modname, imports, from_imports = _static_parse_imports(modpath,
>>> import sys
>>> modname = 'ubelt.__main__'
>>> modpath = modname_to_modpath(modname, hide_main=False)
>>> print('modpath = %r' % (modpath,))
>>> assert modpath.endswith('__main__.py')
>>> modname = 'ubelt'
>>> modpath = modname_to_modpath(modname, hide_init=False)
>>> print('modpath = %r' % (modpath,))
>>> assert modpath.endswith('__init__.py')
>>> modname = 'ubelt'
>>> modpath = modname_to_modpath(modname, hide_init=False, hide_main=False)
>>> print('modpath = %r' % (modpath,))
>>> assert modpath.endswith('__init__.py')
"""
from xdoctest import static_analysis as static
return static.modname_to_modpath(modname, hide_init, hide_main)