summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2011-10-02 08:54:07 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2011-10-02 08:54:07 -0400
commit4bfdb2fd60035c2e7dbee66db6eb8fd4aec2a389 (patch)
tree67047bf88314d1bc68a2c70a30aa87b8a7ed5f8a
parentb19513c8f908ae03cdd63578789b075c3da7e8e8 (diff)
downloadmako-4bfdb2fd60035c2e7dbee66db6eb8fd4aec2a389.tar.gz
undo all that pkg_resources crap and just cache what we get from iter_entry_points.
pkg_resources API feels very fragile
-rw-r--r--mako/cache.py52
-rw-r--r--mako/util.py58
2 files changed, 34 insertions, 76 deletions
diff --git a/mako/cache.py b/mako/cache.py
index 2e2b09e..2caa131 100644
--- a/mako/cache.py
+++ b/mako/cache.py
@@ -6,33 +6,10 @@
from mako import exceptions, util
+_cache_plugins = util.PluginLoader("mako.cache")
-def register_plugin(name, modulename, attrname):
- """Register the given :class:`.CacheImpl` under the given
- name.
-
- This is an alternative to using a setuptools-installed entrypoint,
- and will work even if Mako isn't installed. ``pkg_resources`` is
- required, however.
-
- """
- import pkg_resources
- 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 = {}
- else:
- cache_map = entry_map['mako.cache']
- cache_map[name] = \
- pkg_resources.EntryPoint.parse(
- '%s = %s:%s' %
- (name, modulename, attrname), dist=dist)
-
-try:
- register_plugin("beaker", "mako.ext.beaker_cache", "BeakerCacheImpl")
-except ImportError:
- # in case pkg_resources totally not installed
- pass
+register_plugin = _cache_plugins.register
+register_plugin("beaker", "mako.ext.beaker_cache", "BeakerCacheImpl")
class Cache(object):
@@ -91,24 +68,7 @@ class Cache(object):
self._def_regions = {}
def _load_impl(self, name):
- 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):
- return impl.load()(self)
- else:
- raise exceptions.RuntimeException(
- "Cache implementation '%s' not present" %
- name)
+ return _cache_plugins.load(name)(self)
def get_and_replace(self, key, creation_function, **kw):
"""Retrieve a value from the cache, using the given creation function
@@ -117,7 +77,9 @@ class Cache(object):
if not self.template.cache_enabled:
return creation_function()
- return self.impl.get_and_replace(key, creation_function, **self._get_cache_kw(kw))
+ return self.impl.get_and_replace(key,
+ creation_function,
+ **self._get_cache_kw(kw))
def put(self, key, value, **kw):
"""Place a value in the cache.
diff --git a/mako/util.py b/mako/util.py
index 31fcf70..3cc688b 100644
--- a/mako/util.py
+++ b/mako/util.py
@@ -65,37 +65,33 @@ else:
def exception_name(exc):
return exc.__class__.__name__
-def get_pkg_resources_distribution():
- """Return a pkg_resources.Distribution for Mako.
-
- Creates a fake distribution if Mako is not installed,
- so that tests/apps/etc. can use register_plugin.
-
- """
- import pkg_resources
- try:
- # would like to make this >= 0.5.1, but
- # pkg_resources won't install us if another
- # mako already present as no, no, you're now
- # a "hidden distro" (not documented,
- # just in their source code, no warning/exception,
- # sure seems like silent failure to me...)
- return pkg_resources.get_distribution("Mako")
- except:
- # make a "fake" Distribution, which we very much
- # hope makes it so something global is returned
- # when we say get_distribution() so we can hang
- # our plugins in the same way as when we're
- # installed.
- import mako
- dist = pkg_resources.Distribution(
- pkg_resources.normalize_path(
- os.path.dirname(os.path.dirname(mako.__file__))),
- project_name="Mako",
- version=mako.__version__,
- )
- pkg_resources.working_set.add(dist)
- return pkg_resources.get_distribution("Mako")
+class PluginLoader(object):
+ def __init__(self, group):
+ self.group = group
+ self.impls = {}
+
+ def load(self, name):
+ if name in self.impls:
+ return self.impls[name]()
+ else:
+ import pkg_resources
+ for impl in pkg_resources.iter_entry_points(
+ self.group,
+ name):
+ self.impls[name] = impl.load
+ return impl.load()
+ else:
+ raise exceptions.RuntimeException(
+ "Can't load plugin %s %s" %
+ (self.group, name))
+
+ def register(self, name, modulepath, objname):
+ def load():
+ mod = __import__(modulepath)
+ for token in modulepath.split(".")[1:]:
+ mod = getattr(mod, token)
+ return getattr(mod, objname)
+ self.impls[name] = load
def verify_directory(dir):
"""create and/or verify a filesystem directory."""