Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ########################################################################################################################
- # File: base.py
- # Author: Dan Huckson, https://github.com/unodan
- # Date: 2018-11-11
- ########################################################################################################################
- class Hookman(object):
- instance = None
- def __new__(cls, **kwargs):
- if not cls.instance:
- cls.instance = super(Hookman, cls).__new__(cls)
- cls.instance.manager = _HookManager()
- return cls.instance.manager
- class _HookManager(object):
- def __init__(self):
- self.hooks = {}
- def add(self, target, hook, callback):
- if target not in self.hooks:
- self.hooks[target] = {}
- if hook not in self.hooks[target]:
- self.hooks[target][hook] = {}
- if callback not in self.hooks[target][hook].values():
- key = target + '/' + hook + '/' + str(callback).strip('>').split(' ')[-1]
- self.hooks[target][hook][key] = callback
- return True
- return False
- def remove(self, target, hook, callback):
- key = None
- for _key, _callback in self.hooks[target][hook].items():
- if callback == _callback:
- key = _key
- break
- if key:
- del self.hooks[target][hook][key]
- return True
- return False
- def get_hook(self, target, hook):
- if hook in self.hooks[target]:
- return self.hooks[target][hook]
- return False
- def get_hooks(self, target):
- if target in self.hooks:
- return self.hooks[target]
- return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement