-
Notifications
You must be signed in to change notification settings - Fork 2
/
gsyINI.py
293 lines (200 loc) · 8.54 KB
/
gsyINI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# -*- coding: utf-8 -*-
"""
Custom module for INI file read/write.
Author : 高斯羽 博士 (Dr. GAO, Siyu)
Version : 0.1.3
Last Modified : 2017-11-27
List of functions
----------------------
* read_ini_
* read_ini_to_tb_
* write_ini_
Function definitions
----------------------
"""
from gsyIO import date_time_now
# =============================================================================
# <Read INI file by line return the strings as a list>
# =============================================================================
def read_ini(locStr_ini_file_path):
"""
.. _read_ini :
This funciton reads the given INI file by line and return the read line
as a list object. This does NOT manipulate the read contents.
Parameters
----------
locStr_ini_file_path : str
The INI file full path.
Returns
-------
bool
Returns True if read successful (no exception). Returns False on exception.
locConfig : list or int
Returns the INI read by line contents if read successful.
Returns 0 when read fail.
Examples
--------
>>> read_ini(r'C:\some.ini')
2017-11-23, 11:13:38:Reading INI file...
2017-11-23, 11:13:38:Read INI file complete
(True,
['[User configurations]\\n',
'InputHarmonicOrder=1.7\\n',
'InputPLLOrder=1\\n',
'Samples=200\\n',
'FPS=30\\n',
'BaseFreq=50\\n',
'FFmpegpath=\\n'])
"""
print(date_time_now() + 'Reading INI file...')
try:
# open ini file
locIni_file = open(locStr_ini_file_path, 'r')
# read by lines
locConfig = locIni_file.readlines()
print(date_time_now() + 'Read INI file complete')
return (True, locConfig)
except:
print(date_time_now() + 'Fail to read INI file.')
locConfig = 0
return (False, locConfig)
# =============================================================================
# </Read INI file by line return the strings as a list>
# =============================================================================
# =============================================================================
# <Function: read ini file and put the configs to text boxes>
# =============================================================================
def read_ini_to_tb(locStr_ini_file_path, locList_textbox):
"""
.. _read_ini_to_tb :
Read a INI file and assign the corresponding elements to the matplotlib
textboxes.
This function would assign the INI element value (right of the "=" sign in
the INI file) to the textbox if the label string of the textbox is the same
as the INI element name (left of the "=" sign in the INI file).
The input list for textbox would be copied by value into a new list object
first to prevent modification of the original list.
.. code:: python
locTemp_textbox = list(locList_textbox)
If the INI element is found, the corresponding textbox is removed from
locTemp_textbox to make the search faster.
Parameters
------------
locStr_ini_file_path : string
The path for the INI file.
locList_textbox : list (containing all the matplotlib textboxes)
The list of textboxes to be filled.
Returns
-------
bool
Returns True if deemed successful (no exception). Returns False if deemed
unsuccessful (on exception).
locConfig : list or int
Each element in the list is a line of content read from the given INI
file if read successful.
Returns 0 when read fail.
Examples
--------
>>> read_ini_to_tb(r'C:\some.ini', list_textbox)
2017-11-23, 10:33:29:Reading INI file...
2017-11-23, 10:33:29:Read INI file complete
2017-11-23, 10:33:29:Setting config : InputHarmonicOrder=2
2017-11-23, 10:33:29:Setting config : InputPLLOrder=1
2017-11-23, 10:33:29:Setting config : Samples=300
2017-11-23, 10:33:29:Setting config : FPS=30
2017-11-23, 10:33:29:Setting config : BaseFreq=50
2017-11-23, 10:33:29:Setting config : FFmpegpath=
"""
try:
# call function to read INI file
bool_ini, locConfig = read_ini(locStr_ini_file_path)
# if read INI fale, exit this function
if bool_ini == False:
return (False, locConfig)
# get of the line break for each line
locConfig = [item.strip('\n') for item in locConfig]
locConfig = [item.strip('\r') for item in locConfig]
# copy the values of the list, otherwise the original list object would be modified
locTemp_textbox = list(locList_textbox)
# double for-loop start
for i in locConfig:
locIndex = i.find('=')
# keep the left of the '='
locStr_temp_config = i[:locIndex]
for j in locTemp_textbox:
# strip chars from the labels
locStr_temp_label = (j.label.get_text()
.replace(' ', '')
.replace('\n', '')
.replace('\r', '')
.replace(':', ''))
if locStr_temp_config == locStr_temp_label:
# get the right part of the '=' and assgin it to the text boxes
j.set_val(i[(locIndex+1):])
print(date_time_now() + 'Setting config : ' + i)
# if found, remove from list
locTemp_textbox.pop(locTemp_textbox.index(j))
break
else:
pass
# double for-loop end
return (True, locConfig)
except:
print(date_time_now() + 'INI file read failed')
locConfig = 0
return (False, locConfig)
# =============================================================================
# </Function: read ini file and put the configs to text boxes>
# =============================================================================
# =============================================================================
# <Function: write ini file>
# =============================================================================
def write_ini(locStr_ini_file_path, locStr_ini):
"""
.. _write_ini :
Write the given string into the given INI file path.
Parameters
----------
locStr_ini_file_path : str
The file full path of the INI file. If the extension ".ini" is not included,
it would be added to the path.
locStr_ini : str
The string to be written into the INI file.
Returns
-------
bool
Returns True if deemed successful (no exception). Returns False if deemed
unsuccessful (on exception).
Examples
--------
>>> write_ini('C:\\Temp\\testini', '[User configurations]\\nsome string')
2017-11-21, 16:24:40:INI file save start
2017-11-21, 16:24:40:INI file save complete
Out[51]: True
Content of the INI file would be:
| '[User configurations]
| some string'
"""
print(date_time_now() + 'INI file save start')
try:
# check whether the INI file path ends with '.ini' (case insensitive)
if locStr_ini_file_path[-4:].lower() == '.ini':
# if yes, pass
pass
else:
# if no, append
locStr_ini_file_path = locStr_ini_file_path + '.ini'
# open the INI for write
locIni_file = open(locStr_ini_file_path, 'w')
# write the string into the INI
locIni_file.write(locStr_ini)
# close the INI file
locIni_file.close()
print(date_time_now() + 'INI file save complete')
return True
except:
print(date_time_now() + 'INI file save failed')
return False
# =============================================================================
# </Function: write ini file>
# =============================================================================