Package gen :: Package lib :: Module grampstype
[frames] | no frames]

Source Code for Module gen.lib.grampstype

  1  # 
  2  # Gramps - a GTK+/GNOME based genealogy program 
  3  # 
  4  # Copyright (C) 2000-2006  Donald N. Allingham 
  5  # 
  6  # This program is free software; you can redistribute it and/or modify 
  7  # it under the terms of the GNU General Public License as published by 
  8  # the Free Software Foundation; either version 2 of the License, or 
  9  # (at your option) any later version. 
 10  # 
 11  # This program is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  # GNU General Public License for more details. 
 15  # 
 16  # You should have received a copy of the GNU General Public License 
 17  # along with this program; if not, write to the Free Software 
 18  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 19  # 
 20   
 21  # $Id: grampstype.py 10103 2008-02-24 13:55:55Z acraphae $ 
 22   
 23  """ 
 24  Base type for all gramps types. 
 25  """ 
 26   
 27  #------------------------------------------------------------------------ 
 28  # 
 29  # Python modules 
 30  # 
 31  #------------------------------------------------------------------------ 
 32  from gettext import gettext as _ 
 33   
34 -def init_map(data, key_col, data_col):
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
43 -class GrampsType:
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
55 - def __init__(self, value=None):
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
66 - def __set_tuple(self, value):
67 self.val = value[0] 68 self.string = value[1]
69
70 - def __set_int(self, value):
71 self.val = value 72 self.string = u''
73
74 - def __set_instance(self, value):
75 self.val = value.val 76 self.string = value.string
77
78 - def __set_str(self, value):
79 self.val = self._S2IMAP.get(value, self._CUSTOM) 80 if self.val == self._CUSTOM: 81 self.string = value 82 else: 83 self.string = u''
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
98 - def set_from_xml_str(self, value):
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
110 - def xml_str(self):
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
120 - def serialize(self):
121 """ 122 Convert the object to a serialized tuple of data. 123 """ 124 return (self.val, self.string)
125
126 - def unserialize(self, data):
127 """ 128 Convert a serialized tuple of data to an object. 129 """ 130 self.val, self.string = data
131
132 - def __str__(self):
133 if self.val == self._CUSTOM: 134 return self.string 135 else: 136 return self._I2SMAP.get(self.val, _('Unknown'))
137
138 - def __int__(self):
139 return self.val
140
141 - def get_map(self):
142 return self._I2SMAP
143
144 - def get_standard_names(self):
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
151 - def get_standard_xml(self):
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
158 - def is_custom(self):
159 return self.val == self._CUSTOM
160
161 - def is_default(self):
162 return self.val == self._DEFAULT
163
164 - def get_custom(self):
165 return self._CUSTOM
166
167 - def __cmp__(self, value):
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