From 9271987f1bb617e4774ef2ec128410f5e4a7fb85 Mon Sep 17 00:00:00 2001 From: JeffreyChen <33644111+JE-Chen@users.noreply.github.com> Date: Thu, 8 Jun 2023 11:15:25 +0800 Subject: [PATCH 1/7] Refactor add return value typing Refactor add return value typing --- .../remote/google_drive/delete/delete_manager.py | 4 +++- .../remote/google_drive/dir/folder_manager.py | 4 +++- .../remote/google_drive/download/download_file.py | 8 +++++--- .../remote/google_drive/search/search_drive.py | 8 +++++--- file_automation/remote/google_drive/share/share_file.py | 8 +++++--- .../remote/google_drive/upload/upload_to_driver.py | 9 +++++---- 6 files changed, 26 insertions(+), 15 deletions(-) diff --git a/file_automation/remote/google_drive/delete/delete_manager.py b/file_automation/remote/google_drive/delete/delete_manager.py index 623f3cd..225a6a1 100644 --- a/file_automation/remote/google_drive/delete/delete_manager.py +++ b/file_automation/remote/google_drive/delete/delete_manager.py @@ -1,9 +1,11 @@ +from typing import Union + from googleapiclient.errors import HttpError from file_automation.remote.google_drive.driver_instance import driver_instance -def delete_file(file_id: str): +def delete_file(file_id: str) -> Union[dict, None]: try: file = driver_instance.service.files().delete(fileId=file_id).execute() return file diff --git a/file_automation/remote/google_drive/dir/folder_manager.py b/file_automation/remote/google_drive/dir/folder_manager.py index 90a5b20..c83a87e 100644 --- a/file_automation/remote/google_drive/dir/folder_manager.py +++ b/file_automation/remote/google_drive/dir/folder_manager.py @@ -1,9 +1,11 @@ +from typing import Union + from googleapiclient.errors import HttpError from file_automation.remote.google_drive.driver_instance import driver_instance -def add_folder(folder_name: str): +def add_folder(folder_name: str) -> Union[dict, None]: try: file_metadata = { "name": folder_name, diff --git a/file_automation/remote/google_drive/download/download_file.py b/file_automation/remote/google_drive/download/download_file.py index 1c19395..7f42d7e 100644 --- a/file_automation/remote/google_drive/download/download_file.py +++ b/file_automation/remote/google_drive/download/download_file.py @@ -1,4 +1,6 @@ import io +from io import BytesIO +from typing import Union from googleapiclient.errors import HttpError from googleapiclient.http import MediaIoBaseDownload @@ -6,7 +8,7 @@ from file_automation.remote.google_drive.driver_instance import driver_instance -def download_file(file_id: str, file_name: str): +def download_file(file_id: str, file_name: str) -> BytesIO: try: request = driver_instance.service.files().get_media(fileId=file_id) file = io.BytesIO() @@ -23,7 +25,7 @@ def download_file(file_id: str, file_name: str): return file -def download_file_from_folder(folder_name: str): +def download_file_from_folder(folder_name: str) -> Union[dict, None]: try: files = dict() response = driver_instance.service.files().list( @@ -40,4 +42,4 @@ def download_file_from_folder(folder_name: str): return files except HttpError as error: print(f"An error occurred: {error}") - return None \ No newline at end of file + return None diff --git a/file_automation/remote/google_drive/search/search_drive.py b/file_automation/remote/google_drive/search/search_drive.py index 3c91496..a62b773 100644 --- a/file_automation/remote/google_drive/search/search_drive.py +++ b/file_automation/remote/google_drive/search/search_drive.py @@ -1,9 +1,11 @@ +from typing import Union + from googleapiclient.errors import HttpError from file_automation.remote.google_drive.driver_instance import driver_instance -def search_all_file(): +def search_all_file() -> Union[dict, None]: try: item = dict() response = driver_instance.service.files().list().execute() @@ -15,7 +17,7 @@ def search_all_file(): return None -def search_file_mimetype(mime_type: str): +def search_file_mimetype(mime_type: str) -> Union[dict, None]: try: files = dict() page_token = None @@ -36,7 +38,7 @@ def search_file_mimetype(mime_type: str): return None -def search_field(field_pattern: str): +def search_field(field_pattern: str) -> Union[dict, None]: try: files = dict() response = driver_instance.service.files().list(fields=field_pattern).execute() diff --git a/file_automation/remote/google_drive/share/share_file.py b/file_automation/remote/google_drive/share/share_file.py index d5b4022..e1749d3 100644 --- a/file_automation/remote/google_drive/share/share_file.py +++ b/file_automation/remote/google_drive/share/share_file.py @@ -1,10 +1,12 @@ +from typing import Union + from googleapiclient.errors import HttpError from file_automation.remote.google_drive.driver_instance import driver_instance def share_file_to_user( - file_id: str, user: str, user_role: str = "writer"): + file_id: str, user: str, user_role: str = "writer") -> Union[dict, None]: try: service = driver_instance.service user_permission = { @@ -21,7 +23,7 @@ def share_file_to_user( return None -def share_file_to_anyone(file_id: str, share_role: str = "reader"): +def share_file_to_anyone(file_id: str, share_role: str = "reader") -> Union[dict, None]: try: service = driver_instance.service user_permission = { @@ -39,7 +41,7 @@ def share_file_to_anyone(file_id: str, share_role: str = "reader"): def share_file_to_domain( - file_id: str, domain: str, domain_role: str = "reader"): + file_id: str, domain: str, domain_role: str = "reader") -> Union[dict, None]: try: service = driver_instance.service domain_permission = { diff --git a/file_automation/remote/google_drive/upload/upload_to_driver.py b/file_automation/remote/google_drive/upload/upload_to_driver.py index 8f99b38..52c0847 100644 --- a/file_automation/remote/google_drive/upload/upload_to_driver.py +++ b/file_automation/remote/google_drive/upload/upload_to_driver.py @@ -1,4 +1,5 @@ from pathlib import Path +from typing import List, Union, Optional from googleapiclient.errors import HttpError from googleapiclient.http import MediaFileUpload @@ -6,7 +7,7 @@ from file_automation.remote.google_drive.driver_instance import driver_instance -def upload_to_drive(file_path: str, file_name: str = None): +def upload_to_drive(file_path: str, file_name: str = None) -> Union[dict, None]: try: file_path = Path(file_path) if file_path.is_file(): @@ -32,7 +33,7 @@ def upload_to_drive(file_path: str, file_name: str = None): return None -def upload_to_folder(folder_id: str, file_path: str, file_name: str = None): +def upload_to_folder(folder_id: str, file_path: str, file_name: str = None) -> Union[dict, None]: try: file_path = Path(file_path) if file_path.is_file(): @@ -59,7 +60,7 @@ def upload_to_folder(folder_id: str, file_path: str, file_name: str = None): return None -def upload_dir_to_drive(dir_path: str): +def upload_dir_to_drive(dir_path: str) -> List[Optional[set]]: dir_path = Path(dir_path) ids = list() if dir_path.is_dir(): @@ -72,7 +73,7 @@ def upload_dir_to_drive(dir_path: str): raise FileNotFoundError -def upload_dir_to_folder(folder_id: str, dir_path: str): +def upload_dir_to_folder(folder_id: str, dir_path: str) -> List[Optional[set]]: dir_path = Path(dir_path) ids = list() if dir_path.is_dir(): From 18fa3d7b8069d7574abb7a4a7eeabed42f42e9ed Mon Sep 17 00:00:00 2001 From: JeffreyChen <33644111+JE-Chen@users.noreply.github.com> Date: Thu, 8 Jun 2023 15:16:33 +0800 Subject: [PATCH 2/7] Add local log Add local log --- file_automation/local/dir/dir_process.py | 23 ++++++++--- file_automation/local/file/file_process.py | 39 ++++++++++++++++--- file_automation/local/zip/zip_process.py | 39 +++++++++++++++++-- file_automation/utils/logging/__init__.py | 0 .../utils/logging/loggin_instance.py | 9 +++++ 5 files changed, 95 insertions(+), 15 deletions(-) create mode 100644 file_automation/utils/logging/__init__.py create mode 100644 file_automation/utils/logging/loggin_instance.py diff --git a/file_automation/local/dir/dir_process.py b/file_automation/local/dir/dir_process.py index 3926702..114771a 100644 --- a/file_automation/local/dir/dir_process.py +++ b/file_automation/local/dir/dir_process.py @@ -1,9 +1,9 @@ -import os import shutil import sys from pathlib import Path from file_automation.utils.exception.exceptions import DirNotExistsException +from file_automation.utils.logging.loggin_instance import file_automation_logger def copy_dir(dir_path: str, target_dir_path: str): @@ -12,10 +12,11 @@ def copy_dir(dir_path: str, target_dir_path: str): if dir_path.is_dir(): try: shutil.copytree(dir_path, target_dir_path, dirs_exist_ok=True) + file_automation_logger.info(f"Copy dir {dir_path}") except shutil.Error as error: - print(repr(error)) + file_automation_logger.error(f"Copy dir {dir_path} failed: {repr(error)}") else: - print(repr(DirNotExistsException), file=sys.stderr) + file_automation_logger.error(f"Copy dir {dir_path} failed: {repr(DirNotExistsException)}") def remove_dir_tree(dir_path: str): @@ -23,8 +24,9 @@ def remove_dir_tree(dir_path: str): if dir_path.is_dir(): try: shutil.rmtree(dir_path) + file_automation_logger.info(f"Remove dir tree {dir_path}") except shutil.Error as error: - print(repr(error)) + file_automation_logger.error(f"Remove dir tree {dir_path} error: {repr(error)}") def rename_dir(origin_dir_path, target_dir: str): @@ -32,12 +34,21 @@ def rename_dir(origin_dir_path, target_dir: str): if origin_dir_path.exists() and origin_dir_path.is_dir(): try: Path.rename(origin_dir_path, target_dir) + file_automation_logger.info( + f"Rename dir origin dir path: {origin_dir_path}, target dir path: {target_dir}") except Exception as error: - print(repr(error)) + file_automation_logger.error( + f"Rename dir error: {repr(error)}, " + f"Rename dir origin dir path: {origin_dir_path}, " + f"target dir path: {target_dir}") else: - print(repr(DirNotExistsException), file=sys.stderr) + file_automation_logger.error( + f"Rename dir error: {repr(DirNotExistsException)}, " + f"Rename dir origin dir path: {origin_dir_path}, " + f"target dir path: {target_dir}") def create_dir(dir_path: str): dir_path = Path(dir_path) dir_path.mkdir(exist_ok=True) + file_automation_logger.info(f"Create dir {dir_path}") diff --git a/file_automation/local/file/file_process.py b/file_automation/local/file/file_process.py index 83e3066..4479fcc 100644 --- a/file_automation/local/file/file_process.py +++ b/file_automation/local/file/file_process.py @@ -3,6 +3,7 @@ from pathlib import Path from file_automation.utils.exception.exceptions import FileNotExistsException, DirNotExistsException +from file_automation.utils.logging.loggin_instance import file_automation_logger def copy_file(file_path: str, target_path: str): @@ -10,10 +11,11 @@ def copy_file(file_path: str, target_path: str): if file_path.is_file() and file_path.exists(): try: shutil.copy2(file_path, target_path) + file_automation_logger.info(f"Copy file origin path: {file_path}, target path : {target_path}") except shutil.Error as error: - print(repr(error)) + file_automation_logger.error(f"Copy file failed: {repr(error)}") else: - print(repr(FileNotExistsException), file=sys.stderr) + file_automation_logger.error(f"Copy file failed: {repr(FileNotExistsException)}") def copy_specify_extension_file(file_dir_path: str, target_extension: str, target_path: str): @@ -21,8 +23,13 @@ def copy_specify_extension_file(file_dir_path: str, target_extension: str, targe if file_dir_path.exists() and file_dir_path.is_dir(): for file in file_dir_path.glob(f"**/*.{target_extension}"): copy_file(str(file), target_path) + file_automation_logger.info( + f"Copy specify extension file on dir" + f"origin dir path: {file_dir_path}, target extension: {target_extension}, " + f"to target path {target_path}") else: - print(repr(DirNotExistsException), file=sys.stderr) + file_automation_logger.error( + f"Copy specify extension file failed: {repr(FileNotExistsException)}") def copy_all_file_to_dir(dir_path: str, target_dir_path: str): @@ -31,8 +38,18 @@ def copy_all_file_to_dir(dir_path: str, target_dir_path: str): if dir_path.is_dir() and target_dir_path.is_dir(): try: shutil.move(str(dir_path), str(target_dir_path)) + file_automation_logger.info( + f"Copy all file to dir, " + f"origin dir: {dir_path}, " + f"target dir: {target_dir_path}" + ) except shutil.Error as error: - print(repr(error), file=sys.stderr) + file_automation_logger.error( + f"Copy all file to dir failed, " + f"origin dir: {dir_path}, " + f"target dir: {target_dir_path}, " + f"error: {repr(error)}" + ) else: print(repr(DirNotExistsException), file=sys.stderr) @@ -49,13 +66,23 @@ def rename_file(origin_file_path, target_name: str, file_extension=None): for file in file_list: file.rename(Path(origin_file_path, target_name)) file_index = file_index + 1 + file_automation_logger.info( + f"Renamed file: origin file path:{origin_file_path}, with new name: {target_name}") except Exception as error: - print(repr(error)) + file_automation_logger.error( + f"Rename file failed, " + f"origin file path: {origin_file_path}, " + f"target name: {target_name}, " + f"file_extension: {file_extension}, " + f"error: {repr(error)}" + ) else: - print(repr(DirNotExistsException), file=sys.stderr) + file_automation_logger.error( + f"Rename file failed, error: {repr(DirNotExistsException)}") def remove_file(file_path: str): file_path = Path(file_path) if file_path.exists() and file_path.is_file(): file_path.unlink(missing_ok=True) + file_automation_logger.info(f"Remove file, file path: {file_path}") diff --git a/file_automation/local/zip/zip_process.py b/file_automation/local/zip/zip_process.py index 112e53a..4e03176 100644 --- a/file_automation/local/zip/zip_process.py +++ b/file_automation/local/zip/zip_process.py @@ -1,14 +1,15 @@ -import sys import zipfile from pathlib import Path from shutil import make_archive from typing import List from file_automation.utils.exception.exceptions import ZIPGetWrongFileException +from file_automation.utils.logging.loggin_instance import file_automation_logger def zip_dir(dir_we_want_to_zip: str, zip_name: str): make_archive(root_dir=dir_we_want_to_zip, base_name=zip_name, format="zip") + file_automation_logger.info(f"Dir to zip: {dir_we_want_to_zip}, zip file name: {zip_name}") def zip_file(zip_file_path: str, file: [str, List[str]]): @@ -16,28 +17,43 @@ def zip_file(zip_file_path: str, file: [str, List[str]]): if isinstance(file, str): file_name = Path(file) current_zip.write(file, file_name.name) + file_automation_logger.info( + f"Write file: {file_name} to zip: {current_zip}" + ) else: if isinstance(file, list): for writeable in file: file_name = Path(writeable) current_zip.write(writeable, file_name.name) + file_automation_logger.info( + f"Write file: {writeable} to zip: {current_zip}" + ) else: - print(repr(ZIPGetWrongFileException), file=sys.stderr) + file_automation_logger.error( + repr(ZIPGetWrongFileException)) current_zip.close() def read_zip_file(zip_file_path: str, file_name: str, password: [str, None] = None): current_zip = zipfile.ZipFile(zip_file_path, mode="r") - data = None with current_zip.open(name=file_name, mode="r", pwd=password, force_zip64=True) as read_file: data = read_file.read() current_zip.close() + file_automation_logger.info( + f"Read zip file: {zip_file_path}" + ) return data def unzip_file(zip_file_path: str, extract_member, extract_path: [str, None] = None, password: [str, None] = None): current_zip = zipfile.ZipFile(zip_file_path, mode="r") current_zip.extract(member=extract_member, path=extract_path, pwd=password) + file_automation_logger.info( + f"Unzip file: {zip_file_path}, " + f"extract member: {extract_member}, " + f"extract path: {extract_path}, " + f"password: {password}" + ) current_zip.close() @@ -46,6 +62,12 @@ def unzip_all( extract_path: [str, None] = None, password: [str, None] = None): current_zip = zipfile.ZipFile(zip_file_path, mode="r") current_zip.extractall(members=extract_member, path=extract_path, pwd=password) + file_automation_logger.info( + f"Unzip file: {zip_file_path}, " + f"extract member: {extract_member}, " + f"extract path: {extract_path}, " + f"password: {password}" + ) current_zip.close() @@ -53,6 +75,9 @@ def zip_info(zip_file_path: str): current_zip = zipfile.ZipFile(zip_file_path, mode="r") info_list = current_zip.infolist() current_zip.close() + file_automation_logger.info( + f"Show zip info: {zip_file_path}" + ) return info_list @@ -60,6 +85,9 @@ def zip_file_info(zip_file_path: str): current_zip = zipfile.ZipFile(zip_file_path, mode="r") name_list = current_zip.namelist() current_zip.close() + file_automation_logger.info( + f"Show zip file info: {zip_file_path}" + ) return name_list @@ -67,3 +95,8 @@ def set_zip_password(zip_file_path: str, password: bytes): current_zip = zipfile.ZipFile(zip_file_path) current_zip.setpassword(pwd=password) current_zip.close() + file_automation_logger.info( + f"Set zip file password, " + f"zip file: {zip_file_path}, " + f"zup password: {password}" + ) diff --git a/file_automation/utils/logging/__init__.py b/file_automation/utils/logging/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/file_automation/utils/logging/loggin_instance.py b/file_automation/utils/logging/loggin_instance.py new file mode 100644 index 0000000..cc8746a --- /dev/null +++ b/file_automation/utils/logging/loggin_instance.py @@ -0,0 +1,9 @@ +import logging + +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) From ac64a6e8f840f592eef316df9943a0d1b1d700e9 Mon Sep 17 00:00:00 2001 From: JeffreyChen <33644111+JE-Chen@users.noreply.github.com> Date: Thu, 8 Jun 2023 16:15:08 +0800 Subject: [PATCH 3/7] Add remote (Google Drive) logger Add remote (Google Drive) logger --- .../google_drive/delete/delete_manager.py | 7 +++- .../remote/google_drive/dir/folder_manager.py | 9 +++- .../google_drive/download/download_file.py | 21 ++++++++-- .../remote/google_drive/driver_instance.py | 14 ++++++- .../google_drive/search/search_drive.py | 25 +++++++++-- .../remote/google_drive/share/share_file.py | 28 +++++++++++-- .../google_drive/upload/upload_to_driver.py | 42 ++++++++++++++++--- 7 files changed, 127 insertions(+), 19 deletions(-) diff --git a/file_automation/remote/google_drive/delete/delete_manager.py b/file_automation/remote/google_drive/delete/delete_manager.py index 225a6a1..9206cde 100644 --- a/file_automation/remote/google_drive/delete/delete_manager.py +++ b/file_automation/remote/google_drive/delete/delete_manager.py @@ -3,12 +3,17 @@ from googleapiclient.errors import HttpError from file_automation.remote.google_drive.driver_instance import driver_instance +from file_automation.utils.logging.loggin_instance import file_automation_logger def delete_file(file_id: str) -> Union[dict, None]: try: file = driver_instance.service.files().delete(fileId=file_id).execute() + file_automation_logger.info(f"Delete drive file: {file_id}") return file except HttpError as error: - print(f"An error occurred: {error}") + file_automation_logger.error( + f"Delete file failed," + f"error: {error}" + ) return None diff --git a/file_automation/remote/google_drive/dir/folder_manager.py b/file_automation/remote/google_drive/dir/folder_manager.py index c83a87e..72e45b7 100644 --- a/file_automation/remote/google_drive/dir/folder_manager.py +++ b/file_automation/remote/google_drive/dir/folder_manager.py @@ -3,6 +3,7 @@ from googleapiclient.errors import HttpError from file_automation.remote.google_drive.driver_instance import driver_instance +from file_automation.utils.logging.loggin_instance import file_automation_logger def add_folder(folder_name: str) -> Union[dict, None]: @@ -15,7 +16,13 @@ def add_folder(folder_name: str) -> Union[dict, None]: body=file_metadata, fields="id" ).execute() + file_automation_logger.info( + f"Add drive folder: {folder_name}" + ) return file.get("id") except HttpError as error: - print(f"An error occurred: {error}") + file_automation_logger.error( + f"Delete file failed," + f"error: {error}" + ) return None diff --git a/file_automation/remote/google_drive/download/download_file.py b/file_automation/remote/google_drive/download/download_file.py index 7f42d7e..5e1f389 100644 --- a/file_automation/remote/google_drive/download/download_file.py +++ b/file_automation/remote/google_drive/download/download_file.py @@ -6,6 +6,7 @@ from googleapiclient.http import MediaIoBaseDownload from file_automation.remote.google_drive.driver_instance import driver_instance +from file_automation.utils.logging.loggin_instance import file_automation_logger def download_file(file_id: str, file_name: str) -> BytesIO: @@ -16,12 +17,20 @@ def download_file(file_id: str, file_name: str) -> BytesIO: done = False while done is False: status, done = downloader.next_chunk() - print(f"Download {file_name} {int(status.progress() * 100)}%.") + file_automation_logger.info( + f"Download {file_name} {int(status.progress() * 100)}%." + ) except HttpError as error: - print(f"An error occurred: {error}") + file_automation_logger.error( + f"Delete file failed," + f"error: {error}" + ) return None with open(file_name, "wb") as output_file: output_file.write(file.getbuffer()) + file_automation_logger.info( + f"Download file: {file_id} with name: {file_name}" + ) return file @@ -39,7 +48,13 @@ def download_file_from_folder(folder_name: str) -> Union[dict, None]: for file in response.get("files", []): download_file(file.get("id"), file.get("name")) files.update({file.get("name"): file.get("id")}) + file_automation_logger.info( + f"Download all file on {folder_name} done." + ) return files except HttpError as error: - print(f"An error occurred: {error}") + file_automation_logger.error( + f"Delete file failed," + f"error: {error}" + ) return None diff --git a/file_automation/remote/google_drive/driver_instance.py b/file_automation/remote/google_drive/driver_instance.py index 4cf48bd..960f0cb 100644 --- a/file_automation/remote/google_drive/driver_instance.py +++ b/file_automation/remote/google_drive/driver_instance.py @@ -6,6 +6,8 @@ from googleapiclient.discovery import build from googleapiclient.errors import HttpError +from file_automation.utils.logging.loggin_instance import file_automation_logger + class GoogleDrive(object): @@ -21,6 +23,9 @@ def __init__(self, token_path: str, credentials_path: str): # created automatically when the authorization flow completes for the first # time. if token_path.exists(): + file_automation_logger.info( + f"Token exists try to load." + ) creds = Credentials.from_authorized_user_file(str(token_path), self.scopes) # If there are no (valid) credentials available, let the user log in. if not creds or not creds.valid: @@ -33,11 +38,16 @@ def __init__(self, token_path: str, credentials_path: str): # Save the credentials for the next run with open(str(token_path), 'w') as token: token.write(creds.to_json()) - try: self.service = build('drive', 'v3', credentials=creds) + file_automation_logger.info( + f"Loading service successfully." + ) except HttpError as error: - print(f'An error occurred: {error}') + file_automation_logger.error( + f"Delete file failed," + f"error: {error}" + ) driver_instance = GoogleDrive(str(Path(Path.cwd(), "token.json")), str(Path(Path.cwd(), "credentials.json"))) diff --git a/file_automation/remote/google_drive/search/search_drive.py b/file_automation/remote/google_drive/search/search_drive.py index a62b773..329bc4d 100644 --- a/file_automation/remote/google_drive/search/search_drive.py +++ b/file_automation/remote/google_drive/search/search_drive.py @@ -3,6 +3,7 @@ from googleapiclient.errors import HttpError from file_automation.remote.google_drive.driver_instance import driver_instance +from file_automation.utils.logging.loggin_instance import file_automation_logger def search_all_file() -> Union[dict, None]: @@ -11,9 +12,15 @@ def search_all_file() -> Union[dict, None]: response = driver_instance.service.files().list().execute() for file in response.get("files", []): item.update({file.get("name"): file.get("id")}) + file_automation_logger.info( + f"Search all file on drive" + ) return item except HttpError as error: - print(f"An error occurred: {error}") + file_automation_logger.error( + f"Delete file failed," + f"error: {error}" + ) return None @@ -32,9 +39,15 @@ def search_file_mimetype(mime_type: str) -> Union[dict, None]: page_token = response.get('nextPageToken', None) if page_token is None: break + file_automation_logger.info( + f"Search all {mime_type} file on drive" + ) return files except HttpError as error: - print(f"An error occurred: {error}") + file_automation_logger.error( + f"Delete file failed," + f"error: {error}" + ) return None @@ -44,8 +57,14 @@ def search_field(field_pattern: str) -> Union[dict, None]: response = driver_instance.service.files().list(fields=field_pattern).execute() for file in response.get("files", []): files.update({file.get("name"): file.get("id")}) + file_automation_logger.info( + f"Search all {field_pattern}" + ) return files except HttpError as error: - print(f"An error occurred: {error}") + file_automation_logger.error( + f"Delete file failed," + f"error: {error}" + ) return None diff --git a/file_automation/remote/google_drive/share/share_file.py b/file_automation/remote/google_drive/share/share_file.py index e1749d3..fd49137 100644 --- a/file_automation/remote/google_drive/share/share_file.py +++ b/file_automation/remote/google_drive/share/share_file.py @@ -3,6 +3,7 @@ from googleapiclient.errors import HttpError from file_automation.remote.google_drive.driver_instance import driver_instance +from file_automation.utils.logging.loggin_instance import file_automation_logger def share_file_to_user( @@ -14,12 +15,20 @@ def share_file_to_user( "role": user_role, "emailAddress": user } + file_automation_logger.info( + f"Share file: {file_id}, " + f"to user: {user}, " + f"with user role: {user_role}" + ) return service.permissions().create( fileId=file_id, body=user_permission, fields='id', ).execute() except HttpError as error: - print(f"An error occurred: {error}") + file_automation_logger.error( + f"Delete file failed," + f"error: {error}" + ) return None @@ -31,12 +40,18 @@ def share_file_to_anyone(file_id: str, share_role: str = "reader") -> Union[dict "value": "anyone", "role": share_role } + file_automation_logger.info( + f"Share file to anyone file: {file_id} with role: {share_role}" + ) return service.permissions().create( fileId=file_id, body=user_permission, fields='id', ).execute() except HttpError as error: - print(f"An error occurred: {error}") + file_automation_logger.error( + f"Delete file failed," + f"error: {error}" + ) return None @@ -49,10 +64,17 @@ def share_file_to_domain( "role": domain_role, "domain": domain } + file_automation_logger.info( + f"Share file to domain: {domain}, " + f"with domain role: {domain_role}" + ) return service.permissions().create( fileId=file_id, body=domain_permission, fields='id', ).execute() except HttpError as error: - print(f"An error occurred: {error}") + file_automation_logger.error( + f"Delete file failed," + f"error: {error}" + ) return None diff --git a/file_automation/remote/google_drive/upload/upload_to_driver.py b/file_automation/remote/google_drive/upload/upload_to_driver.py index 52c0847..c420449 100644 --- a/file_automation/remote/google_drive/upload/upload_to_driver.py +++ b/file_automation/remote/google_drive/upload/upload_to_driver.py @@ -5,6 +5,7 @@ from googleapiclient.http import MediaFileUpload from file_automation.remote.google_drive.driver_instance import driver_instance +from file_automation.utils.logging.loggin_instance import file_automation_logger def upload_to_drive(file_path: str, file_name: str = None) -> Union[dict, None]: @@ -25,11 +26,20 @@ def upload_to_drive(file_path: str, file_name: str = None) -> Union[dict, None]: media_body=media, fields="id" ).execute() + file_automation_logger.info( + f"Upload file to drive file: {file_path}, " + f"with name: {file_name}" + ) return file_id else: - raise FileNotFoundError + file_automation_logger.error( + FileNotFoundError + ) except HttpError as error: - print(f"An error occurred: {error}") + file_automation_logger.error( + f"Delete file failed," + f"error: {error}" + ) return None @@ -52,11 +62,21 @@ def upload_to_folder(folder_id: str, file_path: str, file_name: str = None) -> U media_body=media, fields="id" ).execute() + file_automation_logger.info( + f"Upload file to folder: {folder_id}," + f"file_path: {file_path}, " + f"with name: {file_name}" + ) return file_id else: - raise FileNotFoundError + file_automation_logger.error( + FileNotFoundError + ) except HttpError as error: - print(f"An error occurred: {error}") + file_automation_logger.error( + f"Delete file failed," + f"error: {error}" + ) return None @@ -68,9 +88,14 @@ def upload_dir_to_drive(dir_path: str) -> List[Optional[set]]: for path in path_list: if path.is_file(): ids.append(upload_to_drive(str(path.absolute()), path.name)) + file_automation_logger.info( + f"Upload all file on dir: {dir_path} to drive" + ) return ids else: - raise FileNotFoundError + file_automation_logger.error( + FileNotFoundError + ) def upload_dir_to_folder(folder_id: str, dir_path: str) -> List[Optional[set]]: @@ -81,6 +106,11 @@ def upload_dir_to_folder(folder_id: str, dir_path: str) -> List[Optional[set]]: for path in path_list: if path.is_file(): ids.append(upload_to_folder(folder_id, str(path.absolute()), path.name)) + file_automation_logger.info( + f"Upload all file on dir: {dir_path} to folder: {folder_id}" + ) return ids else: - raise FileNotFoundError + file_automation_logger.error( + FileNotFoundError + ) From 11a02e89241f618842c2240c054bc5dc745330da Mon Sep 17 00:00:00 2001 From: JeffreyChen <33644111+JE-Chen@users.noreply.github.com> Date: Thu, 8 Jun 2023 16:19:51 +0800 Subject: [PATCH 4/7] Update dev version Update dev version --- pyproject.toml | 4 ++-- dev.toml => stable.toml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) rename dev.toml => stable.toml (95%) diff --git a/pyproject.toml b/pyproject.toml index 006e36b..b52c384 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,8 +5,8 @@ requires = ["setuptools>=61.0"] build-backend = "setuptools.build_meta" [project] -name = "automation_file" -version = "0.0.3" +name = "automation_file_dev" +version = "0.0.5" authors = [ { name = "JE-Chen", email = "zenmailman@gmail.com" }, ] diff --git a/dev.toml b/stable.toml similarity index 95% rename from dev.toml rename to stable.toml index 3e44f94..006e36b 100644 --- a/dev.toml +++ b/stable.toml @@ -5,8 +5,8 @@ requires = ["setuptools>=61.0"] build-backend = "setuptools.build_meta" [project] -name = "automation_file_dev" -version = "0.0.4" +name = "automation_file" +version = "0.0.3" authors = [ { name = "JE-Chen", email = "zenmailman@gmail.com" }, ] From 7256c47523d7433ad7370246dd2a22074bc5a459 Mon Sep 17 00:00:00 2001 From: JeffreyChen <33644111+JE-Chen@users.noreply.github.com> Date: Thu, 8 Jun 2023 16:40:23 +0800 Subject: [PATCH 5/7] Upload stable version Upload stable version --- stable.toml => dev.toml | 4 ++-- pyproject.toml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) rename stable.toml => dev.toml (95%) diff --git a/stable.toml b/dev.toml similarity index 95% rename from stable.toml rename to dev.toml index 006e36b..b52c384 100644 --- a/stable.toml +++ b/dev.toml @@ -5,8 +5,8 @@ requires = ["setuptools>=61.0"] build-backend = "setuptools.build_meta" [project] -name = "automation_file" -version = "0.0.3" +name = "automation_file_dev" +version = "0.0.5" authors = [ { name = "JE-Chen", email = "zenmailman@gmail.com" }, ] diff --git a/pyproject.toml b/pyproject.toml index b52c384..86e5b91 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,8 +5,8 @@ requires = ["setuptools>=61.0"] build-backend = "setuptools.build_meta" [project] -name = "automation_file_dev" -version = "0.0.5" +name = "automation_file" +version = "0.0.4" authors = [ { name = "JE-Chen", email = "zenmailman@gmail.com" }, ] From 2cb1a19d3c5ea1c2d812e19797b21bc3e2eecb5f Mon Sep 17 00:00:00 2001 From: JeffreyChen <33644111+JE-Chen@users.noreply.github.com> Date: Thu, 8 Jun 2023 16:53:29 +0800 Subject: [PATCH 6/7] Upload dev version Upload dev version --- file_automation/__init__.py | 14 +++++++++++++- .../remote/google_drive/driver_instance.py | 7 +++++-- pyproject.toml | 4 ++-- dev.toml => stable.toml | 4 ++-- tests/unit_test/remote/google_drive/quick_test.py | 6 ++++-- 5 files changed, 26 insertions(+), 9 deletions(-) rename dev.toml => stable.toml (95%) diff --git a/file_automation/__init__.py b/file_automation/__init__.py index e6017f9..67d9eb7 100644 --- a/file_automation/__init__.py +++ b/file_automation/__init__.py @@ -5,10 +5,22 @@ read_zip_file, unzip_file, unzip_all from file_automation.remote.google_drive.driver_instance import driver_instance +from file_automation.remote.google_drive.search.search_drive import \ + search_all_file, search_field, search_file_mimetype +from file_automation.remote.google_drive.upload.upload_to_driver import \ + upload_dir_to_folder, upload_to_folder, upload_dir_to_drive, upload_to_drive +from file_automation.remote.google_drive.dir.folder_manager import add_folder +from file_automation.remote.google_drive.share.share_file import \ + share_file_to_anyone, share_file_to_domain, share_file_to_user +from file_automation.remote.google_drive.delete.delete_manager import delete_file +from file_automation.remote.google_drive.download.download_file import download_file, download_file_from_folder __all__ = [ "copy_file", "rename_file", "remove_file", "copy_all_file_to_dir", "copy_specify_extension_file", "copy_dir", "create_dir", "copy_specify_extension_file", "remove_dir_tree", "zip_dir", "zip_file", "zip_info", "zip_file_info", "set_zip_password", "unzip_file", "read_zip_file", - "unzip_all", "driver_instance", + "unzip_all", "driver_instance", "search_all_file", "search_field", "search_file_mimetype", + "upload_dir_to_folder", "upload_to_folder", "upload_dir_to_drive", "upload_to_drive", + "add_folder", "share_file_to_anyone", "share_file_to_domain", "share_file_to_user", + "delete_file", "download_file", "download_file_from_folder" ] diff --git a/file_automation/remote/google_drive/driver_instance.py b/file_automation/remote/google_drive/driver_instance.py index 960f0cb..274c42a 100644 --- a/file_automation/remote/google_drive/driver_instance.py +++ b/file_automation/remote/google_drive/driver_instance.py @@ -11,11 +11,13 @@ class GoogleDrive(object): - def __init__(self, token_path: str, credentials_path: str): + def __init__(self): self.google_drive_instance = None self.creds = None self.service = None self.scopes = ["https://www.googleapis.com/auth/drive"] + + def later_init(self, token_path: str, credentials_path: str): token_path = Path(token_path) credentials_path = Path(credentials_path) creds = None @@ -50,4 +52,5 @@ def __init__(self, token_path: str, credentials_path: str): ) -driver_instance = GoogleDrive(str(Path(Path.cwd(), "token.json")), str(Path(Path.cwd(), "credentials.json"))) +driver_instance = GoogleDrive() + diff --git a/pyproject.toml b/pyproject.toml index 86e5b91..2b44d63 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,8 +5,8 @@ requires = ["setuptools>=61.0"] build-backend = "setuptools.build_meta" [project] -name = "automation_file" -version = "0.0.4" +name = "automation_file_dev" +version = "0.0.6" authors = [ { name = "JE-Chen", email = "zenmailman@gmail.com" }, ] diff --git a/dev.toml b/stable.toml similarity index 95% rename from dev.toml rename to stable.toml index b52c384..86e5b91 100644 --- a/dev.toml +++ b/stable.toml @@ -5,8 +5,8 @@ requires = ["setuptools>=61.0"] build-backend = "setuptools.build_meta" [project] -name = "automation_file_dev" -version = "0.0.5" +name = "automation_file" +version = "0.0.4" authors = [ { name = "JE-Chen", email = "zenmailman@gmail.com" }, ] diff --git a/tests/unit_test/remote/google_drive/quick_test.py b/tests/unit_test/remote/google_drive/quick_test.py index 4c249ae..882a38a 100644 --- a/tests/unit_test/remote/google_drive/quick_test.py +++ b/tests/unit_test/remote/google_drive/quick_test.py @@ -1,5 +1,7 @@ +from pathlib import Path + +from file_automation import driver_instance from file_automation.remote.google_drive.search.search_drive import search_all_file -from file_automation.remote.google_drive.upload.upload_to_driver import upload_dir_to_folder +driver_instance.later_init(str(Path(Path.cwd(), "token.json")), str(Path(Path.cwd(), "credentials.json"))) print(search_all_file()) -print(upload_dir_to_folder(r"1J-m5cHqkXumaHqlHfL90BySEqWsG9zbP", r"C:\Users\JeffreyChen\Desktop\Paper")) From 5334a9b68e13c76c3b6608a1ea5277666e753661 Mon Sep 17 00:00:00 2001 From: JeffreyChen <33644111+JE-Chen@users.noreply.github.com> Date: Thu, 8 Jun 2023 16:56:34 +0800 Subject: [PATCH 7/7] Upload stable version Upload stable version --- stable.toml => dev.toml | 4 ++-- pyproject.toml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) rename stable.toml => dev.toml (95%) diff --git a/stable.toml b/dev.toml similarity index 95% rename from stable.toml rename to dev.toml index 86e5b91..2b44d63 100644 --- a/stable.toml +++ b/dev.toml @@ -5,8 +5,8 @@ requires = ["setuptools>=61.0"] build-backend = "setuptools.build_meta" [project] -name = "automation_file" -version = "0.0.4" +name = "automation_file_dev" +version = "0.0.6" authors = [ { name = "JE-Chen", email = "zenmailman@gmail.com" }, ] diff --git a/pyproject.toml b/pyproject.toml index 2b44d63..467f09a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,8 +5,8 @@ requires = ["setuptools>=61.0"] build-backend = "setuptools.build_meta" [project] -name = "automation_file_dev" -version = "0.0.6" +name = "automation_file" +version = "0.0.5" authors = [ { name = "JE-Chen", email = "zenmailman@gmail.com" }, ]