当前位置: 首页>>代码示例>>Python>>正文


Python pkg_resources._namespace_packages方法代码示例

本文整理汇总了Python中pkg_resources._namespace_packages方法的典型用法代码示例。如果您正苦于以下问题:Python pkg_resources._namespace_packages方法的具体用法?Python pkg_resources._namespace_packages怎么用?Python pkg_resources._namespace_packages使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pkg_resources的用法示例。


在下文中一共展示了pkg_resources._namespace_packages方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: run_tests

# 需要导入模块: import pkg_resources [as 别名]
# 或者: from pkg_resources import _namespace_packages [as 别名]
def run_tests(self):
        import unittest

        # Purge modules under test from sys.modules. The test loader will
        # re-import them from the build location. Required when 2to3 is used
        # with namespace packages.
        if sys.version_info >= (3,) and getattr(self.distribution, 'use_2to3', False):
            module = self.test_args[-1].split('.')[0]
            if module in _namespace_packages:
                del_modules = []
                if module in sys.modules:
                    del_modules.append(module)
                module += '.'
                for name in sys.modules:
                    if name.startswith(module):
                        del_modules.append(name)
                list(map(sys.modules.__delitem__, del_modules))

        loader_ep = EntryPoint.parse("x="+self.test_loader)
        loader_class = loader_ep.load(require=False)
        cks = loader_class()
        unittest.main(
            None, None, [unittest.__file__]+self.test_args,
            testLoader = cks
        ) 
开发者ID:GeekTrainer,项目名称:Flask,代码行数:27,代码来源:test.py

示例2: is_namespace

# 需要导入模块: import pkg_resources [as 别名]
# 或者: from pkg_resources import _namespace_packages [as 别名]
def is_namespace(modname):
    # pylint: disable=no-member; astroid issue #290, modifying globals at runtime.
    return (pkg_resources is not None
            and modname in pkg_resources._namespace_packages) 
开发者ID:AtomLinter,项目名称:linter-pylama,代码行数:6,代码来源:util.py

示例3: setUp

# 需要导入模块: import pkg_resources [as 别名]
# 或者: from pkg_resources import _namespace_packages [as 别名]
def setUp(self):
        self._ns_pkgs = pkg_resources._namespace_packages.copy()
        self._tmpdir = tempfile.mkdtemp(prefix="tests-setuptools-")
        os.makedirs(os.path.join(self._tmpdir, "site-pkgs"))
        self._prev_sys_path = sys.path[:]
        sys.path.append(os.path.join(self._tmpdir, "site-pkgs")) 
开发者ID:MayOneUS,项目名称:pledgeservice,代码行数:8,代码来源:test_resources.py

示例4: tearDown

# 需要导入模块: import pkg_resources [as 别名]
# 或者: from pkg_resources import _namespace_packages [as 别名]
def tearDown(self):
        shutil.rmtree(self._tmpdir)
        pkg_resources._namespace_packages = self._ns_pkgs.copy()
        sys.path = self._prev_sys_path[:] 
开发者ID:MayOneUS,项目名称:pledgeservice,代码行数:6,代码来源:test_resources.py

示例5: test_two_levels_deep

