Repository navigation
Expand file tree
/
Copy pathproject_config.py
More file actions
290 lines (237 loc) · 8.28 KB
/
Copy pathproject_config.py
File metadata and controls
290 lines (237 loc) · 8.28 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
#!/usr/bin/env python
""" Functions to get/save configuration information for the program.
Intended to be used on YAML configuraiton files for all kinds
of OS-es.
Author: Tomio
Date: 2023-01
License: MIT
Warranty: None
"""
import os
import sys
import time
import yaml
def get_config_dir() -> str:
""" Based on the OS, get the /home/$user/.config/rdm_project
or the c:/users/%user%/AppData/Local folder.
"""
uname = sys.platform
if uname.startswith('win'):
homedir = os.getenv('localAppData')
dirname = os.path.join(homedir, 'rdm_project')
else:
# 'linux', 'darwin' or anything similar
homedir = os.getenv('HOME')
dirname = os.path.join(homedir, '.config/rdm_project')
return dirname
# end get_config_dir
def get_default_config() -> dict:
""" Read some system variables to set some default values
for the configuration.
Read the ../templates/default_configuration if possible,
or go for default values.
return value
a dict containing the configuration parameters.
"""
version = 0.95
if os.name == 'posix':
home_dir = os.getenv('HOME')
editor='nano'
filemanager='xdg-open'
else:
# let us try come up with a preference list
# for default location:
# we can default to:
# os.getenv('HOMEDRIVE')
# os.getenv('HOMEDPATH')
home_dir = 'd:/' if os.path.isdir('d:/')\
else os.path.join(os.getenv('HOMEPATH'), 'Documents')
editor= 'notepad'
filemanager= 'explorer'
#template_dir = os.path.join(config_dir, 'Templates')
template_dir = '../templates'
# now, get the YAML file if it exists:
template_path = os.path.join(template_dir,
'default_configuration')
if os.path.isfile(template_path):
with open(template_path, 'rt', encoding='UTF-8') as fp:
custom_config= yaml.safe_load(fp)
# projects_dir = os.path.join(home_dir, 'Projects')
userID = os.getlogin()
# we may still need:
# - readme template (MD files)
# - default template for forms
default_config= {
'userID': userID,
'editor': editor,
'version': version,
'homeDir': home_dir,
'save config': True,
'templateDir': template_dir,
'projectDir': 'Projects',
'readme': 'readme.md',
'filemanager': filemanager,
'use form': True,
'chemicals': 'Chemicals',
'equipment': 'Equipment',
'ignore': ['References', 'Chemicals', 'Equipment'],
# for connecting to an existing upload source,
# we may have a default configuration here
# first server link, then the token
'server': {'server':'', 'token':''},
'projectsTitle': 'Projects',
# the search fields specify how to
# handle the folder tree...
# top is the projects, then samples,
# then experiments
# we start with 'projectDir' within:
'searchFolders': [
'',
'Data',
''],
'searchNames': [
'project',
'sample',
'experiment'],
'searchTargets': [
'dir',
'dir',
'file'],
'searchPattern': [
'',
'',
'yaml$'],
'templates': [
'folderlist',
'',
''],
# default template is loaded for any
# experiment,
# for directories it is a default Readme.md
# content to start with
'defaultTemplate': [
'readmeProject',
'readmeSample',
'defaultForm']
}
# take over the custom settings, if there is any:
if custom_config:
default_config.update(custom_config)
# now, clean up:
home_dir = default_config.pop('homeDir')
default_config['projectDir'] = os.path.join(
home_dir,
default_config['projectDir']
)
for k in ['chemicals', 'equipment']:
dck = default_config[k]
if not (dck.startswith('/')
or dck[1:].startswith(':/')
or dck[1:].startswith(':\\')):
# relative path is specified
default_config[k] = os.path.join(
default_config['projectDir'],
dck)
# end cleaning up paths
return(default_config)
# end get_default_config
def get_config() -> dict:
""" Find the config folder, and try pulling up the configuration.
Substitute non-existing values with their default counterpart.
Read the ../templates/default_configuration if possible,
or go for default values.
return
a dict with the configuration
"""
# is there a configuration already saved?
config_dir= get_config_dir()
config_path = os.path.join(config_dir, 'config.yaml')
if not os.path.isfile(config_path):
print('Configuration not found')
conf = get_default_config()
if ('save config' in conf
and conf['save config']):
print('Saving default configuration')
save_config(conf)
else:
print('configuration is not saved')
return conf
with open(config_path,
'rt',
encoding='utf8') as fp:
conf = yaml.safe_load(fp)
# we got a config
# get the default and check out what is missing
def_conf = get_default_config()
# if there was a version change, we must
# update the cofiguration
if 'version' not in conf\
or conf['version'] != def_conf['version']:
must_save = True
# update the version!
conf['version'] = def_conf['version']
else:
must_save = False
for k,v in def_conf.items():
if k not in conf:
conf[k] = v
if must_save:
save_config(conf)
return conf
# end get_config
def load_config() -> dict:
""" it is get_config, just for making sure we do not
miss the name
"""
return get_config()
# end load_config
def save_config(config: dict) -> None:
""" take a dict, and write it to the default config
location as YAML file made by pyyaml.
"""
if not config:
return
config_dir = get_config_dir()
if not os.path.isdir(config_dir):
os.makedirs(config_dir)
with open(os.path.join(config_dir, 'config.yaml'),
'wt',
encoding='utf8') as fp:
yaml.dump(config, fp)
# end save_config
def replace_text(text: str, config: dict, root_path: str) -> str:
""" search for specific replacement characters and fill them up
with dynamic content.
Current spacers:
%1, %2, %3: project name, Data, sample name from the
folder name
%u userID from config
%d current date in ISO 8601 format
%D current date, time and time zone in ISO
8601 format.
Parameters:
text string the text to be scanned
config dict configuration dict, we use the project
path, userID from it
root_path string the path of the current object
splitting it up, we get names of the
current project and sample (typically)
return:
string with spacers substituted
"""
if '%' not in text:
return text
relpath = os.path.relpath(root_path, config['projectDir'])
# windows may have '\\' instead of '/'
if '\\' in relpath:
relpath = relpath.split('\\')
else:
relpath = relpath.split('/')
rep = {f'%{i+1}':j for i,j in enumerate(relpath)}
rep['%u'] = config['userID']
rep['%d'] = time.strftime('%Y-%m-%d', time.localtime())
rep['%D'] = time.strftime('%Y-%m-%d %H:%M %z', time.localtime())
for i,v in rep.items():
text= text.replace(i, v)
return text
# end of replace_text