forked from JostTim/pGenUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileio.py
More file actions
411 lines (329 loc) · 13.6 KB
/
fileio.py
File metadata and controls
411 lines (329 loc) · 13.6 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# -*- coding: utf-8 -*-
"""Boilerplate:
Created on Mon Jun 7 15:37:56 2021
@author: Timothe
"""
import os, sys
import pickle as _pickle
import configparser, json
import shutil, pathlib
#sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath("__filename__"))))
#print(os.path.dirname(os.path.dirname(os.path.abspath("__name__"))))
from structs import TwoLayerDict
from workflows import get_all_working_vars, get_glob_varname, get_main_filename
import pathes
class CustomUnpickler(_pickle.Unpickler):
def find_class(self, module, name):
"""
Intercepts a call to pickle load and check namespace of loaded variables. If one matches the name in the case structure below, returns the object holding that function direfcly, without requiring the function to be defined in main of the calling program.
It overrides the function "find_class" of the pickle unpickler library.
Parameters
----------
module : object
module_reference.
name : str
class_name.
Returns
-------
object : class_reference.
"""
try:
return eval(name)
except NameError:
return super().find_class(module, name)
class CustomPickler(_pickle.Pickler):
def find_class(self, module, name):
"""
Intercepts a call to pickle load and check namespace of loaded variables. If one matches the name in the case structure below, returns the object holding that function direfcly, without requiring the function to be defined in main of the calling program.
It overrides the function "find_class" of the pickle unpickler library.
Parameters
----------
module : object
module_reference.
name : str
class_name.
Returns
-------
object : class_reference.
"""
try:
return eval(name)
except NameError:
return super().find_class(module, name)
class Pickle():
def __init__(self,path=None):
"""
You can either enter a path (can be relative to current working dir)
or leave it blank and define global variable __filename__ in main.
Args:
path (TYPE, optional): DESCRIPTION. Defaults to None.
Returns:
None.
"""
if path is None :
path = get_main_filename()+".vars"
self.path = os.path.abspath(path)
def load(self):
if os.path.isfile(self.path):
results = []
with open(self.path,"rb") as f :
while True :
try :
results.append(CustomUnpickler(f).load())
except EOFError :
break
return results if len(results) > 1 else results[0]
return None
def dump(self,data,noiter = True):
with open(self.path,"wb") as f :
if isinstance(data, (list,tuple)) and not noiter:
for item in data :
CustomPickler(f).dump(item)# protocol = _pickle.HIGHEST_PROTOCOL ?
return None
CustomPickler(f).dump(data)
def glob_vardump(self,*variables):
saveable_struct = make_saveable_struct(*variables)
self.dump(saveable_struct)
def glob_varload(self):
loaded_struct = self.load()
unwrap_saveable_struct(loaded_struct)
def glob_varrun(self,force = False):
if os.path.isfile(self.path) and not force:
self.glob_varload()
return False
return True
def make_saveable_struct(*argvars):
savestruct = {}
if len(argvars) == 0 :
argvars = get_all_working_vars()
for var in argvars :
savestruct[get_glob_varname(var)] = var
return savestruct
def unwrap_saveable_struct(save_struct):
globalscope = vars(sys.modules["__main__"])
for k,v in save_struct.items():
globalscope[k] = v
class ConfigFile(TwoLayerDict):
def __init__(self, path, **kwargs):
"""
A class to access config files through an object with indexing,
either for geting or seting values.
Seamless and easy integration in code, ability to load or set multiple
variables at once for more readability when using in static environments.
(e.g. functions or simple classes)
If not existing, the file will be created (but folder must exist)
when key values are assigned.
Python standard variables are supported, as well as numpy arrays, internally
represented as nested lists. Avoid using complex numpy structures as they could be
erroneously loaded from file. (no specific dtypes support #TODO)
Parameters
----------
path : str
Path to the config file.
**kwargs : TYPE
DESCRIPTION.
Returns
-------
A variable which is also a handle to the file.
Can be used to load values with tho text indexes (two layer dictionary)
or set values in the same way (immediately applies changes to the text file on setting variable value)
"""
self.path = path
self.cfg = configparser.ConfigParser()
self.last_mtime = None
self.cursor = None
super(TwoLayerDict, self).__init__({})
self._read_if_changed()
def couples(self):
sections = self.sections()
result = []
[ result.extend( [ ( section, param ) for param in self.params(section) ] ) for section in sections ]
return tuple(result)
def sections(self):
return self.cfg.sections()
def params(self,section = None):
return self.cfg.options(section)
def _read_if_changed(self):
if self._filechanged :
self._read()
def pop(self,key):
retval = super().pop(key)
self._write()
return retval
def __getitem__(self,index):
self._read_if_changed()
try :
return super().__getitem__(index)
except KeyError as e:
raise KeyError(f"Key and section pair may not exist in the config structure. Cannot access value. Key causing the issue : {e}")
def __setitem__(self,key,value):
TwoLayerDict.__setitem__(self,key,value)
self._write()
def _clear_cfg(self):
for section in self.sections():
self.cfg.remove_section(section)
def _create_sections(self):
for section in self.keys():
if not section in self.sections():
self.cfg.add_section(section)
def _write(self):
"""
Question:
#TODO
Make sure we can load many variables types correctly by saving them with pickle dumping in str format. And loading from str with pickle, instead of creating a custom "key type" with json.
Or see if we can jsonize pandas dataframes easily. Could be an idea too. In that case though, we need to jsonize arrays in a better way, including dype. Need to see if numpy doesn't have that ability built in.'
Returns:
TYPE: DESCRIPTION.
"""
def jsonize_if_np_array(array):
if (array.__class__.__module__, array.__class__.__name__) == ('numpy', 'ndarray'):
value = ["np.ndarray", array.tolist()]
return value
return array
def ini_compat_json_dumps(_value):
if isinstance(value,str):
_value = _value.replace("%","%%")
return json.dumps(_value)
self._write_callback()
self._clear_cfg()
self._create_sections()
for section in self.keys():
for param in TwoLayerDict.__getitem__(self,(section,slice(None))).keys():
value = jsonize_if_np_array(super().__getitem__((section,param)))
self.cfg.set(section,param,ini_compat_json_dumps(value))
with open(self.path, 'w') as configfile:
self.cfg.write(configfile)
def _write_callback(self):
pass
def _read(self):
self.cfg.read(self.path)
super().clear()
for sec in self.sections():
TwoLayerDict.__setitem__(self, sec , {param: self._getasvar(sec,param) for param in self.params(sec) } )
self.last_mtime = os.stat(self.path).st_mtime
def _getasvar(self,section,param):
def unjsonize_if_np_array(array):
if isinstance(array,list):
if len(array) == 2 :
if array[0] == "np.ndarray":
import numpy as np
value = np.array(array[1])
return value
return array
try :
#print(section,param)
#print(self.cfg.get(section,param))
val = json.loads(self.cfg.get(section,param))
val = unjsonize_if_np_array(val)
except configparser.NoOptionError:
return None
if isinstance(val,str):
if val[0:1] == "f" :
val = val.replace("''",'"')
if isinstance(val,list):
if len(val) == 2 :
if val[0] == "np.ndarray":
val = np.array(val[1])
return val
@property
def _filechanged(self):
try :
filestatus = os.stat(self.path).st_mtime
if self.last_mtime is None or self.last_mtime != filestatus:
self.last_mtime = filestatus
return True
except FileNotFoundError :
pass
return False
def paste_dir_content(src, dst, include_root_files : bool = True , copy : bool = True ):
def recursive_copy(s,d, symlinks=False, ignore=None):
try :
shutil.copytree(s, d, symlinks, ignore) if os.path.isdir(s) else shutil.copy2(s, d)
except FileExistsError :
pass
operating_function = recursive_copy if copy else shutil.move
if pathlib.Path(src).drive == pathlib.Path(dst).drive :
def produce_distant_path(src_name):
return pathes.switch_root(src_name,src,dst)
else :
def produce_distant_path(src_name):
return os.path.join(dst, pathes.remove_common_prefix(src_name,src))
def operate_on_items(item_producing_function):
for src_item in item_producing_function(src):
dst_item = produce_distant_path(src_item)
pathes.is_or_makedir(os.path.dirname(dst_item))
operating_function(src_item, dst_item)
operate_on_items(pathes.list_toplevel_dirs)
if include_root_files :
operate_on_items(pathes.list_toplevel_files)
# def __FilepathResolverConfigFile(file_path,**kwargs):
# foldup = kwargs.get("foldup",False)
# if foldup :
# file_path = UpFolder(file_path,foldup)
# filename = kwargs.get("filename","config.txt")
# if filename != "config.txt" or os.path.splitext(file_path)[0] == file_path :
# file_path = os.path.join(file_path, filename)
# if not os.path.isfile(file_path) :
# raise OSError(f"File not found : {file_path}")
# return file_path
# def GetAllParamsConfigFile(file_path,section,**kwargs):
# file_path = __FilepathResolverConfigFile(file_path,**kwargs)
# cfg = configparser.ConfigParser()
# cfg.read(file_path)
# return cfg.options(section)
# def GetAllSectionsConfigFile(file_path,**kwargs):
# file_path = __FilepathResolverConfigFile(file_path,**kwargs)
# cfg = configparser.ConfigParser()
# cfg.read(file_path)
# return cfg.sections()
# def CheckConfigFile(file_path,sections,**kwargs):
# file_path = __FilepathResolverConfigFile(file_path,**kwargs)
# cfg = configparser.ConfigParser()
# cfg.read(file_path)
# if not isinstance(sections , list):
# sections = [sections]
# for section in sections :
# if not cfg.has_section(section):
# cfg.add_section(section)
# with open(file_path, "w") as file_handle :
# cfg.write(file_handle)
# def LoadConfigFile(file_path,section,param,**kwargs):
# file_path = __FilepathResolverConfigFile(file_path,**kwargs)
# cfg = configparser.ConfigParser()
# cfg.read(file_path)
# try :
# val = json.loads(cfg.get(section,param))
# except configparser.NoOptionError:
# return None
# if isinstance(val,str):
# if val[0:1] == "f" :
# val = val.replace("''",'"')
# if isinstance(val,list):
# if len(val) == 2 :
# if val[0] == "np.ndarray":
# import numpy as np
# val = np.array(val[1])
# return val
# def WriteToConfigFile(file_path,section,param,value,**kwargs):
# file_path = __FilepathResolverConfigFile(file_path,**kwargs)
# cfg = configparser.ConfigParser()
# cfg.read(file_path)
# if (value.__class__.__module__, value.__class__.__name__) == ('numpy', 'ndarray'):
# value = ["np.ndarray", value.tolist()]
# cfg.set(section, param , json.dumps(value))
# with open(file_path, "w") as file_handle :
# cfg.write(file_handle)
if __name__ == "__main__":
import sys
test = ConfigFile(r"\\157.136.60.15\EqShulz\Timothe\DATA\BehavioralVideos\Whole_area\Low_speed_triggered\gateway.ini")
print(test["outer_architecture","dateformat"])
sys.exit()
import numpy as np
#test = ConfigFile(r"\\157.136.60.15\EqShulz\Timothe\DATA\DataProcessing\Expect_3_mush\CrossAnimals\SpatialScale\scale.txt")
test = ConfigFile(r"test.config")
test["foo","zbo"]=12
test["foo","zbi"]="adas"
test["flee","moulaga"]=142.13
test["flee","ratata"]= np.array([[1,3],[4,56]])
print(test["flee","ratata"])
print(test.couples())