Skip to content

Commit 8e4b271

Browse files
committed
Add comment
Add comment
1 parent fe721b3 commit 8e4b271

File tree

17 files changed

+219
-26
lines changed

17 files changed

+219
-26
lines changed

.readthedocs.yaml

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
1-
version: 2
1+
# .readthedocs.yaml
2+
# Read the Docs configuration file
3+
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details
24

3-
sphinx:
4-
configuration: docs/source/conf.py
5+
# Required
6+
version: 2
57

8+
# Set the OS, Python version and other tools you might need
69
build:
710
os: ubuntu-22.04
811
tools:
912
python: "3.11"
13+
14+
# Build documentation in the "docs/" directory with Sphinx
15+
sphinx:
16+
configuration: docs/source/conf.py
17+
18+
# Optionally build your docs in additional formats such as PDF and ePub
19+
#formats:
20+
# - pdf
21+
# - epub
22+
23+
# Optional but recommended, declare the Python requirements required
24+
# to build your documentation
25+
python:
26+
install:
27+
- requirements: docs/requirements.txt
Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,60 @@
11
import shutil
2-
import sys
32
from pathlib import Path
43

54
from automation_file.utils.exception.exceptions import DirNotExistsException
65
from automation_file.utils.logging.loggin_instance import file_automation_logger
76

87

9-
def copy_dir(dir_path: str, target_dir_path: str) -> None:
8+
def copy_dir(dir_path: str, target_dir_path: str) -> bool:
9+
"""
10+
Copy dir to target path (path need as dir path)
11+
:param dir_path: which dir we want to copy (str path)
12+
:param target_dir_path: copy dir to this path
13+
:return: True if success else False
14+
"""
1015
dir_path = Path(dir_path)
1116
target_dir_path = Path(target_dir_path)
1217
if dir_path.is_dir():
1318
try:
1419
shutil.copytree(dir_path, target_dir_path, dirs_exist_ok=True)
1520
file_automation_logger.info(f"Copy dir {dir_path}")
21+
return True
1622
except shutil.Error as error:
1723
file_automation_logger.error(f"Copy dir {dir_path} failed: {repr(error)}")
1824
else:
1925
file_automation_logger.error(f"Copy dir {dir_path} failed: {repr(DirNotExistsException)}")
26+
return False
2027

2128

22-
def remove_dir_tree(dir_path: str) -> None:
29+
def remove_dir_tree(dir_path: str) -> bool:
30+
"""
31+
:param dir_path: which dir we want to remove (str path)
32+
:return: True if success else False
33+
"""
2334
dir_path = Path(dir_path)
2435
if dir_path.is_dir():
2536
try:
2637
shutil.rmtree(dir_path)
2738
file_automation_logger.info(f"Remove dir tree {dir_path}")
39+
return True
2840
except shutil.Error as error:
2941
file_automation_logger.error(f"Remove dir tree {dir_path} error: {repr(error)}")
42+
return False
3043

3144

32-
def rename_dir(origin_dir_path, target_dir: str) -> None:
45+
def rename_dir(origin_dir_path, target_dir: str) -> bool:
46+
"""
47+
:param origin_dir_path: which dir we want to rename (str path)
48+
:param target_dir: target name as str full path
49+
:return: True if success else False
50+
"""
3351
origin_dir_path = Path(origin_dir_path)
3452
if origin_dir_path.exists() and origin_dir_path.is_dir():
3553
try:
3654
Path.rename(origin_dir_path, target_dir)
3755
file_automation_logger.info(
3856
f"Rename dir origin dir path: {origin_dir_path}, target dir path: {target_dir}")
57+
return True
3958
except Exception as error:
4059
file_automation_logger.error(
4160
f"Rename dir error: {repr(error)}, "
@@ -46,9 +65,14 @@ def rename_dir(origin_dir_path, target_dir: str) -> None:
4665
f"Rename dir error: {repr(DirNotExistsException)}, "
4766
f"Rename dir origin dir path: {origin_dir_path}, "
4867
f"target dir path: {target_dir}")
68+
return False
4969

5070

