[Checkins] SVN: keas.build/trunk/ Fix RawConfigParser usage, it destroys options values by making them all lowercase.

Adam Groszer agroszer at gmail.com
Thu Apr 7 02:54:14 EDT 2011


Log message for revision 121313:
  Fix RawConfigParser usage, it destroys options values by making them all lowercase.

Changed:
  U   keas.build/trunk/CHANGES.txt
  U   keas.build/trunk/src/keas/build/base.py
  U   keas.build/trunk/src/keas/build/build.py

-=-
Modified: keas.build/trunk/CHANGES.txt
===================================================================
--- keas.build/trunk/CHANGES.txt	2011-04-07 06:40:23 UTC (rev 121312)
+++ keas.build/trunk/CHANGES.txt	2011-04-07 06:54:14 UTC (rev 121313)
@@ -4,7 +4,8 @@
 0.2.1 (unreleased)
 ------------------
 
-- Nothing changed yet.
+- Fix RawConfigParser usage, it destroys options values by making them
+  all lowercase.
 
 
 0.2.0 (2011-04-06)

Modified: keas.build/trunk/src/keas/build/base.py
===================================================================
--- keas.build/trunk/src/keas/build/base.py	2011-04-07 06:40:23 UTC (rev 121312)
+++ keas.build/trunk/src/keas/build/base.py	2011-04-07 06:54:14 UTC (rev 121313)
@@ -10,6 +10,8 @@
 __docformat__ = 'ReStructuredText'
 import StringIO
 import base64
+import ConfigParser
+import collections
 import httplib
 import logging
 import optparse
@@ -17,6 +19,7 @@
 import pkg_resources
 import subprocess
 import sys
+from UserDict import DictMixin
 import urllib2
 import urlparse
 
@@ -165,7 +168,115 @@
     logger.debug('Last Version: %s -> %s' %(version, newVersion))
     return newVersion
 
+## {{{ http://code.activestate.com/recipes/576693/ (r6)
+class OrderedDict(dict, DictMixin):
 
+    def __init__(self, *args, **kwds):
+        if len(args) > 1:
+            raise TypeError('expected at most 1 arguments, got %d' % len(args))
+        try:
+            self.__end
+        except AttributeError:
+            self.clear()
+        self.update(*args, **kwds)
+
+    def clear(self):
+        self.__end = end = []
+        end += [None, end, end]         # sentinel node for doubly linked list
+        self.__map = {}                 # key --> [key, prev, next]
+        dict.clear(self)
+
+    def __setitem__(self, key, value):
+        if key not in self:
+            end = self.__end
+            curr = end[1]
+            curr[2] = end[1] = self.__map[key] = [key, curr, end]
+        dict.__setitem__(self, key, value)
+
+    def __delitem__(self, key):
+        dict.__delitem__(self, key)
+        key, prev, next = self.__map.pop(key)
+        prev[2] = next
+        next[1] = prev
+
+    def __iter__(self):
+        end = self.__end
+        curr = end[2]
+        while curr is not end:
+            yield curr[0]
+            curr = curr[2]
+
+    def __reversed__(self):
+        end = self.__end
+        curr = end[1]
+        while curr is not end:
+            yield curr[0]
+            curr = curr[1]
+
+    def popitem(self, last=True):
+        if not self:
+            raise KeyError('dictionary is empty')
+        if last:
+            key = reversed(self).next()
+        else:
+            key = iter(self).next()
+        value = self.pop(key)
+        return key, value
+
+    def __reduce__(self):
+        items = [[k, self[k]] for k in self]
+        tmp = self.__map, self.__end
+        del self.__map, self.__end
+        inst_dict = vars(self).copy()
+        self.__map, self.__end = tmp
+        if inst_dict:
+            return (self.__class__, (items,), inst_dict)
+        return self.__class__, (items,)
+
+    def keys(self):
+        return list(self)
+
+    setdefault = DictMixin.setdefault
+    update = DictMixin.update
+    pop = DictMixin.pop
+    values = DictMixin.values
+    items = DictMixin.items
+    iterkeys = DictMixin.iterkeys
+    itervalues = DictMixin.itervalues
+    iteritems = DictMixin.iteritems
+
+    def __repr__(self):
+        if not self:
+            return '%s()' % (self.__class__.__name__,)
+        return '%s(%r)' % (self.__class__.__name__, self.items())
+
+    def copy(self):
+        return self.__class__(self)
+
+    @classmethod
+    def fromkeys(cls, iterable, value=None):
+        d = cls()
+        for key in iterable:
+            d[key] = value
+        return d
+
+    def __eq__(self, other):
+        if isinstance(other, OrderedDict):
+            return len(self)==len(other) and self.items() == other.items()
+        return dict.__eq__(self, other)
+
+    def __ne__(self, other):
+        return not self == other
+## end of http://code.activestate.com/recipes/576693/ }}}
+
+class NonDestructiveRawConfigParser(ConfigParser.RawConfigParser):
+    def __init__(self):
+        ConfigParser.RawConfigParser.__init__(self, dict_type=OrderedDict)
+
+    def optionxform(self, optionstr):
+        return optionstr
+
+
 parser = optparse.OptionParser()
 parser.add_option(
     "-c", "--config-file", action="store",

Modified: keas.build/trunk/src/keas/build/build.py
===================================================================
--- keas.build/trunk/src/keas/build/build.py	2011-04-07 06:40:23 UTC (rev 121312)
+++ keas.build/trunk/src/keas/build/build.py	2011-04-07 06:54:14 UTC (rev 121313)
@@ -94,7 +94,7 @@
             hash = md5.new(open(infile, 'rb').read()).hexdigest()
             hashes[justname] = hash
 
-    config = ConfigParser.RawConfigParser()
+    config = base.NonDestructiveRawConfigParser()
     config.read(infile)
 
     dependents = set()
@@ -160,7 +160,7 @@
         modified = False
         justname = os.path.split(fname)[-1]
 
-        config = ConfigParser.RawConfigParser()
+        config = base.NonDestructiveRawConfigParser()
         config.read(fname)
 
         try:
@@ -193,6 +193,8 @@
                 pass
 
         if modified:
+            #RawConfigParser fools us... next time it does I'll do
+            #plain replace on the file contents
             config.write(open(newname, 'w'))
             rdep.append(newname)
         else:
@@ -203,12 +205,12 @@
 def build(configFile, options):
     # Read the configuration file.
     logger.info('Loading configuration file: ' + configFile)
-    config = ConfigParser.RawConfigParser()
+    config = base.NonDestructiveRawConfigParser()
     config.read(configFile)
 
     # Create the project config parser
     logger.info('Creating Project Configuration')
-    projectParser = ConfigParser.RawConfigParser()
+    projectParser = base.NonDestructiveRawConfigParser()
     template_path = None
     if config.has_option(base.BUILD_SECTION, 'template'):
         template = config.get(base.BUILD_SECTION, 'template')
@@ -352,7 +354,7 @@
             sys.exit(0)
         deployConfigFilename = '%s-%s-%s.cfg' %(
             config.get(base.BUILD_SECTION, 'name'), section, projectVersion)
-        deployConfig = ConfigParser.RawConfigParser()
+        deployConfig = base.NonDestructiveRawConfigParser()
         deployConfig.readfp(StringIO.StringIO(deployConfigText))
         deployConfig.set('buildout', 'extends', projectConfigFilename)
         logger.info('Writing deployment file: ' + deployConfigFilename)



More information about the checkins mailing list