Skip to content

Commit 4f2f9bb

Browse files
authored
Merge pull request #13 from Integration-Automation/dev
Dev
2 parents d334ace + 5334a9b commit 4f2f9bb

16 files changed

Lines changed: 272 additions & 56 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.4"
9+
version = "0.0.6"
1010
authors = [
1111
{ name = "JE-Chen", email = "zenmailman@gmail.com" },
1212
]

file_automation/__init__.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,22 @@
55
read_zip_file, unzip_file, unzip_all
66

77
from file_automation.remote.google_drive.driver_instance import driver_instance
8+
from file_automation.remote.google_drive.search.search_drive import \
9+
search_all_file, search_field, search_file_mimetype
10+
from file_automation.remote.google_drive.upload.upload_to_driver import \
11+
upload_dir_to_folder, upload_to_folder, upload_dir_to_drive, upload_to_drive
12+
from file_automation.remote.google_drive.dir.folder_manager import add_folder
13+
from file_automation.remote.google_drive.share.share_file import \
14+
share_file_to_anyone, share_file_to_domain, share_file_to_user
15+
from file_automation.remote.google_drive.delete.delete_manager import delete_file
16+
from file_automation.remote.google_drive.download.download_file import download_file, download_file_from_folder
817

918
__all__ = [
1019
"copy_file", "rename_file", "remove_file", "copy_all_file_to_dir", "copy_specify_extension_file",
1120
"copy_dir", "create_dir", "copy_specify_extension_file", "remove_dir_tree",
1221
"zip_dir", "zip_file", "zip_info", "zip_file_info", "set_zip_password", "unzip_file", "read_zip_file",
13-
"unzip_all", "driver_instance",
22+
"unzip_all", "driver_instance", "search_all_file", "search_field", "search_file_mimetype",
23+
"upload_dir_to_folder", "upload_to_folder", "upload_dir_to_drive", "upload_to_drive",
24+
"add_folder", "share_file_to_anyone", "share_file_to_domain", "share_file_to_user",
25+
"delete_file", "download_file", "download_file_from_folder"
1426
]
Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import os
21
import shutil
32
import sys
43
from pathlib import Path
54

65
from file_automation.utils.exception.exceptions import DirNotExistsException
6+
from file_automation.utils.logging.loggin_instance import file_automation_logger
77

88

99
def copy_dir(dir_path: str, target_dir_path: str):
@@ -12,32 +12,43 @@ def copy_dir(dir_path: str, target_dir_path: str):
1212
if dir_path.is_dir():
1313
try:
1414
shutil.copytree(dir_path, target_dir_path, dirs_exist_ok=True)
15+
file_automation_logger.info(f"Copy dir {dir_path}")
1516
except shutil.Error as error:
16-
print(repr(error))
17+
file_automation_logger.error(f"Copy dir {dir_path} failed: {repr(error)}")
1718
else:
18-
print(repr(DirNotExistsException), file=sys.stderr)
19+
file_automation_logger.error(f"Copy dir {dir_path} failed: {repr(DirNotExistsException)}")
1920

2021

2122
def remove_dir_tree(dir_path: str):
2223
dir_path = Path(dir_path)
2324
if dir_path.is_dir():
2425
try:
2526
shutil.rmtree(dir_path)
27+
file_automation_logger.info(f"Remove dir tree {dir_path}")
2628
except shutil.Error as error:
27-
print(repr(error))
29+
file_automation_logger.error(f"Remove dir tree {dir_path} error: {repr(error)}")
2830

2931

3032
def rename_dir(origin_dir_path, target_dir: str):
3133
origin_dir_path = Path(origin_dir_path)
3234
if origin_dir_path.exists() and origin_dir_path.is_dir():
3335
try:
3436
Path.rename(origin_dir_path, target_dir)
37+
file_automation_logger.info(
38+
f"Rename dir origin dir path: {origin_dir_path}, target dir path: {target_dir}")
3539
except Exception as error:
36-
print(repr(error))
40+
file_automation_logger.error(
41+
f"Rename dir error: {repr(error)}, "
42+
f"Rename dir origin dir path: {origin_dir_path}, "
43+
f"target dir path: {target_dir}")
3744
else:
38-
print(repr(DirNotExistsException), file=sys.stderr)
45+
file_automation_logger.error(
46+
f"Rename dir error: {repr(DirNotExistsException)}, "
47+
f"Rename dir origin dir path: {origin_dir_path}, "
48+
f"target dir path: {target_dir}")
3949

