Пример #1
0
def _rectify_to_modpath(modpath_or_name):
    if exists(modpath_or_name):
        modpath = abspath(modpath_or_name)
    else:
        modpath = static.modname_to_modpath(modpath_or_name)
        if modpath is None:
            raise ValueError('Invalid module {}'.format(modpath_or_name))
    return modpath
Пример #2
0
def _rectify_to_modpath(modpath_or_name):
    if exists(modpath_or_name):
        modpath = abspath(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 basename(modpath) == '__init__.py':
        modpath = dirname(modpath)
    return modpath
Пример #3
0
def _static_parse_imports(modpath,
                          submodules=None,
                          external=None,
                          respect_all=True):
    """
    Args:
        modpath (PathLike): base path to a package (with an __init__)
        submodules (List[str]): Submodules to look at in the base package.
            This is implicitly generated if not specified.
        respect_all (bool, default=True):
            if False, does not respect the __all__ attributes of submodules

    CommandLine:
        python -m mkinit.static_autogen _static_parse_imports

    Example:
        >>> modpath = static.modname_to_modpath('mkinit')
        >>> external = ['textwrap']
        >>> tup = _static_parse_imports(modpath, external=external)
        >>> modname, submodules, from_imports = tup
        >>> print('modname = {!r}'.format(modname))
        >>> print('submodules = {!r}'.format(submodules))
        >>> print('from_imports = {!r}'.format(from_imports))
        >>> # assert 'autogen_init' in submodules
    """
    logger.debug('Parse static submodules: {}'.format(modpath))
    # FIXME: handle the case where the __init__.py file doesn't exist yet
    modname = static.modpath_to_modname(modpath, check=False)
    if submodules is None:
        logger.debug('Parsing implicit submodules!')
        import_paths = dict(_find_local_submodules(modpath))
        submodules = sorted(import_paths.keys())
        # logger.debug('Found {} import paths'.format(len(import_paths)))
        # logger.debug('Found {} submodules'.format(len(submodules)))
    else:
        logger.debug('Given explicit submodules')
        if modname is None:
            raise AssertionError('modname is None')

        import_paths = {
            m: static.modname_to_modpath(modname + '.' + m, hide_init=False)
            for m in submodules
        }
        # FIX for relative nested import_paths
        for m in import_paths.keys():
            oldval = import_paths[m]
            if oldval is None:
                candidates = [
                    join(modpath, m),
                    join(modpath, m) + '.py',
                ]
                for newval in candidates:
                    if exists(newval):
                        import_paths[m] = newval
                        break
    imports = ['.' + m for m in submodules]

    from_imports = []
    for rel_modname in submodules:
        sub_modpath = import_paths[rel_modname]
        if sub_modpath is None:
            raise Exception(
                'Failed to submodule lookup {!r}'.format(rel_modname))
        try:
            valid_attrs = _extract_attributes(sub_modpath,
                                              respect_all=respect_all)
        except SyntaxError as ex:
            warnings.warn('Failed to parse module {} ex = {!r}'.format(
                rel_modname, ex))
        else:
            from_imports.append(('.' + rel_modname, sorted(valid_attrs)))

    if external:
        for ext_modname in external:
            ext_modpath = static.modname_to_modpath(ext_modname,
                                                    hide_init=False)
            if ext_modpath is None:
                raise Exception(
                    'Failed to external lookup {!r}'.format(ext_modpath))
            try:
                valid_attrs = _extract_attributes(ext_modpath,
                                                  respect_all=respect_all)
            except SyntaxError as ex:
                warnings.warn('Failed to parse {} module ex = {!r}'.format(
                    ext_modname, ex))
            else:
                from_imports.append((ext_modname, sorted(valid_attrs)))

    return modname, imports, from_imports
Пример #4
0
def _static_parse_imports(modpath, imports=None, use_all=True):
    """
    Args:
        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 mkinit.static_autogen _static_parse_imports

    Example:
        >>> modpath = static.modname_to_modpath('mkinit')
        >>> tup = _static_parse_imports(modpath, None, True)
        >>> modname, imports, from_imports = tup
        >>> # assert 'autogen_init' in imports
    """
    # FIXME: handle the case where the __init__.py file doesn't exist yet
    modname = static.modpath_to_modname(modpath, check=False)
    if imports is not None:
        if modname is None:
            raise AssertionError('modname is 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]
        if sub_modpath is None:
            raise Exception('Failed to lookup {!r}'.format(rel_modname))
        try:
            if six.PY2:
                with open(sub_modpath, 'r') as file:
                    source = file.read()
            else:
                with open(sub_modpath, 'r', encoding='utf8') as file:
                    source = file.read()
        except Exception as ex:  # nocover
            raise IOError('Error reading {}, caused by {}'.format(
                sub_modpath, repr(ex)))
        valid_attrs = None
        if use_all:  # pragma: nobranch
            try:
                valid_attrs = static.parse_static_value('__all__', source)
            except NameError:
                pass
        if valid_attrs is None:
            # The __all__ variable is not specified or we dont care
            top_level = TopLevelVisitor.parse(source)
            attrnames = top_level.attrnames
            # list of names we wont export by default
            invalid_callnames = dir(builtins)
            valid_attrs = []
            for attr in attrnames:
                if attr.startswith('_'):
                    continue
                if attr in invalid_callnames:  # nocover
                    continue
                valid_attrs.append(attr)
        from_imports.append((rel_modname, sorted(valid_attrs)))
    return modname, imports, from_imports