Created
June 1, 2012 01:33
-
-
Save pjz/2847916 to your computer and use it in GitHub Desktop.
aspen auth filters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def by_regex(hook, regex_tuples, default=True): | |
"""A filter for hooks. regex_tuples is a list of (regex, filter?) where if the regex matches | |
the requested URI, then the hook is applied or not based on if filter? is True or False. | |
""" | |
regex_res = [ (re.compile(regex), disposition) for regex, disposition in regex_tuples.iteritems() ] | |
def filtered_hook(request): | |
for regex, disposition in regex_res: | |
if regex.matches(request.line.uri): | |
if disposition: | |
return hook(request) | |
else: | |
return request | |
return default | |
return filtered_hook | |
def by_dict(hook, truthdict, default=True): | |
"""A filter for hooks. | |
truthdict is a mapping of URI -> filter? where if the requested URI is a key in the dict, then | |
the hook is applied based on the filter? value. | |
""" | |
def filtered_hook(request): | |
do_hook = truthdict.get(request.line.uri, default) | |
if do_hook: | |
return hook(request) | |
else: | |
return request | |
return filtered_hook |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment