commit:     07baec53eaadb095a61f9c6c6f21109a20f4cad6
Author:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
AuthorDate: Fri Jul 10 01:25:50 2015 +0000
Commit:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
CommitDate: Fri Jul 10 01:25:50 2015 +0000
URL:        https://gitweb.gentoo.org/proj/grss.git/commit/?id=07baec53

grs/Constants.py: add documentation.

 grs/Constants.py   | 82 ++++++++++++++++++++++++++++++++++++++++++++----------
 tests/systems.conf |  1 -
 2 files changed, 68 insertions(+), 15 deletions(-)

diff --git a/grs/Constants.py b/grs/Constants.py
index 501d79d..b9ea1d9 100644
--- a/grs/Constants.py
+++ b/grs/Constants.py
@@ -5,30 +5,62 @@ import sys
 import configparser
 from copy import deepcopy
 
-CONFIG = '/etc/grs/systems.conf'
 
 class Constants():
-    """ doc here
-        more doc
+    """ Read a global configuration file and set/override constants for
+        each GRS spec.  These constants are exported in the form:
+
+            CONST.nameservers[x] contains the namserver for the xth GRS spec
+            CONST.repo_uris[x] contains the repo_uri for the xth GRS spec
+            etc.
+
+        Notice the 's' added to the list name to distinguish the list from
+        the constant it holds.  Here the x is an integer corresponding to a
+        section in a global config file, which by default is located at
+        '/etc/grs/systes.conf'.  This file is in configparser format and
+        each section introduces a new GRS namespace.  The default values
+        for all possible constants in any given GRS namespace are defined
+        by the space[] dictionary below, but these can be overridden using
+        the item:value pairs from the section of any given GRS namespace.
+        E.g. Suppose /etc/grs/systems.conf contains
+
+        [my-cool-desktop]
+        kernelroot : /tmp/kernel_src_tree
+
+        [my-cool-server]
+        nameserver : 192.168.100.1
+
+        Then CONST.kernelroots[0] is '/tmp/kernel_src_tree' rather than the
+        default value '/var/tmp/grs/my-cool-desktop/kernel'.  The remainder
+        of the constants default as delineated in the space[] dictionary with
+        %s replaced by 'my-cool-desktop'.  Similarly CONST.my-cool-servers[1]
+        is 192.168.100.1 rather than 8.8.8.8.
+
+        Finally, the that class overrides __setattr__, __gettattr__ and
+        __delattr__ so that you cannot add/change/delete constants in
+        a GRS namespace.
     """
 
-    def __init__(self, configfile = CONFIG):
+    def __init__(self, configfile = '/etc/grs/systems.conf'):
+        # If there's no config file, we're dead in the water.
         if not os.path.isfile(configfile):
-            sys.stderr.write('Configuration file %s not found\n' % configfile)
-            sys.exit(1)
+            raise Exception('Configuration file %s not found\n' % configfile)
+
         self.config = configparser.ConfigParser(delimiters = ':', 
comment_prefixes = '#')
         self.config.read(configfile)
 
-        self.names = list(self.config.sections())
-
+        # These values will probably fail in the future, but that's okay
+        # because they really should never be used.  They live outside of
+        # any GRS namespace and are just 'defaults'.
         server    = 'http://distfiles.gentoo.org/'
-        stagedir  = 
'gentoo/releases/amd64/autobuilds/current-stage3-amd64-uclibc-hardened/'
+        stagedir  = 
'releases/amd64/autobuilds/current-stage3-amd64-uclibc-hardened/'
         stagefile = 'stage3-amd64-uclibc-hardened-20150510.tar.bz2'
         default_stage_uri =  server + stagedir + stagefile
 
+        # This is the space of all possible constants for any given GRS 
namespace
         space = {
             'nameserver'          : '8.8.8.8',
-            'repo_uri'            : 'git://tweedledum.dyc.edu/grs',
+            'repo_uri'            : 'git://anongit.gentoo.org/proj/grs.git',
             'stage_uri'           : default_stage_uri,
             'libdir'              : '/var/lib/grs/%s',
             'logfile'             : '/var/log/grs/%s.log',
@@ -40,49 +72,70 @@ class Constants():
             'pidfile'             : '/run/grs-%s.pid'
         }
 
+        # We add an 's' to each list for a particular constant,
+        # and initialize the list to be empty.
         for key in space:
             self.__dict__[key+'s'] = []
 
+        # Each section is a 'namespace' corresponding to each GRS spec.
+        # We export these in the CONST.names[] list.
+        self.names = list(self.config.sections())
+
+        # We go over all the sections in the config file.  The
+        # order here had better be the same as self.names[], else
+        # CONST.names[x] doesn't corresponde to the other CONST.foo[x]
+        # for every x.
         for section in self.config.sections():
             overrides = dict(self.config[section].items())
 
+            # Either we have an override value from the config
+            # file, else we contruct a default name.
             for key in space:
                 if key in overrides:
                     value = overrides[key]
                 else:
+                    # Either the default name has a slot %s to
+                    # file or else it doesn't.
                     try:
                         value = space[key] % section
                     except TypeError:
                         value = space[key]
+                # We're counting on the order in which we append here to
+                # correspond to the GRS namespace for the key:value pair.
                 self.__dict__[key+'s'].append(value)
 
 
+    # Allow CONST.foo = bar only once!
     def __setattr__(self, key, value):
         if not key in self.__dict__:
             self.__dict__[key] = value
         else:
             pass
 
-
+    # Don't retrieve the original else you can overwrite it,
+    # rather deep copy it.
     def __getattr__(self, key, value = None):
         if key in self.__dict__:
             return deepcopy(self.__dict__[key])
 
-
+    # You can't del(CONST.foo).
     def __delattr__(self, key):
         if key in self.__dict__:
             pass
 
 
+# Instantiate once and export all our constant in CONST.
 CONST = Constants()
 
+# Constants outside any GRS namespace.
 CONST.PACKAGE_NAME        = "Gentoo Reference System"
 CONST.PACKAGE_VERSION     = 0.0
 CONST.PACKAGE_DESCRIPTION = "Update a GRS by cloning a predefined system."
 CONST.BUG_REPORTS         = 'http://bugs.gentoo.org'
 
-# The are defaults in case objects are instantiated without namespaces
-# but they should not be used under normal working condidtions.
+# The are defaults in case objects of other classes which depend on values
+# of libdir, logfile, etc. are instantiated outside of any namespaces.
+# They should not be needed under normal working condidtions.
 CONST.LIBDIR              = '/var/lib/grs'
 CONST.LOGFILE             = '/var/log/grs.log'
 CONST.TMPDIR              = '/var/tmp/grs'
@@ -92,6 +145,7 @@ CONST.KERNELROOT          = '/var/tmp/grs/kernel'
 CONST.PORTAGE_CONFIGROOT  = '/var/tmp/grs/system'
 CONST.PIDFILE             = '/run/grs.pid'
 
+# These are used by grsup and are hard coded values.
 CONST.PORTAGE_CONFIGDIR   = '/etc/portage'
 CONST.PORTAGE_DIRTYFILE   = '/etc/portage/.grs_dirty'
 CONST.WORLD_CONFIG        = '/etc/grs/world.conf'

diff --git a/tests/systems.conf b/tests/systems.conf
deleted file mode 120000
index b92ef4f..0000000
--- a/tests/systems.conf
+++ /dev/null
@@ -1 +0,0 @@
-../systems.conf
\ No newline at end of file

Reply via email to