Skip to content
Merged

Dev #13

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.4"
version = "0.0.6"
authors = [
{ name = "JE-Chen", email = "zenmailman@gmail.com" },
]
Expand Down
14 changes: 13 additions & 1 deletion file_automation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
23 changes: 17 additions & 6 deletions file_automation/local/dir/dir_process.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -12,32 +12,43 @@ 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):
dir_path = Path(dir_path)
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):
origin_dir_path = Path(origin_dir_path)
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}")
39 changes: 33 additions & 6 deletions file_automation/local/file/file_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,33 @@
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):
file_path = Path(file_path)
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):
file_dir_path = Path(file_dir_path)
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):
Expand All @@ -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)

Expand All @@ -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}")
39 changes: 36 additions & 3 deletions file_automation/local/zip/zip_process.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,59 @@
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]]):
current_zip = zipfile.ZipFile(zip_file_path, mode="w")
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()


Expand All @@ -46,24 +62,41 @@ 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()


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


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


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}"
)
11 changes: 9 additions & 2 deletions file_automation/remote/google_drive/delete/delete_manager.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
from typing import Union

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):
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
13 changes: 11 additions & 2 deletions file_automation/remote/google_drive/dir/folder_manager.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from typing import Union

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):
def add_folder(folder_name: str) -> Union[dict, None]:
try:
file_metadata = {
"name": folder_name,
Expand All @@ -13,7 +16,13 @@ def add_folder(folder_name: str):
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
29 changes: 23 additions & 6 deletions file_automation/remote/google_drive/download/download_file.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
import io
from io import BytesIO
from typing import Union

from googleapiclient.errors import HttpError
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):
def download_file(file_id: str, file_name: str) -> BytesIO:
try:
request = driver_instance.service.files().get_media(fileId=file_id)
file = io.BytesIO()
downloader = MediaIoBaseDownload(file, request)
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


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(
Expand All @@ -37,7 +48,13 @@ def download_file_from_folder(folder_name: str):
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}")
return None
file_automation_logger.error(
f"Delete file failed,"
f"error: {error}"
)
return None
Loading