conditional models with cached re.compile, thanks Anthony

This commit is contained in:
mdipierro
2012-09-02 12:12:59 -05:00
parent 2295b93f32
commit 71c44e62b8
3 changed files with 22 additions and 6 deletions
+1 -1
View File
@@ -1 +1 @@
Version 2.0.6 (2012-09-02 11:58:52) stable Version 2.0.6 (2012-09-02 12:12:55) stable
-1
View File
@@ -40,7 +40,6 @@ __all__ = ['Cache', 'lazy_cache']
DEFAULT_TIME_EXPIRE = 300 DEFAULT_TIME_EXPIRE = 300
class CacheAbstract(object): class CacheAbstract(object):
""" """
Abstract class for cache implementations. Abstract class for cache implementations.
+20 -3
View File
@@ -91,6 +91,15 @@ def _TEST():
_TEST() _TEST()
""" """
CACHED_REGEXES = {}
def re_compile(regex):
try:
return CACHED_REGEXES[regex]
except KeyError:
compiled_regex = CACHED_REGEXES[regex] = re.compile(regex)
return compiled_regex
class mybuiltin(object): class mybuiltin(object):
""" """
NOTE could simple use a dict and populate it, NOTE could simple use a dict and populate it,
@@ -355,6 +364,10 @@ def build_environment(request, response, session, store_current=True):
environment.update((k,getattr(v, k)) for k in v.__all__) environment.update((k,getattr(v, k)) for k in v.__all__)
if not request.env: if not request.env:
request.env = Storage() request.env = Storage()
# Enable standard conditional models (i.e., /*.py, /[controller]/*.py, and
# /[controller]/[function]/*.py)
response.models_to_run = [r'^\w+\.py$', r'^%s/\w+\.py$' % request.controller,
r'^%s/%s/\w+\.py$' % (request.controller, request.function)]
t = environment['T'] = translator(request) t = environment['T'] = translator(request)
c = environment['cache'] = Cache(request) c = environment['cache'] = Cache(request)
@@ -485,9 +498,13 @@ def run_models_in(environment):
path = pjoin(folder, 'models') path = pjoin(folder, 'models')
models = listdir(path, '^\w+\.py$',0,sort=False) models = listdir(path, '^\w+\.py$',0,sort=False)
compiled=False compiled=False
paths = (path, pjoin(path,c), pjoin(path,c,f)) n = len(path) + 1
for model in models: for model in models:
if not os.path.split(model)[0] in paths and c!='appadmin': regex = environment['response'].models_to_run
if isinstance(regex, list):
regex = re_compile('|'.join(regex))
file = model[n:].replace(os.path.sep, '/').replace('.pyc', '.py')
if not regex.search(file) and c!= 'appadmin':
continue continue
elif compiled: elif compiled:
code = read_pyc(model) code = read_pyc(model)
@@ -583,7 +600,7 @@ def run_view_in(environment):
badv = 'invalid view (%s)' % view badv = 'invalid view (%s)' % view
if response.generic_patterns: if response.generic_patterns:
patterns = response.generic_patterns patterns = response.generic_patterns
regex = re.compile('|'.join(map(fnmatch.translate, patterns))) regex = re_compile('|'.join(map(fnmatch.translate, patterns)))
short_action = '%(controller)s/%(function)s.%(extension)s' % request short_action = '%(controller)s/%(function)s.%(extension)s' % request
allow_generic = regex.search(short_action) allow_generic = regex.search(short_action)
else: else: