def fetch_hooks(self,api_method): """ Fetch a list of hook functions for a certain api method. """ rx_sep=re.compile('[\./]') parts = rx_sep.split(api_method) hooks = {} # loop hook paths for p in self.path: hook_path = os.path.join(p,os.path.join(*parts)) if os.path.exists(hook_path): # the api_path has a corresponding hook dir run through the files for f in os.listdir(hook_path): # parse the hook file hookfile = os.path.join(hook_path,f) try: hookfile = os.path.join(hook_path,f) exec(open(os.path.join(hook_path,f)).read()) except Exception, e: log.write('Failed to parse "%s": %s' % (f,e),context="hooks") continue # Check for required hook function if locals().has_key('hook'): # add the hook to the list of hooks hooks[hookfile] = hook # remove the hook func from locals locals().pop('hook')
def call_hooks(self,api_method,**args): hooks = self.fetch_hooks(api_method) for f,h in hooks.items(): try: h(**args) except Exception, e: log.write('Failed while executing "%s": %s' % (f,e),context="hooks") continue