Changeset View
Changeset View
Standalone View
Standalone View
pykolab/xml/contact.py
Show First 20 Lines • Show All 154 Lines • ▼ Show 20 Lines | class Contact(kolabformat.Contact): | ||||
def get_gender(self, translated=True): | def get_gender(self, translated=True): | ||||
_gender = self.gender() | _gender = self.gender() | ||||
if translated: | if translated: | ||||
return self._translate_value(_gender, self.gender_map) | return self._translate_value(_gender, self.gender_map) | ||||
return _gender | return _gender | ||||
def _translate_value(self, val, map): | def _translate_value(self, val, map): | ||||
name_map = dict([(v, k) for (k, v) in map.iteritems()]) | name_map = dict([(v, k) for (k, v) in map.items()]) | ||||
return name_map[val] if name_map.has_key(val) else 'UNKNOWN' | return name_map[val] if name_map.has_key(val) else 'UNKNOWN' | ||||
def to_dict(self): | def to_dict(self): | ||||
if not self.isValid(): | if not self.isValid(): | ||||
return None | return None | ||||
data = self._names2dict(self.nameComponents()) | data = self._names2dict(self.nameComponents()) | ||||
for p, getter in self.properties_map.iteritems(): | for p, getter in self.properties_map.items(): | ||||
val = None | val = None | ||||
if hasattr(self, getter): | if hasattr(self, getter): | ||||
val = getattr(self, getter)() | val = getattr(self, getter)() | ||||
if isinstance(val, kolabformat.cDateTime): | if isinstance(val, kolabformat.cDateTime): | ||||
val = xmlutils.from_cdatetime(val, True) | val = xmlutils.from_cdatetime(val, True) | ||||
elif isinstance(val, kolabformat.vectori): | elif isinstance(val, kolabformat.vectori): | ||||
val = [int(x) for x in val] | val = [int(x) for x in val] | ||||
elif isinstance(val, kolabformat.vectors): | elif isinstance(val, kolabformat.vectors): | ||||
Show All 37 Lines | def _names2dict(self, namecomp): | ||||
'given': 'given', | 'given': 'given', | ||||
'additional': 'additional', | 'additional': 'additional', | ||||
'prefix': 'prefixes', | 'prefix': 'prefixes', | ||||
'suffix': 'suffixes', | 'suffix': 'suffixes', | ||||
} | } | ||||
data = dict() | data = dict() | ||||
for p, getter in names_map.iteritems(): | for p, getter in names_map.items(): | ||||
val = None | val = None | ||||
if hasattr(namecomp, getter): | if hasattr(namecomp, getter): | ||||
val = getattr(namecomp, getter)() | val = getattr(namecomp, getter)() | ||||
if isinstance(val, kolabformat.vectors): | if isinstance(val, kolabformat.vectors): | ||||
val = [str(x) for x in val][0] if len(val) > 0 else None | val = [str(x) for x in val][0] if len(val) > 0 else None | ||||
if val is not None: | if val is not None: | ||||
data[p] = val | data[p] = val | ||||
return data | return data | ||||
def _affiliation2dict(self, affiliation): | def _affiliation2dict(self, affiliation): | ||||
props_map = { | props_map = { | ||||
'organization': 'organisation', | 'organization': 'organisation', | ||||
'department': 'organisationalUnits', | 'department': 'organisationalUnits', | ||||
'role': 'roles', | 'role': 'roles', | ||||
} | } | ||||
data = dict() | data = dict() | ||||
for p, getter in props_map.iteritems(): | for p, getter in props_map.items(): | ||||
val = None | val = None | ||||
if hasattr(affiliation, getter): | if hasattr(affiliation, getter): | ||||
val = getattr(affiliation, getter)() | val = getattr(affiliation, getter)() | ||||
if isinstance(val, kolabformat.vectors): | if isinstance(val, kolabformat.vectors): | ||||
val = [str(x) for x in val][0] if len(val) > 0 else None | val = [str(x) for x in val][0] if len(val) > 0 else None | ||||
if val is not None: | if val is not None: | ||||
data[p] = val | data[p] = val | ||||
Show All 9 Lines | def _address2dict(self, adr, adrtype=None): | ||||
props_map = { | props_map = { | ||||
'label': 'label', | 'label': 'label', | ||||
'street': 'street', | 'street': 'street', | ||||
'locality': 'locality', | 'locality': 'locality', | ||||
'region': 'region', | 'region': 'region', | ||||
'code': 'code', | 'code': 'code', | ||||
'country': 'country', | 'country': 'country', | ||||
} | } | ||||
addresstype_map = dict([(v, k) for (k, v) in self.addresstype_map.iteritems()]) | addresstype_map = dict([(v, k) for (k, v) in self.addresstype_map.items()]) | ||||
data = dict() | data = dict() | ||||
if adrtype is None: | if adrtype is None: | ||||
adrtype = addresstype_map.get(adr.types(), None) | adrtype = addresstype_map.get(adr.types(), None) | ||||
if adrtype is not None: | if adrtype is not None: | ||||
data['type'] = adrtype | data['type'] = adrtype | ||||
for p, getter in props_map.iteritems(): | for p, getter in props_map.items(): | ||||
val = None | val = None | ||||
if hasattr(adr, getter): | if hasattr(adr, getter): | ||||
val = getattr(adr, getter)() | val = getattr(adr, getter)() | ||||
if isinstance(val, kolabformat.vectors): | if isinstance(val, kolabformat.vectors): | ||||
val = [str(x) for x in val][0] if len(val) > 0 else None | val = [str(x) for x in val][0] if len(val) > 0 else None | ||||
if val is not None: | if val is not None: | ||||
data[p] = val | data[p] = val | ||||
return data | return data | ||||
def _relateds2dict(self, relateds, aslist=True): | def _relateds2dict(self, relateds, aslist=True): | ||||
data = dict() | data = dict() | ||||
related_map = dict([(v, k) for (k, v) in self.related_map.iteritems()]) | related_map = dict([(v, k) for (k, v) in self.related_map.items()]) | ||||
for rel in relateds: | for rel in relateds: | ||||
reltype = related_map.get(rel.relationTypes(), None) | reltype = related_map.get(rel.relationTypes(), None) | ||||
val = rel.uri() if rel.type() == kolabformat.Related.Uid else rel.text() | val = rel.uri() if rel.type() == kolabformat.Related.Uid else rel.text() | ||||
if reltype and val is not None: | if reltype and val is not None: | ||||
if aslist: | if aslist: | ||||
if not data.has_key(reltype): | if not data.has_key(reltype): | ||||
data[reltype] = [] | data[reltype] = [] | ||||
data[reltype].append(val) | data[reltype].append(val) | ||||
else: | else: | ||||
data[reltype] = val | data[reltype] = val | ||||
return data | return data | ||||
def _struct2dict(self, struct, propname, map): | def _struct2dict(self, struct, propname, map): | ||||
type_map = dict([(v, k) for (k, v) in map.iteritems()]) | type_map = dict([(v, k) for (k, v) in map.items()]) | ||||
result = dict() | result = dict() | ||||
if hasattr(struct, 'types'): | if hasattr(struct, 'types'): | ||||
result['type'] = type_map.get(struct.types(), None) | result['type'] = type_map.get(struct.types(), None) | ||||
elif hasattr(struct, 'type'): | elif hasattr(struct, 'type'): | ||||
result['type'] = type_map.get(struct.type(), None) | result['type'] = type_map.get(struct.type(), None) | ||||
if hasattr(struct, propname): | if hasattr(struct, propname): | ||||
Show All 17 Lines |