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