Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
removed dependency on python 2.6 ConfigParser dict_type feature
  • Loading branch information
cdavis committed Dec 13, 2009
1 parent 3bc9949 commit 47deba4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 64 deletions.
27 changes: 26 additions & 1 deletion carbon/lib/carbon/conf.py
Expand Up @@ -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__

Expand Down
5 changes: 2 additions & 3 deletions 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 = []
Expand All @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions carbon/lib/carbon/storage.py
Expand Up @@ -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
Expand Down Expand Up @@ -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():
Expand Down
56 changes: 0 additions & 56 deletions carbon/lib/carbon/util.py
Expand Up @@ -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]

0 comments on commit 47deba4

Please sign in to comment.