4050

4151
def create_dir(dir_path: str):
4252
dir_path = Path(dir_path)
4353
dir_path.mkdir(exist_ok=True)
54+
file_automation_logger.info(f"Create dir {dir_path}")

file_automation/local/file/file_process.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,33 @@
33
from pathlib import Path
44

55
from file_automation.utils.exception.exceptions import FileNotExistsException, DirNotExistsException
6+
from file_automation.utils.logging.loggin_instance import file_automation_logger
67

78

89
def copy_file(file_path: str, target_path: str):
910
file_path = Path(file_path)
1011
if file_path.is_file() and file_path.exists():
1112
try:
1213
shutil.copy2(file_path, target_path)
14+
file_automation_logger.info(f"Copy file origin path: {file_path}, target path : {target_path}")
1315
except shutil.Error as error:
14-
print(repr(error))
16+
file_automation_logger.error(f"Copy file failed: {repr(error)}")
1517
else:
16-
print(repr(FileNotExistsException), file=sys.stderr)
18+
file_automation_logger.error(f"Copy file failed: {repr(FileNotExistsException)}")
1719

1820

1921
def copy_specify_extension_file(file_dir_path: str, target_extension: str, target_path: str):
2022
file_dir_path = Path(file_dir_path)
2123
if file_dir_path.exists() and file_dir_path.is_dir():
2224
for file in file_dir_path.glob(f"**/*.{target_extension}"):
2325
copy_file(str(file), target_path)
26+
file_automation_logger.info(
27+
f"Copy specify extension file on dir"
28+
f"origin dir path: {file_dir_path}, target extension: {target_extension}, "
29+
f"to target path {target_path}")
2430
else:
25-
print(repr(DirNotExistsException), file=sys.stderr)
31+
file_automation_logger.error(
32+
f"Copy specify extension file failed: {repr(FileNotExistsException)}")
2633

2734

2835
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):
3138
if dir_path.is_dir() and target_dir_path.is_dir():
3239
try:
3340
shutil.move(str(dir_path), str(target_dir_path))
41+
file_automation_logger.info(
42+
f"Copy all file to dir, "
43+
f"origin dir: {dir_path}, "
44+
f"target dir: {target_dir_path}"
45+
)
3446
except shutil.Error as error:
35-
print(repr(error), file=sys.stderr)
47+
file_automation_logger.error(
48+
f"Copy all file to dir failed, "
49+
f"origin dir: {dir_path}, "
50+
f"target dir: {target_dir_path}, "
51+
f"error: {repr(error)}"
52+
)
3653
else:
3754
print(repr(DirNotExistsException), file=sys.stderr)
3855

@@ -49,13 +66,23 @@ def rename_file(origin_file_path, target_name: str, file_extension=None):
4966
for file in file_list:
5067
file.rename(Path(origin_file_path, target_name))
5168
file_index = file_index + 1
69+
file_automation_logger.info(
70+
f"Renamed file: origin file path:{origin_file_path}, with new name: {target_name}")
5271
except Exception as error:
53-
print(repr(error))
72+
file_automation_logger.error(
73+
f"Rename file failed, "
74+
f"origin file path: {origin_file_path}, "
75+
f"target name: {target_name}, "
76+
f"file_extension: {file_extension}, "
77+
f"error: {repr(error)}"
78+
)
5479
else:
55-
print(repr(DirNotExistsException), file=sys.stderr)
80+
file_automation_logger.error(
81+
f"Rename file failed, error: {repr(DirNotExistsException)}")
5682

5783

