Package TEES :: Package Utils :: Module Settings
[hide private]

Source Code for Module TEES.Utils.Settings

 1  import sys 
 2  # Import global defaults 
 3  from DefaultSettings import * 
 4   
 5  # Import local configuration 
 6  import os 
 7  if "TEES_SETTINGS" in os.environ: 
 8      import imp 
 9      pathname = os.environ["TEES_SETTINGS"] 
10      imp.load_source("TEESLocalSettings", pathname) 
11      # combine the settings dictionaries 
12      tempURL = URL 
13      tempEVALUATOR = EVALUATOR 
14      # import everything from local settings module 
15      exec "from TEESLocalSettings import *" 
16      # insert new values into the setting dictionaries 
17      tempURL.update(URL) 
18      URL = tempURL 
19      tempEVALUATOR.update(EVALUATOR) 
20      EVALUATOR = tempEVALUATOR 
21       
22 -def setLocal(variable, value, setVariable=True):
23 # the settings file must exist and must be in the path 24 if not setVariable: # notify only, do not add the variable 25 print >> sys.stderr, "Remember to add local setting", str(variable) + "=\"" + str(value) + "\"" 26 return 27 assert "TEES_SETTINGS" in os.environ 28 print >> sys.stderr, "Adding local setting", str(variable) + "=" + str(value), 29 # define the variable in the current module 30 exec (variable + " = '" + str(value) + "'") in globals() 31 # read the local settings file 32 f = open(os.environ["TEES_SETTINGS"], "rt") 33 lines = f.readlines() 34 f.close() 35 36 # if the variable already exists, change its value 37 found = False 38 for i in range(len(lines)): 39 if lines[i].strip() != "" and lines[i].strip()[0] == "#": # skip comment lines 40 continue 41 if lines[i].split("=")[0].strip() == variable: # variable definition line 42 lines[i] = variable + " = '" + str(value) + "'\n" 43 print >> sys.stderr, "(updated existing value)" 44 found = True 45 # if the variable doesn't exist, add it to the end of the file 46 if not found: 47 lines.append(variable + " = '" + str(value) + "'\n") 48 print >> sys.stderr, "(added variable)" 49 50 # write the local settings file 51 f = open(os.environ["TEES_SETTINGS"], "wt") 52 for line in lines: 53 f.write(line) 54 f.close()
55