diff options
author | Bastian Blank <waldi@debian.org> | 2008-01-18 22:45:56 +0000 |
---|---|---|
committer | Bastian Blank <waldi@debian.org> | 2008-01-18 22:45:56 +0000 |
commit | 4b462b2692d74c2d077db296c7d58b00dbbca854 (patch) | |
tree | 95815d9aabe2a2bf6f2e8afbbfbf26d0b8c45779 /debian/lib/python/debian_linux/utils.py | |
parent | de44b72c27a0af1ef1de9d309878fba6e6e797bb (diff) | |
download | kernel_replicant_linux-4b462b2692d74c2d077db296c7d58b00dbbca854.tar.gz kernel_replicant_linux-4b462b2692d74c2d077db296c7d58b00dbbca854.tar.bz2 kernel_replicant_linux-4b462b2692d74c2d077db296c7d58b00dbbca854.zip |
debian/lib/python/debian_linux/utils.py (Templates)
- Add get method.
- Handle KeyError like dict.
svn path=/dists/trunk/linux-2.6/; revision=10135
Diffstat (limited to 'debian/lib/python/debian_linux/utils.py')
-rw-r--r-- | debian/lib/python/debian_linux/utils.py | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/debian/lib/python/debian_linux/utils.py b/debian/lib/python/debian_linux/utils.py index abe46b7b3d40..b0ff1a6e2617 100644 --- a/debian/lib/python/debian_linux/utils.py +++ b/debian/lib/python/debian_linux/utils.py @@ -1,5 +1,7 @@ import debian, re, os, textwrap +_marker = object + class SortedDict(dict): __slots__ = '_list', @@ -36,12 +38,7 @@ class Templates(dict): self.dirs = dirs def __getitem__(self, key): - try: - return super(Templates, self).__getitem__(key) - except KeyError: pass - value = self._read(key) - super(Templates, self).__setitem__(key, value) - return value + return self.get(key) def __setitem__(self, key, value): raise NotImplemented() @@ -56,7 +53,6 @@ class Templates(dict): if prefix == 'control': return self._read_control(f) return f.read() - raise KeyError(name) def _read_control(self, f): entries = [] @@ -93,6 +89,18 @@ class Templates(dict): return entries + def get(self, key, default = _marker): + ret = super(Templates, self).get(key, _marker) + if ret is not _marker: + return ret + value = self._read(key) + if value is None: + if default is _marker: + raise KeyError(key) + return default + super(Templates, self).__setitem__(key, value) + return value + class TextWrapper(textwrap.TextWrapper): wordsep_re = re.compile( r'(\s+|' # any whitespace |