5884
def remove_file(file_path: str):
5985
file_path = Path(file_path)
6086
if file_path.exists() and file_path.is_file():
6187
file_path.unlink(missing_ok=True)
88+
file_automation_logger.info(f"Remove file, file path: {file_path}")
Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,59 @@
1-
import sys
21
import zipfile
32
from pathlib import Path
43
from shutil import make_archive
54
from typing import List
65

76
from file_automation.utils.exception.exceptions import ZIPGetWrongFileException
7+
from file_automation.utils.logging.loggin_instance import file_automation_logger
88

99

1010
def zip_dir(dir_we_want_to_zip: str, zip_name: str):
1111
make_archive(root_dir=dir_we_want_to_zip, base_name=zip_name, format="zip")
12+
file_automation_logger.info(f"Dir to zip: {dir_we_want_to_zip}, zip file name: {zip_name}")
1213

1314

1415
def zip_file(zip_file_path: str, file: [str, List[str]]):
1516
current_zip = zipfile.ZipFile(zip_file_path, mode="w")
1617
if isinstance(file, str):
1718
file_name = Path(file)
1819
current_zip.write(file, file_name.name)
20+
file_automation_logger.info(
21+
f"Write file: {file_name} to zip: {current_zip}"
22+
)
1923
else:
2024
if isinstance(file, list):
2125
for writeable in file:
2226
file_name = Path(writeable)
2327
current_zip.write(writeable, file_name.name)
28+
file_automation_logger.info(
29+
f"Write file: {writeable} to zip: {current_zip}"
30+
)
2431
else:
25-
print(repr(ZIPGetWrongFileException), file=sys.stderr)
32+
file_automation_logger.error(
33+
repr(ZIPGetWrongFileException))
2634
current_zip.close()
2735

2836

2937
def read_zip_file(zip_file_path: str, file_name: str, password: [str, None] = None):
3038
current_zip = zipfile.ZipFile(zip_file_path, mode="r")
31-
data = None
3239
with current_zip.open(name=file_name, mode="r", pwd=password, force_zip64=True) as read_file:
3340
data = read_file.read()
3441
current_zip.close()
42+
file_automation_logger.info(
43+
f"Read zip file: {zip_file_path}"
44+
)
3545
return data
3646

3747

3848
def unzip_file(zip_file_path: str, extract_member, extract_path: [str, None] = None, password: [str, None] = None):
3949
current_zip = zipfile.ZipFile(zip_file_path, mode="r")
4050
current_zip.extract(member=extract_member, path=extract_path, pwd=password)
51+
file_automation_logger.info(
52+
f"Unzip file: {zip_file_path}, "
53+
f"extract member: {extract_member}, "
54+
f"extract path: {extract_path}, "
55+
f"password: {password}"
56+
)
4157
current_zip.close()
4258

4359

@@ -46,24 +62,41 @@ def unzip_all(
4662
extract_path: [str, None] = None, password: [str, None] = None):
4763
current_zip = zipfile.ZipFile(zip_file_path, mode="r")
4864
current_zip.extractall(members=extract_member, path=extract_path, pwd=password)
65+
file_automation_logger.info(
66+
f"Unzip file: {zip_file_path}, "
67+
f"extract member: {extract_member}, "
68+
f"extract path: {extract_path}, "
69+
f"password: {password}"
70+
)
4971
current_zip.close()
5072

5173

5274
def zip_info(zip_file_path: str):
5375
current_zip = zipfile.ZipFile(zip_file_path, mode="r")
5476
info_list = current_zip.infolist()
5577
current_zip.close()
78+
file_automation_logger.info(
79+
f"Show zip info: {zip_file_path}"
80+
)
5681
return info_list
5782

5883

5984
def zip_file_info(zip_file_path: str):
6085
current_zip = zipfile.ZipFile(zip_file_path, mode="r")
6186
name_list = current_zip.namelist()
6287
current_zip.close()
88+
file_automation_logger.info(
89+
f"Show zip file info: {zip_file_path}"
90+
)
6391
return name_list
6492

6593

