Advertisement
Uno-Dan

Untitled

Nov 11th, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.73 KB | None | 0 0
  1. ########################################################################################################################
  2. #    File: base.py
  3. #  Author: Dan Huckson, https://github.com/unodan
  4. #    Date: 2018-11-11
  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 add(self, target, hook, callback):
  23.  
  24.         if target not in self.hooks:
  25.             self.hooks[target] = {}
  26.  
  27.         if hook not in self.hooks[target]:
  28.             self.hooks[target][hook] = {}
  29.  
  30.         if callback not in self.hooks[target][hook].values():
  31.             key = target + '/' + hook + '/' + str(callback).strip('>').split(' ')[-1]
  32.             self.hooks[target][hook][key] = callback
  33.             return True
  34.  
  35.         return False
  36.  
  37.     def remove(self, target, hook, callback):
  38.         key = None
  39.         for _key, _callback in self.hooks[target][hook].items():
  40.             if callback == _callback:
  41.                 key = _key
  42.                 break
  43.  
  44.         if key:
  45.             del self.hooks[target][hook][key]
  46.             return True
  47.  
  48.         return False
  49.  
  50.     def get_hook(self, target, hook):
  51.         if hook in self.hooks[target]:
  52.             return self.hooks[target][hook]
  53.  
  54.         return False
  55.  
  56.     def get_hooks(self, target):
  57.         if target in self.hooks:
  58.             return self.hooks[target]
  59.  
  60.         return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement