From 47deba4588cf775c2e782625a2f1bd1ad9c5795d Mon Sep 17 00:00:00 2001 From: Chris Davis Date: Sat, 12 Dec 2009 19:35:28 -0600 Subject: [PATCH] removed dependency on python 2.6 ConfigParser dict_type feature --- carbon/lib/carbon/conf.py | 27 ++++++++++++++++- carbon/lib/carbon/rules.py | 5 ++-- carbon/lib/carbon/storage.py | 6 ++-- carbon/lib/carbon/util.py | 56 ------------------------------------ 4 files changed, 30 insertions(+), 64 deletions(-) diff --git a/carbon/lib/carbon/conf.py b/carbon/lib/carbon/conf.py index 6b56baa2e..9f5d6faca 100644 --- a/carbon/lib/carbon/conf.py +++ b/carbon/lib/carbon/conf.py @@ -28,9 +28,34 @@ PICKLE_RECEIVER_PORT=2004, CACHE_QUERY_INTERFACE='0.0.0.0', CACHE_QUERY_PORT=7002, - ) + +class OrderedConfigParser(ConfigParser): + """Hacky workaround to ensure sections are always returned in the order + they are defined in. Note that this does *not* make any guarantees about + the order of options within a section or the order in which sections get + written back to disk on write().""" + _ordered_sections = [] + + def read(self, path): + result = ConfigParser.read(self, path) + + sections = [] + for line in open(path): + line = line.strip() + + if line.startswith('[') and line.endswith(']'): + sections.append( line[1:-1] ) + + self._ordered_sections = sections + + return result + + def sections(self): + return list( self._ordered_sections ) # return a copy for safety + + class Settings(dict): __getattr__ = dict.__getitem__ diff --git a/carbon/lib/carbon/rules.py b/carbon/lib/carbon/rules.py index 56787b336..961a4335a 100644 --- a/carbon/lib/carbon/rules.py +++ b/carbon/lib/carbon/rules.py @@ -1,6 +1,5 @@ import re -from ConfigParser import ConfigParser -from carbon.util import OrderedDict +from carbon.conf import OrderedConfigParser rules = [] @@ -20,7 +19,7 @@ def loadRules(path): global defaultRule assert not rules, "rules already loaded" - parser = ConfigParser(dict_type=OrderedDict) + parser = OrderedConfigParser() if not parser.read(path): raise ValueError("Could not read rules file %s" % path) diff --git a/carbon/lib/carbon/storage.py b/carbon/lib/carbon/storage.py index 68cd0971f..5acd359a9 100644 --- a/carbon/lib/carbon/storage.py +++ b/carbon/lib/carbon/storage.py @@ -14,9 +14,7 @@ import os, re from os.path import join, exists -from ConfigParser import ConfigParser -from carbon.conf import Settings, settings -from carbon.util import OrderedDict +from carbon.conf import OrderedConfigParser, Settings, settings try: import cPickle as pickle @@ -107,7 +105,7 @@ def fromString(string): def loadStorageSchemas(): schemaList = [] - config = ConfigParser(dict_type=OrderedDict) + config = OrderedConfigParser() config.read(STORAGE_SCHEMAS_CONFIG) for section in config.sections(): diff --git a/carbon/lib/carbon/util.py b/carbon/lib/carbon/util.py index a2522aec5..f2abad388 100644 --- a/carbon/lib/carbon/util.py +++ b/carbon/lib/carbon/util.py @@ -20,59 +20,3 @@ def dropprivs(user): os.setregid(gid,gid) os.setreuid(uid,uid) return (uid,gid) - - -class OrderedDict(dict): - def __init__(self, *args, **kwargs): - dict.__init__(self, *args, **kwargs) - self._items = dict.items(self) - - def __setitem__(self, key, value): - if key in self: - oldValue = self[key] - self._items.remove( (key,oldValue) ) - - dict.__setitem__(self, key, value) - self._items.append( (key,value) ) - - def items(self): - return self._items - - def iteritems(self): - return iter( self._items ) - - def keys(self): - return [key for key,value in self._items] - - def values(self): - return [value for key,value in self._items] - - def iterkeys(self): - return iter(key for key,value in self._items) - - __iter__ = iterkeys - - def itervalues(self): - return iter(value for key,value in self._items) - - def __delitem__(self, key): - dict.__delitem__(self, key) - self._items = [item for item in self._items if hash(item[0]) != hash(key)] - - def clear(self): - dict.clear(self) - self._items = [] - - def pop(self, key): - value = dict.pop(self, key) - self._items.remove( (key,value) ) - return value - - def popitem(self): - item = dict.popitem(self) - self._items.remove(item) - return item - - def update(self, otherDict): - for key in otherDict: - self[key] = otherDict[key]