commit-gnue
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnue] r9816 - trunk/gnue-common/src/apps


From: johannes
Subject: [gnue] r9816 - trunk/gnue-common/src/apps
Date: Fri, 16 Nov 2007 04:29:24 -0600 (CST)

Author: johannes
Date: 2007-11-16 04:29:24 -0600 (Fri, 16 Nov 2007)
New Revision: 9816

Modified:
   trunk/gnue-common/src/apps/GConfig.py
Log:
More code cleanup (according to pylint)


Modified: trunk/gnue-common/src/apps/GConfig.py
===================================================================
--- trunk/gnue-common/src/apps/GConfig.py       2007-11-16 09:43:52 UTC (rev 
9815)
+++ trunk/gnue-common/src/apps/GConfig.py       2007-11-16 10:29:24 UTC (rev 
9816)
@@ -92,142 +92,205 @@
         __builtin__.__dict__[name] = alias.gConfig
 
 
-    #
-    # loadApplicationConfig
-    #
-    # Loads the specified file only once.
-    # Subsequent calls setup the defaults for any missing values
-    #
+    # -------------------------------------------------------------------------
+    # Load the specified file
+    # -------------------------------------------------------------------------
+
     def loadApplicationConfig(self, configFilename="gnue.conf",
             homeConfigDir=".gnue", section="DEFAULT", defaults=None):
+        """
+        Load the specified file only once. Subsequent calls setup the defaults
+        for any missing values.
 
-        assert gDebug(2,'Reading configuration info from %s section %s' %
+        @raises InvalidFormatError: if parsing of the configuration file fails,
+            this error will indicate why.
+        """
+
+        assert gDebug(2, 'Reading configuration info from %s section %s' %
                 (configFilename, section))
 
-        #
         # Create parser and populate it if it doesn't exist
-        #
         if not self._loadedConfigs.has_key(configFilename):
             if defaults is None:
                 parser = GConfigParser(GCConfig.ConfigOptions)
             else:
                 parser = GConfigParser(defaults + GCConfig.ConfigOptions)
-            self._loadedConfigs[configFilename]=parser
 
+            self._loadedConfigs[configFilename] = parser
 
+
             # Build valid file list
-            fileLocations = []
+            file_locations = []
             etc_base = getInstalledBase('%s_etc' % section, 'common_etc')
 
             # system config file
             if etc_base:
-                fileLocations.append(os.path.join(etc_base,configFilename))
+                file_locations.append(os.path.join(etc_base, configFilename))
 
             # user config file
             try:
