|
| 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) |
0 commit comments