36
36
intver = lambda vs : sum ([int (i ) for i in vs .split ('.' )[0 :2 ]]* np .array ((1000 ,1 )))
37
37
38
38
def GetConfigValue (key ,default = None ,getDefault = False ):
39
- '''Return the configuration file value for key or a default value if not present
40
-
41
- :param str key: a value to be found in the configuration (config.py) file
42
- :param default: a value to be supplied if none is in the config file or
43
- the config file is not found. Defaults to None
39
+ '''Return the configuration file value for key or a default value
40
+ if not specified.
41
+
42
+ :param str key: a value to be found in the configuration settings
43
+ :param any default: a value to be supplied if a value for key is
44
+ not specified in the config file or the config file is not found.
45
+ Defaults to None.
46
+ :param bool getDefault: If True looks up the default value from the
47
+ config_example.py file (default value is False). Do not specify a
48
+ getDefault=True if a value is provided for default.
44
49
:returns: the value found or the default.
45
50
'''
46
51
if getDefault :
@@ -938,7 +943,8 @@ def runScript(cmds=[], wait=False, G2frame=None):
938
943
939
944
def IPyBreak_base (userMsg = None ):
940
945
'''A routine that invokes an IPython session at the calling location
941
- This routine is only used when debug=True is set in config.py
946
+ This routine is only used when debug=True is set in the configuration
947
+ settings
942
948
'''
943
949
savehook = sys .excepthook # save the exception hook
944
950
try :
@@ -970,8 +976,8 @@ def exceptHook(*args):
970
976
'''A routine to be called when an exception occurs. It prints the traceback
971
977
with fancy formatting and then calls an IPython shell with the environment
972
978
of the exception location.
973
-
974
- This routine is only used when debug=True is set in config.py
979
+
980
+ This routine is only used when debug=True is set in the configuration settings
975
981
'''
976
982
try :
977
983
from IPython .core import ultratb
@@ -1012,7 +1018,7 @@ class c(object): pass
1012
1018
1013
1019
def DoNothing ():
1014
1020
'''A routine that does nothing. This is called in place of IPyBreak and pdbBreak
1015
- except when the debug option is set True in config.py
1021
+ except when the debug option is set True in the configuration settings
1016
1022
'''
1017
1023
pass
1018
1024
@@ -1127,7 +1133,8 @@ def SetBinaryPath(showConfigMsg=False):
1127
1133
BinaryPathFailed = pathHacking ._path_discovery (showConfigMsg )
1128
1134
1129
1135
def LoadConfig (printInfo = True ):
1130
- # setup read of config.py, if present
1136
+ '''Read configuration settings from config.py, if present
1137
+ '''
1131
1138
global configDict
1132
1139
try :
1133
1140
import config
@@ -1146,6 +1153,121 @@ def LoadConfig(printInfo=True):
1146
1153
print (60 * '*' )
1147
1154
configDict = {'Clip_on' :True }
1148
1155
1156
+ def XferConfigIni ():
1157
+ '''copy the contents of the config.py file to file ~/.GSASII/config.ini.
1158
+ This is not currently in use in GSAS-II
1159
+ '''
1160
+ import types
1161
+ configDict = {}
1162
+ try :
1163
+ import config
1164
+ #import config_example as config
1165
+ for i in config .__dict__ :
1166
+ if i .startswith ('__' ) and i .endswith ('__' ): continue
1167
+ if isinstance (config .__dict__ [i ],types .ModuleType ): continue
1168
+ configDict .update ({i :str (config .__dict__ [i ])})
1169
+ except ImportError as err :
1170
+ print ("Error importing config.py file\n " ,err )
1171
+ return
1172
+ except Exception as err :
1173
+ print ("Error reading config.py file\n " ,err )
1174
+ return
1175
+ print (f"Contents of { config .__file__ } to be written..." )
1176
+ WriteIniConfi (configDict )
1177
+
1178
+ def WriteIniConfi (configDict ):
1179
+ '''Write the configDict information to the GSAS-II ini settings
1180
+ into file ~/.GSASII/config.ini. This routine will eventually be
1181
+ plumbed into GSASIIctrlGUI.SaveConfigVars.
1182
+ '''
1183
+ import configparser
1184
+
1185
+ localdir = os .path .expanduser ('~/.GSASII' )
1186
+ if not os .path .exists (localdir ):
1187
+ try :
1188
+ os .mkdir (g2local )
1189
+ print (f'Created directory { localdir } ' )
1190
+ except :
1191
+ print (f'Error trying to create directory { localdir } ' )
1192
+ return True
1193
+ cfgfile = os .path .join (localdir ,'config.ini' )
1194
+ cfgP = configparser .ConfigParser ()
1195
+ cfgP ['GUI settings' ] = configDict
1196
+
1197
+ # Write the configuration file
1198
+ with open (cfgfile , 'w' ) as configfile :
1199
+ cfgP .write (configfile )
1200
+ print (f"Configuraton settings saved as { cfgfile } " )
1201
+
1202
+ def LoadIniConfig ():
1203
+ '''
1204
+ Not currently in use, but intended to replace LoadConfig.
1205
+ '''
1206
+ import configparser
1207
+ global configDict
1208
+ configDict = {'Clip_on' :True }
1209
+ cfgfile = os .path .expanduser ('~/.GSASII/config.ini' )
1210
+ if not os .path .exists (cfgfile ):
1211
+ print (f'N.B. Configuration file { cfgfile } does not exist' )
1212
+ return
1213
+ try :
1214
+ import config_example
1215
+ except ImportError as err :
1216
+ print ("Error importing config_example.py file\n " ,err )
1217
+ return
1218
+
1219
+ # get the original capitalization (lost by configparser)
1220
+ capsDict = {key .lower ():key for key in config_example .__dict__ if not key .startswith ('__' )}
1221
+
1222
+ try :
1223
+ cfg = configparser .ConfigParser ()
1224
+ # Read the configuration file
1225
+ cfg .read (cfgfile )
1226
+ except Exception as err :
1227
+ print ("Error reading {cfgfile}\n " ,err )
1228
+ return
1229
+
1230
+ # Access values from the configuration file
1231
+ cfgG = cfg ['GUI settings' ]
1232
+ for key in cfgG :
1233
+ key = key .lower () # not needed... but in case configparser ever changes
1234
+ capKey = capsDict .get (key )
1235
+ if capKey is None :
1236
+ print (f'Item { key } not defined in config_example' )
1237
+ continue
1238
+ try :
1239
+ print ('\n ' ,key ,capKey )
1240
+ print (cfgG [key ])
1241
+ if cfgG [key ] == 'None' :
1242
+ configDict [capKey ] = None
1243
+ elif key .endswith ('_pos' ) or key .endswith ('_size' ): # list of integers
1244
+ configDict [capKey ] = tuple ([int (i ) for i in
1245
+ cfgG [key ].strip ('()' ).split (',' )])
1246
+ elif key .endswith ('_location' ) or key .endswith ('_directory' ) or key .endswith ('_exec' ): # None (above) or str
1247
+ configDict [capKey ] = cfgG .get (key )
1248
+ elif cfgG [key ].startswith ('[' ) and cfgG [key ].endswith (']' ): # list of strings
1249
+ s = cfgG [key ].strip ('[]' )
1250
+ if s == '' :
1251
+ res = []
1252
+ else :
1253
+ res = [i .strip ("'" ).replace (r'\\' ,'\\ ' ) for i in s .split (', ' )]
1254
+ configDict [capKey ] = res
1255
+ elif isinstance (config_example .__dict__ [capKey ],bool ):
1256
+ configDict [capKey ] = cfgG .getboolean (key )
1257
+ elif isinstance (config_example .__dict__ [capKey ],float ):
1258
+ configDict [capKey ] = cfgG .getfloat (key )
1259
+ elif isinstance (config_example .__dict__ [capKey ],int ):
1260
+ configDict [capKey ] = cfgG .getint (key )
1261
+ elif isinstance (config_example .__dict__ [capKey ],str ):
1262
+ configDict [capKey ] = cfgG .get (key ).replace (r'\\' ,'\\ ' )
1263
+ else :
1264
+ print ('*** problem with' ,type (config_example .__dict__ [capKey ]))
1265
+ continue
1266
+ except :
1267
+ continue
1268
+ print (f'{ config_example .__dict__ [capKey ]!r} ' )
1269
+ print (f'{ configDict [capKey ]!r} ' )
1270
+
1149
1271
# def MacStartGSASII(g2script,project=''):
1150
1272
# '''Start a new instance of GSAS-II by opening a new terminal window and starting
1151
1273
# a new GSAS-II process. Used on Mac OS X only.
0 commit comments