Skip to content

Commit 2e275dd

Browse files
authored
Merge pull request #14 from Integration-Automation/dev
Dev
2 parents 4f2f9bb + e549a30 commit 2e275dd

13 files changed

Lines changed: 446 additions & 7 deletions

File tree

dev.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"
66

77
[project]
88
name = "automation_file_dev"
9-
version = "0.0.6"
9+
version = "0.0.7"
1010
authors = [
1111
{ name = "JE-Chen", email = "zenmailman@gmail.com" },
1212
]

file_automation/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
__all__ = [
1919
"copy_file", "rename_file", "remove_file", "copy_all_file_to_dir", "copy_specify_extension_file",
20-
"copy_dir", "create_dir", "copy_specify_extension_file", "remove_dir_tree",
21-
"zip_dir", "zip_file", "zip_info", "zip_file_info", "set_zip_password", "unzip_file", "read_zip_file",
20+
"copy_dir", "create_dir", "remove_dir_tree", "zip_dir", "zip_file", "zip_info",
21+
"zip_file_info", "set_zip_password", "unzip_file", "read_zip_file",
2222
"unzip_all", "driver_instance", "search_all_file", "search_field", "search_file_mimetype",
2323
"upload_dir_to_folder", "upload_to_folder", "upload_dir_to_drive", "upload_to_drive",
2424
"add_folder", "share_file_to_anyone", "share_file_to_domain", "share_file_to_user",

file_automation/utils/callback/__init__.py

Whitespace-only changes.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import typing
2+
from sys import stderr
3+
4+
from file_automation.local.dir.dir_process import copy_dir, create_dir, remove_dir_tree
5+
from file_automation.local.file.file_process import copy_file, remove_file, rename_file, copy_specify_extension_file, \
6+
copy_all_file_to_dir
7+
from file_automation.local.zip.zip_process import zip_dir, zip_file, zip_info, zip_file_info, set_zip_password, \
8+
read_zip_file, unzip_file, unzip_all
9+
from file_automation.remote.google_drive.delete.delete_manager import delete_file
10+
from file_automation.remote.google_drive.dir.folder_manager import add_folder
11+
from file_automation.remote.google_drive.download.download_file import download_file, download_file_from_folder
12+
from file_automation.remote.google_drive.driver_instance import driver_instance
13+
from file_automation.remote.google_drive.search.search_drive import \
14+
search_all_file, search_field, search_file_mimetype
15+
from file_automation.remote.google_drive.share.share_file import \
16+
share_file_to_anyone, share_file_to_domain, share_file_to_user
17+
from file_automation.remote.google_drive.upload.upload_to_driver import \
18+
upload_dir_to_folder, upload_to_folder, upload_dir_to_drive, upload_to_drive
19+
from file_automation.utils.exception.exception_tags import get_bad_trigger_function, get_bad_trigger_method
20+
from file_automation.utils.exception.exceptions import CallbackExecutorException
21+
22+
23+
class CallbackFunctionExecutor(object):
24+
25+
def __init__(self):
26+
self.event_dict: dict = {
27+
"copy_file": copy_file,
28+
"rename_file": rename_file,
29+
"remove_file": remove_file,
30+
"copy_all_file_to_dir": copy_all_file_to_dir,
31+
"copy_specify_extension_file": copy_specify_extension_file,
32+
"copy_dir": copy_dir,
33+
"create_dir": create_dir,
34+
"remove_dir_tree": remove_dir_tree,
35+
"zip_dir": zip_dir,
36+
"zip_file": zip_file,
37+
"zip_info": zip_info,
38+
"zip_file_info": zip_file_info,
39+
"set_zip_password": set_zip_password,
40+
"unzip_file": unzip_file,
41+
"read_zip_file": read_zip_file,
42+
"unzip_all": unzip_all,
43+
"driver_instance": driver_instance,
44+
"search_all_file": search_all_file,
45+
"search_field": search_field,
46+
"search_file_mimetype": search_file_mimetype,
47+
"upload_dir_to_folder": upload_dir_to_folder,
48+
"upload_to_folder": upload_to_folder,
49+
"upload_dir_to_drive": upload_dir_to_drive,
50+
"upload_to_drive": upload_to_drive,
51+
"add_folder": add_folder,
52+
"share_file_to_anyone": share_file_to_anyone,
53+
"share_file_to_domain": share_file_to_domain,
54+
"share_file_to_user": share_file_to_user,
55+
"delete_file": delete_file,
56+
"download_file": download_file,
57+
"download_file_from_folder": download_file_from_folder
58+
}
59+
60+
def callback_function(
61+
self,
62+
trigger_function_name: str,
63+
callback_function: typing.Callable,
64+
callback_function_param: [dict, None] = None,
65+
callback_param_method: str = "kwargs",
66+
**kwargs
67+
) -> typing.Any:
68+
"""
69+
:param trigger_function_name: what function we want to trigger only accept function in event_dict
70+
:param callback_function: what function we want to callback
71+
:param callback_function_param: callback function's param only accept dict
72+
:param callback_param_method: what type param will use on callback function only accept kwargs and args
73+
:param kwargs: trigger_function's param
74+
:return: trigger_function_name return value
75+
"""
76+
try:
77+
if trigger_function_name not in self.event_dict.keys():
78+
raise CallbackExecutorException(get_bad_trigger_function)
79+
execute_return_value = self.event_dict.get(trigger_function_name)(**kwargs)
80+
if callback_function_param is not None:
81+
if callback_param_method not in ["kwargs", "args"]:
82+
raise CallbackExecutorException(get_bad_trigger_method)
83+
if callback_param_method == "kwargs":
84+
callback_function(**callback_function_param)
85+
else:
86+
callback_function(*callback_function_param)
87+
else:
88+
callback_function()
89+
return execute_return_value
90+
except Exception as error:
91+
print(repr(error), file=stderr)
92+
93+
94+
callback_executor = CallbackFunctionExecutor()
95+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
11
token_is_exist: str = "token file is exists"
2+
# Callback executor
3+
get_bad_trigger_method: str = "get bad trigger method, only accept kwargs and args"
4+
get_bad_trigger_function: str = "get bad trigger function only accept function in event_dict"
5+
# add command
6+
add_command_exception: str = "command value type should be as method or function"
7+
# executor
8+
executor_list_error: str = "executor receive wrong data list is none or wrong type"
9+
# json tag
10+
cant_execute_action_error: str = "cant execute action"
11+
cant_generate_json_report: str = "can't generate json report"
12+
cant_find_json_error: str = "cant find json file"
13+
cant_save_json_error: str = "cant save json file"
14+
action_is_null_error: str = "json action is null"
Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
1-
class FileNotExistsException(Exception):
1+
class FileAutomationException(Exception):
22
pass
33

44

5-
class DirNotExistsException(Exception):
5+
class FileNotExistsException(FileAutomationException):
66
pass
77

88

9-
class ZIPGetWrongFileException(Exception):
9+
class DirNotExistsException(FileAutomationException):
10+
pass
11+
12+
13+
class ZIPGetWrongFileException(FileAutomationException):
14+
pass
15+
16+
17+
class CallbackExecutorException(FileAutomationException):
18+
pass
19+
20+
21+
class ExecuteActionException(FileAutomationException):
22+
pass
23+
24+
25+
class AddCommandException(FileAutomationException):
26+
pass
27+
28+
29+
class JsonActionException(FileAutomationException):
1030
pass

file_automation/utils/executor/__init__.py

Whitespace-only changes.
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import builtins
2+
import sys
3+
import types
4+
from inspect import getmembers, isbuiltin
5+
6+
from file_automation.local.dir.dir_process import copy_dir, create_dir, remove_dir_tree
7+
from file_automation.local.file.file_process import copy_file, remove_file, rename_file, copy_specify_extension_file, \
8+
copy_all_file_to_dir
9+
from file_automation.local.zip.zip_process import zip_dir, zip_file, zip_info, zip_file_info, set_zip_password, \
10+
read_zip_file, unzip_file, unzip_all
11+
from file_automation.remote.google_drive.delete.delete_manager import delete_file
12+
from file_automation.remote.google_drive.dir.folder_manager import add_folder
13+
from file_automation.remote.google_drive.download.download_file import download_file, download_file_from_folder
14+
from file_automation.remote.google_drive.driver_instance import driver_instance
15+
from file_automation.remote.google_drive.search.search_drive import \
16+
search_all_file, search_field, search_file_mimetype
17+
from file_automation.remote.google_drive.share.share_file import \
18+
share_file_to_anyone, share_file_to_domain, share_file_to_user
19+
from file_automation.remote.google_drive.upload.upload_to_driver import \
20+
upload_dir_to_folder, upload_to_folder, upload_dir_to_drive, upload_to_drive
21+
from file_automation.utils.exception.exception_tags import add_command_exception, executor_list_error, \
22+
action_is_null_error, cant_execute_action_error
23+
from file_automation.utils.exception.exceptions import ExecuteActionException, AddCommandException
24+
from file_automation.utils.json.json_file import read_action_json
25+
26+
27+
class Executor(object):
28+
29+
def __init__(self):
30+
self.event_dict: dict = {
31+
"copy_file": copy_file,
32+
"rename_file": rename_file,
33+
"remove_file": remove_file,
34+
"copy_all_file_to_dir": copy_all_file_to_dir,
35+
"copy_specify_extension_file": copy_specify_extension_file,
36+
"copy_dir": copy_dir,
37+
"create_dir": create_dir,
38+
"remove_dir_tree": remove_dir_tree,
39+
"zip_dir": zip_dir,
40+
"zip_file": zip_file,
41+
"zip_info": zip_info,
42+
"zip_file_info": zip_file_info,
43+
"set_zip_password": set_zip_password,
44+
"unzip_file": unzip_file,
45+
"read_zip_file": read_zip_file,
46+
"unzip_all": unzip_all,
47+
"driver_instance": driver_instance,
48+
"search_all_file": search_all_file,
49+
"search_field": search_field,
50+
"search_file_mimetype": search_file_mimetype,
51+
"upload_dir_to_folder": upload_dir_to_folder,
52+
"upload_to_folder": upload_to_folder,
53+
"upload_dir_to_drive": upload_dir_to_drive,
54+
"upload_to_drive": upload_to_drive,
55+
"add_folder": add_folder,
56+
"share_file_to_anyone": share_file_to_anyone,
57+
"share_file_to_domain": share_file_to_domain,
58+
"share_file_to_user": share_file_to_user,
59+
"delete_file": delete_file,
60+
"download_file": download_file,
61+
"download_file_from_folder": download_file_from_folder
62+
}
63+
# get all builtin function and add to event dict
64+
for function in getmembers(builtins, isbuiltin):
65+
self.event_dict.update({str(function[0]): function[1]})
66+
67+
def _execute_event(self, action: list):
68+
event = self.event_dict.get(action[0])
69+
if len(action) == 2:
70+
if isinstance(action[1], dict):
71+
return event(**action[1])
72+
else:
73+
return event(*action[1])
74+
elif len(action) == 1:
75+
return event()
76+
else:
77+
raise ExecuteActionException(cant_execute_action_error + " " + str(action))
78+
79+
def execute_action(self, action_list: [list, dict]) -> dict:
80+
"""
81+
use to execute all action on action list(action file or program list)
82+
:param action_list the list include action
83+
for loop the list and execute action
84+
"""
85+
if isinstance(action_list, dict):
86+
action_list: list = action_list.get("auto_control", None)
87+
if action_list is None:
88+
raise ExecuteActionException(executor_list_error)
89+
execute_record_dict = dict()
90+
try:
91+
if len(action_list) > 0 or isinstance(action_list, list):
92+
pass
93+
else:
94+
raise ExecuteActionException(action_is_null_error)
95+
except Exception as error:
96+
print(repr(error), file=sys.stderr, flush=True)
97+
for action in action_list:
98+
try:
99+
event_response = self._execute_event(action)
100+
execute_record = "execute: " + str(action)
101+
execute_record_dict.update({execute_record: event_response})
102+
except Exception as error:
103+
print(repr(error), file=sys.stderr, flush=True)
104+
print(action, file=sys.stderr, flush=True)
105+
execute_record = "execute: " + str(action)
106+
execute_record_dict.update({execute_record: repr(error)})
107+
for key, value in execute_record_dict.items():
108+
print(key, flush=True)
109+
print(value, flush=True)
110+
return execute_record_dict
111+
112+
def execute_files(self, execute_files_list: list) -> list:
113+
"""
114+
:param execute_files_list: list include execute files path
115+
:return: every execute detail as list
116+
"""
117+
execute_detail_list: list = list()
118+
for file in execute_files_list:
119+
execute_detail_list.append(self.execute_action(read_action_json(file)))
120+
return execute_detail_list
121+
122+
123+
executor = Executor()
124+
125+
126+
def add_command_to_executor(command_dict: dict):
127+
"""
128+
:param command_dict: dict include command we want to add to event_dict
129+
"""
130+
for command_name, command in command_dict.items():
131+
if isinstance(command, (types.MethodType, types.FunctionType)):
132+
executor.event_dict.update({command_name: command})
133+
else:
134+
raise AddCommandException(add_command_exception)
135+
136+
137+
def execute_action(action_list: list) -> dict:
138+
return executor.execute_action(action_list)
139+
140+
141+
def execute_files(execute_files_list: list) -> list:
142+
return executor.execute_files(execute_files_list)

file_automation/utils/json/__init__.py

Whitespace-only changes.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import json
2+
from pathlib import Path
3+
from threading import Lock
4+
5+
from file_automation.utils.exception.exception_tags import cant_find_json_error
6+
from file_automation.utils.exception.exceptions import JsonActionException
7+
8+
_lock = Lock()
9+
10+
11+
def read_action_json(json_file_path: str) -> list:
12+
"""
13+
use to read action file
14+
:param json_file_path json file's path to read
15+
"""
16+
_lock.acquire()
17+
try:
18+
file_path = Path(json_file_path)
19+
if file_path.exists() and file_path.is_file():
20+
with open(json_file_path) as read_file:
21+
return json.loads(read_file.read())
22+
except JsonActionException:
23+
raise JsonActionException(cant_find_json_error)
24+
finally:
25+
_lock.release()
26+
27+
28+
def write_action_json(json_save_path: str, action_json: list) -> None:
29+
"""
30+
use to save action file
31+
:param json_save_path json save path
32+
:param action_json the json str include action to write
33+
"""
34+
_lock.acquire()
35+
try:
36+
with open(json_save_path, "w+") as file_to_write:
37+
file_to_write.write(json.dumps(action_json, indent=4))
38+
except AutoControlJsonActionException:
39+
raise AutoControlJsonActionException(cant_save_json_error)
40+
finally:
41+
_lock.release()

0 commit comments

Comments
 (0)