ezKit 1.12.0__py3-none-any.whl → 1.12.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
ezKit/xftp.py CHANGED
@@ -1,4 +1,5 @@
1
- """FTP"""
1
+ """FTP Library"""
2
+
2
3
  # ftplib: https://docs.python.org/3.12/library/ftplib.html
3
4
  import os
4
5
  from ftplib import FTP
@@ -17,7 +18,7 @@ class XFTP:
17
18
  username: str = "anonymous",
18
19
  password: str = "",
19
20
  encoding: str = "UTF-8",
20
- debuglevel: int = 0
21
+ debuglevel: int = 0,
21
22
  ):
22
23
  """Initiation"""
23
24
  self.ftp = FTP()
@@ -61,7 +62,7 @@ class XFTP:
61
62
  logger.success("FTP connect closed")
62
63
  return True
63
64
 
64
- def get_file_list(self, target='/') -> list[str] | None:
65
+ def get_file_list(self, target="/") -> list[str] | None:
65
66
  """Get file list"""
66
67
  try:
67
68
  self.chdir_to_remote(target)
@@ -84,7 +85,7 @@ class XFTP:
84
85
  try:
85
86
  dir_list = target.split("/")
86
87
  for i, _ in enumerate(dir_list):
87
- dir_path = "/".join(dir_list[:i + 1])
88
+ dir_path = "/".join(dir_list[: i + 1])
88
89
  try:
89
90
  self.ftp.mkd(dir_path)
90
91
  except Exception as e:
@@ -111,21 +112,23 @@ class XFTP:
111
112
  # 注意: exit() 并不会退出脚本, 配合 try 使用
112
113
  exit()
113
114
 
114
- def x_exec(self, local_dir=".", local_file="", remote_dir="/", remote_file="", upload=False):
115
+ def x_exec(
116
+ self, local_dir=".", local_file="", remote_dir="/", remote_file="", upload=False
117
+ ):
115
118
  """Download or Upload"""
116
119
 
117
120
  bufsize = 1024
118
121
  local_path = f"{local_dir}/{local_file}"
119
122
  remote_path = f"{remote_dir}/{remote_file}"
120
123
 
121
- info = 'Download'
124
+ info = "Download"
122
125
  if upload is True:
123
- info = 'Upload'
126
+ info = "Upload"
124
127
 
125
128
  # 检查参数
126
129
  if upload is True:
127
130
  if local_file == "":
128
- self.close('Argument Miss: local file')
131
+ self.close("Argument Miss: local file")
129
132
  # 如果没有设置 远程文件 名称, 则使用 本地文件 名称
130
133
  if remote_file == "":
131
134
  remote_file = local_file
@@ -173,11 +176,15 @@ class XFTP:
173
176
  stat = Path(local_file)
174
177
  if stat.exists() and stat.is_file():
175
178
  with open(local_file, "rb") as fid:
176
- self.ftp.storbinary(f'STOR {remote_file}', fid, bufsize)
177
- logger.success(f"{info} success: {local_path.replace('//', '/')} -> {remote_path.replace('//', '/')}")
179
+ self.ftp.storbinary(f"STOR {remote_file}", fid, bufsize)
180
+ logger.success(
181
+ f"{info} success: {local_path.replace('//', '/')} -> {remote_path.replace('//', '/')}"
182
+ )
178
183
  return True
179
184
 
180
- self.x_exit(f"{info} error: {local_path.replace('//', '/')} is not exist")
185
+ self.x_exit(
186
+ f"{info} error: {local_path.replace('//', '/')} is not exist"
187
+ )
181
188
 
182
189
  else:
183
190
 
@@ -189,11 +196,15 @@ class XFTP:
189
196
  # 下载文件
190
197
  if remote_file in self.ftp.nlst():
191
198
  with open(local_file, "wb") as fid:
192
- self.ftp.retrbinary(f'RETR {remote_file}', fid.write, bufsize)
193
- logger.success(f"{info} success: {remote_path.replace('//', '/')} -> {local_path.replace('//', '/')}")
199
+ self.ftp.retrbinary(f"RETR {remote_file}", fid.write, bufsize)
200
+ logger.success(
201
+ f"{info} success: {remote_path.replace('//', '/')} -> {local_path.replace('//', '/')}"
202
+ )
194
203
  return True
195
204
 
196
- self.x_exit(f"{info} error: {remote_path.replace('//', '/')} is not exist")
205
+ self.x_exit(
206
+ f"{info} error: {remote_path.replace('//', '/')} is not exist"
207
+ )
197
208
 
198
209
  except Exception as e:
199
210
  # 第一层 try 使用 self.x_exit() 无效, 直接使用 self.close()
@@ -209,14 +220,25 @@ class XFTP:
209
220
  if stat.exists() is False:
210
221
  self.close(f"Local directory error: {local_dir}")
211
222
  # 获取文件列表
212
- local_files = [f for f in os.listdir(local_dir) if os.path.isfile(os.path.join(local_dir, f))]
223
+ local_files = [
224
+ f
225
+ for f in os.listdir(local_dir)
226
+ if os.path.isfile(os.path.join(local_dir, f))
227
+ ]
213
228
  for i in local_files:
214
- self.x_exec(local_dir=local_dir, remote_dir=remote_dir, local_file=i, upload=True)
229
+ self.x_exec(
230
+ local_dir=local_dir,
231
+ remote_dir=remote_dir,
232
+ local_file=i,
233
+ upload=True,
234
+ )
215
235
  else:
216
236
  remote_files = self.get_file_list(remote_dir)
217
237
  if remote_files is not None:
218
238
  for i in remote_files:
219
- self.x_exec(local_dir=local_dir, remote_dir=remote_dir, remote_file=i)
239
+ self.x_exec(
240
+ local_dir=local_dir, remote_dir=remote_dir, remote_file=i
241
+ )
220
242
 
221
243
  def retrlines(self, remote_dir="/", cmd="LIST"):
222
244
  """Retrlines"""