6694
def set_zip_password(zip_file_path: str, password: bytes):
6795
current_zip = zipfile.ZipFile(zip_file_path)
6896
current_zip.setpassword(pwd=password)
6997
current_zip.close()
98+
file_automation_logger.info(
99+
f"Set zip file password, "
100+
f"zip file: {zip_file_path}, "
101+
f"zup password: {password}"
102+
)
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1+
from typing import Union
2+
13
from googleapiclient.errors import HttpError
24

35
from file_automation.remote.google_drive.driver_instance import driver_instance
6+
from file_automation.utils.logging.loggin_instance import file_automation_logger
47

58

6-
def delete_file(file_id: str):
9+
def delete_file(file_id: str) -> Union[dict, None]:
710
try:
811
file = driver_instance.service.files().delete(fileId=file_id).execute()
12+
file_automation_logger.info(f"Delete drive file: {file_id}")
913
return file
1014
except HttpError as error:
11-
print(f"An error occurred: {error}")
15+
file_automation_logger.error(
16+
f"Delete file failed,"
17+
f"error: {error}"
18+
)
1219
return None
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
from typing import Union
2+
13
from googleapiclient.errors import HttpError
24

35
from file_automation.remote.google_drive.driver_instance import driver_instance
6+
from file_automation.utils.logging.loggin_instance import file_automation_logger
47

58

6-
def add_folder(folder_name: str):
9+
def add_folder(folder_name: str) -> Union[dict, None]:
710
try:
811
file_metadata = {
912
"name": folder_name,
@@ -13,7 +16,13 @@ def add_folder(folder_name: str):
1316
body=file_metadata,
1417
fields="id"
1518
).execute()
19+
file_automation_logger.info(
20+
f"Add drive folder: {folder_name}"
21+
)
1622
return file.get("id")
1723
except HttpError as error:
18-
print(f"An error occurred: {error}")
24+
file_automation_logger.error(
25+
f"Delete file failed,"
26+
f"error: {error}"
27+
)
1928
return None
Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,40 @@
11
import io
2+
from io import BytesIO
3+
from typing import Union
24

35
from googleapiclient.errors import HttpError
46
from googleapiclient.http import MediaIoBaseDownload
57

68
from file_automation.remote.google_drive.driver_instance import driver_instance
9+
from file_automation.utils.logging.loggin_instance import file_automation_logger
710

811

9-
def download_file(file_id: str, file_name: str):
12+
def download_file(file_id: str, file_name: str) -> BytesIO:
1013
try:
1114
request = driver_instance.service.files().get_media(fileId=file_id)
1215
file = io.BytesIO()
1316
downloader = MediaIoBaseDownload(file, request)
1417
done = False
1518
while done is False:
1619
status, done = downloader.next_chunk()
17-
print(f"Download {file_name} {int(status.progress() * 100)}%.")
20+
file_automation_logger.info(
21+
f"Download {file_name} {int(status.progress() * 100)}%."
22+
)
1823
except HttpError as error:
19-
print(f"An error occurred: {error}")
24+
file_automation_logger.error(
25+
f"Delete file failed,"
26+
f"error: {error}"
27+
)
2028
return None
2129
with open(file_name, "wb") as output_file:
2230
output_file.write(file.getbuffer())
31+
file_automation_logger.info(
32+
f"Download file: {file_id} with name: {file_name}"
33+
)
2334
return file
2435

2536

26-
def download_file_from_folder(folder_name: str):
37+
def download_file_from_folder(folder_name: str) -> Union[dict, None]:
2738
try:
2839
files = dict()
2940
response = driver_instance.service.files().list(
@@ -37,7 +48,13 @@ def download_file_from_folder(folder_name: str):
3748
for file in response.get("files", []):
3849
download_file(file.get("id"), file.get("name"))
3950
files.update({file.get("name"): file.get("id")})
51+
file_automation_logger.info(
52+
f"Download all file on {folder_name} done."
53+
)
4054
return files
4155
except HttpError as error:
42-
print(f"An error occurred: {error}")
43-
return None
56+
file_automation_logger.error(
57+
f"Delete file failed,"
58+
f"error: {error}"
59+
)
60+
return None

0 commit comments

Comments
 (0)