Initial Commit

master
a.jurcenko 2023-08-05 15:27:29 +02:00
commit 902768fb9c
7 changed files with 203 additions and 0 deletions

70
action.py 100644
View File

@ -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

26
app.py 100644
View File

@ -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()

32
config.py 100644
View File

@ -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)

1
config.txt 100644
View File

@ -0,0 +1 @@
MyTest; IsDirectoryEmpty C:\Users\a.jurcenko\PycharmProjects\logreader\emptydir; LoggMessage Alles Gut; LoggMessage Alles Schlecht

27
entry.py 100644
View File

@ -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

40
logger.py 100644
View File

@ -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)

7
main.py 100644
View File

@ -0,0 +1,7 @@
from app import App
app = App()
if __name__ == '__main__':
app.start()