a9d055f5b83acd80a4ba2f987edd1d490ccf284b
[pub/Android/ownCloud.git] / third_party / transifex-client / txclib / config.py
1 import ConfigParser
2
3
4 class OrderedRawConfigParser( ConfigParser.RawConfigParser ):
5 """
6 Overload standard Class ConfigParser.RawConfigParser
7 """
8 def write(self, fp):
9 """Write an .ini-format representation of the configuration state."""
10 if self._defaults:
11 fp.write("[%s]\n" % DEFAULTSECT)
12 for key in sorted( self._defaults ):
13 fp.write( "%s = %s\n" % (key, str( self._defaults[ key ]
14 ).replace('\n', '\n\t')) )
15 fp.write("\n")
16 for section in self._sections:
17 fp.write("[%s]\n" % section)
18 for key in sorted( self._sections[section] ):
19 if key != "__name__":
20 fp.write("%s = %s\n" %
21 (key, str( self._sections[section][ key ]
22 ).replace('\n', '\n\t')))
23 fp.write("\n")
24
25 optionxform = str
26
27
28 _NOTFOUND = object()
29
30
31 class Flipdict(dict):
32 """An injective (one-to-one) python dict. Ensures that each key maps
33 to a unique value, and each value maps back to that same key.
34
35 Code mostly taken from here:
36 http://code.activestate.com/recipes/576968-flipdict-python-dict-that-also-maintains-a-one-to-/
37 """
38
39 def __init__(self, *args, **kw):
40 self._flip = dict.__new__(self.__class__)
41 setattr(self._flip, "_flip", self)
42 for key, val in dict(*args, **kw).iteritems():
43 self[key] = val
44
45 @property
46 def flip(self):
47 """The inverse mapping."""
48 return self._flip
49
50 def __repr__(self):
51 return "%s(%r)" % (self.__class__.__name__, dict(self))
52
53 __str__ = __repr__
54
55 def copy(self):
56 return self.__class__(self)
57
58 @classmethod
59 def fromkeys(cls, keys, value=None):
60 return cls(dict.fromkeys(keys, value))
61
62 def __setitem__(self, key, val):
63 k = self._flip.get(val, _NOTFOUND)
64 if not (k is _NOTFOUND or k==key):
65 raise KeyError('(key,val) would erase mapping for value %r' % val)
66
67 v = self.get(key, _NOTFOUND)
68 if v is not _NOTFOUND:
69 dict.__delitem__(self._flip, v)
70
71 dict.__setitem__(self, key, val)
72 dict.__setitem__(self._flip, val, key)
73
74 def setdefault(self, key, default = None):
75 # Copied from python's UserDict.DictMixin code.
76 try:
77 return self[key]
78 except KeyError:
79 self[key] = default
80 return default
81
82 def update(self, other = None, **kwargs):
83 # Copied from python's UserDict.DictMixin code.
84 # Make progressively weaker assumptions about "other"
85 if other is None:
86 pass
87 elif hasattr(other, 'iteritems'): # iteritems saves memory and lookups
88 for k, v in other.iteritems():
89 self[k] = v
90 elif hasattr(other, 'keys'):
91 for k in other.keys():
92 self[k] = other[k]
93 else:
94 for k, v in other:
95 self[k] = v
96 if kwargs:
97 self.update(kwargs)
98
99 def __delitem__(self, key):
100 val = dict.pop(self, key)
101 dict.__delitem__(self._flip, val)
102
103 def pop(self, key, *args):
104 val = dict.pop(self, key, *args)
105 dict.__delitem__(self._flip, val)
106 return val
107
108 def popitem(self):
109 key, val = dict.popitem(self)
110 dict.__delitem__(self._flip, val)
111 return key, val
112
113 def clear(self):
114 dict.clear(self)
115 dict.clear(self._flip)