Skip to content
Merged

Dev #15

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dev.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "automation_file_dev"
version = "0.0.7"
version = "0.0.8"
authors = [
{ name = "JE-Chen", email = "zenmailman@gmail.com" },
]
Expand Down
2 changes: 1 addition & 1 deletion file_automation/local/zip/zip_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,5 @@ def set_zip_password(zip_file_path: str, password: bytes):
file_automation_logger.info(
f"Set zip file password, "
f"zip file: {zip_file_path}, "
f"zup password: {password}"
f"zip password: {password}"
)
11 changes: 9 additions & 2 deletions file_automation/utils/callback/callback_function_executor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import typing
from sys import stderr

from file_automation.local.dir.dir_process import copy_dir, create_dir, remove_dir_tree
from file_automation.local.file.file_process import copy_file, remove_file, rename_file, copy_specify_extension_file, \
Expand All @@ -18,6 +17,7 @@
upload_dir_to_folder, upload_to_folder, upload_dir_to_drive, upload_to_drive
from file_automation.utils.exception.exception_tags import get_bad_trigger_function, get_bad_trigger_method
from file_automation.utils.exception.exceptions import CallbackExecutorException
from file_automation.utils.logging.loggin_instance import file_automation_logger


class CallbackFunctionExecutor(object):
Expand Down Expand Up @@ -76,19 +76,26 @@ def callback_function(
try:
if trigger_function_name not in self.event_dict.keys():
raise CallbackExecutorException(get_bad_trigger_function)
file_automation_logger.info(f"Callback trigger {trigger_function_name} with param {kwargs}")
execute_return_value = self.event_dict.get(trigger_function_name)(**kwargs)
if callback_function_param is not None:
if callback_param_method not in ["kwargs", "args"]:
raise CallbackExecutorException(get_bad_trigger_method)
if callback_param_method == "kwargs":
callback_function(**callback_function_param)
file_automation_logger.info(
f"Callback function {callback_function} with param {callback_function_param}")
else:
callback_function(*callback_function_param)
file_automation_logger.info(
f"Callback function {callback_function} with param {callback_function_param}")
else:
callback_function()
file_automation_logger.info(f"Callback function {callback_function}")
return execute_return_value
except Exception as error:
print(repr(error), file=stderr)
file_automation_logger.error(
f"Callback function failed. {repr(error)}")


callback_executor = CallbackFunctionExecutor()
Expand Down
19 changes: 13 additions & 6 deletions file_automation/utils/executor/action_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
action_is_null_error, cant_execute_action_error
from file_automation.utils.exception.exceptions import ExecuteActionException, AddCommandException
from file_automation.utils.json.json_file import read_action_json
from file_automation.utils.logging.loggin_instance import file_automation_logger


class Executor(object):
Expand Down Expand Up @@ -93,20 +94,23 @@ def execute_action(self, action_list: [list, dict]) -> dict:
else:
raise ExecuteActionException(action_is_null_error)
except Exception as error:
print(repr(error), file=sys.stderr, flush=True)
file_automation_logger.error(
f"Execute {action_list} failed. {repr(error)}"
)
for action in action_list:
try:
event_response = self._execute_event(action)
execute_record = "execute: " + str(action)
file_automation_logger.info(
f"Execute {action}"
)
execute_record_dict.update({execute_record: event_response})
except Exception as error:
print(repr(error), file=sys.stderr, flush=True)
print(action, file=sys.stderr, flush=True)
file_automation_logger.error(
f"Execute {action} failed. {repr(error)}"
)
execute_record = "execute: " + str(action)
execute_record_dict.update({execute_record: repr(error)})
for key, value in execute_record_dict.items():
print(key, flush=True)
print(value, flush=True)
return execute_record_dict

def execute_files(self, execute_files_list: list) -> list:
Expand All @@ -127,6 +131,9 @@ def add_command_to_executor(command_dict: dict):
"""
:param command_dict: dict include command we want to add to event_dict
"""
file_automation_logger.info(
f"Add command to executor {command_dict}"
)
for command_name, command in command_dict.items():
if isinstance(command, (types.MethodType, types.FunctionType)):
executor.event_dict.update({command_name: command})
Expand Down
13 changes: 10 additions & 3 deletions file_automation/utils/json/json_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from pathlib import Path
from threading import Lock

from file_automation.utils.exception.exception_tags import cant_find_json_error
from file_automation.utils.exception.exception_tags import cant_find_json_error, cant_save_json_error
from file_automation.utils.exception.exceptions import JsonActionException
from file_automation.utils.logging.loggin_instance import file_automation_logger

_lock = Lock()

Expand All @@ -17,6 +18,9 @@ def read_action_json(json_file_path: str) -> list:
try:
file_path = Path(json_file_path)
if file_path.exists() and file_path.is_file():
file_automation_logger.info(
f"Read json file {json_file_path}"
)
with open(json_file_path) as read_file:
return json.loads(read_file.read())
except JsonActionException:
Expand All @@ -33,9 +37,12 @@ def write_action_json(json_save_path: str, action_json: list) -> None:
"""
_lock.acquire()
try:
file_automation_logger.info(
f"Write {action_json} as file {json_save_path}"
)
with open(json_save_path, "w+") as file_to_write:
file_to_write.write(json.dumps(action_json, indent=4))
except AutoControlJsonActionException:
raise AutoControlJsonActionException(cant_save_json_error)
except JsonActionException:
raise JsonActionException(cant_save_json_error)
finally:
_lock.release()
19 changes: 13 additions & 6 deletions file_automation/utils/logging/loggin_instance.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import logging
import sys

logging.getLogger().setLevel(logging.INFO)
file_automation_logger = logging.getLogger("File Automation")
handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
file_automation_logger.addHandler(handler)
file_automation_logger.setLevel(logging.DEBUG)
file_automation_logger.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s | %(name)s | %(levelname)s | %(message)s')
# Stream handler
stream_handler = logging.StreamHandler(stream=sys.stderr)
stream_handler.setFormatter(formatter)
stream_handler.setLevel(logging.WARNING)
file_automation_logger.addHandler(stream_handler)
# File handler
file_handler = logging.FileHandler(filename="FileAutomation.log", mode="w+")
file_handler.setFormatter(formatter)
file_automation_logger.addHandler(file_handler)

20 changes: 20 additions & 0 deletions file_automation/utils/scheduler/extend_apscheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.util import undefined

from file_automation.utils.logging.loggin_instance import file_automation_logger


class SchedulerManager(object):

Expand Down Expand Up @@ -38,9 +40,15 @@ def get_nonblocking_scheduler(self):
return self._background_schedulers

def start_block_scheduler(self, *args, **kwargs):
file_automation_logger.info(
f"Start blocking scheduler with {args}, {kwargs}"
)
self._blocking_schedulers.start(*args, **kwargs)

def start_nonblocking_scheduler(self, *args, **kwargs):
file_automation_logger.info(
f"Start background scheduler with {args}, {kwargs}"
)
self._background_schedulers.start(*args, **kwargs)

def start_all_scheduler(self, *args, **kwargs):
Expand Down Expand Up @@ -116,13 +124,25 @@ def add_cron_nonblocking(
self.add_nonblocking_job(func=function, id=id, trigger="cron", **trigger_args)

def remove_blocking_job(self, id: str, jobstore: str = 'default'):
file_automation_logger.info(
f"Remove blocking job {id}, store on {jobstore}"
)
self._blocking_schedulers.remove_job(job_id=id, jobstore=jobstore)

def remove_nonblocking_job(self, id: str, jobstore: str = 'default'):
file_automation_logger.info(
f"Remove nonblocking job {id}, store on {jobstore}"
)
self._background_schedulers.remove_job(job_id=id, jobstore=jobstore)

def shutdown_blocking_scheduler(self, wait: bool = False):
file_automation_logger.info(
f"Shutdown blocking scheduler wait = {wait}"
)
self._blocking_schedulers.shutdown(wait=wait)

def shutdown_nonblocking_scheduler(self, wait: bool = False):
file_automation_logger.info(
f"Shutdown nonblocking scheduler wait = {wait}"
)
self._background_schedulers.shutdown(wait=wait)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "automation_file"
version = "0.0.6"
version = "0.0.7"
authors = [
{ name = "JE-Chen", email = "zenmailman@gmail.com" },
]
Expand Down