def _internal_init(self): self.init() # automatic register event handlers for event_handler in [h for h in dir(self) if h.endswith('_event')]: event_name = '_'.join(event_handler.split('_')[:-1]) EventManager.register(event_name, getattr(self, event_handler))
def install_module(module_name): """Install a new module. :param module_name: module name :return: is sucess """ module_path = os.path.join(MODULES_PATH, module_name) if os.path.exists(module_path): return False infos = get_module_infos(module_name) if not infos: Log.error('Module "%s" not found' % module_name) return False # install module zip_file = BytesIO(requests.get(infos["zip_url"]).content) with ZipFile(zip_file) as z: # security + locate path files content_dir = "" for member in z.namelist(): if member.startswith("/") or "./" in member: msg = 'Security threat on "%s" module install (%s)' % (module_name, member) Log.critical(msg) EventManager.fire(EventManager.Event("security_thread", __package__, msg=msg)) return False if "Module.py" in member: content_dir = member.replace("Module.py", "") # unzip os.makedirs(module_path) for member in z.namelist(): # skip directories if not os.path.basename(member): continue # unzip path = os.path.join(module_path, member.replace(content_dir, "")) with z.open(member) as s, open(path, "wb") as t: shutil.copyfileobj(s, t) # starting module ModuleManager.reindex_modules() ModuleManager.init(module_name) ModuleManager.start(module_name) return True
def fire(self, event_name, *args, **kwargs): """Fire an Event.""" kwargs['source'] = self return EventManager.fire(EventManager.Event(event_name, *args, **kwargs))
from flask import Flask, request, json from core import EventManager from utilits import settings app = Flask(__name__) manager = EventManager() manager.initialization_handlers() @app.route('/handler/vk') def handler_vk(): # Обработчик запросов от вк try: data = json.loads(request.get_data()) except: return "Data error", 200 vk_settings = settings('vk') if data['type'] == 'confirmation': return vk_settings['code'] if data['secret'] != vk_settings['secret'] or int( data['group_id']) not in vk_settings['groups']: return "Verification error", 200 manager.new_event(data) return "Ok", 200 if __name__ == '__main__': app.run()
from core import EventManager EventManager().initialization_handlers()
def __init__(self): EventManager.bind('security_alert', self.__event_security_alert)