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/LUParserINI.py ADDED
@@ -0,0 +1,371 @@
1
+ """LUParserINI.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
+ LUParserINI.py
13
+
14
+ =======================================================
15
+ """
16
+
17
+ #------------------------------------------
18
+ # БИБЛИОТЕКИ python
19
+ #------------------------------------------
20
+ import configparser
21
+ import logging
22
+
23
+ #------------------------------------------
24
+ # БИБЛИОТЕКИ сторонние
25
+ #------------------------------------------
26
+
27
+ #------------------------------------------
28
+ # БИБЛИОТЕКА LU
29
+ #------------------------------------------
30
+ import lyr.LULog as LULog
31
+ import lyr.LUConst as LUConst
32
+ import lyr.LUFile as LUFile
33
+ import lyr.LUos as LUos
34
+ import lyr.LUStrDecode as LUStrDecode
35
+ import lyr.LUStrUtils as LUStrUtils
36
+
37
+ class TINIFile (configparser.ConfigParser):
38
+ """TINIFile"""
39
+ luClassName = 'TINIFile'
40
+
41
+ #--------------------------------------------------
42
+ # constructor
43
+ #--------------------------------------------------
44
+ """
45
+ class configparser.ConfigParser (
46
+ defaults=None,
47
+ dict_type=dict,
48
+ allow_no_value=False,
49
+ delimiters=('=', ':'),
50
+ comment_prefixes=('#', ';'),
51
+ inline_comment_prefixes=None,
52
+ strict=True,
53
+ empty_lines_in_values=True,
54
+ default_section=configparser.DEFAULTSECT,
55
+ interpolation=BasicInterpolation(),
56
+ converters={}
57
+ )
58
+ """
59
+ @staticmethod
60
+ def __GetINIFileName (AFileName: str) -> str:
61
+ """__GetINIFileName"""
62
+ #beginfunction
63
+ P = LUFile.ExtractFileDir (AFileName)
64
+ F = LUFile.ExtractFileName (AFileName)
65
+ E = LUFile.ExtractFileExt (AFileName)
66
+ if E == '':
67
+ F = F + '.ini'
68
+ #endif
69
+ if P == '':
70
+ LWinDir = LUos.GetEnvVar (LUos.cWINDIR)
71
+ APath = LUos.GetCurrentDir () + ';' + LWinDir
72
+ LResult = LUFile.FileSearch (F, APath)
73
+ if LResult == '':
74
+ LResult = LUFile.IncludeTrailingBackslash (LWinDir) + F
75
+ #endif
76
+ else:
77
+ LResult = LUFile.ExpandFileName (AFileName)
78
+ #endif
79
+ return LResult
80
+ #endfunction
81
+
82
+ def __init__ (self, empty_lines_in_values=True, **kwargs): # allow_no_value=True
83
+ """Constructor"""
84
+ #beginfunction
85
+ super ().__init__ (empty_lines_in_values=True, **kwargs)
86
+ self.__FSectionName: str = ''
87
+ self.__FOptionName: str = ''
88
+ self.__FOptionValue: str = ''
89
+ self.__FChangedFileINI: bool = False
90
+ self.__FFileNameINI: str = ''
91
+ #endfunction
92
+
93
+ #--------------------------------------------------
94
+ # destructor
95
+ #--------------------------------------------------
96
+ def __del__ (self):
97
+ """destructor"""
98
+ #beginfunction
99
+ LClassName = self.__class__.__name__
100
+ s = '{} уничтожен'.format (LClassName)
101
+ # LULog.LoggerTOOLS_AddLevel (LULog.DEBUGTEXT, s)
102
+ #print (s)
103
+ #endfunction
104
+
105
+ #--------------------------------------------------
106
+ # @property ConfigParser
107
+ #--------------------------------------------------
108
+ # getter
109
+ @property
110
+ def ConfigParser(self) -> configparser.ConfigParser:
111
+ #beginfunction
112
+ return self
113
+ #endfunction
114
+
115
+ #--------------------------------------------------
116
+ # @property Sections
117
+ #--------------------------------------------------
118
+ # getter
119
+ @property
120
+ def Sections(self):
121
+ #beginfunction
122
+ return self.sections()
123
+ #endfunction
124
+
125
+ #--------------------------------------------------
126
+ # @property Options
127
+ #--------------------------------------------------
128
+ # getter
129
+ @property
130
+ def Options(self):
131
+ #beginfunction
132
+ return self.options(self.SectionName)
133
+ #endfunction
134
+
135
+ #--------------------------------------------------
136
+ # @property SectionName
137
+ #--------------------------------------------------
138
+ # getter
139
+ @property
140
+ def SectionName(self):
141
+ #beginfunction
142
+ return self.__FSectionName
143
+ #endfunction
144
+ # setter
145
+ @SectionName.setter
146
+ def SectionName (self, AValue: str):
147
+ #beginfunction
148
+ self.__FSectionName = AValue
149
+ #endfunction
150
+
151
+ #--------------------------------------------------
152
+ # @property OptionName
153
+ #--------------------------------------------------
154
+ # getter
155
+ @property
156
+ def OptionName(self):
157
+ #beginfunction
158
+ return self.__FOptionName
159
+ #endfunction
160
+ # setter
161
+ @OptionName.setter
162
+ def OptionName (self, AValue: str):
163
+ #beginfunction
164
+ self.__FOptionName = AValue
165
+ #endfunction
166
+
167
+ #--------------------------------------------------
168
+ # @property OptionValue
169
+ #--------------------------------------------------
170
+ # getter
171
+ @property
172
+ def OptionValue(self):
173
+ #beginfunction
174
+ self.__FOptionValue = self.get(self.SectionName, self.OptionName)
175
+ return self.__FOptionValue
176
+ #endfunction
177
+ # setter
178
+ @OptionValue.setter
179
+ def OptionValue (self, AValue: str):
180
+ #beginfunction
181
+ self.__FOptionValue = AValue
182
+ self.set(self.SectionName, self.OptionName, AValue)
183
+ #endfunction
184
+
185
+ #--------------------------------------------------
186
+ # @property FileNameINI
187
+ #--------------------------------------------------
188
+ # getter
189
+ @property
190
+ def FileNameINI(self):
191
+ #beginfunction
192
+ return self.__FFileNameINI
193
+ #endfunction
194
+ # setter
195
+ @FileNameINI.setter
196
+ def FileNameINI (self, AValue: str):
197
+ #beginfunction
198
+ LFullFileName = self.__GetINIFileName (AValue)
199
+ if not LUFile.FileExists (LFullFileName):
200
+ LFullFileName = LUFile.ExpandFileName (LUFile.ExtractFileName(AValue))
201
+ LUFile.CreateTextFile (LFullFileName, '', LUStrDecode.cCP1251)
202
+ self.__FFileNameINI = LFullFileName
203
+ self.__OpenFileINI ()
204
+ self.ChangedFileINI = False
205
+ #endfunction
206
+
207
+ #--------------------------------------------------
208
+ # @property ChangedFileINI
209
+ #--------------------------------------------------
210
+ # getter
211
+ @property
212
+ def ChangedFileINI(self):
213
+ #beginfunction
214
+ return self.__FChangedFileINI
215
+ #endfunction
216
+ # setter
217
+ @ChangedFileINI.setter
218
+ def ChangedFileINI (self, AValue: bool):
219
+ #beginfunction
220
+ self.__FChangedFileINI = AValue
221
+ #endfunction
222
+
223
+ def __OpenFileINI (self):
224
+ """__OpenFileINI"""
225
+ #beginfunction
226
+ self.read (self.FileNameINI)
227
+ #endfunction
228
+
229
+ def IsSection (self, ASectionName: str) -> bool:
230
+ """IsSection"""
231
+ #beginfunction
232
+ return self.has_section(ASectionName)
233
+ #endfunction
234
+
235
+ def IsOption (self, ASectionName: str, AOption: str) -> bool:
236
+ """IsOption"""
237
+ #beginfunction
238
+ return self.has_option(ASectionName, AOption)
239
+ #endfunction
240
+
241
+ def UpdateFileINI (self):
242
+ """UpdateFileINI"""
243
+ #beginfunction
244
+ if self.ChangedFileINI:
245
+ with open (self.FileNameINI, 'w', encoding = LUStrDecode.cCP1251) as LFileINI:
246
+ self.write (LFileINI)
247
+ #endwith
248
+ self.__OpenFileINI ()
249
+ #endif
250
+ #endfunction
251
+
252
+ def RefreashOption (self):
253
+ """RefreashOption"""
254
+ #beginfunction
255
+ self.__OpenFileINI ()
256
+ #endfunction
257
+
258
+ def GetOption (self, ASectionName: str, AOptionName: str, AValueDefault):
259
+ """GetOption"""
260
+ #beginfunction
261
+ if self.has_section(ASectionName):
262
+ if type(AValueDefault) == int:
263
+ try:
264
+ i = self.getint (ASectionName, AOptionName)
265
+ except:
266
+ i = AValueDefault
267
+ #endtry
268
+ return i
269
+ elif type(AValueDefault) == bool:
270
+ s = self.get(ASectionName, AOptionName)
271
+ return LUStrUtils.strtobool(s)
272
+ elif type(AValueDefault) == float:
273
+ try:
274
+ f = self.getfloat (ASectionName, AOptionName)
275
+ except:
276
+ f = AValueDefault
277
+ #endtry
278
+ return f
279
+ else:
280
+ s = self.get(ASectionName, AOptionName)
281
+ return s
282
+ #endif
283
+ else:
284
+ return AValueDefault
285
+ #endif
286
+ #endfunction
287
+
288
+ def SetOption (self, ASectionName: str, AOptionName: str, AValue):
289
+ """SetOption"""
290
+ #beginfunction
291
+ if not self.has_section(ASectionName):
292
+ self.add_section (ASectionName)
293
+ #endif
294
+ s = ''
295
+ if type(AValue) == int:
296
+ try:
297
+ s = str (AValue)
298
+ self.ChangedFileINI = True
299
+ except:
300
+ self.ChangedFileINI = False
301
+ #endtry
302
+ elif type(AValue) == bool:
303
+ s = LUStrUtils.booltostr (AValue)
304
+ self.ChangedFileINI = True
305
+ elif type(AValue) == float:
306
+ try:
307
+ s = str (AValue)
308
+ self.ChangedFileINI = True
309
+ except:
310
+ self.ChangedFileINI = False
311
+ #endtry
312
+ else:
313
+ s = AValue
314
+ self.ChangedFileINI = True
315
+ #endif
316
+ if self.ChangedFileINI:
317
+ self.set (ASectionName, AOptionName, s)
318
+ self.UpdateFileINI ()
319
+ #endif
320
+ #endfunction
321
+
322
+ def DeleteSection (self, ASectionName: str):
323
+ """DeleteSection"""
324
+ #beginfunction
325
+ if self.IsSection (ASectionName):
326
+ self.remove_section(ASectionName)
327
+ self.UpdateFileINI ()
328
+ #endif
329
+ self.ChangedFileINI = True
330
+ #endfunction
331
+
332
+ def DeleteOption (self, ASectionName: str, AOptionName: str):
333
+ """DeleteOption"""
334
+ #beginfunction
335
+ if self.IsOption (ASectionName, AOptionName):
336
+ self.remove_option(ASectionName, AOptionName)
337
+ self.UpdateFileINI ()
338
+ #endif
339
+ self.ChangedFileINI = True
340
+ #endfunction
341
+
342
+ #endclass
343
+
344
+ #---------------------------------------------------------
345
+ # CreateTINIFile
346
+ #---------------------------------------------------------
347
+ def CreateTINIFile () -> TINIFile:
348
+ """CreateTINIFile"""
349
+ #beginfunction
350
+ return TINIFile ()
351
+ #endfunction
352
+
353
+ GINIFile = CreateTINIFile ()
354
+
355
+ #---------------------------------------------------------
356
+ # main
357
+ #---------------------------------------------------------
358
+ def main ():
359
+ #beginfunction
360
+ ...
361
+ #endfunction
362
+
363
+ #------------------------------------------
364
+ #
365
+ #------------------------------------------
366
+ #beginmodule
367
+ if __name__ == "__main__":
368
+ main()
369
+ #endif
370
+
371
+ #endmodule