# 需要导入模块: import pkg_resources [as 别名]
# 或者: from pkg_resources import _namespace_packages [as 别名]
def test_two_levels_deep(self):
        """
        Test nested namespace packages
        Create namespace packages in the following tree :
            site-packages-1/pkg1/pkg2
            site-packages-2/pkg1/pkg2
        Check both are in the _namespace_packages dict and that their __path__
        is correct
        """
        sys.path.append(os.path.join(self._tmpdir, "site-pkgs2"))
        os.makedirs(os.path.join(self._tmpdir, "site-pkgs", "pkg1", "pkg2"))
        os.makedirs(os.path.join(self._tmpdir, "site-pkgs2", "pkg1", "pkg2"))
        ns_str = "__import__('pkg_resources').declare_namespace(__name__)\n"
        for site in ["site-pkgs", "site-pkgs2"]:
            pkg1_init = open(os.path.join(self._tmpdir, site,
                             "pkg1", "__init__.py"), "w")
            pkg1_init.write(ns_str)
            pkg1_init.close()
            pkg2_init = open(os.path.join(self._tmpdir, site,
                             "pkg1", "pkg2", "__init__.py"), "w")
            pkg2_init.write(ns_str)
            pkg2_init.close()
        import pkg1
        self._assertIn("pkg1", pkg_resources._namespace_packages.keys())
        try:
            import pkg1.pkg2
        except ImportError:
            self.fail("Setuptools tried to import the parent namespace package")
        # check the _namespace_packages dict
        self._assertIn("pkg1.pkg2", pkg_resources._namespace_packages.keys())
        self.assertEqual(pkg_resources._namespace_packages["pkg1"], ["pkg1.pkg2"])
        # check the __path__ attribute contains both paths
        self.assertEqual(pkg1.pkg2.__path__, [
            os.path.join(self._tmpdir, "site-pkgs", "pkg1", "pkg2"),
            os.path.join(self._tmpdir, "site-pkgs2", "pkg1", "pkg2")]) 
开发者ID:MayOneUS,项目名称:pledgeservice,代码行数:37,代码来源:test_resources.py

示例6: patched_path

# 需要导入模块: import pkg_resources [as 别名]
# 或者: from pkg_resources import _namespace_packages [as 别名]
def patched_path(self, tmpdir):
        """
        Patch sys.path to include the 'site-pkgs' dir. Also
        restore pkg_resources._namespace_packages to its
        former state.
        """
        saved_ns_pkgs = pkg_resources._namespace_packages.copy()
        saved_sys_path = sys.path[:]
        site_pkgs = tmpdir.mkdir('site-pkgs')
        sys.path.append(str(site_pkgs))
        try:
            yield
        finally:
            pkg_resources._namespace_packages = saved_ns_pkgs
            sys.path = saved_sys_path 
开发者ID:pypa,项目名称:pkg_resources,代码行数:17,代码来源:test_resources.py

示例7: test_two_levels_deep

# 需要导入模块: import pkg_resources [as 别名]
# 或者: from pkg_resources import _namespace_packages [as 别名]
def test_two_levels_deep(self, symlinked_tmpdir):
        """
        Test nested namespace packages
        Create namespace packages in the following tree :
            site-packages-1/pkg1/pkg2
            site-packages-2/pkg1/pkg2
        Check both are in the _namespace_packages dict and that their __path__
        is correct
        """
        real_tmpdir = symlinked_tmpdir.realpath()
        tmpdir = symlinked_tmpdir
        sys.path.append(str(tmpdir / 'site-pkgs2'))
        site_dirs = tmpdir / 'site-pkgs', tmpdir / 'site-pkgs2'
        for site in site_dirs:
            pkg1 = site / 'pkg1'
            pkg2 = pkg1 / 'pkg2'
            pkg2.ensure_dir()
            (pkg1 / '__init__.py').write_text(self.ns_str, encoding='utf-8')
            (pkg2 / '__init__.py').write_text(self.ns_str, encoding='utf-8')
        import pkg1
        assert "pkg1" in pkg_resources._namespace_packages
        # attempt to import pkg2 from site-pkgs2
        import pkg1.pkg2
        # check the _namespace_packages dict
        assert "pkg1.pkg2" in pkg_resources._namespace_packages
        assert pkg_resources._namespace_packages["pkg1"] == ["pkg1.pkg2"]
        # check the __path__ attribute contains both paths
        expected = [
            str(real_tmpdir / "site-pkgs" / "pkg1" / "pkg2"),
            str(real_tmpdir / "site-pkgs2" / "pkg1" / "pkg2"),
        ]
        assert pkg1.pkg2.__path__ == expected 
