# HG changeset patch # User Paul Boddie # Date 1482272516 -3600 # Node ID 5dbc885f21db4a116ac93238c4dda48ace1f2f9c # Parent f12ef637a7dd77dbcf8644542131dac28c5780a6 Added convenience functions to make unique identifiers and new calendar objects. diff -r f12ef637a7dd -r 5dbc885f21db imiptools/client.py --- a/imiptools/client.py Tue Dec 20 16:43:44 2016 +0100 +++ b/imiptools/client.py Tue Dec 20 23:21:56 2016 +0100 @@ -22,8 +22,9 @@ from datetime import datetime, timedelta from imiptools import config from imiptools.data import Object, check_delegation, get_address, get_uri, \ - get_window_end, is_new_object, make_freebusy, to_part, \ - uri_dict, uri_item, uri_items, uri_parts, uri_values + get_window_end, is_new_object, make_freebusy, \ + make_uid, to_part, uri_dict, uri_item, uri_items, \ + uri_parts, uri_values from imiptools.dates import check_permitted_values, format_datetime, get_default_timezone, \ get_duration, get_timestamp from imiptools.i18n import get_translator @@ -263,8 +264,7 @@ # Invent a unique identifier. - utcnow = get_timestamp() - uid = "imip-agent-%s-%s" % (utcnow, get_address(self.user)) + uid = make_uid(self.user) freebusy = freebusy or self.store.get_freebusy(self.user) diff -r f12ef637a7dd -r 5dbc885f21db imiptools/data.py --- a/imiptools/data.py Tue Dec 20 16:43:44 2016 +0100 +++ b/imiptools/data.py Tue Dec 20 23:21:56 2016 +0100 @@ -27,8 +27,8 @@ get_datetime_tzid, \ get_duration, get_period, get_period_item, \ get_recurrence_start_point, \ - get_time, get_tzid, to_datetime, to_timezone, \ - to_utc_datetime + get_time, get_timestamp, get_tzid, to_datetime, \ + to_timezone, to_utc_datetime from imiptools.period import FreeBusyPeriod, Period, RecurringPeriod from vCalendar import iterwrite, parse, ParseError, to_dict, to_node from vRecurrence import get_parameters, get_rule @@ -61,6 +61,10 @@ Then, the specific object must be presented as follows: object = Object({"VEVENT" : event}) + + A convienience function is also provided to initialise objects: + + object = new_object("VEVENT") """ self.objtype, (self.details, self.attr) = fragment.items()[0] @@ -119,8 +123,19 @@ # Structure access. + def add(self, obj): + + "Add 'obj' to the structure." + + name = obj.objtype + if not self.details.has_key(name): + l = self.details[name] = [] + else: + l = self.details[name] + l.append((obj.details, obj.attr)) + def copy(self): - return Object(to_dict(self.to_node())) + return Object(self.to_dict()) def get_items(self, name, all=True): return get_items(self.details, name, all) @@ -163,6 +178,9 @@ # Serialisation. + def to_dict(self): + return to_dict(self.to_node()) + def to_node(self): return to_node({self.objtype : [(self.details, self.attr)]}) @@ -714,6 +732,19 @@ finally: out.close() +def new_object(object_type): + + "Make a new object of the given 'object_type'." + + return Object({object_type : ({}, {})}) + +def make_uid(user): + + "Return a unique identifier for a new object by the given 'user'." + + utcnow = get_timestamp() + return "imip-agent-%s-%s" % (utcnow, get_address(user)) + # Structure access functions. def get_items(d, name, all=True): diff -r f12ef637a7dd -r 5dbc885f21db imipweb/calendar.py --- a/imipweb/calendar.py Tue Dec 20 16:43:44 2016 +0100 +++ b/imipweb/calendar.py Tue Dec 20 23:21:56 2016 +0100 @@ -20,7 +20,8 @@ """ from datetime import datetime, timedelta -from imiptools.data import get_address, get_uri, get_verbose_address, uri_parts +from imiptools.data import get_address, get_uri, get_verbose_address, make_uid, \ + uri_parts from imiptools.dates import format_datetime, get_date, get_datetime, \ get_datetime_item, get_end_of_day, get_start_of_day, \ get_start_of_next_day, get_timestamp, ends_on_same_day, \ @@ -118,8 +119,7 @@ # Invent a unique identifier. - utcnow = get_timestamp() - uid = "imip-agent-%s-%s" % (utcnow, get_address(self.user)) + uid = make_uid(self.user) # Create a calendar object and store it as a request.