Exemplo n.º 1
0
    def member_names(self):
        r"""
        Map from parent module/package C{fullname} to known member names.

          >>> impset = ImportSet("import numpy.linalg.info\nfrom sys import exit as EXIT")
          >>> import pprint
          >>> pprint.pprint(impset.member_names)
          {'': ('EXIT', 'numpy', 'sys'),
           'numpy': ('linalg',),
           'numpy.linalg': ('info',),
           'sys': ('exit',)}

        This is used by the autoimporter module for implementing tab completion.

        @rtype:
          C{dict} mapping from C{str} to tuple of C{str}
        """
        d = defaultdict(set)
        for imp in self._importset:
            if '.' not in imp.import_as:
                d[""].add(imp.import_as)
            prefixes = dotted_prefixes(imp.fullname)
            d[""].add(prefixes[0])
            for prefix in prefixes[1:]:
                splt = prefix.rsplit(".", 1)
                d[splt[0]].add(splt[1])
        return dict((k, tuple(sorted(v))) for k, v in six.iteritems(d))
Exemplo n.º 2
0
    def by_fullname_or_import_as(self):
        """
        Map from C{fullname} and C{import_as} to L{Import}s.

          >>> import pprint
          >>> db = ImportDB('from aa.bb import cc as dd')
          >>> pprint.pprint(db.by_fullname_or_import_as)
          {'aa': (Import('import aa'),),
           'aa.bb': (Import('import aa.bb'),),
           'dd': (Import('from aa.bb import cc as dd'),)}

        @rtype:
          C{dict} mapping from C{str} to tuple of L{Import}s
        """
        # TODO: make known_imports take into account the below forget_imports,
        # then move this function into ImportSet
        d = defaultdict(set)
        for imp in self.known_imports.imports:
            # Given an import like "from foo.bar import quux as QUUX", add the
            # following entries:
            #   - "QUUX"         => "from foo.bar import quux as QUUX"
            #   - "foo.bar"      => "import foo.bar"
            #   - "foo"          => "import foo"
            # We don't include an entry labeled "quux" because the user has
            # implied he doesn't want to pollute the global namespace with
            # "quux", only "QUUX".
            d[imp.import_as].add(imp)
            for prefix in dotted_prefixes(imp.fullname)[:-1]:
                d[prefix].add(Import.from_parts(prefix, prefix))
        return dict((k, tuple(sorted(v - set(self.forget_imports.imports))))
                    for k, v in six.iteritems(d))
Exemplo n.º 3
0
    def without_imports(self, removals):
        """
        Return a copy of self without the given imports.

          >>> imports = ImportSet('from m import t1, t2, t3, t4')
          >>> imports.without_imports(['from m import t3'])
          ImportSet('''
            from m import t1, t2, t4
          ''')

        @type removals:
          L{ImportSet} (or convertible)
        @rtype:
          L{ImportSet}
        """
        removals = ImportSet(removals)
        if not removals:
            return self  # Optimization
        # Preprocess star imports to remove.
        star_module_removals = set([
            imp.split.module_name for imp in removals
            if imp.split.member_name == "*"
        ])
        # Filter imports.
        new_imports = []
        for imp in self:
            if imp in removals:
                continue
            if star_module_removals and imp.split.module_name:
                prefixes = dotted_prefixes(imp.split.module_name)
                if any(pfx in star_module_removals for pfx in prefixes):
                    continue
            new_imports.append(imp)
        # Return.
        if len(new_imports) == len(self):
            return self  # Space optimization
        return type(self)._from_imports(new_imports)
Exemplo n.º 4
0
def test_dotted_prefixes_dot_reverse_1():
    assert dotted_prefixes(".aa.bb", reverse=True) == ['.aa.bb', '.aa', '.']
Exemplo n.º 5
0
def test_dotted_prefixes_dot_1():
    assert dotted_prefixes(".aa.bb") == ['.', '.aa', '.aa.bb']
Exemplo n.º 6
0
def test_dotted_prefixes_reverse_1():
    result = dotted_prefixes("aa.bb.cc", reverse=True)
    assert result == ['aa.bb.cc', 'aa.bb', 'aa']
Exemplo n.º 7
0
def test_dotted_prefixes_1():
    assert dotted_prefixes("aa.bb.cc") == ['aa', 'aa.bb', 'aa.bb.cc']