开发者ID:pypa,项目名称:pkg_resources,代码行数:34,代码来源:test_resources.py

示例8: test_two_levels_deep

# 需要导入模块: import pkg_resources [as 别名]
# 或者: from pkg_resources import _namespace_packages [as 别名]
def test_two_levels_deep(self):
        """
        Test nested namespace packages
        Create namespace packages in the following tree :
            site-packages-1/pkg1/pkg2
            site-packages-2/pkg1/pkg2
        Check both are in the _namespace_packages dict and that their __path__
        is correct
        """
        sys.path.append(os.path.join(self._tmpdir, "site-pkgs2"))
        os.makedirs(os.path.join(self._tmpdir, "site-pkgs", "pkg1", "pkg2"))
        os.makedirs(os.path.join(self._tmpdir, "site-pkgs2", "pkg1", "pkg2"))
        ns_str = "__import__('pkg_resources').declare_namespace(__name__)\n"
        for site in ["site-pkgs", "site-pkgs2"]:
            pkg1_init = open(os.path.join(self._tmpdir, site,
                             "pkg1", "__init__.py"), "w")
            pkg1_init.write(ns_str)
            pkg1_init.close()
            pkg2_init = open(os.path.join(self._tmpdir, site,
                             "pkg1", "pkg2", "__init__.py"), "w")
            pkg2_init.write(ns_str)
            pkg2_init.close()
        import pkg1
        assert "pkg1" in pkg_resources._namespace_packages
        try:
            import pkg1.pkg2
        except ImportError:
            self.fail("Setuptools tried to import the parent namespace package")
        # check the _namespace_packages dict
        assert "pkg1.pkg2" in pkg_resources._namespace_packages
        assert pkg_resources._namespace_packages["pkg1"] == ["pkg1.pkg2"]
        # check the __path__ attribute contains both paths
        expected = [
            os.path.join(self._tmpdir, "site-pkgs", "pkg1", "pkg2"),
            os.path.join(self._tmpdir, "site-pkgs2", "pkg1", "pkg2"),
        ]
        assert pkg1.pkg2.__path__ == expected 
开发者ID:aliyun,项目名称:oss-ftp,代码行数:39,代码来源:test_resources.py

示例9: setup_method

# 需要导入模块: import pkg_resources [as 别名]
# 或者: from pkg_resources import _namespace_packages [as 别名]
def setup_method(self, method):
        self._ns_pkgs = pkg_resources._namespace_packages.copy()
        self._tmpdir = tempfile.mkdtemp(prefix="tests-setuptools-")
        os.makedirs(os.path.join(self._tmpdir, "site-pkgs"))
        self._prev_sys_path = sys.path[:]
        sys.path.append(os.path.join(self._tmpdir, "site-pkgs")) 
开发者ID:francelabs,项目名称:datafari,代码行数:8,代码来源:test_resources.py

示例10: teardown_method

# 需要导入模块: import pkg_resources [as 别名]
# 或者: from pkg_resources import _namespace_packages [as 别名]
def teardown_method(self, method):
        shutil.rmtree(self._tmpdir)
        pkg_resources._namespace_packages = self._ns_pkgs.copy()
        sys.path = self._prev_sys_path[:] 
开发者ID:francelabs,项目名称:datafari,代码行数:6,代码来源:test_resources.py

示例11: test_two_levels_deep

