[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[or-cvs] r16874: {} Merge in the configuration parsing code. This was written by (projects/gettor)
Author: ioerror
Date: 2008-09-12 19:19:08 -0400 (Fri, 12 Sep 2008)
New Revision: 16874
Added:
projects/gettor/gettor_readconfig.py
Log:
Merge in the configuration parsing code. This was written by kaner and we refactored it together. Yay!
Added: projects/gettor/gettor_readconfig.py
===================================================================
--- projects/gettor/gettor_readconfig.py (rev 0)
+++ projects/gettor/gettor_readconfig.py 2008-09-12 23:19:08 UTC (rev 16874)
@@ -0,0 +1,98 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+'''
+This grabs configurable values from the users' gettor config file
+if that file is not present, it will supply reasonable defaults.
+'''
+
+import os
+import sys
+import ConfigParser
+
+class gettorConf:
+ '''
+ Initialize gettor with default values if one or more
+ values are missing from the config file.
+ This will return entirely default values if the configuration file is
+ missing. Our default file location is ~/.gettorrc for the current user.
+ '''
+
+ stateDir = "/var/lib/gettor/"
+ blStateDir = stateDir + "bl/"
+ srcEmail = "gettor@xxxxxxxxxxxxxx"
+ distDir = "/var/lib/gettor/pkg/"
+ locale = "en"
+ configFile = "~/.gettorrc"
+
+ def __init__(self, path = os.path.expanduser(configFile)):
+
+ self.configFile = os.path.expanduser(path)
+
+ try:
+ if os.access(self.configFile, os.R_OK):
+ readableConfigFile = True
+ else:
+ readableConfigFile = False
+
+ except OSError:
+ readableConfigFile = False
+
+ if readableConfigFile:
+ self.config = ConfigParser.ConfigParser()
+ try:
+ # It's likely that a user will make a mistake in their config
+ # If they make a mistake for now we'll ignore *everything* :-)
+ self.config.read(self.configFile)
+ if self.config.has_option("global", "stateDir"):
+ self.stateDir = self.config.get("global", "stateDir")
+ else:
+ self.config.set("global", "stateDir", self.stateDir)
+
+ if self.config.has_option("global", "blStateDir"):
+ self.blStateDir = self.config.get("global", "blStateDir")
+ else:
+ self.config.set("global", "blStateDir", self.blStateDir)
+
+ if self.config.has_option("global", "srcEmail"):
+ self.srcEmail = self.config.get("global", "srcEmail")
+ else:
+ self.config.set("global", "srcEmail", self.srcEmail)
+
+ if self.config.has_option("global", "distDir"):
+ self.distDir = self.config.get("global", "distDir")
+ else:
+ self.config.set("global", "distDir", self.distDir)
+
+ if self.config.has_option("global", "locale"):
+ self.lang = self.config.get("global", "locale")
+ else:
+ self.config.set("global", "locale", self.locale)
+
+ except:
+ return None
+ else:
+ return None
+
+ # All getter routines live below
+ def getStateDir(self):
+ return self.stateDir
+
+ def getBlStateDir(self):
+ return self.blStateDir
+
+ def getSrcEmail(self):
+ return self.srcEmail
+
+ def getDistDir(self):
+ return self.distDir
+
+ def getLang(self):
+ return self.lang
+
+if __name__ == "__main__" :
+ c = gettorConf()
+ print "; This is a sample gettor configuration file. "
+ print "StateDir -> ", c.getStateDir()
+ print "BlStateDir -> ", c.getBlStateDir()
+ print "SrcEmail -> ", c.getSrcEmail()
+ print "DistDir -> ", c.getDistDir()
Property changes on: projects/gettor/gettor_readconfig.py
___________________________________________________________________
Name: svn:executable
+ *