admintools/actions.py

110 lines
2.8 KiB
Python

import os
import shutil
from abc import ABC, abstractmethod
from typing import Tuple, List
from logger import Logger
class Action(ABC):
def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
@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])
messages = args[0]
if isinstance(messages, list):
for m in messages:
Logger.write_message(entry, "\t" + str(m))
else:
Logger.write_message(entry, "\t" + str(messages))
class StubAction(Action):
def execute(self, *args, **kwargs):
return True, None
class ReadAction(Action):
def execute(self, *args, **kwargs):
pass
class MoveAction(Action):
def execute(self, *args, **kwargs):
files = args[0]
destination = self.args[0]
if destination:
if os.path.exists(destination) and not os.path.isdir(destination):
return False, "Not a Directory"
elif not os.path.exists(destination):
os.makedirs(destination)
if isinstance(files, list):
for file in files:
f = os.path.basename(file)
os.rename(file, destination + os.path.sep + f)
else:
f = os.path.basename(files)
os.rename(files, destination + os.path.sep + f)
return True, files
return False, None
class CopyAction(Action):
def execute(self, *args, **kwargs):
files = args[0]
destination = self.args[0]
if destination:
if os.path.exists(destination) and not os.path.isdir(destination):
return False, "Not a Directory"
elif not os.path.exists(destination):
os.makedirs(destination)
if isinstance(files, list):
for file in files:
f = os.path.basename(file)
shutil.copy(file, destination + os.path.sep + f)
else:
f = os.path.basename(files)
shutil.copy(files, destination + os.path.sep + f)
return True, files
return False, None
class RemoveAction(Action):
def execute(self, *args, **kwargs):
files = args[0]
if isinstance(files, list):
for file in files:
os.remove(file)
else:
os.remove(files)
return True, files
# return False, None
class CompressAction(Action):
def execute(self, *args, **kwargs):
pass
actions = {
"LoggMessage": LoggMessageAction,
"Stub": StubAction,
"Move": MoveAction,
"Copy": CopyAction,
"Remove": RemoveAction,
}