# 需要导入模块: import pkg_resources [as 别名]
# 或者: from pkg_resources import _namespace_packages [as 别名]
def test_two_levels_deep(self):
        """
        Test nested namespace packages
        Create namespace packages in the following tree :
            site-packages-1/pkg1/pkg2
            site-packages-2/pkg1/pkg2
        Check both are in the _namespace_packages dict and that their __path__
        is correct
        """
        sys.path.append(os.path.join(self._tmpdir, "site-pkgs2"))
        os.makedirs(os.path.join(self._tmpdir, "site-pkgs", "pkg1", "pkg2"))
        os.makedirs(os.path.join(self._tmpdir, "site-pkgs2", "pkg1", "pkg2"))
        ns_str = "__import__('pkg_resources').declare_namespace(__name__)\n"
        for site in ["site-pkgs", "site-pkgs2"]:
            pkg1_init = open(os.path.join(self._tmpdir, site,
                             "pkg1", "__init__.py"), "w")
            pkg1_init.write(ns_str)
            pkg1_init.close()
            pkg2_init = open(os.path.join(self._tmpdir, site,
                             "pkg1", "pkg2", "__init__.py"), "w")
            pkg2_init.write(ns_str)
            pkg2_init.close()
        import pkg1
        assert "pkg1" in pkg_resources._namespace_packages
        # attempt to import pkg2 from site-pkgs2
        import pkg1.pkg2
        # check the _namespace_packages dict
        assert "pkg1.pkg2" in pkg_resources._namespace_packages
        assert pkg_resources._namespace_packages["pkg1"] == ["pkg1.pkg2"]
        # check the __path__ attribute contains both paths
        expected = [
            os.path.join(self._tmpdir, "site-pkgs", "pkg1", "pkg2"),
            os.path.join(self._tmpdir, "site-pkgs2", "pkg1", "pkg2"),
        ]
        assert pkg1.pkg2.__path__ == expected 
开发者ID:francelabs,项目名称:datafari,代码行数:37,代码来源:test_resources.py

示例12: __enter__

# 需要导入模块: import pkg_resources [as 别名]
# 或者: from pkg_resources import _namespace_packages [as 别名]
def __enter__(self):
    # pkg_resources comes with setuptools.
    try:
      import pkg_resources
      nsdict = copy.deepcopy(pkg_resources._namespace_packages)
      declare_namespace = pkg_resources.declare_namespace
      pkg_resources.declare_namespace = self._declare_namespace
    except ImportError:
      nsdict = None
      declare_namespace = None

    # Save the global importer state.
    self.state = {
      'nsdict': nsdict,
      'declare_namespace': declare_namespace,
      'nspaths': {},
      'path': sys.path[:],
      'meta_path': sys.meta_path[:],
      'disables': {},
      'pkgutil.extend_path': pkgutil.extend_path,
    }

    # Update the systems meta path and apply function mocks.
    sys.path[:] = self.path
    sys.meta_path[:] = self.meta_path + sys.meta_path
    pkgutil.extend_path = extend_path

    # If this function is called not the first time, we need to
    # restore the modules that have been imported with it and
    # temporarily disable the ones that would be shadowed.
    for key, mod in items(self.modules):
      try: self.state['disables'][key] = sys.modules.pop(key)
      except KeyError: pass
      sys.modules[key] = mod

    # Evaluate imports from the .pth files, if any.
    for fn, lineno, stmt in self.pth_imports:
      exec_pth_import(fn, lineno, stmt)

    # Add the original path to sys.path.
    sys.path += self.state['path']

    # Update the __path__ of all namespace modules.
    for key, mod in items(sys.modules):
      if mod is None:
        # Relative imports could have lead to None-entries in
        # sys.modules. Get rid of them so they can be re-evaluated.
        prefix = key.rpartition('.')[0]
        if hasattr(sys.modules.get(prefix), '__path__'):
          del sys.modules[key]
      elif hasattr(mod, '__path__'):
        self.state['nspaths'][key] = copy.copy(mod.__path__)
        mod.__path__ = pkgutil.extend_path(mod.__path__, mod.__name__)

    self.in_context = True
    if self.do_autodisable:
      self.autodisable()
    return self 
开发者ID:NiklasRosenstein,项目名称:c4ddev,代码行数:60,代码来源:localimport.py


注:本文中的pkg_resources._namespace_packages方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。