-                fileLocations.append(os.path.join(os.environ['HOME'],
+                file_locations.append(os.path.join(os.environ['HOME'],
                     homeConfigDir, configFilename))
             except KeyError:
                 pass
 
             # system fixed config file
             if etc_base:
-                fileLocations.append(os.path.join(etc_base,
-                    configFilename+'.fixed'))
+                file_locations.append(os.path.join(etc_base,
+                    configFilename + '.fixed'))
 
-            #
             # Load the values from the files specified
-            #
             try:
-                parser.read(fileLocations)
+                parser.read(file_locations)
                 assert gDebug(2, \
                         'Configuration files were read in this order:  %s' % \
-                                 fileLocations)
+                                 file_locations)
+
             except ConfigParser.DuplicateSectionError:
                 raise InvalidFormatError, \
                     u_('Configuration file has duplicate sections.')
+
             except ConfigParser.MissingSectionHeaderError:
                 raise InvalidFormatError, \
                     u_('Configuration file has no sections.')
+
             except:
                 raise InvalidFormatError, \
                     u_('Configuration file cannot be parsed:\n%s') % \
                     sys.exc_value
 
-            #
-            # Common only needs checked once
-            #
-            # Load any [common] defaults
-            self._integrateDefaultDict(configFilename, 'common',
-                    self._buildDefaults(GCConfig.ConfigOptions))
+            # Common only needs checked once, load any [common] defaults
+            self.__integrate_default_dict(configFilename, 'common',
+                    self.__build_defaults(GCConfig.ConfigOptions))
 
-        #
         # Load anything set in the DEFAULT section
-        #
-        self._integrateDefaultDict(configFilename, section,
+        self.__integrate_default_dict(configFilename, section,
                 self._loadedConfigs[configFilename].defaults())
 
-        #
-        # If any values are still blank after loading from file's
-        # specific section and then the default section then load the
-        # defaults specified by the application itself.
-        #
-        self._integrateDefaultDict(configFilename, section,
-                self._buildDefaults(defaults))
+        # If any values are still blank after loading from file's specific
+        # section and then the default section then load the defaults specified
+        # by the application itself.
+        self.__integrate_default_dict(configFilename, section,
+                self.__build_defaults(defaults))
 
 
-    def _integrateDefaultDict(self,filename, section,defaults):
+    # -------------------------------------------------------------------------
+    # Integrate default values from a dictionary into a configuration section
+    # -------------------------------------------------------------------------
+
+    def __integrate_default_dict(self, filename, section, defaults):
+
         try:
             self._loadedConfigs[filename].add_section(section)
+
         except ConfigParser.DuplicateSectionError:
             pass
-        for key in defaults.keys():
-            # Only set the value to the default if config file didn't contain
-            # custom setting.
+
+        # Only set the value to the default if config file didn't contain
+        # custom setting.
+        for key in defaults:
             try:
-                self._loadedConfigs[filename].get(section,key)
+                self._loadedConfigs[filename].get(section, key)
+
             except ConfigParser.NoOptionError:
-                self._loadedConfigs[filename].set(section,key,defaults[key])
+                self._loadedConfigs[filename].set(section, key, defaults[key])
 
 
+    # -------------------------------------------------------------------------
+    # Create a dictionary of default values from a definition dictionary
+    # -------------------------------------------------------------------------
+
+    def __build_defaults(self, definitions):
+
+        defaults = {}
+        if definitions:
+            for item in definitions:
+                defaults[item['Name'].lower()] = str(item['Default'])
+
+        return defaults
+
+
+    # -------------------------------------------------------------------------
+    # Retrieve an option value from a configuration files' section
+    # -------------------------------------------------------------------------
+
     def gConfig(self, var_name, configFilename=None, section=None):
-        if not configFilename: configFilename = self._default_config_filename
-        if not section: section = self._default_section
+        """
+        Retrieve an option from a section in a configuration file
+
+        @param var_name: name of the option to retrieve
+        @type var_name: string
+
+        @param configFilename: name of the configuration file to retrieve the
+            option from.  If None, the default configuration file will be used.
+        @type configFilename: string
+
+        @param section: name of the section to retrieve the option from.  If
+            None, the default section will be used.
+
+        @returns: the retrieve option value
+        """
+
+        if not configFilename:
+            configFilename = self._default_config_filename
+        if not section:
+            section = self._default_section
+
+        cfg_parser = self._loadedConfigs[configFilename]
+
         try:
-            return self._loadedConfigs[configFilename].get(section,var_name)
+            return cfg_parser.get(section, var_name)
+
         except ConfigParser.NoSectionError:
-            self._loadedConfigs[configFilename].add_section(section)
-            return self._loadedConfigs[configFilename].get(section,var_name)
+            cfg_parser.add_section(section)
+            return cfg_parser.get(section, var_name)
+
         except ConfigParser.NoOptionError:
             section = 'common'
             try:
-                return 
self._loadedConfigs[configFilename].get(section,var_name)
+                return cfg_parser.get(section, var_name)
+
             except ConfigParser.NoSectionError:
-                self._loadedConfigs[configFilename].add_section(section)
-                return 
self._loadedConfigs[configFilename].get(section,var_name)
+                cfg_parser.add_section(section)
+                return cfg_parser.get(section, var_name)
 
+
+    # -------------------------------------------------------------------------
+    # Get a dictionary with all configuration options in a section
+    # -------------------------------------------------------------------------
+
     def gConfigDict(self, configFilename=None, section=None):
-        if not configFilename: configFilename = self._default_config_filename
-        if not section:      section = self._default_section
+        """
+        Build a dictionary containing all configuration options of a given
+        section within a given configuration file.
 
-        c = self._loadedConfigs[configFilename]
-        if c.has_section(section):
-            options = {}
-            for option in c.options(section):
-                options[option] = c.get(section, option.lower())
-            return options
-        else:
-            return {}
+        @param configFilename: name of the configuration file to retrieve the
+            options from.  If None, the default configuration file will be
+            used.
+        @type configFilename: string
+        @param section: name of the section to retrieve the options from.  If
+            None, the default section will be used.
 
-    def _buildDefaults(self, defaultDefinitions):
-        defaults = {}
-        if defaultDefinitions:
-            for definition in defaultDefinitions:
-                defaults[definition['Name'].lower()] = \
-                        str(definition['Default'])
-        return defaults
+        @returns: dictionary containing the options and their values
+        @rtype: dict
+        """
 
+        if not configFilename:
+            configFilename = self._default_config_filename
+        if not section:
+            section = self._default_section
 
+        cfg_parser = self._loadedConfigs[configFilename]
+
+        result = {}
+        if cfg_parser.has_section(section):
+            for option in cfg_parser.options(section):
+                result[option] = cfg_parser.get(section, option.lower())
+
+        return result
+
+
+
 # =============================================================================
 # GNUe Config Parser class supporting the GTypecast system
 # =============================================================================





reply via email to

[Prev in Thread] Current Thread [Next in Thread]