Author: jure
Date: Wed Mar 6 13:21:15 2013
New Revision: 1453323
URL: http://svn.apache.org/r1453323
Log:
Product environment factory + simple LRU cache
Modified:
incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/multiproduct/env.py
incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/multiproduct/hooks.py
incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/multiproduct/util.py
incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_theme/bhtheme/theme.py
Modified:
incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/multiproduct/env.py
URL:
http://svn.apache.org/viewvc/incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/multiproduct/env.py?rev=1453323&r1=1453322&r2=1453323&view=diff
==============================================================================
---
incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/multiproduct/env.py
(original)
+++
incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/multiproduct/env.py
Wed Mar 6 13:21:15 2013
@@ -135,7 +135,7 @@ class Environment(trac.env.Environment):
return True
if needs_upgrade_in_env_list([self], self._global_setup_participants):
return True
- product_envs = [self] + [ProductEnvironment(self, product) for product
in Product.select(self)]
+ product_envs = [self] + [ProductEnvironmentFactory(self, product) for
product in Product.select(self)]
if needs_upgrade_in_env_list(product_envs,
self._product_setup_participants):
return True
return False
@@ -167,7 +167,7 @@ class Environment(trac.env.Environment):
return upgraders
def upgraders_for_product_envs():
- product_envs = [self] + [ProductEnvironment(self, product) for
product in Product.select(self)]
+ product_envs = [self] + [ProductEnvironmentFactory(self, product)
for product in Product.select(self)]
return upgraders_for_env_list(product_envs,
self._product_setup_participants)
# first enumerate components that are multi product aware and require
upgrade
@@ -804,3 +804,10 @@ class ProductEnvironment(Component, Comp
self._abs_href = Href(self.base_url)
return self._abs_href
+from multiproduct.util import lru_cache
+
+@lru_cache(maxsize=100)
+def ProductEnvironmentFactory(global_env, product):
+ """Product environment factory
+ """
+ return ProductEnvironment(global_env, product)
Modified:
incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/multiproduct/hooks.py
URL:
http://svn.apache.org/viewvc/incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/multiproduct/hooks.py?rev=1453323&r1=1453322&r2=1453323&view=diff
==============================================================================
---
incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/multiproduct/hooks.py
(original)
+++
incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/multiproduct/hooks.py
Wed Mar 6 13:21:15 2013
@@ -42,5 +42,5 @@ class MultiProductEnvironmentFactory(Env
# happen from within trac.web.main.dispatch_request
req = RequestWithSession(environ, None)
global_env._abs_href = req.abs_href
- env = multiproduct.env.ProductEnvironment(global_env, pid)
+ env = multiproduct.env.ProductEnvironmentFactory(global_env, pid)
return env
Modified:
incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/multiproduct/util.py
URL:
http://svn.apache.org/viewvc/incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/multiproduct/util.py?rev=1453323&r1=1453322&r2=1453323&view=diff
==============================================================================
---
incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/multiproduct/util.py
(original)
+++
incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/multiproduct/util.py
Wed Mar 6 13:21:15 2013
@@ -21,6 +21,9 @@
from trac import db_default
from multiproduct.api import MultiProductSystem
+import collections
+import functools
+
class ProductDelegate(object):
@staticmethod
def add_product(env, product, keys, field_data):
@@ -52,3 +55,24 @@ class ProductDelegate(object):
db.executemany("INSERT INTO permission (%s) VALUES (%s)" %
(','.join(cols), ','.join(['%s' for c in cols])), rows)
+def lru_cache(maxsize=100):
+ """Simple LRU cache decorator, using `collections.OrderedDict` for
+ item store
+ """
+ def wrap(f):
+ cache = collections.OrderedDict()
+ @functools.wraps(f)
+ def wrapped_func(*args, **kwargs):
+ key = args
+ if kwargs:
+ key += tuple(sorted(kwargs.items()))
+ try:
+ item = cache.pop(key)
+ except KeyError:
+ item = f(*args, **kwargs)
+ if len(cache) >= maxsize:
+ cache.popitem(last=False)
+ cache[key] = item
+ return item
+ return wrapped_func
+ return wrap
Modified:
incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_theme/bhtheme/theme.py
URL:
http://svn.apache.org/viewvc/incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_theme/bhtheme/theme.py?rev=1453323&r1=1453322&r2=1453323&view=diff
==============================================================================
---
incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_theme/bhtheme/theme.py
(original)
+++
incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_theme/bhtheme/theme.py
Wed Mar 6 13:21:15 2013
@@ -51,7 +51,7 @@ from urlparse import urlparse
from wsgiref.util import setup_testing_defaults
from multiproduct.model import Product
-from multiproduct.env import ProductEnvironment
+from multiproduct.env import ProductEnvironment, ProductEnvironmentFactory
try:
from multiproduct.ticket.web_ui import ProductTicketModule
@@ -335,7 +335,7 @@ class BloodhoundTheme(ThemeBase):
# Reaquest's permissions are thus copied and associated with
# another ProductEnvironment for each check.
perm = copy.copy(req.perm)
- perm.env = ProductEnvironment(product._env.parent,
product.prefix)
+ perm.env = ProductEnvironmentFactory(product._env.parent,
product.prefix)
if 'PRODUCT_VIEW' in perm(product.resource):
product_list.append(product)
else: