diff options
| author | Chris McDonough <chrism@agendaless.com> | 2009-08-27 14:50:12 +0000 |
|---|---|---|
| committer | Chris McDonough <chrism@agendaless.com> | 2009-08-27 14:50:12 +0000 |
| commit | 6efd813a8bc00c64d8652655036cf5e003982753 (patch) | |
| tree | e21da90770afeab05e302cf57372cde57d2ebc97 | |
| parent | 53edffcc361c057e5349dd257998e3b281a2a801 (diff) | |
| download | pyramid-6efd813a8bc00c64d8652655036cf5e003982753.tar.gz pyramid-6efd813a8bc00c64d8652655036cf5e003982753.tar.bz2 pyramid-6efd813a8bc00c64d8652655036cf5e003982753.zip | |
Add package_name function.
""" If this function is passed a module, return the dotted Python
package name of the package in which the module lives. If this
function is passed a package, return the dotted Python package
name of the package itself."""
| -rw-r--r-- | repoze/bfg/path.py | 13 | ||||
| -rw-r--r-- | repoze/bfg/tests/test_path.py | 18 |
2 files changed, 31 insertions, 0 deletions
diff --git a/repoze/bfg/path.py b/repoze/bfg/path.py index b79ff3816..6963d651f 100644 --- a/repoze/bfg/path.py +++ b/repoze/bfg/path.py @@ -16,6 +16,19 @@ def caller_module(level=2): module = sys.modules[module_name] return module +def package_name(pkg_or_module): + """ If this function is passed a module, return the dotted Python + package name of the package in which the module lives. If this + function is passed a package, return the dotted Python package + name of the package itself.""" + pkg_filename = pkg_or_module.__file__ + pkg_name = pkg_or_module.__name__ + splitted = os.path.split(pkg_filename) + if splitted[-1] in ('__init__.py', '__init__.pyc', '__init__.pyo'): + # it's a package + return pkg_name + return pkg_name.rsplit('.', 1)[0] + def caller_package(level=2, caller_module=caller_module): # caller_module in arglist for tests module = caller_module(level+1) diff --git a/repoze/bfg/tests/test_path.py b/repoze/bfg/tests/test_path.py index 636481fde..a6ff94327 100644 --- a/repoze/bfg/tests/test_path.py +++ b/repoze/bfg/tests/test_path.py @@ -112,6 +112,23 @@ class TestPackagePath(unittest.TestCase): result = self._callFUT(module) self.failIf(hasattr(module, '__bfg_abspath__')) self.assertEqual(result, module.package_path) + +class TestPackageName(unittest.TestCase): + def _callFUT(self, package): + from repoze.bfg.path import package_name + return package_name(package) + + def test_it_package(self): + from repoze.bfg import tests + package = DummyPackageOrModule(tests) + result = self._callFUT(package) + self.assertEqual(result, 'repoze.bfg.tests') + + def test_it_module(self): + from repoze.bfg.tests import test_path + module = DummyPackageOrModule(test_path) + result = self._callFUT(module) + self.assertEqual(result, 'repoze.bfg.tests') class DummyPackageOrModule: def __init__(self, real_package_or_module, raise_exc=None): @@ -120,6 +137,7 @@ class DummyPackageOrModule: import os self.__dict__['package_path'] = os.path.dirname( os.path.abspath(real_package_or_module.__file__)) + self.__dict__['__file__'] = real_package_or_module.__file__ def __setattr__(self, key, val): if self.raise_exc is not None: |
