Immutable wrapper class for VCALENDAR (VEVENT, VTODO) and VCARD
| 33 | |
| 34 | |
| 35 | class Item: |
| 36 | |
| 37 | '''Immutable wrapper class for VCALENDAR (VEVENT, VTODO) and |
| 38 | VCARD''' |
| 39 | |
| 40 | def __init__(self, raw): |
| 41 | assert isinstance(raw, str), type(raw) |
| 42 | self._raw = raw |
| 43 | |
| 44 | def with_uid(self, new_uid): |
| 45 | parsed = _Component.parse(self.raw) |
| 46 | stack = [parsed] |
| 47 | while stack: |
| 48 | component = stack.pop() |
| 49 | stack.extend(component.subcomponents) |
| 50 | |
| 51 | if component.name in ('VEVENT', 'VTODO', 'VJOURNAL', 'VCARD'): |
| 52 | del component['UID'] |
| 53 | if new_uid: |
| 54 | component['UID'] = new_uid |
| 55 | |
| 56 | return Item('\r\n'.join(parsed.dump_lines())) |
| 57 | |
| 58 | @cached_property |
| 59 | def raw(self): |
| 60 | '''Raw content of the item, as unicode string. |
| 61 | |
| 62 | Vdirsyncer doesn't validate the content in any way. |
| 63 | ''' |
| 64 | return self._raw |
| 65 | |
| 66 | @cached_property |
| 67 | def uid(self): |
| 68 | '''Global identifier of the item, across storages, doesn't change after |
| 69 | a modification of the item.''' |
| 70 | # Don't actually parse component, but treat all lines as single |
| 71 | # component, avoiding traversal through all subcomponents. |
| 72 | x = _Component('TEMP', self.raw.splitlines(), []) |
| 73 | try: |
| 74 | return x['UID'].strip() or None |
| 75 | except KeyError: |
| 76 | return None |
| 77 | |
| 78 | @cached_property |
| 79 | def hash(self): |
| 80 | '''Hash of self.raw, used for etags.''' |
| 81 | return hash_item(self.raw) |
| 82 | |
| 83 | @cached_property |
| 84 | def ident(self): |
| 85 | '''Used for generating hrefs and matching up items during |
| 86 | synchronization. This is either the UID or the hash of the item's |
| 87 | content.''' |
| 88 | |
| 89 | # We hash the item instead of directly using its raw content, because |
| 90 | # |
| 91 | # 1. The raw content might be really large, e.g. when it's a contact |
| 92 | # with a picture, which bloats the status file. |
no outgoing calls