diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-10-01 18:55:08 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-10-01 18:55:08 -0400 |
commit | 907a37e75e7011ba5cbca0300981800cb2884410 (patch) | |
tree | 44eed47049606eb3036c4da1a85dcc9fbda92ced | |
parent | a6b60799419dd5d304ad2203f6309157bd73c240 (diff) | |
download | mako-907a37e75e7011ba5cbca0300981800cb2884410.tar.gz |
pkg_resources hoops
-rw-r--r-- | mako/cache.py | 22 | ||||
-rw-r--r-- | mako/util.py | 22 |
2 files changed, 39 insertions, 5 deletions
diff --git a/mako/cache.py b/mako/cache.py index 5793987..b5b7ac6 100644 --- a/mako/cache.py +++ b/mako/cache.py @@ -4,7 +4,7 @@ # This module is part of Mako and is released under # the MIT License: http://www.opensource.org/licenses/mit-license.php -from mako import exceptions +from mako import exceptions, util def register_plugin(name, modulename, attrname): @@ -15,7 +15,7 @@ def register_plugin(name, modulename, attrname): """ import pkg_resources - dist = pkg_resources.get_distribution("mako") + dist = util.get_pkg_resources_distribution() entry_map = dist.get_entry_map() if 'mako.cache' not in entry_map: entry_map['mako.cache'] = cache_map = {} @@ -24,7 +24,12 @@ def register_plugin(name, modulename, attrname): cache_map[name] = \ pkg_resources.EntryPoint.parse('%s = %s:%s' % (name, modulename, attrname), dist=dist) -register_plugin("beaker", "mako.ext.beaker_cache", "BeakerCacheImpl") +try: + register_plugin("beaker", "mako.ext.beaker_cache", "BeakerCacheImpl") +except ImportError: + # in case pkg_resources totally not installed + pass + class Cache(object): """Represents a data content cache made available to the module @@ -82,7 +87,16 @@ class Cache(object): self._def_regions = {} def _load_impl(self, name): - import pkg_resources + try: + import pkg_resources + except ImportError: + # hardcode down to Beaker if the environment + # doesn't have pkg_resources installed + if name == 'beaker': + from mako.ext.beaker_cache import BeakerCacheImpl + return BeakerCacheImpl(self) + else: + raise for impl in pkg_resources.iter_entry_points( "mako.cache", name): diff --git a/mako/util.py b/mako/util.py index 5518b4d..6bc100b 100644 --- a/mako/util.py +++ b/mako/util.py @@ -64,7 +64,27 @@ if py24: else: def exception_name(exc): return exc.__class__.__name__ - + +def get_pkg_resources_distribution(): + """Return a pkg_resources.Distribution for Mako. + + Pulls all kinds of strings to ensure one is + available even if Mako is not installed. + + """ + import pkg_resources + try: + dist = pkg_resources.get_distribution("mako") + except: + import mako + dist = pkg_resources.Distribution( + project_name="mako", location="mako", version=mako.__version__ + ) + dist.activate() + pkg_resources.working_set.add(dist) + dist = pkg_resources.get_distribution("mako") + return dist + def verify_directory(dir): """create and/or verify a filesystem directory.""" |