5171
def create_dir(dir_path: str) -> None:
72+
"""
73+
:param dir_path: create dir on dir_path
74+
:return: None
75+
"""
5276
dir_path = Path(dir_path)
5377
dir_path.mkdir(exist_ok=True)
5478
file_automation_logger.info(f"Create dir {dir_path}")

automation_file/local/file/file_process.py

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,32 @@
66
from automation_file.utils.logging.loggin_instance import file_automation_logger
77

88

9-
def copy_file(file_path: str, target_path: str) -> None:
9+
def copy_file(file_path: str, target_path: str) -> bool:
10+
"""
11+
:param file_path: which file we want to copy (str path)
12+
:param target_path: put copy file on target path
13+
:return: True if success else False
14+
"""
1015
file_path = Path(file_path)
1116
if file_path.is_file() and file_path.exists():
1217
try:
1318
shutil.copy2(file_path, target_path)
1419
file_automation_logger.info(f"Copy file origin path: {file_path}, target path : {target_path}")
20+
return True
1521
except shutil.Error as error:
1622
file_automation_logger.error(f"Copy file failed: {repr(error)}")
1723
else:
1824
file_automation_logger.error(f"Copy file failed: {repr(FileNotExistsException)}")
25+
return False
1926

2027

21-
def copy_specify_extension_file(file_dir_path: str, target_extension: str, target_path: str) -> None:
28+
def copy_specify_extension_file(file_dir_path: str, target_extension: str, target_path: str) -> bool:
29+
"""
30+
:param file_dir_path: which dir we want to search
31+
:param target_extension: Which extension do we search
32+
:param target_path: copy file to target path
33+
:return: True if success else False
34+
"""
2235
file_dir_path = Path(file_dir_path)
2336
if file_dir_path.exists() and file_dir_path.is_dir():
2437
for file in file_dir_path.glob(f"**/*.{target_extension}"):
@@ -27,12 +40,19 @@ def copy_specify_extension_file(file_dir_path: str, target_extension: str, targe
2740
f"Copy specify extension file on dir"
2841
f"origin dir path: {file_dir_path}, target extension: {target_extension}, "
2942
f"to target path {target_path}")
43+
return True
3044
else:
3145
file_automation_logger.error(
3246
f"Copy specify extension file failed: {repr(FileNotExistsException)}")
47+
return False
3348

3449

35-
def copy_all_file_to_dir(dir_path: str, target_dir_path: str) -> None:
50+
def copy_all_file_to_dir(dir_path: str, target_dir_path: str) -> bool:
51+
"""
52+
:param dir_path: copy all file on dir
53+
:param target_dir_path: put file to target dir
54+
:return: True if success else False
55+
"""
3656
dir_path = Path(dir_path)
3757
target_dir_path = Path(target_dir_path)
3858
if dir_path.is_dir() and target_dir_path.is_dir():
@@ -43,6 +63,7 @@ def copy_all_file_to_dir(dir_path: str, target_dir_path: str) -> None:
4363
f"origin dir: {dir_path}, "
4464
f"target dir: {target_dir_path}"
4565
)
66+
return True
4667
except shutil.Error as error:
4768
file_automation_logger.error(
4869
f"Copy all file to dir failed, "
@@ -52,9 +73,16 @@ def copy_all_file_to_dir(dir_path: str, target_dir_path: str) -> None:
5273
)
5374
else:
5475
print(repr(DirNotExistsException), file=sys.stderr)
76+
return False
5577

5678

