-
Notifications
You must be signed in to change notification settings - Fork 73
/
config_tools.py
executable file
·255 lines (180 loc) · 7.51 KB
/
config_tools.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
""" This module contains tools for configuring the solcore installation, adding/removing sources with material data,
etc.
"""
from configparser import ConfigParser
import os
import shutil
import solcore
import glob
home_folder = os.path.expanduser('~')
user_config = os.path.join(home_folder, '.solcore_config.txt')
user_config_data = ConfigParser()
user_config_data.read(user_config)
default_config_data = ConfigParser()
default_config_data.read(solcore.default_config)
def reset_defaults(confirm=False):
""" Resets the default Solcore configuration in the user home folder.
:return: None
"""
global user_config_data
if not confirm:
response = input('This action will delete any customisation of the Solcore configuration. '
'Are you sure you want to continue (Y/n)?')
if response in 'Yy':
confirm = True
if confirm:
shutil.copy2(solcore.default_config, user_config)
user_config_data.read(user_config)
user_config_data.remove_option(section='Configuration', option='version')
save_user_config()
print('Default Solcore configuration has been restored!')
def save_user_config():
""" Saves the current user configuration
:return: None
"""
with open(user_config, 'w') as fp:
user_config_data.write(fp)
def remove_source(section, name):
""" General function to remove sources from the configuration files. It checks if the source exists and, if so, if
it is a default Solcore source. In the later case, it disable the source by setting it to an empty string rather
than removing it.
:param section: The section to remove a source from.
:param name: The name of the source.
:return: None
"""
if name not in user_config_data.options(section):
print('Source {} does not exists. Valid options are: {}'.format(name, user_config_data.options(section)))
return
if name in default_config_data.options(section):
print('Source {} is a default Solcore source. It can not be removed and will be disabled instead.'.format(name))
add_units_source(name, '')
return
user_config_data.remove_option(section, name)
save_user_config()
print('{}:{} source removed!'.format(section, name))
def add_source(section, name, location):
""" General function to add sources to the configuration files. If the source already exists, its value will be
replaced by the new one.
:param section: The section to add a source to.
:param name: The name of the source.
:return: None
"""
user_config_data.set(section, name, value=location)
save_user_config()
print('{}:{} source added!'.format(section, name))
def restore_default_source(section, name):
""" Restores the default value of a source, assuming the source has a default value.
:param section: The section of the source.
:param name: The name of the source.
:return: None
"""
if name not in default_config_data.options(section):
print('There is no default value for {} source: {}. '.format(section, name))
print('Available default {} sources are: {}'.format(section, default_config_data.options(section)))
return
user_config_data[section][name] = default_config_data[section][name]
save_user_config()
print('Default Solcore value for {} source {} has been restored!'.format(section, name))
def add_units_source(name, location):
""" Adds a Units source to Solcore.
:param name: The name of the source.
:param location: The full path to the location of the source. The source must be a ConfigParser formatted file.
:return: None
"""
add_source('Units', name, location=location)
def add_parameters_source(name, location):
""" Adds a Parameters source to Solcore.
:param name: The name of the source.
:param location: The full path to the location of the source. The source must be a ConfigParser formatted file.
:return: None
"""
add_source('Parameters', name, location=location)
def add_materials_source(name, location):
""" Adds a Materials source to Solcore.
:param name: The name of the source.
:param location: The full path to the location of the source. The source must be a folder containing the n and k
data of the material. See the the Material System in the manual for more information.
:return: None
"""
add_source('Materials', name, location=location)
def remove_units_source(name):
""" Removes a Units source from Solcore.
:param name: The name of the source.
:return: None
"""
remove_source('Units', name)
def remove_parameters_source(name):
""" Removes a Parameters source from Solcore.
:param name: The name of the source.
:return: None
"""
remove_source('Parameters', name)
def remove_materials_source(name):
""" Removes a Materials source from Solcore.
:param name: The name of the source.
:return: None
"""
remove_source('Materials', name)
def restore_default_units_source(name):
""" Restores the default value of a Units source from Solcore.
:param name: The name of the source.
:return: None
"""
restore_default_source('Units', name)
def restore_default_parameters_source(name):
""" Restores the default value of a Parameters source from Solcore.
:param name: The name of the source.
:return: None
"""
restore_default_source('Parameters', name)
def restore_default_materials_source(name):
""" Restores the default value of a Materials source from Solcore.
:param name: The name of the source.
:return: None
"""
restore_default_source('Materials', name)
def welcome_message(show):
""" Sets if the welcome message must be shown or not
:param show: True/False for showing/hiding the welcome message
:return: None
"""
user_config_data['Configuration']['welcome_message'] = int(show)
def verbose_loading(show):
""" Sets if the loading messages (besides the welcome message) must be shown or not
:param show: True/False for showing/hiding the loading messages
:return: None
"""
user_config_data['Configuration']['verbose_loading'] = int(show)
def set_location_of_spice(location):
""" Sets the location of the spice executable. It does not test if it works.
:param location: The location of the spice executable.
:return: None
"""
user_config_data['External programs']['spice'] = location
save_user_config()
def set_location_of_smarts(location):
""" Sets the location of the SMARTS distribution (the root folder). It does not test if it works.
:param location: The location of the SMARTS distribution.
:return: None
"""
user_config_data['External programs']['smarts'] = location
save_user_config()
def get_current_config():
""" Prints the current Solcore configuration
:return: None
"""
for section in user_config_data.sections():
print('[{}]'.format(section))
for option in user_config_data.options(section):
print('{} = {}'.format(option, user_config_data.get(section, option)))
print()
def check_user_config():
""" Checks if there's a user configuration file, asking if it needs to be created.
:return: None
"""
if len(user_config_data.sections()) == 0:
response = input('No user configuration was detected. Do you want to create one (Y/n)?')
if response in 'Yy':
reset_defaults(True)
user_config_data = ConfigParser()
user_config_data.read(user_config)