| 1 |
import os |
| 2 |
|
| 3 |
# this file implements the mechanism of conf class auto-registration, |
| 4 |
# don't modify this file if you have no idea what you're doing |
| 5 |
|
| 6 |
|
| 7 |
def gen_hook(): |
| 8 |
hook_table = {} |
| 9 |
|
| 10 |
class Wrapper: |
| 11 |
""" |
| 12 |
Decorator class which implements the conf class registration. |
| 13 |
""" |
| 14 |
def __init__(self, alias=None): |
| 15 |
self.alias = alias |
| 16 |
|
| 17 |
def __call__(self, cls): |
| 18 |
# register the class object with the name of the class |
| 19 |
hook_table[cls.__name__] = cls |
| 20 |
if self.alias: |
| 21 |
# also register the alias of the class |
| 22 |
hook_table[self.alias] = cls |
| 23 |
|
| 24 |
return cls |
| 25 |
|
| 26 |
def find_hook(name): |
| 27 |
try: |
| 28 |
return hook_table[name] |
| 29 |
except: |
| 30 |
raise AttributeError |
| 31 |
|
| 32 |
return Wrapper, find_hook |
| 33 |
|
| 34 |
_register, find_conf = gen_hook() |
| 35 |
hook = rule = _register |
| 36 |
|
| 37 |
__all__ = ['hook', 'rule'] |
| 38 |
|
| 39 |
for module in os.listdir(os.path.dirname(__file__)): |
| 40 |
# import every module under this package except __init__.py, |
| 41 |
# so that the decorator `register` applies |
| 42 |
# (nothing happens if the script is not loaded) |
| 43 |
if module != '__init__.py' and module.endswith('.py'): |
| 44 |
module_name = module[:-3] |
| 45 |
mod = __import__('%s.%s' % (__name__, module_name), |
| 46 |
globals(), |
| 47 |
locals()) |
| 48 |
__all__.append(module_name) |