commit 902768fb9c2551d5d252665f65be736389edfd42 Author: a.jurcenko Date: Sat Aug 5 15:27:29 2023 +0200 Initial Commit diff --git a/action.py b/action.py new file mode 100644 index 0000000..b41d50b --- /dev/null +++ b/action.py @@ -0,0 +1,70 @@ +import abc +import os +from abc import ABC +from logger import Logger + + +class Action(ABC): + def __init__(self, *args, **kwargs): + self.args = args + self.kwargs = kwargs + + @abc.abstractmethod + def execute(self, *args, **kwargs): + pass + + +class LoggMessageAction(Action): + + def execute(self, *args, **kwargs): + entry = self.kwargs.get('entry') + Logger.write_message(entry, self.args[0]) + + +class StubAction(Action): + def execute(self, *args, **kwargs): + pass + + +class ReadAction(Action): + + def execute(self, *args, **kwargs): + pass + + +class MoveAction(Action): + + def execute(self, *args, **kwargs): + pass + + +class CompressAction(Action): + + def execute(self, *args, **kwargs): + pass + + +class IsFileExistsAction(Action): + + def execute(self, *args, **kwargs): + path = self.kwargs.get('path') + entry = self.kwargs.get('entry') + if not path: + Logger.write_error(entry, 'Need a path') + else: + return os.path.exists(path) + + +class IsDirectoryEmptyAction(Action): + + def execute(self, *args, **kwargs): + path = self.kwargs.get('path') + entry = self.kwargs.get('entry') + if not path: + Logger.write_error(entry, 'Need a path') + else: + files = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))] + if files: + return False + return True + diff --git a/app.py b/app.py new file mode 100644 index 0000000..a6fe865 --- /dev/null +++ b/app.py @@ -0,0 +1,26 @@ +import os +from config import Config +from entry import Entry +from action import * +from logger import Logger + + +class App: + + def __init__(self): + Logger.set_log_file('//testdata/log.txt') + self._config = Config() + + def start(self): + self._config.read_config() + self._config.add_entry(Entry('myTest', + IsDirectoryEmptyAction(path='//emptydir', entry='myTest'), + LoggMessageAction('success', entry='myTest'), + LoggMessageAction('fail', entry='myTest'))) + + for entry in self._config: + result = entry.action.execute() + if result: + entry.success.execute() + else: + entry.fail.execute() diff --git a/config.py b/config.py new file mode 100644 index 0000000..4825c2f --- /dev/null +++ b/config.py @@ -0,0 +1,32 @@ +from entry import Entry + + +class Config: + + def __init__(self): + self._entries = [] + + def add_entry(self, entry: Entry): + self._entries.append(entry) + + def get_next_entry(self): + return self._entries.pop() + + def __next__(self): + self.get_next_entry() + + def __iter__(self): + return iter(self._entries) + + def read_config(self, *args, **kwargs): + with open('config.txt', 'r') as file: + for line in file: + self._create_entry(line) + + def _create_entry(self, line): + splitts = line.split(';') + assert(len(splitts) == 4) + name = splitts[0] + action, act_param = splitts[1].strip().split(' ') + success, suc_param = splitts[2].strip().split(' ', 1) + fail, fail_param = splitts[3].strip().split(' ', 1) diff --git a/config.txt b/config.txt new file mode 100644 index 0000000..9a6905b --- /dev/null +++ b/config.txt @@ -0,0 +1 @@ +MyTest; IsDirectoryEmpty C:\Users\a.jurcenko\PycharmProjects\logreader\emptydir; LoggMessage Alles Gut; LoggMessage Alles Schlecht \ No newline at end of file diff --git a/entry.py b/entry.py new file mode 100644 index 0000000..bec0e02 --- /dev/null +++ b/entry.py @@ -0,0 +1,27 @@ +from action import Action + + +class Entry: + + def __init__(self, name: str, action: Action, success: Action, fail: Action): + self._name = name + self._action = action + self._success = success + self._fail = fail + + + @property + def name(self): + return self._name + + @property + def action(self): + return self._action + + @property + def success(self): + return self._success + + @property + def fail(self): + return self._fail diff --git a/logger.py b/logger.py new file mode 100644 index 0000000..b2e4024 --- /dev/null +++ b/logger.py @@ -0,0 +1,40 @@ +from enum import Enum +import os +import datetime + + +class LoggerStage(Enum): + MESSAGE = 1 + WARNING = 2 + ERROR = 3 + + +class Logger: + + path = None + + @classmethod + def set_log_file(cls, path): + cls.path = path + + @classmethod + def _write(cls, entry, msg, kind): + if not cls.path: + return + if not os.path.exists(path=cls.path): + os.makedirs(os.path.dirname(cls.path), exist_ok=True) + with open(cls.path, 'a') as file: + message = datetime.datetime.now().strftime('%Y.%m.%d %H:%M:%S') + ' ' + entry + ': ' + msg + ' ' + str(kind) + '\n' + file.write(message) + + @classmethod + def write_message(cls, entry, message): + cls._write(entry, message, LoggerStage.MESSAGE) + + @classmethod + def write_warning(cls, entry, warning): + cls._write(entry, warning, LoggerStage.WARNING) + + @classmethod + def write_error(cls, entry, error): + cls._write(entry, error, LoggerStage.ERROR) diff --git a/main.py b/main.py new file mode 100644 index 0000000..47a53ff --- /dev/null +++ b/main.py @@ -0,0 +1,7 @@ +from app import App + + +app = App() + +if __name__ == '__main__': + app.start()