3535intver = lambda vs : sum ([int (i ) for i in vs .split ('.' )[0 :2 ]]* np .array ((1000 ,1 )))
3636
3737def GetConfigValue (key ,default = None ,getDefault = False ):
38- '''Return the configuration file value for key or a default value if not present
38+ '''Return the configuration file value for key or a default value
39+ if not specified.
3940
40- :param str key: a value to be found in the configuration (config.py) file
41- :param default: a value to be supplied if none is in the config file or
42- the config file is not found. Defaults to None
41+ :param str key: a value to be found in the configuration settings
42+ :param any default: a value to be supplied if a value for key is
43+ not specified in the config file or the config file is not found.
44+ Defaults to None.
45+ :param bool getDefault: If True looks up the default value from the
46+ config_example.py file (default value is False). Do not specify a
47+ getDefault=True if a value is provided for default.
4348 :returns: the value found or the default.
4449 '''
4550 if getDefault :
@@ -1779,7 +1784,8 @@ def runScript(cmds=[], wait=False, G2frame=None):
17791784
17801785def IPyBreak_base (userMsg = None ):
17811786 '''A routine that invokes an IPython session at the calling location
1782- This routine is only used when debug=True is set in config.py
1787+ This routine is only used when debug=True is set in the configuration
1788+ settings
17831789 '''
17841790 savehook = sys .excepthook # save the exception hook
17851791 try :
@@ -1812,7 +1818,7 @@ def exceptHook(*args):
18121818 with fancy formatting and then calls an IPython shell with the environment
18131819 of the exception location.
18141820
1815- This routine is only used when debug=True is set in config.py
1821+ This routine is only used when debug=True is set in the configuration settings
18161822 '''
18171823 try :
18181824 from IPython .core import ultratb
@@ -1853,7 +1859,7 @@ class c(object): pass
18531859
18541860def DoNothing ():
18551861 '''A routine that does nothing. This is called in place of IPyBreak and pdbBreak
1856- except when the debug option is set True in config.py
1862+ except when the debug option is set True in the configuration settings
18571863 '''
18581864 pass
18591865
@@ -2010,7 +2016,8 @@ def appendIfExists(searchpathlist,loc,subdir):
20102016 LoadConfig (printInfo )
20112017
20122018def LoadConfig (printInfo = True ):
2013- # setup read of config.py, if present
2019+ '''Read configuration settings from config.py, if present
2020+ '''
20142021 global configDict
20152022 try :
20162023 import config
@@ -2029,6 +2036,121 @@ def LoadConfig(printInfo=True):
20292036 print (60 * '*' )
20302037 configDict = {'Clip_on' :True }
20312038
2039+ def XferConfigIni ():
2040+ '''copy the contents of the config.py file to file ~/.GSASII/config.ini.
2041+ This is not currently in use in GSAS-II
2042+ '''
2043+ import types
2044+ configDict = {}
2045+ try :
2046+ import config
2047+ #import config_example as config
2048+ for i in config .__dict__ :
2049+ if i .startswith ('__' ) and i .endswith ('__' ): continue
2050+ if isinstance (config .__dict__ [i ],types .ModuleType ): continue
2051+ configDict .update ({i :str (config .__dict__ [i ])})
2052+ except ImportError as err :
2053+ print ("Error importing config.py file\n " ,err )
2054+ return
2055+ except Exception as err :
2056+ print ("Error reading config.py file\n " ,err )
2057+ return
2058+ print (f"Contents of { config .__file__ } to be written..." )
2059+ WriteIniConfi (configDict )
2060+
2061+ def WriteIniConfi (configDict ):
2062+ '''Write the configDict information to the GSAS-II ini settings
2063+ into file ~/.GSASII/config.ini. This routine will eventually be
2064+ plumbed into GSASIIctrlGUI.SaveConfigVars.
2065+ '''
2066+ import configparser
2067+
2068+ localdir = os .path .expanduser ('~/.GSASII' )
2069+ if not os .path .exists (localdir ):
2070+ try :
2071+ os .mkdir (g2local )
2072+ print (f'Created directory { localdir } ' )
2073+ except :
2074+ print (f'Error trying to create directory { localdir } ' )
2075+ return True
2076+ cfgfile = os .path .join (localdir ,'config.ini' )
2077+ cfgP = configparser .ConfigParser ()
2078+ cfgP ['GUI settings' ] = configDict
2079+
2080+ # Write the configuration file
2081+ with open (cfgfile , 'w' ) as configfile :
2082+ cfgP .write (configfile )
2083+ print (f"Configuraton settings saved as { cfgfile } " )
2084+
2085+ def LoadIniConfig ():
2086+ '''
2087+ Not currently in use, but intended to replace LoadConfig.
2088+ '''
2089+ import configparser
2090+ global configDict
2091+ configDict = {'Clip_on' :True }
2092+ cfgfile = os .path .expanduser ('~/.GSASII/config.ini' )
2093+ if not os .path .exists (cfgfile ):
2094+ print (f'N.B. Configuration file { cfgfile } does not exist' )
2095+ return
2096+ try :
2097+ import config_example
2098+ except ImportError as err :
2099+ print ("Error importing config_example.py file\n " ,err )
2100+ return
2101+
2102+ # get the original capitalization (lost by configparser)
2103+ capsDict = {key .lower ():key for key in config_example .__dict__ if not key .startswith ('__' )}
2104+
2105+ try :
2106+ cfg = configparser .ConfigParser ()
2107+ # Read the configuration file
2108+ cfg .read (cfgfile )
2109+ except Exception as err :
2110+ print ("Error reading {cfgfile}\n " ,err )
2111+ return
2112+
2113+ # Access values from the configuration file
2114+ cfgG = cfg ['GUI settings' ]
2115+ for key in cfgG :
2116+ key = key .lower () # not needed... but in case configparser ever changes
2117+ capKey = capsDict .get (key )
2118+ if capKey is None :
2119+ print (f'Item { key } not defined in config_example' )
2120+ continue
2121+ try :
2122+ print ('\n ' ,key ,capKey )
2123+ print (cfgG [key ])
2124+ if cfgG [key ] == 'None' :
2125+ configDict [capKey ] = None
2126+ elif key .endswith ('_pos' ) or key .endswith ('_size' ): # list of integers
2127+ configDict [capKey ] = tuple ([int (i ) for i in
2128+ cfgG [key ].strip ('()' ).split (',' )])
2129+ elif key .endswith ('_location' ) or key .endswith ('_directory' ) or key .endswith ('_exec' ): # None (above) or str
2130+ configDict [capKey ] = cfgG .get (key )
2131+ elif cfgG [key ].startswith ('[' ) and cfgG [key ].endswith (']' ): # list of strings
2132+ s = cfgG [key ].strip ('[]' )
2133+ if s == '' :
2134+ res = []
2135+ else :
2136+ res = [i .strip ("'" ).replace (r'\\' ,'\\ ' ) for i in s .split (', ' )]
2137+ configDict [capKey ] = res
2138+ elif isinstance (config_example .__dict__ [capKey ],bool ):
2139+ configDict [capKey ] = cfgG .getboolean (key )
2140+ elif isinstance (config_example .__dict__ [capKey ],float ):
2141+ configDict [capKey ] = cfgG .getfloat (key )
2142+ elif isinstance (config_example .__dict__ [capKey ],int ):
2143+ configDict [capKey ] = cfgG .getint (key )
2144+ elif isinstance (config_example .__dict__ [capKey ],str ):
2145+ configDict [capKey ] = cfgG .get (key ).replace (r'\\' ,'\\ ' )
2146+ else :
2147+ print ('*** problem with' ,type (config_example .__dict__ [capKey ]))
2148+ continue
2149+ except :
2150+ continue
2151+ print (f'{ config_example .__dict__ [capKey ]!r} ' )
2152+ print (f'{ configDict [capKey ]!r} ' )
2153+
20322154# def MacStartGSASII(g2script,project=''):
20332155# '''Start a new instance of GSAS-II by opening a new terminal window and starting
20342156# a new GSAS-II process. Used on Mac OS X only.
0 commit comments