From 64372401084889a440c9d990a0febc221e3e4b5c Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Mon, 25 Oct 2010 10:29:31 -0400 Subject: first pass at converting bfg to pyramid namespace --- repoze/bfg/path.py | 69 ------------------------------------------------------ 1 file changed, 69 deletions(-) delete mode 100644 repoze/bfg/path.py (limited to 'repoze/bfg/path.py') diff --git a/repoze/bfg/path.py b/repoze/bfg/path.py deleted file mode 100644 index b5850968f..000000000 --- a/repoze/bfg/path.py +++ /dev/null @@ -1,69 +0,0 @@ -import os -import pkg_resources -import sys -import imp - -ignore_types = [ imp.C_EXTENSION, imp.C_BUILTIN ] -init_names = [ '__init__%s' % x[0] for x in imp.get_suffixes() if - x[0] and x[2] not in ignore_types ] - -def caller_path(path, level=2): - if not os.path.isabs(path): - module = caller_module(level+1) - prefix = package_path(module) - path = os.path.join(prefix, path) - return path - -def caller_module(level=2): - module_globals = sys._getframe(level).f_globals - module_name = module_globals['__name__'] - 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.""" - if pkg_or_module is None: - return '__main__' - pkg_filename = pkg_or_module.__file__ - pkg_name = pkg_or_module.__name__ - splitted = os.path.split(pkg_filename) - if splitted[-1] in init_names: - # it's a package - return pkg_name - return pkg_name.rsplit('.', 1)[0] - -def package_of(pkg_or_module): - """ Return the package of a module or return the package itself """ - pkg_name = package_name(pkg_or_module) - __import__(pkg_name) - return sys.modules[pkg_name] - -def caller_package(level=2, caller_module=caller_module): - # caller_module in arglist for tests - module = caller_module(level+1) - if '__init__.py' in getattr(module, '__file__', ''): # empty at >>> - # Module is a package - return module - # Go up one level to get package - package_name = module.__name__.rsplit('.', 1)[0] - return sys.modules[package_name] - -def package_path(package): - # computing the abspath is actually kinda expensive so we memoize - # the result - prefix = getattr(package, '__bfg_abspath__', None) - if prefix is None: - prefix = pkg_resources.resource_filename(package.__name__, '') - # pkg_resources doesn't care whether we feed it a package - # name or a module name within the package, the result - # will be the same: a directory name to the package itself - try: - package.__bfg_abspath__ = prefix - except: - # this is only an optimization, ignore any error - pass - return prefix - -- cgit v1.2.3