diff options
author | Jonas Gunz <himself@jonasgunz.de> | 2023-02-10 00:50:13 +0100 |
---|---|---|
committer | Jonas Gunz <himself@jonasgunz.de> | 2023-02-10 00:50:13 +0100 |
commit | a7aee0c454fd830b4475d5ce1a334a51be05dfaf (patch) | |
tree | 3f8bb7edc725969ef585f6457d377068f39e2db3 | |
parent | b904ef52c5c6df6e720a91675d9ca489ea59590e (diff) | |
download | automato-a7aee0c454fd830b4475d5ce1a334a51be05dfaf.tar.gz |
logging
-rw-r--r-- | automato/action.py | 15 | ||||
-rwxr-xr-x | automato/command_line.py | 14 | ||||
-rw-r--r-- | automato/endpoint.py | 15 | ||||
-rw-r--r-- | automato/state.py | 9 | ||||
-rw-r--r-- | automato/trigger.py | 17 |
5 files changed, 36 insertions, 34 deletions
diff --git a/automato/action.py b/automato/action.py index 7bb50ea..785a0ee 100644 --- a/automato/action.py +++ b/automato/action.py @@ -1,5 +1,6 @@ from typing import Dict import logging +logger = logging.getLogger(__name__) from . import endpoint from . import trigger @@ -21,37 +22,37 @@ class Action: def _setup_triggers(self): for trg_list_item in self._trigger_cfg: if len(trg_list_item.keys()) != 1: - logging.error(f'Action "{self._name}" encountered error while adding trigger "{trg_list_item}"') + logger.error(f'Action "{self._name}" encountered error while adding trigger "{trg_list_item}"') raise Exception trg_key = list(trg_list_item.keys())[0] trg_config = trg_list_item[trg_key] if not trg_key in self._triggers: - logging.error(f'Action "{self._name}": Trigger "{trg_key}" is not configured.') + logger.error(f'Action "{self._name}": Trigger "{trg_key}" is not configured.') raise Exception self._configured_trigger_keys.append(trg_key) self._triggers[trg_key].addInstance(self._name, **trg_config) - logging.debug(f'Action "{self._name}" was registered with "{trg_key}"') + logger.debug(f'Action "{self._name}" was registered with "{trg_key}"') def execute(self): if not all([self._triggers[b].evaluate(self._name) for b in self._configured_trigger_keys]): - logging.debug(f'Action "{self._name}" will not execute. Conditions not met.') + logger.debug(f'Action "{self._name}" will not execute. Conditions not met.') return - logging.info(f'Executing Action "{self._name}". Conditions are met.') + logger.info(f'Executing Action "{self._name}". Conditions are met.') for then_item in self._then_cfg: if len(then_item.keys()) != 1: - logging.error(f'Action "{self._name}" encountered error while executing command "{then_item}"') + logger.error(f'Action "{self._name}" encountered error while executing command "{then_item}"') raise Exception cmd_key = list(then_item.keys())[0] cmd_config = then_item[cmd_key] - logging.info(f'Executing command "{cmd_key}"') + logger.info(f'Executing command "{cmd_key}"') endpoint, command = cmd_key.split('.', 1) self._endpoints[endpoint].executeCommand(command, **cmd_config) diff --git a/automato/command_line.py b/automato/command_line.py index b47bfac..51e70bc 100755 --- a/automato/command_line.py +++ b/automato/command_line.py @@ -8,7 +8,10 @@ import time from automato import transport, state, command, endpoint, trigger, misc, action def main(): - logging.basicConfig(level=logging.DEBUG) + logging.basicConfig(level=logging.DEBUG, + format='%(asctime)s | %(levelname)s | %(name)s - %(message)s', + datefmt='%c') + logging.getLogger('paramiko').setLevel(logging.WARNING) # Use a TypeDict here @@ -45,6 +48,8 @@ def main(): for k in endpoints: endpoints[k].connectTransport() + logging.info('Successfully initialized!') + looptime = 5 # TODO while True: starttime = time.time() @@ -60,10 +65,3 @@ def main(): time.sleep(max(0, looptime - elapsed)) - - - #for act_key in action_config: - # actions[act_key].execute() - - #for act_key in action_config: - # actions[act_key].execute() diff --git a/automato/endpoint.py b/automato/endpoint.py index 411b2e8..2099017 100644 --- a/automato/endpoint.py +++ b/automato/endpoint.py @@ -1,4 +1,5 @@ import logging +logger = logging.getLogger(__name__) from . import transport @@ -18,7 +19,7 @@ class Endpoint: # sweet mother of jesus, you are ugly for tp_key in config['transports']: tp_cfg = config['transports'][tp_key] - logging.debug(f'loading transport "{tp_key}"') + logger.debug(f'loading transport "{tp_key}"') # TODO Handle failure tp_class = import_class(tp_cfg['class']) @@ -28,7 +29,7 @@ class Endpoint: for cmd_key in config['commands']: cmd_cfg = config['commands'][cmd_key] - logging.debug(f'loading command "{cmd_key}"') + logger.debug(f'loading command "{cmd_key}"') # TODO Handle failure cmd_class = import_class(cmd_cfg['class']) @@ -36,7 +37,7 @@ class Endpoint: if cmd_cfg['transport'] not in transports: # TODO should we be lenient with errors? - logging.error(f'transport "{cmd_cfg["transport"]}" for command "{cmd_key}" was not found.') + logger.error(f'transport "{cmd_cfg["transport"]}" for command "{cmd_key}" was not found.') continue tp = transports[cmd_cfg['transport']] @@ -47,7 +48,7 @@ class Endpoint: # you look familiar for stt_key in config['states']: stt_cfg = config['states'][stt_key] - logging.debug(f'loading state "{stt_key}"') + logger.debug(f'loading state "{stt_key}"') # TODO Handle failure stt_class = import_class(stt_cfg['class']) @@ -55,7 +56,7 @@ class Endpoint: if stt_cfg['transport'] not in transports: # TODO should we be lenient with errors? - logging.error(f'transport "{stt_cfg["transport"]}" for command "{stt_key}" was not found.') + logger.error(f'transport "{stt_cfg["transport"]}" for command "{stt_key}" was not found.') continue tp = transports[stt_cfg['transport']] @@ -78,7 +79,7 @@ class Endpoint: elif self._transports[k].CONNECTION == transport.THROWAWAY: self._transports[k].check() else: - logging.error(f'"{self._transports[k].CONNECTION}" is an unknown connection type in transport "{k}"') + logger.error(f'"{self._transports[k].CONNECTION}" is an unknown connection type in transport "{k}"') # forces a recollect of all states. should not be needed, states should # handle that themselves via TTL @@ -93,7 +94,7 @@ class Endpoint: state, key = state_key.split('.', 1) if state not in self._states: - logging.error(f'State "{state}" was not found for "{self._name}"') + logger.error(f'State "{state}" was not found for "{self._name}"') return None return self._states[state].get(key) diff --git a/automato/state.py b/automato/state.py index d891aa5..d34044c 100644 --- a/automato/state.py +++ b/automato/state.py @@ -1,5 +1,6 @@ import time import logging +logger = logging.getLogger(__name__) from . import transport @@ -37,7 +38,7 @@ class State: def _get(self, key: str): if key not in self._data: - logging.error(f'Data key {key} was not found.') + logger.error(f'Data key {key} was not found.') return None return self._data[key] @@ -47,10 +48,10 @@ class State: def get(self, key: str): if self._shouldCollect(): - logging.debug(f'Cached value for "{key}" is too old. refreshing.') + logger.debug(f'Cached value for "{key}" is too old. refreshing.') self.collect() else: - logging.debug(f'Using cached value for "{key}".') + logger.debug(f'Using cached value for "{key}".') return self._get(key) @@ -83,7 +84,7 @@ class UserSessionState(State): for l in lines: name, _ = l.split(' ', 1) - logging.debug(f'Found user session {name}') + logger.debug(f'Found user session {name}') if name not in self._data: self._data[name] = 0 diff --git a/automato/trigger.py b/automato/trigger.py index eda40d5..7acb100 100644 --- a/automato/trigger.py +++ b/automato/trigger.py @@ -1,7 +1,8 @@ from typing import Dict from pyparsing import alphanums, alphas, printables, pyparsing_common, pyparsing_common, Word, infix_notation, CaselessKeyword, opAssoc, ParserElement -import logging import time +import logging +logger = logging.getLogger(__name__) from . import endpoint from . import misc @@ -40,7 +41,7 @@ class Trigger: def addInstance(self, action: str, interval: int=30, **kwargs): self._instances[action] = {'lastupdate':0,'interval':interval,'last':False,'args':kwargs} self._addInstance(action) - logging.debug(f'Trigger: Action "{action}" registered.') + logger.debug(f'Trigger: Action "{action}" registered.') def _evaluate(self, action: str) -> bool: raise NotImplemented @@ -50,11 +51,11 @@ class Trigger: def evaluate(self, action: str) -> bool: if action not in self._instances: - logging.error(f'Trigger: Action "{action}" was not found. Evaluating to False.') + logger.error(f'Trigger: Action "{action}" was not found. Evaluating to False.') return False if self._shouldReevaluate(action): - logging.debug(f'Re-evaluating trigger condition for action "{action}"') + logger.debug(f'Re-evaluating trigger condition for action "{action}"') result = self._evaluate(action) self._instances[action]['last'] = result @@ -110,23 +111,23 @@ class ConditionalTrigger(Trigger): ) def _parseVariable(self, var): - logging.debug(f'Looking up variable "{var[0]}"') + logger.debug(f'Looking up variable "{var[0]}"') endpoint, key = var[0].split('.',1) if not endpoint in self._endpoints: - logging.error(f'Parser: Endpoint "{endpoint}" not found') + logger.error(f'Parser: Endpoint "{endpoint}" not found') return None return self._endpoints[endpoint].getState(key) def _evaluate(self, action: str) -> bool: - logging.debug(f"{self._instances[action]['args']['when']}") + logger.debug(f"{self._instances[action]['args']['when']}") results = [] for s in self._instances[action]['args']['when']: r = self._parser.parse_string(str(s))[0] - logging.debug(f'Condition "{s}" evaluated to "{r}"') + logger.debug(f'Condition "{s}" evaluated to "{r}"') results.append(r) return all(results) |