admintools/conditions.py

40 lines
1.0 KiB
Python

from abc import ABC, abstractmethod
import os
from logger import Logger
from typing import Tuple, List
class Condition(ABC):
def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
@abstractmethod
def execute(self, *args, **kwargs) -> Tuple[bool, List]:
pass
class IsFileExistsCondition(Condition):
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 IsDirectoryEmptyCondition(Condition):
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