Skip to content

Commit 5c1532d

Browse files
committed
Bring latest changes in master into develop
2 parents a843128 + 4583007 commit 5c1532d

File tree

2 files changed

+136
-14
lines changed

2 files changed

+136
-14
lines changed

GSASII/GSASIIpath.py

Lines changed: 132 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,16 @@
3636
intver = lambda vs: sum([int(i) for i in vs.split('.')[0:2]]*np.array((1000,1)))
3737

3838
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.
4449
:returns: the value found or the default.
4550
'''
4651
if getDefault:
@@ -938,7 +943,8 @@ def runScript(cmds=[], wait=False, G2frame=None):
938943

939944
def IPyBreak_base(userMsg=None):
940945
'''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
942948
'''
943949
savehook = sys.excepthook # save the exception hook
944950
try:
@@ -970,8 +976,8 @@ def exceptHook(*args):
970976
'''A routine to be called when an exception occurs. It prints the traceback
971977
with fancy formatting and then calls an IPython shell with the environment
972978
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
975981
'''
976982
try:
977983
from IPython.core import ultratb
@@ -1012,7 +1018,7 @@ class c(object): pass
10121018

10131019
def DoNothing():
10141020
'''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
10161022
'''
10171023
pass
10181024

@@ -1127,7 +1133,8 @@ def SetBinaryPath(showConfigMsg=False):
11271133
BinaryPathFailed = pathHacking._path_discovery(showConfigMsg)
11281134

11291135
def LoadConfig(printInfo=True):
1130-
# setup read of config.py, if present
1136+
'''Read configuration settings from config.py, if present
1137+
'''
11311138
global configDict
11321139
try:
11331140
import config
@@ -1146,6 +1153,121 @@ def LoadConfig(printInfo=True):
11461153
print(60*'*')
11471154
configDict = {'Clip_on':True}
11481155

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+
11491271
# def MacStartGSASII(g2script,project=''):
11501272
# '''Start a new instance of GSAS-II by opening a new terminal window and starting
11511273
# a new GSAS-II process. Used on Mac OS X only.

GSASII/config_example.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,19 +138,19 @@
138138
'''DefaultAutoScale selects one of the AutoScale_ParmNames.
139139
Used in AutoIntegration
140140
'''
141-
Main_Size = '(700,450)'
141+
Main_Size = (700,450)
142142
'''Main window size (width, height) - initially uses wx.DefaultSize but will updated
143143
and saved as the user changes the window
144144
'''
145-
Main_Pos = '(100,100)'
145+
Main_Pos = (100,100)
146146
'''Main window location - will be updated & saved when user moves
147147
it. If position is outside screen then it will be repositioned to default
148148
'''
149-
Plot_Size = '(700,600)'
149+
Plot_Size = (700,600)
150150
'''Plot window size (width, height) - initially uses wx.DefaultSize but will updated
151151
and saved as the user changes the window
152152
'''
153-
Plot_Pos = '(200,200)'
153+
Plot_Pos = (200,200)
154154
'''Plot window location - will be updated & saved when user moves it
155155
these widows. If position is outside screen then it will be repositioned to default
156156
'''

0 commit comments

Comments
 (0)