another distcheck fix
[debian/gnuradio] / grc / src / grc / Utils.py
1 """
2 Copyright 2008 Free Software Foundation, Inc.
3 This file is part of GNU Radio
4
5 GNU Radio Companion is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 GNU Radio Companion is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18 """
19 ##@package grc.gui.Utils
20 #Utility methods and classes.
21
22 from UserDict import DictMixin
23
24 class odict(DictMixin):
25
26         def __init__(self, d={}):
27                 self._keys = list(d.keys())
28                 self._data = dict(d.copy())
29
30         def __setitem__(self, key, value):
31                 if key not in self._data:
32                         self._keys.append(key)
33                 self._data[key] = value
34
35         def __getitem__(self, key):
36                 return self._data[key]
37
38         def __delitem__(self, key):
39                 del self._data[key]
40                 self._keys.remove(key)
41
42         def keys(self):
43                 return list(self._keys)
44
45         def copy(self):
46                 copy_dict = odict()
47                 copy_dict._data = self._data.copy()
48                 copy_dict._keys = list(self._keys)
49                 return copy_dict
50
51 def exists_or_else(d, key, alt):
52         if d.has_key(key): return d[key]
53         else: return alt
54
55 def listify(d, key):
56         obj = exists_or_else(d, key, [])
57         if isinstance(obj, list): return obj
58         return [obj]
59