116 lines
3.7 KiB
Python
116 lines
3.7 KiB
Python
import datetime
|
|
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 IfFileExistsCondition(Condition):
|
|
def execute(self, *args, **kwargs):
|
|
path = None
|
|
if len(self.args) > 0:
|
|
path = self.args[0]
|
|
entry = self.kwargs.get('entry')
|
|
if not path:
|
|
Logger.write_error(entry, 'Need a path')
|
|
else:
|
|
if os.path.exists(path):
|
|
return True, path
|
|
return False, None
|
|
|
|
|
|
class IfDirectoryEmptyCondition(Condition):
|
|
def execute(self, *args, **kwargs):
|
|
path = None
|
|
if len(self.args) > 0:
|
|
path = self.args[0]
|
|
entry = self.kwargs.get('entry')
|
|
if not path:
|
|
Logger.write_error(entry, 'Need a path')
|
|
else:
|
|
files = [os.path.join(path, f) for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]
|
|
if files:
|
|
return False, files
|
|
return True, None
|
|
|
|
|
|
class IfDirectoryNotEmptyCondition(Condition):
|
|
def execute(self, *args, **kwargs):
|
|
path = None
|
|
if len(self.args) > 0:
|
|
path = self.args[0]
|
|
entry = self.kwargs.get('entry')
|
|
if not path:
|
|
Logger.write_error(entry, 'Need a path')
|
|
else:
|
|
files = [os.path.join(path, f) for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]
|
|
if files:
|
|
return True, files
|
|
return False, None
|
|
|
|
|
|
class GetCreationTimeCondition(Condition):
|
|
def execute(self, *args, **kwargs):
|
|
path = args[0]
|
|
result = list()
|
|
if isinstance(path, list):
|
|
for p in path:
|
|
time = datetime.datetime.fromtimestamp(os.path.getctime(p))
|
|
time = time.strftime("%Y.%m.%d %H:%M:%S")
|
|
result.append(time)
|
|
else:
|
|
time = datetime.datetime.fromtimestamp(os.path.getctime(path))
|
|
time = time.strftime("%Y.%m.%d %H:%M:%S")
|
|
result.append(time)
|
|
return True, result
|
|
|
|
|
|
class IfCreationTimeCondition(Condition):
|
|
def execute(self, *args, **kwargs):
|
|
s = self.args[0].split(' ', 1)
|
|
if len(s) > 1:
|
|
oper = s[0]
|
|
time = s[1]
|
|
search_time = datetime.datetime.strptime(time, "%Y.%m.%d %H:%M:%S")
|
|
path = args[0]
|
|
result = list()
|
|
if isinstance(path, list):
|
|
for p in path:
|
|
time = datetime.datetime.fromtimestamp(os.path.getctime(p))
|
|
# time = time.strftime("%Y.%m.%d %H:%M:%S")
|
|
if oper == '<' and time < search_time:
|
|
result.append(p)
|
|
elif oper == '=' and time == search_time:
|
|
result.append(p)
|
|
elif oper == '>' and time > search_time:
|
|
result.append(p)
|
|
else:
|
|
time = datetime.datetime.fromtimestamp(os.path.getctime(path))
|
|
# time = time.strftime("%Y.%m.%d %H:%M:%S")
|
|
if oper == '<' and time < search_time:
|
|
result.append(path)
|
|
elif oper == '=' and time == search_time:
|
|
result.append(path)
|
|
elif oper == '>' and time > search_time:
|
|
result.append(path)
|
|
return True, result
|
|
|
|
|
|
conditions = {
|
|
"IfFileExists": IfFileExistsCondition,
|
|
"IfDirectoryEmpty": IfDirectoryEmptyCondition,
|
|
"IfDirectoryNotEmpty": IfDirectoryNotEmptyCondition,
|
|
"GetCreationTime": GetCreationTimeCondition,
|
|
"IfCreationTime": IfCreationTimeCondition,
|
|
}
|