iro/config.py
branchdevel
changeset 267 ef2df3f23cb1
parent 228 944edbe51145
child 269 0d134b173cb1
--- a/iro/config.py	Fri Mar 30 02:25:20 2012 +0200
+++ b/iro/config.py	Fri Mar 30 11:23:22 2012 +0200
@@ -8,11 +8,16 @@
 from error import NeededOption 
 
 class MyConfigParser(ConfigParser):
+    """Configparser that also validate configfile.
+
+    It is possile to restiger function, that are called, when config file is reloaded
+    """
     def __init__(self):
         ConfigParser.__init__(self)
         self.reloadList=[]
 
     def read(self,files):
+        """reads an validate configuration file"""
         from offer import getProvider
         r = ConfigParser.read(self, files)
         for s in self.sections():
@@ -23,14 +28,27 @@
         return r
 
     def reload_(self):
+        """run all registered function."""
         for f in self.reloadList:
             f()
 
     def registerReload(self, func):
+        """adds **func** to reloadList.
+        
+        func ist called with no arguments.
+        """
         self.reloadList.append(func)
 
 class Option():
+    """One Option in the configuration file"""
     def __init__(self, validate, long="", help="", must=False, default=None):
+        """
+        :param func validate: a validate function, it has to return the value, if valid and raise an error if not.
+        :param string long: long description
+        :param string help: the help text
+        :param boolean must: Is this option nessasary
+        :param default: default value
+        """
         self.validate = validate
         self.long=long
         self.help = help
@@ -38,17 +56,45 @@
         self.default = default
 
 class Config:
+    """Base class for all classes, that uses option from configfile.
+    
+    If one option is valid, the attribute is created with the value of the validate function.
+    """
     def __init__(self, name):
+        """
+        :param string name: section name.
+        """
         self.name = name
         self.options={
             "port":Option(partial(vInteger,minv=0),long="Port under that twisted is running",must=True),
             "dburl":Option(lambda x,y:x,long="Connection URL to database",must=True),
         }
+        """Options dict for Options used in configuration file (see :class:`iro.config.Option`). Ordering of configuration fields are done by :attr:`order`. 
+        
+        Sample::
+        
+               {"port":Option(partial(vInteger,minv=0),long="Port under that twisted is running",must=True),
+                "dburl":Option(lambda x,y:x,long="Connection URL to database",must=True)
+               }
+        
+        A child class typically use update to add more options.
+        """
+
         self.order = ["dburl","port"]
+        """ A list for ordering the options dict (:attr:`options`). """
+
         self._init = True
+        """indecates, if the config is loaded with config values."""
 
 
     def _read(self, cfg, write=False):
+        """Test or set configuration options.
+        
+        :param dict cfg: The Configuration dict. Normally you use ``configParser.items("section")``.
+        :param boolean write: test or set the option to actual object.
+
+        :raises: :exc:`iro.error.NeededOption`
+        """
         c = dict(cfg)
 
         for o in self.options:
@@ -65,12 +111,21 @@
                     setattr(self, o, option.default)
 
     def validate(self, cfg):
+        """Validate configuration.
+
+        :param dict cfg: The Configuration dict. Normally you use ``configParser.items("section")``.
+        """
         self._read(cfg, False)
 
     def load(self, cfg):
+        """Loads configuration into object.
+
+        :param dict cfg: The Configuration dict. Normally you use ``configParser.items("section")``.
+        """
         self._read(cfg, True)
 
     def same(self, other):
+        """returns ``True``, if the options of other object are the same"""
         for o in self.options:
             if getattr(self,o) != getattr(other,o):
                 return False
@@ -78,6 +133,12 @@
             return True
 
     def sampleConf(self):
+        """returns a sample Configuration section.
+
+        This function also adds the long help text to the sample section.
+
+        :return: a list of lines
+        """
         ret=[]
         for o in self.order:
             opt=self.options[o]
@@ -95,6 +156,7 @@
         return ["[%s]"%self.name,]+ret
 
 def readConfig():
+    """Read the configuration and update all registered object (see :meth:`MyConfigParser.reload_`)."""
     log.msg("Reading configs.")
     configParser.read(confFiles)
     configParser.reload_()
@@ -107,6 +169,7 @@
             raise Exception("Main options can't be reloaded, please restart your Application.")
 
 def init():
+    """Load the main options."""
     configParser.read(confFiles)
     main.load(configParser.items("main"))
 
@@ -118,6 +181,10 @@
     signal.signal(signal.SIGUSR2,rC)
 
 configParser = MyConfigParser()
+"""configParser to get configuration."""
+
 confFiles=["iro.conf", "~/iro.conf","/etc/iro/iro.conf"]
+"""Configfile list """
 
 main = Config("main")
+"""Main config options"""