1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """
24 Base type for all gramps types.
25 """
26
27
28
29
30
31
32 from gettext import gettext as _
33
35 """
36 Initialize the map, building a new map from the specified columns.
37 """
38 new_data = {}
39 for item in data:
40 new_data[item[key_col]] = item[data_col]
41 return new_data
42
44
45 _CUSTOM = 0
46 _DEFAULT = 0
47
48 _DATAMAP = []
49
50 _I2SMAP = init_map(_DATAMAP, 0, 1)
51 _S2IMAP = init_map(_DATAMAP, 1, 0)
52 _I2EMAP = init_map(_DATAMAP, 0, 2)
53 _E2IMAP = init_map(_DATAMAP, 2, 0)
54
56 """
57 Create a new type, initialize the value from one of several possible
58 states.
59 """
60 if value:
61 self.set(value)
62 else:
63 self.val = self._DEFAULT
64 self.string = u''
65
67 self.val = value[0]
68 self.string = value[1]
69
71 self.val = value
72 self.string = u''
73
75 self.val = value.val
76 self.string = value.string
77
84
85 - def set(self, value):
86 if type(value) == tuple:
87 self.__set_tuple(value)
88 elif type(value) == int:
89 self.__set_int(value)
90 elif isinstance(value, self.__class__):
91 self.__set_instance(value)
92 elif type(value) in (str,unicode):
93 self.__set_str(value)
94 else:
95 self.val = self._DEFAULT
96 self.string = u''
97
99 """
100 This method sets the type instance based on the untranslated string
101 (obtained e.g. from XML).
102 """
103 if self._E2IMAP.has_key(value):
104 self.val = self._E2IMAP[value]
105 self.string = u''
106 else:
107 self.val = self._CUSTOM
108 self.string = value
109
111 """
112 Return the untranslated string (e.g. suitable for XML) corresponding
113 to the type.
114 """
115 if self.val == self._CUSTOM:
116 return self.string
117 else:
118 return self._I2EMAP[self.val]
119
121 """
122 Convert the object to a serialized tuple of data.
123 """
124 return (self.val, self.string)
125
127 """
128 Convert a serialized tuple of data to an object.
129 """
130 self.val, self.string = data
131
133 if self.val == self._CUSTOM:
134 return self.string
135 else:
136 return self._I2SMAP.get(self.val, _('Unknown'))
137
140
143
145 """
146 Return the list of localized names for all standard types.
147 """
148 return [s for (i, s) in self._I2SMAP.items()
149 if (i != self._CUSTOM) and s.strip()]
150
152 """
153 Return the list of XML (english) names for all standard types.
154 """
155 return [s for (i, s) in self._I2EMAP.items()
156 if (i != self._CUSTOM) and s.strip()]
157
160
163
166
168 if type(value) == int:
169 return cmp(self.val, value)
170 elif type(value) in (str, unicode):
171 if self.val == self._CUSTOM:
172 return cmp(self.string, value)
173 else:
174 return cmp(self._I2SMAP.get(self.val), value)
175 elif type(value) == tuple:
176 return cmp((self.val, self.string), value)
177 else:
178 if value.val == self._CUSTOM:
179 return cmp(self.string, value.string)
180 else:
181 return cmp(self.val, value.val)
182