Changeset View
Changeset View
Standalone View
Standalone View
pykolab/xml/utils.py
Show First 20 Lines • Show All 152 Lines • ▼ Show 20 Lines | property_labels = { | ||||
"classification": N_("Classification"), | "classification": N_("Classification"), | ||||
"percent-complete": N_("Progress") | "percent-complete": N_("Progress") | ||||
} | } | ||||
def property_label(propname): | def property_label(propname): | ||||
""" | """ | ||||
Return a localized name for the given object property | Return a localized name for the given object property | ||||
""" | """ | ||||
return _(property_labels[propname]) if property_labels.has_key(propname) else _(propname) | return _(property_labels[propname]) if propname in property_labels else _(propname) | ||||
def property_to_string(propname, value): | def property_to_string(propname, value): | ||||
""" | """ | ||||
Render a human readable string for the given object property | Render a human readable string for the given object property | ||||
""" | """ | ||||
date_format = _("%Y-%m-%d") | date_format = _("%Y-%m-%d") | ||||
time_format = _("%H:%M (%Z)") | time_format = _("%H:%M (%Z)") | ||||
Show All 11 Lines | elif isinstance(value, str): | ||||
return value[:maxlen].rsplit(' ', 1)[0] + '...' | return value[:maxlen].rsplit(' ', 1)[0] + '...' | ||||
return value | return value | ||||
elif isinstance(value, object) and hasattr(value, 'to_dict'): | elif isinstance(value, object) and hasattr(value, 'to_dict'): | ||||
value = value.to_dict() | value = value.to_dict() | ||||
if isinstance(value, dict): | if isinstance(value, dict): | ||||
if propname == 'attendee': | if propname == 'attendee': | ||||
from . import attendee | from . import attendee | ||||
name = value['name'] if value.has_key('name') and not value['name'] == '' else value['email'] | name = value['name'] if 'name' in value and not value['name'] == '' else value['email'] | ||||
return "%s, %s" % (name, attendee.participant_status_label(value['partstat'])) | return "%s, %s" % (name, attendee.participant_status_label(value['partstat'])) | ||||
elif propname == 'organizer': | elif propname == 'organizer': | ||||
return value['name'] if value.has_key('name') and not value['name'] == '' else value['email'] | return value['name'] if 'name' in value and not value['name'] == '' else value['email'] | ||||
elif propname == 'rrule': | elif propname == 'rrule': | ||||
from . import recurrence_rule | from . import recurrence_rule | ||||
rrule = recurrence_rule.frequency_label(value['freq']) % (value['interval']) | rrule = recurrence_rule.frequency_label(value['freq']) % (value['interval']) | ||||
if value.has_key('count') and value['count'] > 0: | if 'count' in value and value['count'] > 0: | ||||
rrule += " " + _("for %d times") % (value['count']) | rrule += " " + _("for %d times") % (value['count']) | ||||
elif value.has_key('until') and (isinstance(value['until'], datetime.datetime) or isinstance(value['until'], datetime.date)): | elif 'until' in value and (isinstance(value['until'], datetime.datetime) or isinstance(value['until'], datetime.date)): | ||||
rrule += " " + _("until %s") % (value['until'].strftime(date_format)) | rrule += " " + _("until %s") % (value['until'].strftime(date_format)) | ||||
return rrule | return rrule | ||||
elif propname == 'alarm': | elif propname == 'alarm': | ||||
alarm_type_labels = { | alarm_type_labels = { | ||||
'DISPLAY': _("Display message"), | 'DISPLAY': _("Display message"), | ||||
'EMAIL': _("Send email"), | 'EMAIL': _("Send email"), | ||||
'AUDIO': _("Play sound") | 'AUDIO': _("Play sound") | ||||
Show All 21 Lines | if isinstance(value, dict): | ||||
if minutes or (hours and seconds): | if minutes or (hours and seconds): | ||||
offsets.append(_("%d minute(s)") % (minutes)) | offsets.append(_("%d minute(s)") % (minutes)) | ||||
if len(offsets): | if len(offsets): | ||||
alarm += " " + rel % (", ".join(offsets)) | alarm += " " + rel % (", ".join(offsets)) | ||||
return alarm | return alarm | ||||
elif propname == 'attach': | elif propname == 'attach': | ||||
return value['label'] if value.has_key('label') else value['fmttype'] | return value['label'] if 'label' in value else value['fmttype'] | ||||
return None | return None | ||||
def compute_diff(a, b, reduced=False): | def compute_diff(a, b, reduced=False): | ||||
""" | """ | ||||
List the differences between two given dicts | List the differences between two given dicts | ||||
""" | """ | ||||
diff = [] | diff = [] | ||||
properties = a.keys() | properties = a.keys() | ||||
properties.extend([x for x in b.keys() if x not in properties]) | properties.extend([x for x in b if x not in properties]) | ||||
for prop in properties: | for prop in properties: | ||||
aa = a[prop] if a.has_key(prop) else None | aa = a[prop] if prop in a else None | ||||
bb = b[prop] if b.has_key(prop) else None | bb = b[prop] if prop in b else None | ||||
# compare two lists | # compare two lists | ||||
if isinstance(aa, list) or isinstance(bb, list): | if isinstance(aa, list) or isinstance(bb, list): | ||||
if not isinstance(aa, list): | if not isinstance(aa, list): | ||||
aa = [aa] | aa = [aa] | ||||
if not isinstance(bb, list): | if not isinstance(bb, list): | ||||
bb = [bb] | bb = [bb] | ||||
▲ Show 20 Lines • Show All 70 Lines • ▼ Show 20 Lines | if isinstance(aa, dict) and isinstance(bb, dict): | ||||
bb = dict(bb) | bb = dict(bb) | ||||
# ignore some properties for comparison | # ignore some properties for comparison | ||||
for k in ignore_keys: | for k in ignore_keys: | ||||
aa.pop(k, None) | aa.pop(k, None) | ||||
bb.pop(k, None) | bb.pop(k, None) | ||||
# accept partial match | # accept partial match | ||||
if partial: | if partial: | ||||
for k,v in aa.iteritems(): | for k,v in aa.items(): | ||||
if bb.has_key(k) and bb[k] == v: | if k in bb and bb[k] == v: | ||||
return True | return True | ||||
return False | return False | ||||
return aa == bb | return aa == bb | ||||
def reduce_properties(aa, bb): | def reduce_properties(aa, bb): | ||||
""" | """ | ||||
Compares two given structs and removes equal values in bb | Compares two given structs and removes equal values in bb | ||||
""" | """ | ||||
if not isinstance(aa, dict) or not isinstance(bb, dict): | if not isinstance(aa, dict) or not isinstance(bb, dict): | ||||
return (aa, bb) | return (aa, bb) | ||||
properties = aa.keys() | properties = aa.keys() | ||||
properties.extend([x for x in bb.keys() if x not in properties]) | properties.extend([x for x in bb if x not in properties]) | ||||
for prop in properties: | for prop in properties: | ||||
if not aa.has_key(prop) or not bb.has_key(prop): | if prop not in aa or prop not in bb: | ||||
continue | continue | ||||
if isinstance(aa[prop], dict) and isinstance(bb[prop], dict): | if isinstance(aa[prop], dict) and isinstance(bb[prop], dict): | ||||
(aa[prop], bb[prop]) = reduce_properties(aa[prop], bb[prop]) | (aa[prop], bb[prop]) = reduce_properties(aa[prop], bb[prop]) | ||||
if aa[prop] == bb[prop]: | if aa[prop] == bb[prop]: | ||||
# del aa[prop] | # del aa[prop] | ||||
del bb[prop] | del bb[prop] | ||||
return (aa, bb) | return (aa, bb) |