Advertisement
Uno-Dan

Untitled

Nov 10th, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.46 KB | None | 0 0
  1. ########################################################################################################################
  2. #    File: base.py
  3. #  Author: Dan Huckson, https://github.com/unodan
  4. #    Date: 2018-11-10
  5. ########################################################################################################################
  6.  
  7. class Hookman(object):
  8.     instance = None
  9.  
  10.     def __new__(cls, **kwargs):
  11.         if not cls.instance:
  12.             cls.instance = super(Hookman, cls).__new__(cls)
  13.             cls.instance.manager = _HookManager()
  14.  
  15.         return cls.instance.manager
  16.  
  17.  
  18. class _HookManager(object):
  19.     def __init__(self):
  20.         self.hooks = {}
  21.  
  22.     def register(self, target, hook, callback):
  23.  
  24.         if target not in self.hooks:
  25.             self.hooks[target] = {hook: {}}
  26.  
  27.         key = target + '/' + hook + '/' + str(len(self.hooks[target][hook]))
  28.         self.hooks[target][hook].update({key: callback})
  29.  
  30.     def remove(self, target, hook, callback):
  31.         key = None
  32.         for _key, _callback in self.hooks[target][hook].items():
  33.             if callback == _callback:
  34.                 key = _key
  35.                 break
  36.            
  37.         if key:    
  38.             del self.hooks[target][hook][key]
  39.  
  40.     def get_hook(self, target, hook):
  41.         if hook in self.hooks[target]:
  42.             return self.hooks[target][hook]
  43.  
  44.     def get_hooks(self, target):
  45.         if target in self.hooks:
  46.             return self.hooks[target]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement