Imported Upstream version 3.2.2
[debian/gnuradio] / grc / base / odict.py
1 """
2 Copyright 2008, 2009 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
20 from UserDict import DictMixin
21
22 class odict(DictMixin):
23
24         def __init__(self, d={}):
25                 self._keys = list(d.keys())
26                 self._data = dict(d.copy())
27
28         def __setitem__(self, key, value):
29                 if key not in self._data:
30                         self._keys.append(key)
31                 self._data[key] = value
32
33         def __getitem__(self, key):
34                 return self._data[key]
35
36         def __delitem__(self, key):
37                 del self._data[key]
38                 self._keys.remove(key)
39
40         def keys(self):
41                 return list(self._keys)
42
43         def copy(self):
44                 copy_dict = odict()
45                 copy_dict._data = self._data.copy()
46                 copy_dict._keys = list(self._keys)
47                 return copy_dict
48
49         def insert_after(self, pos_key, key, val):
50                 """
51                 Insert the new key, value entry after the entry given by the position key.
52                 If the positional key is None, insert at the end.
53                 @param pos_key the positional key
54                 @param key the key for the new entry
55                 @param val the value for the new entry
56                 """
57                 index = (pos_key is None) and len(self._keys) or self._keys.index(pos_key)
58                 assert key not in self._keys
59                 self._keys.insert(index+1, key)
60                 self._data[key] = val
61
62         def insert_before(self, pos_key, key, val):
63                 """
64                 Insert the new key, value entry before the entry given by the position key.
65                 If the positional key is None, insert at the begining.
66                 @param pos_key the positional key
67                 @param key the key for the new entry
68                 @param val the value for the new entry
69                 """
70                 index = (pos_key is not None) and self._keys.index(pos_key) or 0 
71                 assert key not in self._keys
72                 self._keys.insert(index, key)
73                 self._data[key] = val
74
75         def find(self, key):
76                 """
77                 Get the value for this key if exists.
78                 @param key the key to search for
79                 @return the value or None
80                 """
81                 if self.has_key(key): return self[key]
82                 return None
83
84         def findall(self, key):
85                 """
86                 Get a list of values for this key.
87                 @param key the key to search for
88                 @return a list of values or empty list
89                 """
90                 obj = self.find(key)
91                 if obj is None: obj = list()
92                 if isinstance(obj, list): return obj
93                 return [obj]