mirror of
https://github.com/norohind/SquadsActivityMonitor.git
synced 2025-04-04 17:30:01 +03:00
Implement hook system
This commit is contained in:
parent
a6fda1705e
commit
f389804558
18
Hook.py
Normal file
18
Hook.py
Normal file
@ -0,0 +1,18 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
|
||||
class Hook(ABC): # See at Hook class as to observer in observer pattern
|
||||
"""
|
||||
In order to implement hook, subclass this class and pass instance to HookSystem.add_update_hook
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def update(self, action_id: int, diff: list[dict]) -> None:
|
||||
"""
|
||||
|
||||
:param action_id: action id of diff
|
||||
:param diff: the diff like from /diff/123 endpoint
|
||||
:return:
|
||||
"""
|
||||
|
||||
raise NotImplemented
|
33
HookSystem.py
Normal file
33
HookSystem.py
Normal file
@ -0,0 +1,33 @@
|
||||
import os
|
||||
from Hook import Hook
|
||||
from model import model
|
||||
import importlib.machinery
|
||||
|
||||
|
||||
class HookSystem:
|
||||
hooks_updated: list[Hook] = list()
|
||||
|
||||
def __init__(self):
|
||||
# hooks load
|
||||
for file_name in sorted(os.listdir('hooks')):
|
||||
if file_name.endswith('.py') and not file_name[0] in ['.', '_']:
|
||||
path = os.path.join('hooks', file_name)
|
||||
hook_name = file_name[:-3]
|
||||
module = importlib.machinery.SourceFileLoader(hook_name, path).load_module()
|
||||
setup_func = getattr(module, 'setup', None)
|
||||
if setup_func is not None:
|
||||
setup_func(self)
|
||||
|
||||
else:
|
||||
raise AttributeError(f'No setup method in {file_name} hook')
|
||||
|
||||
def add_update_hook(self, hook: Hook) -> None:
|
||||
self.hooks_updated.append(hook)
|
||||
|
||||
def remove_update_hook(self, hook: Hook) -> None:
|
||||
self.hooks_updated.remove(hook)
|
||||
|
||||
def notify_updated(self, action_id: int) -> None:
|
||||
diff_record = model.get_diff_action_id(action_id)
|
||||
for hook in self.hooks_updated:
|
||||
hook.update(action_id, diff_record)
|
@ -17,3 +17,5 @@ log_level = os.getenv('LOG_LEVEL', 'DEBUG').upper()
|
||||
time_between_requests = os.getenv("TIME_BETWEEN_REQUESTS")
|
||||
|
||||
sqlite2postgres_sqlite_location = os.getenv('SQLITE2POSTGRES_SQLITE_LOCATION')
|
||||
|
||||
discord_hook_url_1 = os.getenv('DISCORD_HOOK_URL_1', None)
|
||||
|
8
main.py
8
main.py
@ -1,9 +1,10 @@
|
||||
import requests
|
||||
from model import model
|
||||
# from EDMCLogging import get_main_logger
|
||||
import utils
|
||||
import HookSystem
|
||||
|
||||
model.open_model()
|
||||
hook_system = HookSystem.HookSystem()
|
||||
|
||||
|
||||
def request_leaderboard(platform_enum: utils.Platform, leaderboard_type_enum: utils.LeaderboardTypes) -> dict:
|
||||
@ -31,7 +32,7 @@ def request_leaderboard(platform_enum: utils.Platform, leaderboard_type_enum: ut
|
||||
|
||||
def get_and_save_leaderboard(platform_enum: utils.Platform, leaderboard_type_enum: utils.LeaderboardTypes) -> None:
|
||||
"""
|
||||
High logic function to get and save information about specified for type and platform leaderboard
|
||||
High logic function to get and save information about specified for type and platform leaderboard and call hooks
|
||||
|
||||
:param platform_enum:
|
||||
:param leaderboard_type_enum:
|
||||
@ -39,7 +40,8 @@ def get_and_save_leaderboard(platform_enum: utils.Platform, leaderboard_type_enu
|
||||
"""
|
||||
|
||||
req = request_leaderboard(platform_enum, leaderboard_type_enum)
|
||||
model.insert_leaderboard_db(req)
|
||||
action_id = model.insert_leaderboard_db(req)
|
||||
hook_system.notify_updated(action_id)
|
||||
|
||||
|
||||
def main():
|
||||
|
@ -17,7 +17,7 @@ class AbstractModel(abc.ABC):
|
||||
raise NotImplemented
|
||||
|
||||
@abc.abstractmethod
|
||||
def insert_leaderboard_db(self, leaderboard_list: dict) -> None:
|
||||
def insert_leaderboard_db(self, leaderboard_list: dict) -> int:
|
||||
raise NotImplemented
|
||||
|
||||
@abc.abstractmethod
|
||||
|
@ -101,7 +101,7 @@ class PostgresModel(AbstractModel):
|
||||
return result
|
||||
|
||||
@errors_catcher
|
||||
def insert_leaderboard_db(self, leaderboard_list: dict) -> None:
|
||||
def insert_leaderboard_db(self, leaderboard_list: dict) -> int:
|
||||
"""
|
||||
Takes leaderboard as list, it platform, type, db connection and insert leaderboard to DB
|
||||
|
||||
@ -128,6 +128,7 @@ class PostgresModel(AbstractModel):
|
||||
self.db.commit()
|
||||
|
||||
cache.delete_all() # drop cache
|
||||
return action_id
|
||||
|
||||
@errors_catcher
|
||||
def get_diff_action_id(self, action_id: int) -> list:
|
||||
|
Loading…
x
Reference in New Issue
Block a user