[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] r16875: {projects} Update the configuration class to have a more useful main(). (projects/gettor)



Author: ioerror
Date: 2008-09-12 20:27:41 -0400 (Fri, 12 Sep 2008)
New Revision: 16875

Modified:
   projects/gettor/gettor_readconfig.py
Log:
Update the configuration class to have a more useful main(). Users can now get the default configuration as gettor sees it. This is useful for generating their own initial configuration file.


Modified: projects/gettor/gettor_readconfig.py
===================================================================
--- projects/gettor/gettor_readconfig.py	2008-09-12 23:19:08 UTC (rev 16874)
+++ projects/gettor/gettor_readconfig.py	2008-09-13 00:27:41 UTC (rev 16875)
@@ -17,12 +17,13 @@
     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"
+    stateDir = "/var/lib/gettor/"
+    blStateDir = stateDir + "bl/"
+    srcEmail = "gettor@xxxxxxxxxxxxxx"
+    distDir = "/var/lib/gettor/pkg/"
+    locale = "en"
     configFile = "~/.gettorrc"
+    config = ConfigParser.ConfigParser()
 
     def __init__(self, path = os.path.expanduser(configFile)):
 
@@ -38,41 +39,43 @@
             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)
+            except:
+                self.config.add_section("global")
+        else:
+            self.config.add_section("global")
 
-                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", "stateDir"):
+            self.stateDir = self.config.get("global", "stateDir")
+        else:
+            self.config.set("global", "stateDir", self.stateDir)
 
-                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", "blStateDir"):
+            self.blStateDir = self.config.get("global", "blStateDir")
+        else:
+            self.config.set("global", "blStateDir", self.blStateDir)
 
-                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", "srcEmail"):
+            self.srcEmail = self.config.get("global", "srcEmail")
+        else:
+            self.config.set("global", "srcEmail", self.srcEmail)
 
-                if self.config.has_option("global", "locale"):
-                    self.lang = self.config.get("global", "locale")
-                else:
-                    self.config.set("global", "locale", self.locale)
+        if self.config.has_option("global", "distDir"):
+            self.distDir = self.config.get("global", "distDir")
+        else:
+            self.config.set("global", "distDir", self.distDir)
 
-            except:
-                return None
+        if self.config.has_option("global", "locale"):
+            self.lang = self.config.get("global", "locale")
         else:
-            return None
+            self.config.set("global", "locale", self.locale)
 
+    def printConfiguration(self):
+        return self.config.write(sys.stdout)
+
     # All getter routines live below
     def getStateDir(self):
         return self.stateDir
@@ -91,8 +94,5 @@
         
 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()
+    print "# This is a suitable default configuration. Tune to fit your needs."
+    c.printConfiguration()