#! python: #w#VIC configuration.py module # # Copyright 2002, 2003 by Timothy Rue <3seas@threeseas.net> # # VIC configuration.py module: version 0.5.1.python (BETA) # # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation version 2 # of the License. http://www.gnu.org/copyleft/gpl.html # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Or access http://www.gnu.org/copyleft/gpl.html ########################################################################## #August 29, 2003 made file IQ parseable 0.5.1 #July 24, 2003 initial release 0.5 # ########################################################################## #s#system modules from ConfigParser import * import os.path import string #w#class-Configuration class Configuration: #s#def __init__ def __init__(self,config_file,ai_file): self.file = os.path.abspath(config_file) self.ai_file = os.path.abspath(ai_file) self.cfg = ConfigParser() self.cfg.read(config_file) #s#def setNextAI def setNextAI(self,next_id): try: fout = open(self.ai_file,"w") fout.write("[AI]\nnext_id="+str(next_id)+"\n\n") fout.close() except: print "Warning: Unable to write "+self.ai_file pass #s#def getNextAI def getNextAI(self): try: fin = open(self.ai_file,"r") content = fin.read() fin.close() content = string.lower(content) content = string.replace(content,"[ai]","") content = string.replace(content,"next_id","") content = string.replace(content,"=","") content = string.strip(content) ai = int(content) except: ai = 1 return ai #s#def getVar def getVar(self,name,default): result = default try: section,option = string.split(name,".",1) if (self.cfg.has_option(section,option)): result = self.cfg.get(section,option) except: pass if (len(result) > 3 and result[0] == "\"" and result[-1] == "\""): result = result[1:-1] return result #s#def getVarOrElse def getVarOrElse(self,name): result = "$$$ unique config string $$$" try: section,option = string.split(name,".",1) if (self.cfg.has_option(section,option)): result = self.cfg.get(section,option) except: pass if (result == "$$$ unique config string $$$"): import sys print "FATAL Error: '"+name+"' not defined in the configuration" sys.exit(2) if (len(result) > 3 and result[0] == "\"" and result[-1] == "\""): result = result[1:-1] return result #s#def save def save(self): if(1): out = open(self.file,"w") self.cfg.write(out) out.close() else: print "Unable to save RC \""+self.file+"\""