From 8a6cbd632b62ecf59a69cfcfe4fdd36ccf99f3f5 Mon Sep 17 00:00:00 2001 From: JeffreyChen Date: Thu, 3 Aug 2023 10:09:18 +0800 Subject: [PATCH] Add CLI mode & Update dev and stable version Add CLI mode & Update dev and stable version --- .gitignore | 4 +- dev.toml | 2 +- file_automation/__main__.py | 64 +++++++++++++++++++ .../utils/exception/exception_tags.py | 2 + file_automation/utils/exception/exceptions.py | 4 ++ pyproject.toml | 2 +- tests/unit_test/executor/executor_test.py | 11 ++++ tests/unit_test/executor/test.txt | 1 + 8 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 file_automation/__main__.py create mode 100644 tests/unit_test/executor/executor_test.py create mode 100644 tests/unit_test/executor/test.txt diff --git a/.gitignore b/.gitignore index d635b5b..7600a35 100644 --- a/.gitignore +++ b/.gitignore @@ -152,5 +152,7 @@ cython_debug/ .idea/ # Google Drive API token -tests/unit_test/remote/google_drive/credentials.json token.json +credentials.json +**/token.json +**/credentials.json diff --git a/dev.toml b/dev.toml index b627a12..803db2e 100644 --- a/dev.toml +++ b/dev.toml @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta" [project] name = "automation_file_dev" -version = "0.0.16" +version = "0.0.17" authors = [ { name = "JE-Chen", email = "zenmailman@gmail.com" }, ] diff --git a/file_automation/__main__.py b/file_automation/__main__.py new file mode 100644 index 0000000..992370c --- /dev/null +++ b/file_automation/__main__.py @@ -0,0 +1,64 @@ +# argparse +import argparse +import json +import sys + +from file_automation.utils.exception.exception_tags import \ + argparse_get_wrong_data +from file_automation.utils.exception.exceptions import \ + ArgparseException +from file_automation.utils.executor.action_executor import execute_action +from file_automation.utils.executor.action_executor import execute_files +from file_automation.utils.file_process.get_dir_file_list import \ + get_dir_files_as_list +from file_automation.utils.json.json_file import read_action_json +from file_automation.utils.project.create_project_structure import create_project_dir + +if __name__ == "__main__": + try: + def preprocess_execute_action(file_path: str): + execute_action(read_action_json(file_path)) + + + def preprocess_execute_files(file_path: str): + execute_files(get_dir_files_as_list(file_path)) + + + def preprocess_read_str_execute_action(execute_str: str): + execute_str = json.loads(execute_str) + execute_action(execute_str) + + + argparse_event_dict = { + "execute_file": preprocess_execute_action, + "execute_dir": preprocess_execute_files, + "execute_str": preprocess_read_str_execute_action, + "create_project": create_project_dir + } + parser = argparse.ArgumentParser() + parser.add_argument( + "-e", "--execute_file", + type=str, help="choose action file to execute" + ) + parser.add_argument( + "-d", "--execute_dir", + type=str, help="choose dir include action file to execute" + ) + parser.add_argument( + "-c", "--create_project", + type=str, help="create project with template" + ) + parser.add_argument( + "--execute_str", + type=str, help="execute json str" + ) + args = parser.parse_args() + args = vars(args) + for key, value in args.items(): + if value is not None: + argparse_event_dict.get(key)(value) + if all(value is None for value in args.values()): + raise ArgparseException(argparse_get_wrong_data) + except Exception as error: + print(repr(error), file=sys.stderr) + sys.exit(1) diff --git a/file_automation/utils/exception/exception_tags.py b/file_automation/utils/exception/exception_tags.py index cdf3928..ed28c1e 100644 --- a/file_automation/utils/exception/exception_tags.py +++ b/file_automation/utils/exception/exception_tags.py @@ -12,3 +12,5 @@ cant_find_json_error: str = "cant find json file" cant_save_json_error: str = "cant save json file" action_is_null_error: str = "json action is null" +# argparse +argparse_get_wrong_data: str = "argparse receive wrong data" diff --git a/file_automation/utils/exception/exceptions.py b/file_automation/utils/exception/exceptions.py index 995eb58..33c5b60 100644 --- a/file_automation/utils/exception/exceptions.py +++ b/file_automation/utils/exception/exceptions.py @@ -28,3 +28,7 @@ class AddCommandException(FileAutomationException): class JsonActionException(FileAutomationException): pass + + +class ArgparseException(FileAutomationException): + pass diff --git a/pyproject.toml b/pyproject.toml index d6c0c46..f0b77d0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta" [project] name = "automation_file" -version = "0.0.14" +version = "0.0.15" authors = [ { name = "JE-Chen", email = "zenmailman@gmail.com" }, ] diff --git a/tests/unit_test/executor/executor_test.py b/tests/unit_test/executor/executor_test.py new file mode 100644 index 0000000..8b58463 --- /dev/null +++ b/tests/unit_test/executor/executor_test.py @@ -0,0 +1,11 @@ +from file_automation import execute_action + +test_list = [ + ["FA_drive_later_init", {"token_path": "token.json", "credentials_path": "credentials.json"}], + ["FA_drive_search_all_file"], + ["FA_drive_upload_to_drive", {"file_path": "test.txt"}], + ["FA_drive_add_folder", {"folder_name": "test_folder"}], + ["FA_drive_search_all_file"] +] + +execute_action(test_list) diff --git a/tests/unit_test/executor/test.txt b/tests/unit_test/executor/test.txt new file mode 100644 index 0000000..976493f --- /dev/null +++ b/tests/unit_test/executor/test.txt @@ -0,0 +1 @@ +test123456789 \ No newline at end of file