lyrpy 2025.0.1__py3-none-any.whl → 2025.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.

Potentially problematic release.


This version of lyrpy might be problematic. Click here for more details.

Files changed (109) hide show
  1. SRC/LIB/__init__.py +21 -0
  2. SRC/lyrpy/LUConsole.py +402 -0
  3. SRC/lyrpy/LUConst.py +358 -0
  4. SRC/lyrpy/LUDateTime.py +205 -0
  5. SRC/lyrpy/LUDecotators.py +417 -0
  6. SRC/lyrpy/LUDict.py +116 -0
  7. SRC/lyrpy/LUDoc.py +62 -0
  8. SRC/lyrpy/LUErrors.py +79 -0
  9. SRC/lyrpy/LUFile.py +1228 -0
  10. SRC/lyrpy/LUFileUtils.py +501 -0
  11. SRC/lyrpy/LULog.py +2324 -0
  12. SRC/lyrpy/LUNetwork.py +277 -0
  13. SRC/lyrpy/LUNumUtils.py +305 -0
  14. SRC/lyrpy/LUObjects.py +208 -0
  15. SRC/lyrpy/LUObjectsYT.py +846 -0
  16. SRC/lyrpy/LUParserARG.py +364 -0
  17. SRC/lyrpy/LUParserINI.py +376 -0
  18. SRC/lyrpy/LUParserREG.py +514 -0
  19. SRC/lyrpy/LUProc.py +110 -0
  20. SRC/lyrpy/LUQThread.py +141 -0
  21. SRC/lyrpy/LUQTimer.py +197 -0
  22. SRC/lyrpy/LUSheduler.py +941 -0
  23. SRC/lyrpy/LUStrDecode.py +223 -0
  24. SRC/lyrpy/LUStrUtils.py +633 -0
  25. SRC/lyrpy/LUSupport.py +124 -0
  26. SRC/lyrpy/LUTelegram.py +428 -0
  27. SRC/lyrpy/LUThread.py +177 -0
  28. SRC/lyrpy/LUTimer.py +141 -0
  29. SRC/lyrpy/LUVersion.py +383 -0
  30. SRC/lyrpy/LUYouTube.py +203 -0
  31. SRC/lyrpy/LUos.py +807 -0
  32. SRC/lyrpy/LUsys.py +47 -0
  33. SRC/lyrpy/__init__.py +21 -0
  34. SRC/lyrpy/__main__.py +20 -0
  35. __SRC/LIB/__init__.py +0 -0
  36. __SRC/LIB/lyrpy/LUConsole.py +402 -0
  37. __SRC/LIB/lyrpy/LUConst.py +358 -0
  38. __SRC/LIB/lyrpy/LUDateTime.py +205 -0
  39. __SRC/LIB/lyrpy/LUDecotators.py +417 -0
  40. __SRC/LIB/lyrpy/LUDict.py +116 -0
  41. __SRC/LIB/lyrpy/LUDoc.py +62 -0
  42. __SRC/LIB/lyrpy/LUErrors.py +79 -0
  43. __SRC/LIB/lyrpy/LUFile.py +1228 -0
  44. __SRC/LIB/lyrpy/LUFileUtils.py +501 -0
  45. __SRC/LIB/lyrpy/LULog.py +2324 -0
  46. __SRC/LIB/lyrpy/LUNetwork.py +277 -0
  47. __SRC/LIB/lyrpy/LUNumUtils.py +305 -0
  48. __SRC/LIB/lyrpy/LUObjects.py +208 -0
  49. __SRC/LIB/lyrpy/LUObjectsYT.py +846 -0
  50. __SRC/LIB/lyrpy/LUParserARG.py +364 -0
  51. __SRC/LIB/lyrpy/LUParserINI.py +376 -0
  52. __SRC/LIB/lyrpy/LUParserREG.py +514 -0
  53. __SRC/LIB/lyrpy/LUProc.py +110 -0
  54. __SRC/LIB/lyrpy/LUQThread.py +141 -0
  55. __SRC/LIB/lyrpy/LUQTimer.py +197 -0
  56. __SRC/LIB/lyrpy/LUSheduler.py +941 -0
  57. __SRC/LIB/lyrpy/LUStrDecode.py +223 -0
  58. __SRC/LIB/lyrpy/LUStrUtils.py +633 -0
  59. __SRC/LIB/lyrpy/LUSupport.py +124 -0
  60. __SRC/LIB/lyrpy/LUTelegram.py +428 -0
  61. __SRC/LIB/lyrpy/LUThread.py +177 -0
  62. __SRC/LIB/lyrpy/LUTimer.py +141 -0
  63. __SRC/LIB/lyrpy/LUVersion.py +383 -0
  64. __SRC/LIB/lyrpy/LUYouTube.py +203 -0
  65. __SRC/LIB/lyrpy/LUos.py +807 -0
  66. __SRC/LIB/lyrpy/LUsys.py +47 -0
  67. __SRC/LIB/lyrpy/__init__.py +21 -0
  68. __SRC/LIB/lyrpy/__main__.py +20 -0
  69. __SRC/__init__.py +0 -0
  70. ____src/__init__.py +0 -0
  71. ____src/lyrpy/LUConsole.py +402 -0
  72. ____src/lyrpy/LUConst.py +358 -0
  73. ____src/lyrpy/LUDateTime.py +205 -0
  74. ____src/lyrpy/LUDecotators.py +417 -0
  75. ____src/lyrpy/LUDict.py +116 -0
  76. ____src/lyrpy/LUDoc.py +62 -0
  77. ____src/lyrpy/LUErrors.py +79 -0
  78. ____src/lyrpy/LUFile.py +1228 -0
  79. ____src/lyrpy/LUFileUtils.py +501 -0
  80. ____src/lyrpy/LULog.py +2324 -0
  81. ____src/lyrpy/LUNetwork.py +277 -0
  82. ____src/lyrpy/LUNumUtils.py +305 -0
  83. ____src/lyrpy/LUObjects.py +208 -0
  84. ____src/lyrpy/LUObjectsYT.py +846 -0
  85. ____src/lyrpy/LUParserARG.py +364 -0
  86. ____src/lyrpy/LUParserINI.py +376 -0
  87. ____src/lyrpy/LUParserREG.py +514 -0
  88. ____src/lyrpy/LUProc.py +110 -0
  89. ____src/lyrpy/LUQThread.py +141 -0
  90. ____src/lyrpy/LUQTimer.py +197 -0
  91. ____src/lyrpy/LUSheduler.py +941 -0
  92. ____src/lyrpy/LUStrDecode.py +223 -0
  93. ____src/lyrpy/LUStrUtils.py +633 -0
  94. ____src/lyrpy/LUSupport.py +124 -0
  95. ____src/lyrpy/LUTelegram.py +428 -0
  96. ____src/lyrpy/LUThread.py +177 -0
  97. ____src/lyrpy/LUTimer.py +141 -0
  98. ____src/lyrpy/LUVersion.py +383 -0
  99. ____src/lyrpy/LUYouTube.py +203 -0
  100. ____src/lyrpy/LUos.py +807 -0
  101. ____src/lyrpy/LUsys.py +47 -0
  102. ____src/lyrpy/__init__.py +21 -0
  103. ____src/lyrpy/__main__.py +20 -0
  104. {lyrpy-2025.0.1.dist-info → lyrpy-2025.0.2.dist-info}/METADATA +1 -1
  105. lyrpy-2025.0.2.dist-info/RECORD +145 -0
  106. lyrpy-2025.0.1.dist-info/RECORD +0 -43
  107. {lyrpy-2025.0.1.dist-info → lyrpy-2025.0.2.dist-info}/WHEEL +0 -0
  108. {lyrpy-2025.0.1.dist-info → lyrpy-2025.0.2.dist-info}/licenses/LICENSE +0 -0
  109. {lyrpy-2025.0.1.dist-info → lyrpy-2025.0.2.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,1228 @@
1
+ """LUFile.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
+ LUFile.py
13
+
14
+ =======================================================
15
+ """
16
+
17
+ import errno
18
+ #------------------------------------------
19
+ # БИБЛИОТЕКИ python
20
+ #------------------------------------------
21
+ import os
22
+ import platform
23
+ import stat
24
+ import datetime
25
+ import logging
26
+ import tempfile
27
+ import re
28
+ import ctypes
29
+ import copy
30
+ import pathlib
31
+
32
+ # if platform.system() == 'Windows':
33
+ # import win32api
34
+ # print('Windows')
35
+ # import win32con
36
+ # #endif
37
+ # if platform.system() == 'Linux':
38
+ # ...
39
+ # #endif
40
+
41
+ #------------------------------------------
42
+ # БИБЛИОТЕКИ сторонние
43
+ #------------------------------------------
44
+ import shutil
45
+ import chardet
46
+
47
+ #------------------------------------------
48
+ # БИБЛИОТЕКА LU
49
+ #------------------------------------------
50
+ import lyrpy.LUErrors as LUErrors
51
+ import lyrpy.LUStrDecode as LUStrDecode
52
+ import lyrpy.LUDateTime as LUDateTime
53
+ import lyrpy.LUos as LUos
54
+ import lyrpy.LULog as LULog
55
+ # import lyrpy.LUFile as LUFile
56
+
57
+ """
58
+ #--------------------------------------------------------------------------------
59
+ f = open(file_name, access_mode, encoding='')
60
+ file_name = имя открываемого файла
61
+ access_mode = режим открытия файла. Он может быть: для чтения, записи и т. д.
62
+ По умолчанию используется режим чтения (r), если другое не указано.
63
+ Далее полный список режимов открытия файла
64
+ Режим Описание
65
+ r Только для чтения.
66
+ w Только для записи. Создаст новый файл, если не найдет с указанным именем.
67
+ rb Только для чтения (бинарный).
68
+ wb Только для записи (бинарный). Создаст новый файл, если не найдет с указанным именем.
69
+ r+ Для чтения и записи.
70
+ rb+ Для чтения и записи (бинарный).
71
+ w+ Для чтения и записи. Создаст новый файл для записи, если не найдет с указанным именем.
72
+ wb+ Для чтения и записи (бинарный). Создаст новый файл для записи, если не найдет с указанным именем.
73
+ a Откроет для добавления нового содержимого. Создаст новый файл для записи, если не найдет с указанным именем.
74
+ a+ Откроет для добавления нового содержимого. Создаст новый файл для чтения записи, если не найдет с указанным именем.
75
+ ab Откроет для добавления нового содержимого (бинарный). Создаст новый файл для записи, если не найдет с указанным именем.
76
+ ab+ Откроет для добавления нового содержимого (бинарный). Создаст новый файл для чтения записи, если не найдет с указанным именем.
77
+
78
+ # LFile = open (AFileName, 'r', encoding='utf-8')
79
+ # LFile = open (AFileName, 'r', encoding='cp1251')
80
+ #--------------------------------------------------------------------------------
81
+ """
82
+
83
+ # cDefaultEncoding = 'cp1251'
84
+ cDefaultEncoding = 'utf-8'
85
+
86
+ #--------------------------------------------------------------------------------
87
+ # DirectoryExists
88
+ #--------------------------------------------------------------------------------
89
+ def DirectoryExists (APath: str) -> bool:
90
+ """DirectoryExists """
91
+ #beginfunction
92
+ return os.path.isdir(APath)
93
+ #endfunction
94
+
95
+ #--------------------------------------------------------------------------------
96
+ # ForceDirectories
97
+ #--------------------------------------------------------------------------------
98
+ def ForceDirectories (ADir: str) -> bool:
99
+ """ForceDirectories"""
100
+ #beginfunction
101
+ try:
102
+ os.makedirs (ADir, exist_ok = True)
103
+ except:
104
+ s = f'Unable to create directory {ADir:s} ...'
105
+ # LULog.LoggerTOOLS_AddLevel(logging.error, s)
106
+ LULog.LoggerAdd(LULog.LoggerTOOLS, logging.error, s)
107
+ #endtry
108
+ LResult = DirectoryExists (ADir)
109
+ return LResult
110
+ #endfunction
111
+
112
+ #--------------------------------------------------------------------------------
113
+ # GetDirectoryTreeSize
114
+ #--------------------------------------------------------------------------------
115
+ def GetDirectoryTreeSize(ADir: str) -> int:
116
+ """GetDirectoryTreeSize"""
117
+ """Return total size of files in given path and subdirs"""
118
+ #beginfunction
119
+ Ltotal = 0
120
+ for Lentry in os.scandir(ADir):
121
+ if Lentry.is_dir(follow_symlinks=False):
122
+ Ltotal += GetDirectoryTreeSize(Lentry.path)
123
+ else:
124
+ Ltotal += Lentry.stat(follow_symlinks=False).st_size
125
+ return Ltotal
126
+ #endfunction
127
+
128
+ #--------------------------------------------------------------------------------
129
+ # DeleteDirectoryTree
130
+ #--------------------------------------------------------------------------------
131
+ def DeleteDirectoryTree (ADir: str) -> bool:
132
+ """DeleteDirectoryTree"""
133
+ """
134
+ Удалить дерево каталогов в Windows,
135
+ где для некоторых файлов установлен бит только для чтения.
136
+ Он использует обратный вызов onerror, чтобы очистить бит readonly и повторить попытку удаления.
137
+ """
138
+ def remove_readonly (func, path, _):
139
+ """remove_readonly"""
140
+ #beginfunction
141
+ Ls = f'Clear the readonly bit and reattempt the removal {path:s} ...'
142
+ # LULog.LoggerTOOLS_AddLevel(logging.DEBUG, Ls)
143
+ LULog.LoggerAdd(LULog.LoggerTOOLS, logging.DEBUG, Ls)
144
+ os.chmod (path, stat.S_IWRITE)
145
+ func (path)
146
+ #endfunction
147
+
148
+ def errorRemoveReadonly (func, path, exc):
149
+ """errorRemoveReadonly"""
150
+ #beginfunction
151
+ excvalue = exc [1]
152
+ if func in (os.rmdir, os.remove) and excvalue.errno == errno.EACCES:
153
+ # change the file to be readable,writable,executable: 0777
154
+ os.chmod (path, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
155
+ # retry
156
+ func (path)
157
+ else:
158
+ # raiseenter code here
159
+ ...
160
+ #endif
161
+ #endfunction
162
+
163
+ #beginfunction
164
+ LResult = True
165
+ if DirectoryExists (ADir):
166
+ s = f'DeleteDirectoryTree {ADir:s} ...'
167
+ # LULog.LoggerTOOLS_AddLevel(logging.DEBUG, s)
168
+ LULog.LoggerAdd(LULog.LoggerTOOLS, logging.DEBUG, s)
169
+ try:
170
+ # shutil.rmtree (ADirectoryName, ignore_errors = True, onexc = None)
171
+ shutil.rmtree (ADir, ignore_errors = False, onerror = remove_readonly)
172
+ LResult = True
173
+ except:
174
+ s = f'Unable delete directory {ADir:s} ...'
175
+ # LULog.LoggerTOOLS_AddLevel (logging.error, s)
176
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.error, s)
177
+ LResult = False
178
+ #endtry
179
+ #endif
180
+ return LResult
181
+ #endfunction
182
+
183
+ #--------------------------------------------------------------------------------
184
+ # DeleteDirectory_walk
185
+ #--------------------------------------------------------------------------------
186
+ # Delete everything reachable from the directory named in 'top',
187
+ # assuming there are no symbolic links.
188
+ # CAUTION: This is dangerous! For example, if top == '/', it
189
+ # could delete all your disk files.
190
+ #--------------------------------------------------------------------------------
191
+ def DirectoryClear (ADir: str) -> bool:
192
+ """DirectoryClear"""
193
+ #beginfunction
194
+ LResult = True
195
+ if DirectoryExists (ADir):
196
+ for root, dirs, files in os.walk (ADir, topdown = False):
197
+ for file in files:
198
+ os.remove (os.path.join (root, file))
199
+ #endfor
200
+ for ldir in dirs:
201
+ os.rmdir (os.path.join (root, ldir))
202
+ #endfor
203
+ #endfor
204
+ LResult = True
205
+ #endif
206
+ return LResult
207
+ #endfunction
208
+
209
+ #--------------------------------------------------------------------------------
210
+ # FileExists
211
+ #--------------------------------------------------------------------------------
212
+ def FileExists (AFileName: str) -> bool:
213
+ """FileExists"""
214
+ #beginfunction
215
+ return os.path.isfile(AFileName)
216
+ #endfunction
217
+
218
+ #--------------------------------------------------------------------------------
219
+ # GetFileDateTime
220
+ #--------------------------------------------------------------------------------
221
+ def GetFileDateTime (AFileName: str) -> ():
222
+ """GetFileDateTime"""
223
+ #beginfunction
224
+ LTuple = ()
225
+ LFileTimeCreate = 0
226
+ LFileTimeMod = 0
227
+ LFileTimeCreateDate = 0
228
+ LFileTimeModDate = 0
229
+ if FileExists (AFileName):
230
+ # file creation
231
+ LFileTimeCreate: datetime = os.path.getctime (AFileName)
232
+ # file modification
233
+ LFileTimeMod: datetime = os.path.getmtime (AFileName)
234
+ # convert creation timestamp into DateTime object
235
+ LFileTimeCreateDate: datetime = datetime.datetime.fromtimestamp (LFileTimeCreate)
236
+ # convert timestamp into DateTime object
237
+ LFileTimeModDate: datetime = datetime.datetime.fromtimestamp (LFileTimeMod)
238
+ #endif
239
+ LTuple = (LFileTimeMod, LFileTimeCreate, LFileTimeModDate, LFileTimeCreateDate)
240
+ return LTuple
241
+ #endfunction
242
+
243
+ #--------------------------------------------------------------------------------
244
+ # GetDirDateTime
245
+ #--------------------------------------------------------------------------------
246
+ def GetDirDateTime (AFileName: str) -> ():
247
+ """GetDirDateTime"""
248
+ #beginfunction
249
+ LTuple = ()
250
+ LFileTimeCreate = 0
251
+ LFileTimeMod = 0
252
+ LFileTimeCreateDate = 0
253
+ LFileTimeModDate = 0
254
+ if DirectoryExists (AFileName):
255
+ # file creation
256
+ LFileTimeCreate: datetime = os.path.getctime (AFileName)
257
+ # file modification
258
+ LFileTimeMod: datetime = os.path.getmtime (AFileName)
259
+ # convert creation timestamp into DateTime object
260
+ LFileTimeCreateDate: datetime = datetime.datetime.fromtimestamp (LFileTimeCreate)
261
+ # convert timestamp into DateTime object
262
+ LFileTimeModDate: datetime = datetime.datetime.fromtimestamp (LFileTimeMod)
263
+ #endif
264
+ LTuple = (LFileTimeMod, LFileTimeCreate, LFileTimeModDate, LFileTimeCreateDate)
265
+ return LTuple
266
+ #endfunction
267
+
268
+ def cmptimestamps(AFileNameSource: str, AFileNameDest: str, _use_ctime):
269
+ """ Compare time stamps of two files and return True
270
+ if file1 (source) is more recent than file2 (target) """
271
+ #beginfunction
272
+ st1 = os.stat (AFileNameSource)
273
+ st2 = os.stat (AFileNameDest)
274
+ mtime_cmp = int((st1.st_mtime - st2.st_mtime) * 1000) > 0
275
+ if _use_ctime:
276
+ return mtime_cmp or int((AFileNameSource.st_ctime - AFileNameDest.st_mtime) * 1000) > 0
277
+ else:
278
+ return mtime_cmp
279
+ #endif
280
+ #endfunction
281
+
282
+ #--------------------------------------------------------------------------------
283
+ # COMPAREFILETIMES
284
+ #--------------------------------------------------------------------------------
285
+ def COMPAREFILETIMES (AFileNameSource: str, AFileNameDest: str) -> int:
286
+ """COMPAREFILETIMES"""
287
+ #beginfunction
288
+ if not FileExists (AFileNameSource):
289
+ #-2 File1 could not be opened (see @ERROR for more information).
290
+ return -2
291
+ #endif
292
+ if not FileExists (AFileNameDest):
293
+ #-3 File2 could not be opened (see @ERROR for more information).
294
+ return -3
295
+ #endif
296
+
297
+ LFileName1m = GetFileDateTime (AFileNameSource)[0]
298
+ LFileName2m = GetFileDateTime (AFileNameDest)[0]
299
+ LFileName1c = GetFileDateTime (AFileNameSource)[0]
300
+ LFileName2c = GetFileDateTime (AFileNameDest)[0]
301
+
302
+ if LFileName1m == LFileName2m:
303
+ #0 File1 and file2 have the same date and time.
304
+ return 0
305
+ else:
306
+ if LFileName1m > LFileName2m:
307
+ #1 File1 is more recent than file2.
308
+ return 1
309
+ else:
310
+ #-1 File1 is older than file2.
311
+ return -1
312
+ #endif
313
+ #endif
314
+ #------------------------------------------------------------------------------
315
+ # if int ((LFileName1m - LFileName2m) * 1000) == 0:
316
+ # return 0
317
+ # else:
318
+ # if int ((LFileName1m - LFileName2m) * 1000) > 0:
319
+ # return 1
320
+ # else:
321
+ # return -1
322
+ # #endif
323
+ # #endif
324
+ #------------------------------------------------------------------------------
325
+ #endfunction
326
+
327
+ #--------------------------------------------------------------------------------
328
+ # CheckFileExt
329
+ #--------------------------------------------------------------------------------
330
+ def CheckFileExt (AFileName: str, AExt: str) -> bool:
331
+ """CheckFileExt"""
332
+ #beginfunction
333
+ if AExt != "":
334
+ LResult = ExtractFileName(AFileName).endswith(AExt)
335
+ else:
336
+ LResult = False
337
+ #endif
338
+ return LResult
339
+ #endfunction
340
+
341
+ #--------------------------------------------------------------------------------
342
+ # GetFileSize
343
+ #--------------------------------------------------------------------------------
344
+ def GetFileSize (AFileName: str) -> int:
345
+ """GetFileSize"""
346
+ #beginfunction
347
+ if FileExists (AFileName):
348
+ LResult = os.path.getsize (AFileName)
349
+ else:
350
+ LResult = 0
351
+ #endif
352
+ return LResult
353
+ #endfunction
354
+
355
+ #--------------------------------------------------------------------------------
356
+ # ExpandFileName
357
+ #--------------------------------------------------------------------------------
358
+ def ExpandFileName (APath: str) -> str:
359
+ """ExpandFileName"""
360
+ #beginfunction
361
+ LResult = os.path.abspath(APath)
362
+ return LResult
363
+ #endfunction
364
+
365
+ #--------------------------------------------------------------------------------
366
+ # ExtractFileDir
367
+ #--------------------------------------------------------------------------------
368
+ def ExtractFileDir (APath: str) -> str:
369
+ """ExtractFileDir"""
370
+ #beginfunction
371
+ LDir, LFileName = os.path.split(APath)
372
+ return LDir
373
+ #endfunction
374
+
375
+ #--------------------------------------------------------------------------------
376
+ # ExtractFileName
377
+ #--------------------------------------------------------------------------------
378
+ def ExtractFileName (APath: str) -> str:
379
+ """ExtractFileName"""
380
+ #beginfunction
381
+ LPath, LFileName = os.path.split(APath)
382
+ return LFileName
383
+ #endfunction
384
+
385
+ #-------------------------------------------------------------------------------
386
+ # ExtractFileNameWithoutExt
387
+ #-------------------------------------------------------------------------------
388
+ def ExtractFileNameWithoutExt (AFileName: str) -> str:
389
+ """ExtractFileNameWithoutExt"""
390
+ #beginfunction
391
+ LResult = os.path.basename (AFileName).split ('.') [0]
392
+ return LResult
393
+ #endfunction
394
+
395
+ #--------------------------------------------------------------------------------
396
+ # ExtractFileExt
397
+ #--------------------------------------------------------------------------------
398
+ def ExtractFileExt (AFileName: str) -> str:
399
+ """ExtractFileExt"""
400
+ #beginfunction
401
+ LResult = os.path.basename(AFileName)
402
+ LFileName, LFileExt = os.path.splitext(LResult)
403
+ return LFileExt
404
+ #endfunction
405
+
406
+ #---------------------------------------------------------------------------------------------
407
+ # GetFileDir (APath: str) -> str:
408
+ #---------------------------------------------------------------------------------------------
409
+ def GetFileDir (APath: str) -> str:
410
+ """GetFileDir"""
411
+ #beginfunction
412
+ return ExtractFileDir (APath)
413
+ #endfunction
414
+
415
+ #--------------------------------------------------------------------------------
416
+ # GetFileName (APath: str) -> str:
417
+ #--------------------------------------------------------------------------------
418
+ def GetFileName (APath: str) -> str:
419
+ """GetFileName"""
420
+ #beginfunction
421
+ return ExtractFileNameWithoutExt (APath)
422
+ #endfunction
423
+
424
+ #-------------------------------------------------------------------------------
425
+ # GetFileNameWithoutExt (AFileName: str) -> str:
426
+ #-------------------------------------------------------------------------------
427
+ def GetFileNameWithoutExt (AFileName: str) -> str:
428
+ """GetFileNameWithoutExt"""
429
+ #beginfunction
430
+ return ExtractFileNameWithoutExt (AFileName)
431
+ #endfunction
432
+
433
+ #---------------------------------------------------------------------------------------------
434
+ # GetFileExt (AFileName: str) -> str:
435
+ #---------------------------------------------------------------------------------------------
436
+ def GetFileExt (AFileName: str) -> str:
437
+ """GetFileExt"""
438
+ #beginfunction
439
+ return ExtractFileExt (AFileName)
440
+ #endfunction
441
+
442
+ #--------------------------------------------------------------------------------
443
+ # GetFileEncoding (AFileName: str) -> str:
444
+ #--------------------------------------------------------------------------------
445
+ def GetFileEncoding (AFileName: str) -> str:
446
+ """GetFileEncoding"""
447
+ #beginfunction
448
+ LEncoding = ''
449
+ if FileExists(AFileName):
450
+ LFile = open (AFileName, 'rb')
451
+ LRawData = LFile.read ()
452
+ LResult = chardet.detect (LRawData)
453
+ LEncoding = LResult ['encoding']
454
+ LFile.close ()
455
+ #endif
456
+ return LEncoding
457
+ #endfunction
458
+
459
+ #--------------------------------------------------------------------------------
460
+ # IncludeTrailingBackslash
461
+ #--------------------------------------------------------------------------------
462
+ def IncludeTrailingBackslash (APath: str) -> str:
463
+ """IncludeTrailingBackslash"""
464
+ #beginfunction
465
+ LResult = APath.rstrip('\\')+'\\'
466
+ # LResult = pathlib.WindowsPath (APath)
467
+ # LResult = APath.rstrip('/')+'/'
468
+ return LResult
469
+ #endfunction
470
+
471
+ #--------------------------------------------------------------------------------
472
+ # GetDirNameYYMMDD
473
+ #--------------------------------------------------------------------------------
474
+ def GetDirNameYYMMDD (ARootDir: str, ADate: datetime.datetime) -> str:
475
+ """GetDirNameYYMMDD"""
476
+ #beginfunction
477
+ LYMDStr: str = LUDateTime.DateTimeStr(False, ADate, LUDateTime.cFormatDateYYMMDD_02, False)
478
+ LResult = IncludeTrailingBackslash(ARootDir)+LYMDStr
479
+ return LResult
480
+ #endfunction
481
+
482
+ #--------------------------------------------------------------------------------
483
+ # GetDirNameYYMM
484
+ #--------------------------------------------------------------------------------
485
+ def GetDirNameYYMM (ARootDir: str, ADate: datetime.datetime) -> str:
486
+ """GetDirNameYYMM"""
487
+ #beginfunction
488
+ LYMDStr: str = LUDateTime.DateTimeStr(False, ADate, LUDateTime.cFormatDateYYMM_02, False)
489
+ LResult = IncludeTrailingBackslash(ARootDir)+LYMDStr
490
+ return LResult
491
+ #endfunction
492
+
493
+ #--------------------------------------------------------------------------------
494
+ # GetTempDir
495
+ #--------------------------------------------------------------------------------
496
+ def GetTempDir () -> str:
497
+ """GetTempDir"""
498
+ #beginfunction
499
+ # LResult = win32api.GetTempPath()
500
+ LResult = tempfile.gettempdir ()
501
+ print('TEMP:',LResult)
502
+ return LResult
503
+ #endfunction
504
+
505
+ #-------------------------------------------------------------------------------
506
+ # GetFileAttrStr
507
+ #-------------------------------------------------------------------------------
508
+ def GetFileAttrStr (Aattr: int) -> str:
509
+ """GetFileAttrStr"""
510
+ #beginfunction
511
+ #-------------------------------------------------------------------------------
512
+ # 0x 00 00 20 20
513
+ # 0b00000000 00000000 00100000 00100000
514
+ #-------------------------------------------------------------------------------
515
+ #stat.FILE_ATTRIBUTE_NO_SCRUB_DATA 0b00000000 00000010 00000000 00000000
516
+ #stat.FILE_ATTRIBUTE_VIRTUAL 0b00000000 00000001 00000000 00000000
517
+
518
+ #stat.FILE_ATTRIBUTE_INTEGRITY_STREAM 0b00000000 00000000 10000000 00000000
519
+ #stat.FILE_ATTRIBUTE_ENCRYPTED 0b00000000 00000000 01000000 00000000
520
+ #stat.FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0b00000000 00000000 00100000 00000000
521
+ #stat.FILE_ATTRIBUTE_OFFLINE 0b00000000 00000000 00010000 00000000
522
+ #stat.FILE_ATTRIBUTE_COMPRESSED 0b00000000 00000000 00001000 00000000
523
+ #stat.FILE_ATTRIBUTE_REPARSE_POINT 0b00000000 00000000 00000100 00000000
524
+ #stat.FILE_ATTRIBUTE_SPARSE_FILE 0b00000000 00000000 00000010 00000000
525
+ #stat.FILE_ATTRIBUTE_TEMPORARY 0b00000000 00000000 00000001 00000000
526
+
527
+ #stat.FILE_ATTRIBUTE_NORMAL 0b00000000 00000000 00000000 10000000
528
+ #stat.FILE_ATTRIBUTE_DEVICE 0b00000000 00000000 00000000 01000000
529
+ #-------------------------------------------------------------------------------
530
+ #stat.FILE_ATTRIBUTE_ARCHIVE 0b00000000 00000000 00000000 00100000
531
+ #-------------------------------------------------------------------------------
532
+ #stat.FILE_ATTRIBUTE_DIRECTORY 0b00000000 00000000 00000000 00010000
533
+ #-------------------------------------------------------------------------------
534
+ #stat. 0b00000000 00000000 00000000 00001000
535
+ #-------------------------------------------------------------------------------
536
+ #stat.FILE_ATTRIBUTE_SYSTEM 0b00000000 00000000 00000000 00000100
537
+ #-------------------------------------------------------------------------------
538
+ #stat.FILE_ATTRIBUTE_HIDDEN 0b00000000 00000000 00000000 00000010
539
+ #-------------------------------------------------------------------------------
540
+ #stat.FILE_ATTRIBUTE_READONLY 0b00000000 00000000 00000000 00000001
541
+ #-------------------------------------------------------------------------------
542
+ Lattr = Aattr
543
+ sa = ''
544
+ sa += '????????'
545
+ sa += '1' if Lattr & 0b100000000000000000000000 else '?'
546
+ sa += '1' if Lattr & 0b010000000000000000000000 else '?'
547
+ sa += '1' if Lattr & 0b001000000000000000000000 else '?'
548
+ sa += '1' if Lattr & 0b000100000000000000000000 else '?'
549
+ sa += '1' if Lattr & 0b000010000000000000000000 else '?'
550
+ sa += '1' if Lattr & 0b000001000000000000000000 else '?'
551
+ sa += '1' if Lattr & stat.FILE_ATTRIBUTE_NO_SCRUB_DATA else '.'
552
+ sa += '1' if Lattr & stat.FILE_ATTRIBUTE_VIRTUAL else '.'
553
+
554
+ sa += '1' if Lattr & stat.FILE_ATTRIBUTE_INTEGRITY_STREAM else '.'
555
+ sa += '1' if Lattr & stat.FILE_ATTRIBUTE_ENCRYPTED else '.'
556
+ sa += '1' if Lattr & stat.FILE_ATTRIBUTE_NOT_CONTENT_INDEXED else '.'
557
+ sa += '1' if Lattr & stat.FILE_ATTRIBUTE_OFFLINE else '.'
558
+ sa += '1' if Lattr & stat.FILE_ATTRIBUTE_COMPRESSED else '.'
559
+ sa += '1' if Lattr & stat.FILE_ATTRIBUTE_REPARSE_POINT else '.'
560
+ sa += '1' if Lattr & stat.FILE_ATTRIBUTE_SPARSE_FILE else '.'
561
+ sa += '1' if Lattr & stat.FILE_ATTRIBUTE_TEMPORARY else '.'
562
+
563
+ sa += '1' if Lattr & stat.FILE_ATTRIBUTE_NORMAL else '.'
564
+ sa += '1' if Lattr & stat.FILE_ATTRIBUTE_DEVICE else '.'
565
+ sa += 'a' if Lattr & stat.FILE_ATTRIBUTE_ARCHIVE else '.'
566
+ sa += 'd' if Lattr & stat.FILE_ATTRIBUTE_DIRECTORY else '.'
567
+ sa += '.'
568
+ sa += 's' if Lattr & stat.FILE_ATTRIBUTE_SYSTEM else '.'
569
+ sa += 'h' if Lattr & stat.FILE_ATTRIBUTE_HIDDEN else '.'
570
+ sa += 'r' if Lattr & stat.FILE_ATTRIBUTE_READONLY else '.'
571
+ return sa
572
+ #endfunction
573
+
574
+ #-------------------------------------------------------------------------------
575
+ # GetFileModeStrUnix
576
+ #-------------------------------------------------------------------------------
577
+ def GetFileModeStrUnix (Amode: int) -> str:
578
+ """GetFileModeStrUnix"""
579
+ #beginfunction
580
+ # chmod(path,mode)
581
+ # s = f'stat.S_ISUID: {bin (stat.S_ISUID):s}'
582
+ # LULog.LoggerTOOLS_AddLevel (LULog.TEXT, s)
583
+ # s = f'stat.S_ISGID: {bin (stat.S_ISGID):s}'
584
+ # LULog.LoggerTOOLS_AddLevel (LULog.TEXT, s)
585
+ # s = f'stat.S_ENFMT: {bin (stat.S_ENFMT):s}'
586
+ # LULog.LoggerTOOLS_AddLevel (LULog.TEXT, s)
587
+ # s = f'stat.S_ISVTX: {bin (stat.S_ISVTX):s}'
588
+ # LULog.LoggerTOOLS_AddLevel (LULog.TEXT, s)
589
+ # s = f'stat.S_IREAD: {bin (stat.S_IREAD):s}'
590
+ # LULog.LoggerTOOLS_AddLevel (LULog.TEXT, s)
591
+ # s = f'stat.S_IWRITE: {bin (stat.S_IWRITE):s}'
592
+ # LULog.LoggerTOOLS_AddLevel (LULog.TEXT, s)
593
+ # s = f'stat.S_IEXEC: {bin (stat.S_IEXEC):s}'
594
+ # LULog.LoggerTOOLS_AddLevel (LULog.TEXT, s)
595
+ # s = f'stat.S_IRWXU: {bin (stat.S_IRWXU):s}'
596
+ # LULog.LoggerTOOLS_AddLevel (LULog.TEXT, s)
597
+ # s = f'stat.S_IRUSR: {bin (stat.S_IRUSR):s}'
598
+ # LULog.LoggerTOOLS_AddLevel (LULog.TEXT, s)
599
+ # s = f'stat.S_IWUSR: {bin (stat.S_IWUSR):s}'
600
+ # LULog.LoggerTOOLS_AddLevel (LULog.TEXT, s)
601
+ # s = f'stat.S_IXUSR: {bin (stat.S_IXUSR):s}'
602
+ # LULog.LoggerTOOLS_AddLevel (LULog.TEXT, s)
603
+ # s = f'stat.S_IRWXG: {bin (stat.S_IRWXG):s}'
604
+ # LULog.LoggerTOOLS_AddLevel (LULog.TEXT, s)
605
+ # s = f'stat.S_IRGRP: {bin (stat.S_IRGRP):s}'
606
+ # LULog.LoggerTOOLS_AddLevel (LULog.TEXT, s)
607
+ # s = f'stat.S_IWGRP: {bin (stat.S_IWGRP):s}'
608
+ # LULog.LoggerTOOLS_AddLevel (LULog.TEXT, s)
609
+ # s = f'stat.S_IXGRP: {bin (stat.S_IXGRP):s}'
610
+ # LULog.LoggerTOOLS_AddLevel (LULog.TEXT, s)
611
+ # s = f'stat.S_IRWXO: {bin (stat.S_IRWXO):s}'
612
+ # LULog.LoggerTOOLS_AddLevel (LULog.TEXT, s)
613
+ # s = f'stat.S_IROTH: {bin (stat.S_IROTH):s}'
614
+ # LULog.LoggerTOOLS_AddLevel (LULog.TEXT, s)
615
+ # s = f'stat.S_IWOTH: {bin (stat.S_IWOTH):s}'
616
+ # LULog.LoggerTOOLS_AddLevel (LULog.TEXT, s)
617
+ # s = f'stat.S_IXOTH: {bin (stat.S_IXOTH):s}'
618
+ # LULog.LoggerTOOLS_AddLevel (LULog.TEXT, s)
619
+
620
+ #-------------------------------------------------------------------------------
621
+ # stat.S_ISUID − Set user ID on execution
622
+ # stat.S_ISUID: 0b00010000 00000000
623
+ # stat.S_ISGID − Set group ID on execution
624
+ # stat.S_ISGID: 0b00000100 00000000
625
+ # stat.S_ENFMT – Enforced record locking
626
+ # stat.S_ENFMT: 0b00000100 00000000
627
+ # stat.S_ISVTX – After execution, save text image
628
+ # stat.S_ISVTX: 0b00000010 00000000
629
+ #-------------------------------------------------------------------------------
630
+ # stat.S_IREAD − Read by owner
631
+ # stat.S_IREAD: 0b00000001 00000000
632
+ # stat.S_IWRITE − Write by owner
633
+ # stat.S_IWRITE: 0b00000000 10000000
634
+ # stat.S_IEXEC − Execute by owner
635
+ # stat.S_IEXEC: 0b00000000 01000000
636
+ #-------------------------------------------------------------------------------
637
+ # stat.S_IRWXU − Read, write, and execute by owner
638
+ # stat.S_IRWXU: 0b00000001 11000000 Owner
639
+ #-------------------------------------------------------------------------------
640
+ # stat.S_IRUSR − Read by owner
641
+ # stat.S_IRUSR: 0b00000001 00000000
642
+ # stat.S_IWUSR − Write by owner
643
+ # stat.S_IWUSR: 0b00000000 10000000
644
+ # stat.S_IXUSR − Execute by owner
645
+ # stat.S_IXUSR: 0b00000000 01000000
646
+ #-------------------------------------------------------------------------------
647
+ # stat.S_IRWXG − Read, write, and execute by group
648
+ # stat.S_IRWXG: 0b00000000 00111000 Group
649
+ #-------------------------------------------------------------------------------
650
+ # stat.S_IRGRP − Read by group
651
+ # stat.S_IRGRP: 0b00000000 00100000
652
+ # stat.S_IWGRP − Write by group
653
+ # stat.S_IWGRP: 0b00000000 00010000
654
+ # stat.S_IXGRP − Execute by group
655
+ # stat.S_IXGRP: 0b00000000 00001000
656
+ #-------------------------------------------------------------------------------
657
+ # stat.S_IRWXO − Read, write, and execute by others
658
+ # stat.S_IRWXO: 0b00000000 00000111 Others
659
+ #-------------------------------------------------------------------------------
660
+ # stat.S_IROTH − Read by others
661
+ # stat.S_IROTH: 0b00000000 00000100
662
+ # stat.S_IWOTH − Write by others
663
+ # stat.S_IWOTH: 0b00000000 00000010
664
+ # stat.S_IXOTH − Execute by others
665
+ # stat.S_IXOTH: 0b00000000 00000001
666
+ #-------------------------------------------------------------------------------
667
+ Lmode = Amode
668
+ sa = ''
669
+ sa += '1' if Lmode & 0b1000000000000000 else '-'
670
+ sa += '1' if Lmode & 0b0100000000000000 else '-'
671
+ sa += '1' if Lmode & 0b0010000000000000 else '-'
672
+
673
+ sa += '1' if Lmode & stat.S_ISUID else '-'
674
+ sa += '1' if Lmode & stat.S_ISGID else '-'
675
+ sa += '1' if Lmode & stat.S_ENFMT else '-'
676
+ sa += '1' if Lmode & stat.S_ISVTX else '-'
677
+ #-------------------------------------------------------------------------------
678
+ sa += 'r' if Lmode & stat.S_IRUSR else '-'
679
+ sa += 'w' if Lmode & stat.S_IWUSR else '-'
680
+ sa += 'x' if Lmode & stat.S_IXUSR else '-'
681
+ #-------------------------------------------------------------------------------
682
+ sa += 'r' if Lmode & stat.S_IRGRP else '-'
683
+ sa += 'w' if Lmode & stat.S_IWGRP else '-'
684
+ sa += 'x' if Lmode & stat.S_IXGRP else '-'
685
+ #-------------------------------------------------------------------------------
686
+ sa += 'r' if Lmode & stat.S_IROTH else '-'
687
+ sa += 'w' if Lmode & stat.S_IWOTH else '-'
688
+ sa += 'x' if Lmode & stat.S_IXOTH else '-'
689
+ return sa
690
+ #endfunction
691
+
692
+ #-------------------------------------------------------------------------------
693
+ # GetFileAttr
694
+ #-------------------------------------------------------------------------------
695
+ def GetFileAttr (AFileName: str) -> int:
696
+ """GetFileAttr"""
697
+ #beginfunction
698
+ s = f'GetFileAttr: {AFileName:s}'
699
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
700
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.DEBUG, s)
701
+
702
+ LResult = 0
703
+
704
+ if FileExists (AFileName) or DirectoryExists (AFileName):
705
+ LStat = os.stat (AFileName)
706
+
707
+ LOSInfo = LUos.TOSInfo ()
708
+ match LOSInfo.system:
709
+ case 'Windows':
710
+ Lmode = LStat.st_mode
711
+ s = f'Lmode: {Lmode:d} {hex (Lmode):s} {bin (Lmode):s} {stat.filemode (Lmode):s}'
712
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
713
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.DEBUG, s)
714
+ Lattr = LStat.st_file_attributes
715
+
716
+ # Lattr = win32api.GetFileAttributes (AFileName)
717
+
718
+ s = f'Lattr:{Lattr:d} {hex (Lattr):s} {bin (Lattr):s} {GetFileAttrStr (Lattr):s}'
719
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
720
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.DEBUG, s)
721
+ LResult = Lattr
722
+ case 'Linux':
723
+ Lmode = LStat.st_mode
724
+ s = f'Lmode:{Lmode:d} {hex (Lmode):s} {bin (Lmode):s} {GetFileModeStrUnix (Lmode):s}'
725
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
726
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.DEBUG, s)
727
+ LResult = Lmode
728
+ case _:
729
+ s = f'Неизвестная система ...'
730
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
731
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.DEBUG, s)
732
+ LResult = 0
733
+ #endmatch
734
+ #endif
735
+ return LResult
736
+ #endfunction
737
+
738
+ #-------------------------------------------------------------------------------
739
+ # SetFileAttr
740
+ #-------------------------------------------------------------------------------
741
+ def SetFileAttr (AFileName: str, Aattr: int, AClear: bool):
742
+ """SetFileAttr"""
743
+ #beginfunction
744
+ s = f'SetFileAttr: {Aattr:d} {hex (Aattr):s} {bin (Aattr):s}'
745
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
746
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.DEBUG, s)
747
+
748
+ LOSInfo = LUos.TOSInfo ()
749
+ match LOSInfo.system:
750
+ case 'Windows':
751
+ Lattr = GetFileAttr(AFileName)
752
+ s = f'Lattr - current: {Lattr:d} {hex (Lattr):s} {bin (Lattr):s}'
753
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
754
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.DEBUG, s)
755
+
756
+ if AClear:
757
+ LattrNew = Lattr & ~Aattr
758
+ s = f'[clear]: {bin (LattrNew):s} {LattrNew:d} {hex (LattrNew):s} {bin (LattrNew):s}'
759
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
760
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.DEBUG, s)
761
+ else:
762
+ LattrNew = Lattr | ~Aattr
763
+ s = f'[set]: {bin (LattrNew):s} {LattrNew:d} {hex (LattrNew):s} {bin (LattrNew):s}'
764
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
765
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.DEBUG, s)
766
+ #endif
767
+
768
+ # if os.path.isdir (AFileName):
769
+ # LResult = ctypes.windll.kernel32.SetFileAttributesW (AFileName, LattrNew)
770
+ # else:
771
+ # win32api.SetFileAttributes (AFileName, LattrNew)
772
+ # #endif
773
+
774
+ LResult = ctypes.windll.kernel32.SetFileAttributesW (AFileName, LattrNew)
775
+ case 'Linux':
776
+ raise NotImplementedError('SetFileAttr Linux not implemented ...')
777
+ case _:
778
+ s = f'Неизвестная система ...'
779
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
780
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.DEBUG, s)
781
+ #endmatch
782
+ #endfunction
783
+
784
+ #-------------------------------------------------------------------------------
785
+ # SetFileMode
786
+ #-------------------------------------------------------------------------------
787
+ def SetFileMode (AFileName: str, Amode: int, AClear: bool, Aflags: int):
788
+ """SetFileMode"""
789
+ #beginfunction
790
+ s = f'SetFileMode: {Amode:d} {hex (Amode):s} {bin (Amode):s}'
791
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
792
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.DEBUG, s)
793
+
794
+ Lattr = 0
795
+
796
+ LOSInfo = LUos.TOSInfo ()
797
+ match LOSInfo.system:
798
+ case 'Windows':
799
+ # Change the file's permissions to writable
800
+ # os.chmod (AFileName, os.W_OK)
801
+ ...
802
+ case 'Linux':
803
+ # os.chflags() method in Python used to set the flags of path to the numeric flags;
804
+ # available in Unix only
805
+ # os.UF_HIDDEN
806
+ if AClear:
807
+ LattrNew = Lattr & ~Aflags
808
+ s = f'SetFileAttr [clear]: {bin (Aflags):s} {LattrNew:d} {hex (LattrNew):s} {bin (LattrNew):s}'
809
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
810
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.DEBUG, s)
811
+ else:
812
+ LattrNew = Lattr | Aflags
813
+ s = f'SetFileAttr [set]: {bin (Aflags):s}{LattrNew:d} {hex (LattrNew):s} {bin (LattrNew):s}'
814
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
815
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.DEBUG, s)
816
+ #endif
817
+ os.chflags (AFileName, Aflags)
818
+ case _:
819
+ s = f'Неизвестная система ...'
820
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
821
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.DEBUG, s)
822
+ #endmatch
823
+ #endfunction
824
+
825
+ #-------------------------------------------------------------------------------
826
+ # SetFileFlags
827
+ #-------------------------------------------------------------------------------
828
+ def SetFileFlags (AFileName: str, Aflags: int, AClear: bool):
829
+ """SetFileMode"""
830
+ #beginfunction
831
+ s = f'SetFileMode: {Aflags:d} {hex (Aflags):s} {bin (Aflags):s}'
832
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
833
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.DEBUG, s)
834
+
835
+ LOSInfo = LUos.TOSInfo ()
836
+ match LOSInfo.system:
837
+ case 'Windows':
838
+ raise NotImplementedError('SetFileAttr Windows not implemented...')
839
+ case 'Linux':
840
+ # os.chflags() method in Python used to set the flags of path to the numeric flags;
841
+ # available in Unix only
842
+ # os.UF_HIDDEN
843
+
844
+ Lattr = 0
845
+
846
+ if AClear:
847
+ LflagsNew = Lattr & ~Aflags
848
+ s = f'[clear]: {bin (LflagsNew):s} {LflagsNew:d} {hex (LflagsNew):s} {bin (LflagsNew):s}'
849
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
850
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.DEBUG, s)
851
+ else:
852
+ LflagsNew = Lattr | ~Aflags
853
+ s = f'[set]: {bin (LflagsNew):s} {LflagsNew:d} {hex (LflagsNew):s} {bin (LflagsNew):s}'
854
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
855
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.DEBUG, s)
856
+ #endif
857
+ os.chflags (AFileName, LflagsNew)
858
+ case _:
859
+ s = f'Неизвестная система ...'
860
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
861
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.DEBUG, s)
862
+ #endmatch
863
+ #endfunction
864
+
865
+ #-------------------------------------------------------------------------------
866
+ # FileDelete
867
+ #-------------------------------------------------------------------------------
868
+ def FileDelete (AFileName: str) -> bool:
869
+ """FileDelete"""
870
+ #beginfunction
871
+ s = f'FileDelete: {AFileName:s}'
872
+
873
+ # LULog.LoggerTOOLS_setLevel(logging.INFO)
874
+ # LULog.LoggerTOOLS_setLevel(logging.DEBUG)
875
+
876
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
877
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.DEBUG, s)
878
+ LResult = True
879
+
880
+ if FileExists (AFileName):
881
+ LOSInfo = LUos.TOSInfo ()
882
+ match LOSInfo.system:
883
+ case 'Windows':
884
+ try:
885
+ Lattr = GetFileAttr (AFileName)
886
+ if Lattr & stat.FILE_ATTRIBUTE_READONLY:
887
+ s = f'Clear ReadOnly ...'
888
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
889
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.DEBUG, s)
890
+ SetFileAttr (AFileName, stat.FILE_ATTRIBUTE_READONLY, True)
891
+
892
+ # FileSetAttr (FileName, FileGetAttr(FileName) and (faReadOnly xor $FF));
893
+ # Change the file's permissions to writable
894
+ # os.chmod (AFileName, os.W_OK)
895
+
896
+ #endif
897
+ os.remove (AFileName)
898
+ LResult = True
899
+ except:
900
+ s = f'ERROR: FileDelete ...'
901
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
902
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.DEBUG, s)
903
+ LResult = False
904
+ #endtry
905
+ case 'Linux':
906
+ raise NotImplementedError('FileDelete Linux not implemented...')
907
+ case _:
908
+ s = f'Неизвестная система ...'
909
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
910
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.DEBUG, s)
911
+ #endmatch
912
+ #endif
913
+ return LResult
914
+ #endfunction
915
+
916
+ #-------------------------------------------------------------------------------
917
+ # FileCopy
918
+ #-------------------------------------------------------------------------------
919
+ def FileCopy (AFileNameSource: str, AFileNameDest: str, Overwrite: bool) -> bool:
920
+ """FileCopy"""
921
+ #beginfunction
922
+ s = f'FileCopy: {AFileNameSource:s} -> {AFileNameDest:s}'
923
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
924
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.DEBUG, s)
925
+
926
+ LResult = True
927
+
928
+ if FileExists (AFileNameSource):
929
+
930
+ LDestPath = ExtractFileDir (AFileNameDest)
931
+ if not DirectoryExists (LDestPath):
932
+ ForceDirectories (LDestPath)
933
+ #endif
934
+
935
+ LOSInfo = LUos.TOSInfo ()
936
+ match LOSInfo.system:
937
+ case 'Windows':
938
+ try:
939
+ # Функция shutil.copy() копирует данные файла и режима доступа к файлу.
940
+ # Другие метаданные, такие как время создания и время изменения файла не сохраняются.
941
+ # Чтобы сохранить все метаданные файла из оригинала, используйте функцию shutil.copy2().
942
+
943
+ # LResult = shutil.copy (AFileNameSource, AFileNameDest) != ''
944
+
945
+ # LResult = True
946
+ LResult = shutil.copy2 (AFileNameSource, AFileNameDest) != ''
947
+ # LResult = shutil.copy2 (AFileNameSource, LDestPath) != ''
948
+ # shutil.copystat (AFileNameSource, AFileNameDest)
949
+
950
+ except:
951
+ s = f'ERROR: FileCopy ...'
952
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
953
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.DEBUG, s)
954
+ LResult = False
955
+ #endtry
956
+ case 'Linux':
957
+ # unix
958
+ # LFileNameSource_stat = os.stat (AFileNameSource)
959
+ # Lowner = LFileNameSource_stat [stat.ST_UID]
960
+ # Lgroup = LFileNameSource_stat [stat.ST_GID]
961
+ # os.chown (AFileNameDest, Lowner, Lgroup)
962
+ raise NotImplementedError('FileCopy Linux not implemented...')
963
+ case _:
964
+ s = f'Неизвестная система ...'
965
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
966
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.DEBUG, s)
967
+ #endmatch
968
+ #endif
969
+ return LResult
970
+ #endfunction
971
+
972
+ #-------------------------------------------------------------------------------
973
+ # FileMove
974
+ #-------------------------------------------------------------------------------
975
+ def FileMove (AFileNameSource: str, APathNameDest: str) -> bool:
976
+ """FileMove"""
977
+ #beginfunction
978
+ s = f'FileMove: {AFileNameSource:s} -> {APathNameDest:s}'
979
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
980
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.DEBUG, s)
981
+
982
+ if not DirectoryExists(APathNameDest):
983
+ ForceDirectories(APathNameDest)
984
+ #endif
985
+ # LFileNameSource = ExtractFileName (AFileNameSource)
986
+ # LFileNameDest = os.path.join (APathNameDest, LFileNameSource)
987
+ LResult = shutil.move(AFileNameSource, APathNameDest, copy_function=shutil.copy2())
988
+ return LResult
989
+ #endfunction
990
+
991
+ #-------------------------------------------------------------------------------
992
+ # CheckFileNameMask
993
+ #-------------------------------------------------------------------------------
994
+ def CheckFileNameMask (AFileName: str, AMask: str) -> bool:
995
+ """CheckFileNameMask"""
996
+ #beginfunction
997
+ if AMask != '':
998
+ LFileName = AFileName
999
+ # LMask = '^[a-zA-Z0-9]+.py$' # *.py - только латинские буквы и цифры
1000
+ # LMask = '^.*..*$' # *.* - все символы
1001
+ # LMask = '^.*.py$' # *.py - все символы
1002
+ # LMask = '^[\\S ]*.py$' # *.py - все символы включая пробелы
1003
+ # LMask = '^[a-zA-Z0-9]*.py$' # *.py - только латинские буквы и цифры
1004
+ LMask = AMask
1005
+ # print (LMask, LFileName)
1006
+ #-------------------------------------------------------------------------------
1007
+ # regex = re.compile (LMask)
1008
+ # Lresult = regex.match(LFileName)
1009
+ #-------------------------------------------------------------------------------
1010
+ # эквивалентно
1011
+ #-------------------------------------------------------------------------------
1012
+ try:
1013
+ Lresult = re.match (LMask, LFileName)
1014
+ # Lresult = re.search (LMask, LFileName)
1015
+ except Exception as e:
1016
+ Lresult = False
1017
+ #endtry
1018
+ #-------------------------------------------------------------------------------
1019
+ else:
1020
+ Lresult = False
1021
+ #endif
1022
+ return Lresult
1023
+ #endfunction
1024
+
1025
+ #-------------------------------------------------------------------------------
1026
+ # CreateTextFile
1027
+ #-------------------------------------------------------------------------------
1028
+ def CreateTextFile(AFileName: str, AText: str, AEncoding: str):
1029
+ """CreateTextFile"""
1030
+ #beginfunction
1031
+ s = f'CreateTextFile: {AFileName:s} ...'
1032
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
1033
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.DEBUG, s)
1034
+
1035
+ LEncoding = AEncoding
1036
+ if AEncoding == '':
1037
+ # LEncoding = LUStrDecode.cCP1251
1038
+ LEncoding = cDefaultEncoding
1039
+ #endif
1040
+ if len(AText) > 0:
1041
+ LHandle = open (AFileName, 'w', encoding = LEncoding)
1042
+ LHandle.write (AText + '\n')
1043
+ LHandle.flush ()
1044
+ LHandle.close ()
1045
+ else:
1046
+ FileDelete (AFileName)
1047
+ LHandle = open (AFileName, 'w', encoding = LEncoding)
1048
+ LHandle.flush ()
1049
+ LHandle.close ()
1050
+ #endif
1051
+ #endfunction
1052
+
1053
+ #--------------------------------------------------------------------------------
1054
+ # WriteStrToFile
1055
+ #--------------------------------------------------------------------------------
1056
+ def WriteStrToFile (AFileName: str, AStr: str, AEncoding: str = ''):
1057
+ """WriteStrToFile"""
1058
+ #beginfunction
1059
+ s = f'WriteStrToFile: {AFileName:s} ...'
1060
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
1061
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.DEBUG, s)
1062
+
1063
+ # Откроет для добавления нового содержимого.
1064
+ # Создаст новый файл для чтения записи, если не найдет с указанным именем.
1065
+ LEncoding = GetFileEncoding (AFileName)
1066
+ if AEncoding == '':
1067
+ LEncoding = LUStrDecode.cCP1251
1068
+ LEncoding = cDefaultEncoding
1069
+ else:
1070
+ LEncoding = AEncoding
1071
+ #endif
1072
+
1073
+ if len(AStr) >= 0:
1074
+ LHandle = open (AFileName, 'a+', encoding = LEncoding)
1075
+ LHandle.write (AStr + '\n')
1076
+ LHandle.flush ()
1077
+ LHandle.close ()
1078
+ #endif
1079
+ #endfunction
1080
+
1081
+ #-------------------------------------------------------------------------------
1082
+ # OpenTextFile
1083
+ #-------------------------------------------------------------------------------
1084
+ def OpenTextFile(AFileName: str, AEncoding: str) -> int:
1085
+ """OpenTextFile"""
1086
+ #beginfunction
1087
+ s = f'OpenTextFile: {AFileName:s} ...'
1088
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
1089
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.DEBUG, s)
1090
+
1091
+ LEncoding = AEncoding
1092
+ if AEncoding == '':
1093
+ # LEncoding = LUStrDecode.cCP1251
1094
+ LEncoding = cDefaultEncoding
1095
+ #endif
1096
+ LHandle = open (AFileName, 'a+', encoding = LEncoding)
1097
+ return LHandle
1098
+ #endfunction
1099
+
1100
+ #-------------------------------------------------------------------------------
1101
+ # WriteTextFile
1102
+ #-------------------------------------------------------------------------------
1103
+ def WriteTextFile(AHandle, AStr: str):
1104
+ """WriteTextFile"""
1105
+ #beginfunction
1106
+ AHandle.write (AStr+'\n')
1107
+ AHandle.flush ()
1108
+ #endfunction
1109
+
1110
+ #-------------------------------------------------------------------------------
1111
+ # CloseTextFile
1112
+ #-------------------------------------------------------------------------------
1113
+ def CloseTextFile (AHandle):
1114
+ """CloseTextFile"""
1115
+ #beginfunction
1116
+ s = f'CloseTextFile ...'
1117
+ # LULog.LoggerTOOLS_AddLevel (logging.DEBUG, s)
1118
+ LULog.LoggerAdd (LULog.LoggerTOOLS, logging.DEBUG, s)
1119
+
1120
+ AHandle.flush ()
1121
+ AHandle.close ()
1122
+ #endfunction
1123
+
1124
+ #--------------------------------------------------------------------------------
1125
+ # SearchFile
1126
+ #--------------------------------------------------------------------------------
1127
+ def __SearchFile (ADir: str, AFileName: str, AMask: str, AExt: str, ASubDir: bool) -> []:
1128
+ """SearchFile"""
1129
+ #beginfunction
1130
+ LList = []
1131
+ if ADir == '':
1132
+ # искать в текущем каталоге
1133
+ LDir = LUos.GetCurrentDir ()
1134
+ else:
1135
+ # искать в каталоге ADir
1136
+ LDir = ExpandFileName (ADir)
1137
+ #endif
1138
+ Lhere = pathlib.Path (LDir)
1139
+ # print('LDir:', LDir)
1140
+ LFileName = ExtractFileName (AFileName)
1141
+ # print('LFileName:', LFileName)
1142
+ # print('AMask:', AMask)
1143
+ # print('AExt:', AExt)
1144
+
1145
+ if ASubDir:
1146
+ # Searching for LDir Recursively in Python
1147
+ LStr = '**/*'
1148
+ else:
1149
+ # Searching for LDir
1150
+ LStr = '*'
1151
+ #endif
1152
+ # Finding a Single File Recursively
1153
+ # LStr = here.glob ("**/something.txt")
1154
+
1155
+ Lfiles = Lhere.glob (LStr)
1156
+ for item in Lfiles:
1157
+ if item.name == LFileName:
1158
+ # print (item)
1159
+ LList.append (item)
1160
+ else:
1161
+ if CheckFileNameMask (item.name, AMask):
1162
+ # print (item)
1163
+ LList.append (item)
1164
+ else:
1165
+ if CheckFileExt (item, AExt):
1166
+ # print (item)
1167
+ LList.append (item)
1168
+ #endif
1169
+ #endif
1170
+ #endif
1171
+ #endfor
1172
+ return LList
1173
+ #endfunction
1174
+
1175
+ #--------------------------------------------------------------------------------
1176
+ # SearchFileDirs
1177
+ #--------------------------------------------------------------------------------
1178
+ def SearchFileDirs (ADirs: [], AFileName: str, AMask: str, AExt: str, ASubDir: bool) -> []:
1179
+ """SearchFileDirs"""
1180
+ #beginfunction
1181
+ LListDirs = []
1182
+ for LDir in ADirs:
1183
+ # print('LDir:', LDir)
1184
+ LList = __SearchFile (LDir, AFileName, AMask, AExt, ASubDir)
1185
+ if len(LList) > 0:
1186
+ LListDirs += LList
1187
+ #endif
1188
+ #endfor
1189
+ return LListDirs
1190
+ #endfunction
1191
+
1192
+ #--------------------------------------------------------------------------------
1193
+ # GetWindowsPath
1194
+ #--------------------------------------------------------------------------------
1195
+ def GetWindowsPath (APath: str) -> str:
1196
+ """GetWindowsPat"""
1197
+ #beginfunction
1198
+ LResult = pathlib.WindowsPath (APath)
1199
+ return LResult
1200
+ #endfunction
1201
+
1202
+ #--------------------------------------------------------------------------------
1203
+ # GetPureWindowsPath
1204
+ #--------------------------------------------------------------------------------
1205
+ def GetPureWindowsPath (APath: str) -> str:
1206
+ """GetPureWindowsPath"""
1207
+ #beginfunction
1208
+ LResult = pathlib.PureWindowsPath (APath)
1209
+ return LResult
1210
+ #endfunction
1211
+
1212
+ #-------------------------------------------------------------------------------
1213
+ # main
1214
+ #-------------------------------------------------------------------------------
1215
+ def main ():
1216
+ #beginfunction
1217
+ print('main LUFile.py ...')
1218
+ #endfunction
1219
+
1220
+ #------------------------------------------
1221
+ # module
1222
+ #------------------------------------------
1223
+ #beginmodule
1224
+ if __name__ == "__main__":
1225
+ main()
1226
+ #endif
1227
+
1228
+ #endmodule