lyrpy 0.0.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.
lyr/LUFileUtils.py ADDED
@@ -0,0 +1,486 @@
1
+ """LUFileUtils.py"""
2
+ # -*- coding: UTF-8 -*-
3
+ __annotations__ = """
4
+ =======================================================
5
+ Copyright (c) 2023-2024
6
+ Author:
7
+ Lisitsin Y.R.
8
+ Project:
9
+ LU_PY
10
+ Python (LU)
11
+ Module:
12
+ LUFileUtils.py
13
+
14
+ =======================================================
15
+ """
16
+
17
+ #------------------------------------------
18
+ # БИБЛИОТЕКИ python
19
+ #------------------------------------------
20
+ import os
21
+ import stat
22
+ import sys
23
+ import time
24
+ import chardet
25
+ import logging
26
+
27
+ #------------------------------------------
28
+ # БИБЛИОТЕКИ сторонние
29
+ #------------------------------------------
30
+ import datetime
31
+ import shutil
32
+ import platform
33
+ # if platform.system() == 'Windows':
34
+ # import win32api
35
+ # import win32con
36
+ # #endif
37
+
38
+ #------------------------------------------
39
+ # БИБЛИОТЕКА LU
40
+ #------------------------------------------
41
+ import lyr.LUErrors as LUErrors
42
+ import lyr.LUFile as LUFile
43
+ import lyr.LUStrDecode as LUStrDecode
44
+ import lyr.LULog as LULog
45
+ import lyr.LUDateTime as LUDateTime
46
+
47
+ #------------------------------------------
48
+ #CONST
49
+ #------------------------------------------
50
+ GLevel = 0
51
+ GFileCount = 0
52
+ GFileSize = 0
53
+ GDir = ''
54
+ GMask = '.*'
55
+ GDirCount = 0
56
+ GLevelMAX = sys.maxsize
57
+
58
+ #-------------------------------------------------------------------------------
59
+ # __OUTFILE
60
+ #-------------------------------------------------------------------------------
61
+ def __OUTFILE (s: str, _OutFile: str):
62
+ #beginfunction
63
+ if (_OutFile) and (s != ''):
64
+ if (_OutFile.upper () == 'CONSOLE'):
65
+ print (s)
66
+ else:
67
+ LUFile.WriteStrToFile (_OutFile, s + '\n')
68
+ #endif
69
+ #endif
70
+ #endfunction
71
+
72
+ #-------------------------------------------------------------------------------
73
+ # __ListFile
74
+ #-------------------------------------------------------------------------------
75
+ def __ListFile (APathSource, AMask, APathDest,
76
+ _OutFile, _Option, _FuncDir, _FuncFile) -> int:
77
+ global GFileCount
78
+ global GFileSize
79
+ #beginfunction
80
+ LFileCount = 0
81
+ with os.scandir(APathSource) as LFiles:
82
+ for LFile in LFiles:
83
+ if (not LFile.is_symlink ()):
84
+ if LFile.is_file() and LUFile.CheckFileNameMask (LFile.name, AMask):
85
+ #------------------------------------------------------------
86
+ # class os.DirEntry - Это файл
87
+ #------------------------------------------------------------
88
+ LBaseName = os.path.basename (LFile.path)
89
+ LFileTimeSource = LUFile.GetFileDateTime (LFile.path)[2]
90
+ LFileSizeSource = LUFile.GetFileSize (LFile.path)
91
+
92
+ GFileCount = GFileCount + 1
93
+ LFileCount = LFileCount + 1
94
+ GFileSize = GFileSize + LFileSizeSource
95
+
96
+ match _Option:
97
+ case 1 | 11:
98
+ s = f'{LFileTimeSource:%d.%m.%Y %H:%M} {LFileSizeSource:-17,d} {LBaseName:s}'
99
+ case 2 | 12:
100
+ s = f'{LFileTimeSource:%d.%m.%Y %H:%M} {LFileSizeSource:-17,d} {LBaseName:s}'
101
+ case _:
102
+ s = ''
103
+ #endmatch
104
+ __OUTFILE (s, _OutFile)
105
+
106
+ if _FuncFile:
107
+ s = f'_FuncFile: {_FuncFile.__name__:s}'
108
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
109
+ _FuncFile (LUFile.ExpandFileName (LFile.path), APathDest)
110
+ #endif
111
+ #endif
112
+ #endif
113
+ if LFile.is_dir (): # and (not LFile.name.startswith('.')):
114
+ #------------------------------------------------------------
115
+ # class os.DirEntry - Это каталог
116
+ #------------------------------------------------------------
117
+ LBaseName = os.path.basename (LFile.path)
118
+ LPathTimeSource = LUFile.GetDirDateTime (LFile.path) [2]
119
+
120
+ match _Option:
121
+ case 1 | 11:
122
+ s = f'{LPathTimeSource:%d.%m.%Y %H:%M} {' <DIR>':17s} {LBaseName:s}'
123
+ case 2 | 12:
124
+ s = f'{LPathTimeSource:%d.%m.%Y %H:%M} {' <DIR>':17s} {LBaseName:s}'
125
+ case _:
126
+ s = ''
127
+ #endmatch
128
+ __OUTFILE (s, _OutFile)
129
+
130
+ #------------------------------------------------------------
131
+ #
132
+ #------------------------------------------------------------
133
+ if _FuncDir:
134
+ s = f'_FuncDir: {_FuncDir.__name__:s}'
135
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
136
+ _FuncDir (LUFile.ExpandFileName (LFile.path), APathDest)
137
+ #endif
138
+ #endif
139
+ #endfor
140
+ #endwith
141
+ return LFileCount
142
+ #endfunction
143
+
144
+ #-------------------------------------------------------------------------------
145
+ # __ListDir
146
+ #-------------------------------------------------------------------------------
147
+ def __ListDir (APathSource, AMask, ASubdir, APathDest,
148
+ _OutFile, _Option, _FuncDir, _FuncFile):
149
+ #beginfunction
150
+ global GLevel
151
+ global GFileCount
152
+ global GFileSize
153
+
154
+ #------------------------------------------------------------
155
+ # Dir
156
+ #------------------------------------------------------------
157
+ LBaseName = os.path.basename (APathSource)
158
+ LPathTimeSource = LUFile.GetDirDateTime (APathSource)[2]
159
+
160
+ GFileCount = 0
161
+ GFileSize = 0
162
+
163
+ #------------------------------------------------------------
164
+ # список файлов в каталоге
165
+ #------------------------------------------------------------
166
+ if _Option != 0:
167
+ s = f"\nСодержимое папки {APathSource:s}\n"
168
+ __OUTFILE (s, _OutFile)
169
+ #endif
170
+ match _Option:
171
+ case 1 | 11:
172
+ s = f'{LPathTimeSource:%d.%m.%Y %H:%M} {' <DIR>':17s} {LBaseName:s}'
173
+ case 2 | 12:
174
+ s = f'{LPathTimeSource:%d.%m.%Y %H:%M} {' <DIR>':17s} {LBaseName:s}'
175
+ case _:
176
+ s = ''
177
+ #endmatch
178
+ __OUTFILE (s, _OutFile)
179
+
180
+ LFileCount = __ListFile (APathSource, AMask, APathDest, _OutFile, _Option, _FuncDir, _FuncFile)
181
+
182
+ match _Option:
183
+ case 1 | 11:
184
+ s = f'{GFileCount:16d} файлов {GFileSize:16,d} байт'
185
+ case 2 | 12:
186
+ s = f'{GFileCount:16d} файлов {GFileSize:16,d} байт'
187
+ case _:
188
+ s = ''
189
+ #endmatch
190
+ __OUTFILE (s, _OutFile)
191
+ #------------------------------------------------------------
192
+
193
+ with os.scandir(APathSource) as LFiles:
194
+ for LFile in LFiles:
195
+ if (not LFile.is_symlink()):
196
+ if LFile.is_dir (): # and (not LFile.name.startswith('.')):
197
+ #------------------------------------------------------------
198
+ # class os.DirEntry - Это каталог
199
+ #------------------------------------------------------------
200
+ LBaseName = os.path.basename (LFile.path)
201
+ LPathTimeSource = LUFile.GetFileDateTime (LFile.path)[2]
202
+
203
+ #------------------------------------------------------------
204
+ # на следующий уровень
205
+ #------------------------------------------------------------
206
+ if ASubdir:
207
+ GLevel = GLevel + 1
208
+ if APathDest != '':
209
+ LPathDest = os.path.join (APathDest, LFile.name)
210
+ else:
211
+ LPathDest = ''
212
+ #endif
213
+ __ListDir (LFile.path, AMask, ASubdir, LPathDest, _OutFile, _Option, _FuncDir, _FuncFile)
214
+ #endif
215
+ #endif
216
+ #endif
217
+ #endfor
218
+ GLevel = GLevel - 1
219
+ #endwith
220
+ #endfunction
221
+
222
+ #-------------------------------------------------------------------------------
223
+ # BacFiles
224
+ #-------------------------------------------------------------------------------
225
+ def BacFiles (APathSource, AMask, ASubDir, APathDest,
226
+ _OutFile, _Option, _ASync: bool=False):
227
+
228
+ #-------------------------------------------------------------------------------
229
+ # FuncDir
230
+ #-------------------------------------------------------------------------------
231
+ def FuncDir (_APathSource: str, _APathDest: str):
232
+ #beginfunction
233
+ LPathSource = _APathSource
234
+ LBaseName = os.path.basename (_APathSource)
235
+ LPathDest = os.path.join (_APathDest, LBaseName)
236
+ LPathTimeSource = LUFile.GetFileDateTime (_APathSource) [2]
237
+ Lattr = LUFile.GetFileAttr (_APathSource)
238
+ if not _ASync:
239
+ if not LUFile.DirectoryExists(LPathDest):
240
+ s = f'Create {LPathDest:s} ...'
241
+ LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
242
+ LUFile.ForceDirectories(LPathDest)
243
+ LUFile.SetFileAttr(LPathDest, Lattr, False)
244
+ #endif
245
+ else:
246
+ if not LUFile.DirectoryExists(LPathDest):
247
+ s = f'Delete {LPathSource:s} ...'
248
+ LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
249
+ LUFile.DirectoryDelete(LPathSource)
250
+ #endif
251
+ #endif
252
+ #endfunction
253
+
254
+ #-------------------------------------------------------------------------------
255
+ # FuncFile
256
+ #-------------------------------------------------------------------------------
257
+ def FuncFile (AFileName: str, _APathDest: str):
258
+ #beginfunction
259
+ LFileNameSource = AFileName
260
+ LBaseName = os.path.basename (AFileName)
261
+ LFileTimeSource = LUFile.GetFileDateTime (AFileName) [2]
262
+ LFileSizeSource = LUFile.GetFileSize (AFileName)
263
+ LFileAttrSource = LUFile.GetFileAttr (AFileName)
264
+
265
+ LFileNameDest = os.path.join (_APathDest, LBaseName)
266
+
267
+ #--------------------------------------------------------------------
268
+ LResult = LUFile.COMPAREFILETIMES(LFileNameSource, LFileNameDest)
269
+ #--------------------------------------------------------------------
270
+ # Check Result
271
+ #--------------------------------------------------------------------
272
+ match LResult:
273
+ case -3:
274
+ # -3 File2 could not be opened (see @ERROR for more information).
275
+ LFileSizeDest = 0
276
+ LFileTimeDest = 0
277
+ LDelete = False
278
+ LCopy = True
279
+ if _ASync:
280
+ LCopy = False
281
+ LDelete = True
282
+ #endif
283
+ case -2:
284
+ # -2 File1 could not be opened (see @ERROR for more information).
285
+ LDelete = False
286
+ LCopy = False
287
+ case -1:
288
+ # -1 File1 is older than file2.
289
+ LDelete = False
290
+ LCopy = False
291
+ case 0:
292
+ # 0 File1 and file2 have the same date and time.
293
+ LDelete = False
294
+ LCopy = False
295
+ LFileTimeDest = LUFile.GetFileDateTime (LFileNameDest) [2]
296
+ LFileSizeDest = LUFile.GetFileSize (LFileNameDest)
297
+ LFileAttrDest = LUFile.GetFileAttr (LFileNameDest)
298
+ if (LFileSizeSource != LFileSizeDest):
299
+ LCopy = True
300
+ else:
301
+ LUFile.SetFileAttr(LFileNameDest, LFileAttrSource, False)
302
+ # shutil.copystat (LFileNameSource, LFileNameDest)
303
+ #endif
304
+ case 1:
305
+ # 1 File1 is more recent than file2.
306
+ LDelete = False
307
+ LCopy = True
308
+ #endmatch
309
+
310
+ #--------------------------------------------------------------------
311
+ # Copy
312
+ #--------------------------------------------------------------------
313
+ if LCopy == True:
314
+ s = f'Copy {LFileNameSource:s} -> {LFileNameDest:s} ...'
315
+ LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
316
+ LUFile.FileCopy (LFileNameSource, LFileNameDest, True)
317
+ #endif
318
+
319
+ #--------------------------------------------------------------------
320
+ # Delete
321
+ #--------------------------------------------------------------------
322
+ if LDelete == True:
323
+ s = f'Delete {LFileNameSource:s} ...'
324
+ LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
325
+ LUFile.FileDelete (LFileNameSource)
326
+ #endif
327
+ #endfunction
328
+
329
+ #beginfunction
330
+ if (APathSource != "") and (APathDest != ""):
331
+ LBaseName = os.path.basename (APathSource)
332
+ LPathDest = APathDest
333
+ s = f'BacFiles: {APathSource:s} {AMask:s} => {APathDest:s} ...'
334
+ LULog.LoggerTOOLS_AddLevel (LULog.TEXT, s)
335
+ __ListDir (APathSource, AMask, ASubDir, LPathDest, _OutFile, _Option, FuncDir, FuncFile)
336
+ #endif
337
+ #endfunction
338
+
339
+ #-------------------------------------------------------------------------------
340
+ # SyncFiles
341
+ #-------------------------------------------------------------------------------
342
+ def SyncFiles (APathSource, AMask, APathDest, _OutFile, _Option):
343
+ #beginfunction
344
+ s = f'SyncFiles: {APathSource:s} {AMask:s} => {APathDest:s} ...'
345
+ LULog.LoggerTOOLS_AddLevel (LULog.TEXT, s)
346
+
347
+ BacFiles (APathSource, AMask, True, APathDest, _OutFile, _Option, False)
348
+ BacFiles (APathDest, AMask, True, APathSource, _OutFile, _Option, True)
349
+ #endfunction
350
+
351
+ #-------------------------------------------------------------------------------
352
+ # DirFiles
353
+ #-------------------------------------------------------------------------------
354
+ def DirFiles (APathSource, AMask, ASubDir,
355
+ _OutFile, _Option, _FuncDir, _FuncFile):
356
+ #beginfunction
357
+ if (APathSource != ""):
358
+ s = f'DirFiles: {APathSource:s} {AMask:s} ...'
359
+ LULog.LoggerTOOLS_AddLevel (LULog.TEXT, s)
360
+ __ListDir(APathSource, AMask, ASubDir, '', _OutFile, _Option, _FuncDir, _FuncFile)
361
+ #endif
362
+ #endfunction
363
+
364
+ #-------------------------------------------------------------------------------
365
+ # __FakeFile
366
+ #-------------------------------------------------------------------------------
367
+ def __FakeFile (APathSource,
368
+ _OutFile, _Option, _FuncDir, _FuncFile):
369
+ global GLevel
370
+ #beginfunction
371
+ for LFileCount in range(0, 2):
372
+ s = f'FakeFile_{str(GLevel+1):s}_{str(LFileCount+1):s}.txt'
373
+ LFileName = os.path.join (APathSource, s)
374
+
375
+ LHahdle = LUFile.OpenTextFile(LFileName, '')
376
+ LUFile.WriteTextFile(LHahdle, 'test')
377
+ LUFile.WriteTextFile(LHahdle, 'тест')
378
+ LUFile.CloseTextFile(LHahdle)
379
+
380
+ if _FuncFile:
381
+ s = f'_FuncFile: {_FuncFile.__name__:s}'
382
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
383
+ _FuncFile (LFileName)
384
+ #endif
385
+ #endfor
386
+ #endfunction
387
+
388
+ #-------------------------------------------------------------------------------
389
+ # __FakeDir
390
+ #-------------------------------------------------------------------------------
391
+ def __FakeDir (APathSource,
392
+ _OutFile, _Option, _FuncDir, _FuncFile):
393
+ #beginfunction
394
+ global GLevel
395
+
396
+ #------------------------------------------------------------
397
+ # Dir
398
+ #------------------------------------------------------------
399
+ LBaseName = os.path.basename (APathSource)
400
+ LULog.LoggerTOOLS_AddLevel (LULog.TEXT, LUFile.ExpandFileName (APathSource))
401
+
402
+ #------------------------------------------------------------
403
+ #
404
+ #------------------------------------------------------------
405
+ if _FuncDir:
406
+ s = f'_FuncDir: {_FuncDir.__name__:s}'
407
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
408
+ _FuncDir (LUFile.ExpandFileName (APathSource))
409
+ #endif
410
+
411
+ __FakeFile (APathSource, _OutFile, _Option, _FuncDir, _FuncFile)
412
+
413
+ #------------------------------------------------------------
414
+ # на следующий уровень
415
+ #------------------------------------------------------------
416
+ if GLevel < 3:
417
+ for LDirCount in range (0, 2):
418
+ s = f'FakeDir_{str(GLevel+1):s}_{str(LDirCount+1):s}'
419
+ LPathSource = os.path.join (APathSource, s)
420
+ LUFile.ForceDirectories(LPathSource)
421
+
422
+ GLevel = GLevel + 1
423
+ __FakeDir(LPathSource, _OutFile, _Option, _FuncDir, _FuncFile)
424
+ #endfor
425
+ #endif
426
+ GLevel = GLevel - 1
427
+ #endfunction
428
+
429
+ #-------------------------------------------------------------------------------
430
+ # FakeFiles
431
+ #-------------------------------------------------------------------------------
432
+ def FakeFiles (APathSource,
433
+ _OutFile, _Option, _FuncDir, _FuncFile):
434
+ #beginfunction
435
+ if (APathSource != ""):
436
+ s = f'FakeFiles: {APathSource:s} ...'
437
+ LULog.LoggerTOOLS_AddLevel (LULog.TEXT, s)
438
+ __FakeDir(APathSource, _OutFile, _Option, _FuncDir, _FuncFile)
439
+ #endif
440
+ #endfunction
441
+
442
+ #-------------------------------------------------------------------------------
443
+ # DelFiles
444
+ #-------------------------------------------------------------------------------
445
+ def DelFiles (APathSource, AMask, ASubDir, _OutFile, _Option, _Older: int):
446
+ #beginfunction
447
+
448
+ #-------------------------------------------------------------------------------
449
+ # DelFile
450
+ #-------------------------------------------------------------------------------
451
+ def DelFile (AFileName: str):
452
+ #beginfunction
453
+ LDay = LUDateTime.Now ()
454
+ # print(LUFile.GetFileDateTime (AFileName))
455
+ LFileTimeSource = LUFile.GetFileDateTime (AFileName) [2]
456
+ # print ((LDay - LFileTimeSource).days)
457
+ if (LDay - LFileTimeSource).days > _Older:
458
+ s = f'Delete {AFileName:s} ...'
459
+ LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
460
+ LUFile.FileDelete(AFileName)
461
+ #endif
462
+ #endfunction
463
+
464
+ #beginfunction
465
+ if (APathSource != ""):
466
+ stat = f'DelFiles: {APathSource:s} {AMask:s} ...'
467
+ LULog.LoggerTOOLS_AddLevel (lyr.LULog.TEXT, s)
468
+ __ListDir (APathSource, AMask, ASubDir, '', _OutFile, _Option, None, DelFile)
469
+ #endif
470
+ #endfunction
471
+
472
+ #------------------------------------------
473
+ def main ():
474
+ #beginfunction
475
+ print('main LUFileUtils.py ...')
476
+ #endfunction
477
+
478
+ #------------------------------------------
479
+ #
480
+ #------------------------------------------
481
+ #beginmodule
482
+ if __name__ == "__main__":
483
+ main()
484
+ #endif
485
+
486
+ #endmodule