97 lines
3.6 KiB
Python
97 lines
3.6 KiB
Python
from timer import RepeatedTimer
|
|
import sys
|
|
import os
|
|
import importlib
|
|
import inspect
|
|
import json
|
|
|
|
path = os.path.realpath(__file__)
|
|
path = path[:path.rfind('/')]
|
|
|
|
#####################
|
|
# Import extensions #
|
|
#####################
|
|
|
|
# Import checkers #
|
|
# relative imports suck - https://gideonbrimleaf.github.io/2021/01/26/relative-imports-python.html
|
|
sys.path.insert(1, f'{path}/extensions/checkers')
|
|
|
|
# importlib used to import stuff programmatically, rather than using the hardcoded import keyword, basically the same but it seems it can't import *just* a class
|
|
checker_extension_imports = []
|
|
for ext_name in os.listdir(f'{path}/extensions/checkers'):
|
|
if ext_name[0] != '.':
|
|
checker_extension_imports.append(f'{ext_name}.{ext_name}')
|
|
|
|
# checkers contains all the classes for the checker extensions
|
|
# e.g. {'CheckerTemplate': checker_template.checker_template.CheckerTemplate}
|
|
checkers = {}
|
|
for ext in [importlib.import_module(ext) for ext in checker_extension_imports]:
|
|
for name, obj in inspect.getmembers(ext):
|
|
if inspect.isclass(obj):
|
|
checkers[name] = obj
|
|
|
|
# Import alerts #
|
|
# same as above, just for alerts
|
|
sys.path.insert(1, f'{path}/extensions/alerts')
|
|
|
|
alerts_extension_imports = []
|
|
for ext_name in os.listdir(f'{path}/extensions/alerts'):
|
|
if ext_name[0] != '.':
|
|
alerts_extension_imports.append(f'{ext_name}.{ext_name}')
|
|
|
|
# alerts contains all the classes for the checker extensions
|
|
# e.g. {'AlertsTemplate': alerts_template.alerts_template.AlertsTemplate}
|
|
alerts = {}
|
|
for ext in [importlib.import_module(ext) for ext in alerts_extension_imports]:
|
|
for name, obj in inspect.getmembers(ext):
|
|
if inspect.isclass(obj):
|
|
alerts[name] = obj
|
|
|
|
# Import logging #
|
|
# same as above, just for logging
|
|
sys.path.insert(1, f'{path}/extensions/logging')
|
|
|
|
logging_extension_imports = []
|
|
for ext_name in os.listdir(f'{path}/extensions/logging'):
|
|
if ext_name[0] != '.':
|
|
logging_extension_imports.append(f'{ext_name}.{ext_name}')
|
|
|
|
# logging contains all the classes for the checker extensions
|
|
# e.g. {'LoggingTemplate': logging_template.logging_template.LoggingTemplate}
|
|
logging = {}
|
|
for ext in [importlib.import_module(ext) for ext in logging_extension_imports]:
|
|
for name, obj in inspect.getmembers(ext):
|
|
if inspect.isclass(obj):
|
|
logging[name] = obj
|
|
|
|
# get config from services.json
|
|
if 'services.json' in os.listdir():
|
|
config_filename = 'services.json'
|
|
elif 'services-example.json' in os.listdir():
|
|
config_filename = 'services-example.json'
|
|
|
|
with open(config_filename, 'rt') as config_file:
|
|
config = json.loads(''.join(config_file.readlines()))
|
|
|
|
|
|
def create_instances(config):
|
|
'''
|
|
Creates instances of all the extensions according to config
|
|
Parameters:
|
|
config: the dictionary containing the config
|
|
Returns:
|
|
instances (dict): A dictionary containing instances of the extensions
|
|
example: {'site': {'checker': instanceOfCheckerTemplate}}
|
|
'''
|
|
instances = {}
|
|
for service in config.keys():
|
|
instances[service] = {}
|
|
# just creates an instance of the checker with the arguments for it
|
|
instances[service]['checker'] = checkers[config[service]
|
|
['checker']](config[service]['checker-args'])
|
|
instances[service]['alerts'] = alerts[config[service]
|
|
['alerts']](config[service]['alerts-args'])
|
|
instances[service]['logging'] = logging[config[service]
|
|
['logging']](config[service]['logging-args'])
|
|
|
|
return instances
|