57-
def rename_file(origin_file_path, target_name: str, file_extension=None) -> None:
79+
def rename_file(origin_file_path, target_name: str, file_extension=None) -> bool:
80+
"""
81+
:param origin_file_path: which dir we want to search file
82+
:param target_name: rename file to target name
83+
:param file_extension: Which extension do we search
84+
:return: True if success else False
85+
"""
5886
origin_file_path = Path(origin_file_path)
5987
if origin_file_path.exists() and origin_file_path.is_dir():
6088
if file_extension is None:
@@ -68,6 +96,7 @@ def rename_file(origin_file_path, target_name: str, file_extension=None) -> None
6896
file_index = file_index + 1
6997
file_automation_logger.info(
7098
f"Renamed file: origin file path:{origin_file_path}, with new name: {target_name}")
99+
return True
71100
except Exception as error:
72101
file_automation_logger.error(
73102
f"Rename file failed, "
@@ -79,15 +108,25 @@ def rename_file(origin_file_path, target_name: str, file_extension=None) -> None
79108
else:
80109
file_automation_logger.error(
81110
f"Rename file failed, error: {repr(DirNotExistsException)}")
111+
return False
82112

83113

84114
def remove_file(file_path: str) -> None:
115+
"""
116+
:param file_path: which file we want to remove
117+
:return: None
118+
"""
85119
file_path = Path(file_path)
86120
if file_path.exists() and file_path.is_file():
87121
file_path.unlink(missing_ok=True)
88122
file_automation_logger.info(f"Remove file, file path: {file_path}")
89123

90124

91125
def create_file(file_path: str, content: str) -> None:
126+
"""
127+
:param file_path: create file on path
128+
:param content: what content will write to file
129+
:return: None
130+
"""
92131
with open(file_path, "w+") as file:
93132
file.write(content)

automation_file/local/zip/zip_process.py

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,28 @@
22
from pathlib import Path
33
from shutil import make_archive
44
from typing import List
5+
from zipfile import ZipInfo
56

67
from automation_file.utils.exception.exceptions import ZIPGetWrongFileException
78
from automation_file.utils.logging.loggin_instance import file_automation_logger
89

910

1011
def zip_dir(dir_we_want_to_zip: str, zip_name: str) -> None:
12+
"""
13+
:param dir_we_want_to_zip: dir str path
14+
:param zip_name: zip file name
15+
:return: None
16+
"""
1117
make_archive(root_dir=dir_we_want_to_zip, base_name=zip_name, format="zip")
1218
file_automation_logger.info(f"Dir to zip: {dir_we_want_to_zip}, zip file name: {zip_name}")
1319

1420

1521
def zip_file(zip_file_path: str, file: [str, List[str]]) -> None:
22+
"""
23+
:param zip_file_path: add file to zip file
24+
:param file: single file path or list of file path (str) to add into zip
25+
:return: None
26+
"""
1627
current_zip = zipfile.ZipFile(zip_file_path, mode="w")
1728
if isinstance(file, str):
1829
file_name = Path(file)
@@ -34,7 +45,13 @@ def zip_file(zip_file_path: str, file: [str, List[str]]) -> None:
3445
current_zip.close()
3546

3647

37-
def read_zip_file(zip_file_path: str, file_name: str, password: [str, None] = None) -> None:
48+
def read_zip_file(zip_file_path: str, file_name: str, password: [str, None] = None) -> bytes:
49+
"""
50+
:param zip_file_path: which zip we want to read
51+
:param file_name: which file on zip we want to read
52+
:param password: if zip have password use this password to unzip zip file
53+
:return:
54+
"""
3855
current_zip = zipfile.ZipFile(zip_file_path, mode="r")
3956
with current_zip.open(name=file_name, mode="r", pwd=password, force_zip64=True) as read_file:
4057
data = read_file.read()
@@ -47,6 +64,13 @@ def read_zip_file(zip_file_path: str, file_name: str, password: [str, None] = No
4764

4865
def unzip_file(
4966
zip_file_path: str, extract_member, extract_path: [str, None] = None, password: [str, None] = None) -> None:
67+
"""
68+
:param zip_file_path: which zip we want to unzip
69+
:param extract_member: which member we want to unzip
70+
:param extract_path: extract member to path
71+
:param password: if zip have password use this password to unzip zip file
72+
:return: None
73+
"""
5074
current_zip = zipfile.ZipFile(zip_file_path, mode="r")
5175
current_zip.extract(member=extract_member, path=extract_path, pwd=password)
5276
file_automation_logger.info(
@@ -61,6 +85,13 @@ def unzip_file(
6185
def unzip_all(
6286
zip_file_path: str, extract_member: [str, None] = None,
6387
extract_path: [str, None] = None, password: [str, None] = None) -> None:
88+
"""
89+
:param zip_file_path: which zip we want to unzip
90+
:param extract_member: which member we want to unzip
91+
:param extract_path: extract to path
92+
:param password: if zip have password use this password to unzip zip file
93+
:return: None
94+
"""
6495
current_zip = zipfile.ZipFile(zip_file_path, mode="r")
6596
current_zip.extractall(members=extract_member, path=extract_path, pwd=password)
6697
file_automation_logger.info(
@@ -72,7 +103,11 @@ def unzip_all(
72103
current_zip.close()
73104

74105

75-
def zip_info(zip_file_path: str) -> None:
106+
def zip_info(zip_file_path: str) -> List[ZipInfo]:
107+
"""
108+
:param zip_file_path: read zip file info
109+
:return: List[ZipInfo]
110+
"""
76111
current_zip = zipfile.ZipFile(zip_file_path, mode="r")
77112
info_list = current_zip.infolist()
78113
current_zip.close()
@@ -82,7 +117,11 @@ def zip_info(zip_file_path: str) -> None:
82117
return info_list
83118

84119

85-
def zip_file_info(zip_file_path: str) -> None:
120+
def zip_file_info(zip_file_path: str) -> List[str]:
121+
"""
122+
:param zip_file_path: read inside zip file info
123+
:return: List[str]
124+
"""
86125
current_zip = zipfile.ZipFile(zip_file_path, mode="r")
87126
name_list = current_zip.namelist()
88127
current_zip.close()
@@ -93,6 +132,11 @@ def zip_file_info(zip_file_path: str) -> None:
93132

94133

95134
def set_zip_password(zip_file_path: str, password: bytes) -> None:
135+
"""
136+
:param zip_file_path: which zip we want to set password
137+
:param password: password will be set
138+
:return: None
139+
"""
96140
current_zip = zipfile.ZipFile(zip_file_path)
97141
current_zip.setpassword(pwd=password)
98142
current_zip.close()

automation_file/remote/google_drive/delete/delete_manager.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88

99
def drive_delete_file(file_id: str) -> Union[Dict[str, str], None]:
10+
"""
11+
:param file_id: Google Drive file id
12+
:return: Dict[str, str] or None
13+
"""
1014
try:
1115
file = driver_instance.service.files().delete(fileId=file_id).execute()
1216
file_automation_logger.info(f"Delete drive file: {file_id}")

automation_file/remote/google_drive/dir/folder_manager.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88

99
def drive_add_folder(folder_name: str) -> Union[dict, None]:
10+
"""
11+
:param folder_name: folder name will create on Google Drive
12+
:return: dict or None
13+
"""
1014
try:
1115
file_metadata = {
1216
"name": folder_name,

automation_file/remote/google_drive/download/download_file.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010

1111

1212
def drive_download_file(file_id: str, file_name: str) -> BytesIO:
13+
"""
14+
:param file_id: file have this id will download
15+
:param file_name: file save on local name
16+
:return: file
17+
"""
1318
try:
1419
request = driver_instance.service.files().get_media(fileId=file_id)
1520
file = io.BytesIO()
@@ -35,6 +40,10 @@ def drive_download_file(file_id: str, file_name: str) -> BytesIO:
3540

3641

3742
def drive_download_file_from_folder(folder_name: str) -> Union[dict, None]:
43+
"""
44+
:param folder_name: which folder we want to download file
45+
:return: dict or None
46+
"""
3847
try:
3948
files = dict()
4049
response = driver_instance.service.files().list(

automation_file/remote/google_drive/driver_instance.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ def __init__(self):
1818
self.scopes = ["https://www.googleapis.com/auth/drive"]
1919

2020
def later_init(self, token_path: str, credentials_path: str):
21+
"""
22+
:param token_path: Google Drive token path
23+
:param credentials_path: Google Drive credentials path
24+
:return: None
25+
"""
2126
token_path = Path(token_path)
2227
credentials_path = Path(credentials_path)
2328
creds = None

0 commit comments

Comments
 (0)