1 # misc.py -- For dealing with python2.4 oddness
2 # Copyright (C) 2008 Canonical Ltd.
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; version 2
7 # of the License or (at your option) a later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 """Misc utilities to work with python <2.6.
21 These utilities can all be deleted when dulwich decides it wants to stop
22 support for python <2.6.
30 from urlparse import parse_qs
32 from cgi import parse_qs
35 from os import SEEK_END
42 class defaultdict(dict):
43 """A python 2.4 equivalent of collections.defaultdict."""
45 def __init__(self, default_factory=None, *a, **kw):
46 if (default_factory is not None and
47 not hasattr(default_factory, '__call__')):
48 raise TypeError('first argument must be callable')
49 dict.__init__(self, *a, **kw)
50 self.default_factory = default_factory
52 def __getitem__(self, key):
54 return dict.__getitem__(self, key)
56 return self.__missing__(key)
58 def __missing__(self, key):
59 if self.default_factory is None:
61 self[key] = value = self.default_factory()
65 if self.default_factory is None:
68 args = self.default_factory,
69 return type(self), args, None, None, self.items()
72 return self.__copy__()
75 return type(self)(self.default_factory, self)
77 def __deepcopy__(self, memo):
79 return type(self)(self.default_factory,
80 copy.deepcopy(self.items()))
82 return 'defaultdict(%s, %s)' % (self.default_factory,
86 def make_sha(source=''):
87 """A python2.4 workaround for the sha/hashlib module fiasco."""
89 return hashlib.sha1(source)
91 sha1 = sha.sha(source)
95 def unpack_from(fmt, buf, offset=0):
96 """A python2.4 workaround for struct missing unpack_from."""
98 return struct.unpack_from(fmt, buf, offset)
99 except AttributeError:
100 b = buf[offset:offset+struct.calcsize(fmt)]
101 return struct.unpack(fmt, b)