1.1 --- a/imiptools/content.py Tue Jan 13 00:18:53 2015 +0100
1.2 +++ b/imiptools/content.py Tue Jan 13 22:39:53 2015 +0100
1.3 @@ -20,33 +20,21 @@
1.4 this program. If not, see <http://www.gnu.org/licenses/>.
1.5 """
1.6
1.7 -from datetime import date, datetime, timedelta
1.8 +from datetime import datetime, timedelta
1.9 from email.mime.text import MIMEText
1.10 +from imiptools.dates import *
1.11 from imiptools.period import have_conflict, insert_period, remove_period
1.12 -from pytz import timezone, UnknownTimeZoneError
1.13 +from pytz import timezone
1.14 from vCalendar import parse, ParseError, to_dict
1.15 from vRecurrence import get_parameters, get_rule
1.16 import email.utils
1.17 import imip_store
1.18 -import re
1.19
1.20 try:
1.21 from cStringIO import StringIO
1.22 except ImportError:
1.23 from StringIO import StringIO
1.24
1.25 -# iCalendar date and datetime parsing (from DateSupport in MoinSupport).
1.26 -
1.27 -date_icalendar_regexp_str = ur'(?P<year>[0-9]{4})(?P<month>[0-9]{2})(?P<day>[0-9]{2})'
1.28 -datetime_icalendar_regexp_str = date_icalendar_regexp_str + \
1.29 - ur'(?:' \
1.30 - ur'T(?P<hour>[0-2][0-9])(?P<minute>[0-5][0-9])(?P<second>[0-6][0-9])' \
1.31 - ur'(?P<utc>Z)?' \
1.32 - ur')?'
1.33 -
1.34 -match_date_icalendar = re.compile(date_icalendar_regexp_str, re.UNICODE).match
1.35 -match_datetime_icalendar = re.compile(datetime_icalendar_regexp_str, re.UNICODE).match
1.36 -
1.37 # Content interpretation.
1.38
1.39 def get_items(d, name, all=True):
1.40 @@ -101,38 +89,6 @@
1.41 dt = get_datetime(value, attr)
1.42 return to_utc_datetime(dt)
1.43
1.44 -def to_utc_datetime(dt):
1.45 - if not dt:
1.46 - return None
1.47 - elif isinstance(dt, datetime):
1.48 - return dt.astimezone(timezone("UTC"))
1.49 - else:
1.50 - return dt
1.51 -
1.52 -def to_timezone(dt, name):
1.53 - try:
1.54 - tz = name and timezone(name) or None
1.55 - except UnknownTimeZoneError:
1.56 - tz = None
1.57 - if tz is not None:
1.58 - if not dt.tzinfo:
1.59 - return tz.localize(dt)
1.60 - else:
1.61 - return dt.astimezone(tz)
1.62 - else:
1.63 - return dt
1.64 -
1.65 -def format_datetime(dt):
1.66 - if not dt:
1.67 - return None
1.68 - elif isinstance(dt, datetime):
1.69 - if dt.tzname() == "UTC":
1.70 - return dt.strftime("%Y%m%dT%H%M%SZ")
1.71 - else:
1.72 - return dt.strftime("%Y%m%dT%H%M%S")
1.73 - else:
1.74 - return dt.strftime("%Y%m%d")
1.75 -
1.76 def get_addresses(values):
1.77 return [address for name, address in email.utils.getaddresses(values)]
1.78
1.79 @@ -151,34 +107,6 @@
1.80 def uri_items(items):
1.81 return [(get_uri(value), attr) for value, attr in items]
1.82
1.83 -def get_datetime(value, attr=None):
1.84 -
1.85 - """
1.86 - Return a datetime object from the given 'value' in iCalendar format, using
1.87 - the 'attr' mapping (if specified) to control the conversion.
1.88 - """
1.89 -
1.90 - if not attr or attr.get("VALUE") in (None, "DATE-TIME"):
1.91 - m = match_datetime_icalendar(value)
1.92 - if m:
1.93 - dt = datetime(
1.94 - int(m.group("year")), int(m.group("month")), int(m.group("day")),
1.95 - int(m.group("hour")), int(m.group("minute")), int(m.group("second"))
1.96 - )
1.97 -
1.98 - # Impose the indicated timezone.
1.99 - # NOTE: This needs an ambiguity policy for DST changes.
1.100 -
1.101 - return to_timezone(dt, m.group("utc") and "UTC" or attr and attr.get("TZID") or None)
1.102 -
1.103 - if not attr or attr.get("VALUE") == "DATE":
1.104 - m = match_date_icalendar(value)
1.105 - if m:
1.106 - return date(
1.107 - int(m.group("year")), int(m.group("month")), int(m.group("day"))
1.108 - )
1.109 - return None
1.110 -
1.111 # NOTE: Need to expose the 100 day window for recurring events in the
1.112 # NOTE: configuration.
1.113