1.1 --- a/EventAggregatorSupport.py Sun Mar 14 22:53:48 2010 +0100
1.2 +++ b/EventAggregatorSupport.py Sun Mar 14 23:21:52 2010 +0100
1.3 @@ -21,11 +21,6 @@
1.4 except NameError:
1.5 from sets import Set as set
1.6
1.7 -try:
1.8 - import pytz
1.9 -except ImportError:
1.10 - pytz = None
1.11 -
1.12 __version__ = "0.6"
1.13
1.14 # Date labels.
1.15 @@ -50,16 +45,14 @@
1.16 month_regexp_str = ur'(?P<year>[0-9]{4})-(?P<month>[0-9]{2})'
1.17 date_regexp_str = ur'(?P<year>[0-9]{4})-(?P<month>[0-9]{2})-(?P<day>[0-9]{2})'
1.18 time_regexp_str = ur'(?P<hour>[0-2][0-9]):(?P<minute>[0-5][0-9])(?::(?P<second>[0-6][0-9]))?'
1.19 -timezone_olson_str = ur'(?P<olson>[a-zA-Z]+/[-_a-zA-Z]+)'
1.20 timezone_offset_str = ur'(?:UTC)?(?P<sign>[-+])(?P<hours>[0-9]{2})(?::?(?P<minutes>[0-9]{2}))?'
1.21 -timezone_regexp_str = ur'(?P<zone>' + timezone_olson_str + '|' + timezone_offset_str + ')'
1.22 +timezone_regexp_str = ur'(?P<zone>' + timezone_offset_str + ')'
1.23 datetime_regexp_str = date_regexp_str + ur'(?:\s+' + time_regexp_str + ur'(?:\s+' + timezone_regexp_str + ur')?)?'
1.24
1.25 month_regexp = re.compile(month_regexp_str, re.UNICODE)
1.26 date_regexp = re.compile(date_regexp_str, re.UNICODE)
1.27 time_regexp = re.compile(time_regexp_str, re.UNICODE)
1.28 datetime_regexp = re.compile(datetime_regexp_str, re.UNICODE)
1.29 -timezone_olson_regexp = re.compile(timezone_olson_str, re.UNICODE)
1.30 timezone_offset_regexp = re.compile(timezone_offset_str, re.UNICODE)
1.31
1.32 verbatim_regexp = re.compile(ur'(?:'
1.33 @@ -336,10 +329,6 @@
1.34
1.35 if details.has_key(term):
1.36
1.37 - # Perform additional event configuration.
1.38 -
1.39 - self.events[-1].fixTimeZone()
1.40 -
1.41 # Make a new event.
1.42
1.43 details = {}
1.44 @@ -347,10 +336,6 @@
1.45
1.46 details[term] = desc
1.47
1.48 - # Perform additional event configuration.
1.49 -
1.50 - self.events[-1].fixTimeZone()
1.51 -
1.52 return self.events
1.53
1.54 def setEvents(self, events):
1.55 @@ -499,16 +484,6 @@
1.56 self.page = page
1.57 self.details = details
1.58
1.59 - def fixTimeZone(self):
1.60 -
1.61 - # Combine location and time zone information.
1.62 -
1.63 - location = self.details.get("location")
1.64 -
1.65 - if location:
1.66 - self.details["start"].apply_location(location)
1.67 - self.details["end"].apply_location(location)
1.68 -
1.69 def __cmp__(self, other):
1.70
1.71 """
1.72 @@ -1056,6 +1031,8 @@
1.73 "Return the UTC offset in hours and minutes."
1.74
1.75 zone = self.time_zone()
1.76 + if not zone:
1.77 + return None
1.78
1.79 # Attempt to return a UTC offset where an explicit offset has been set.
1.80
1.81 @@ -1070,74 +1047,8 @@
1.82 minutes = int(match.group("minutes") or 0) * sign
1.83 return hours, minutes
1.84
1.85 - # Attempt to return an offset where an Olson identifier has been given.
1.86 -
1.87 - match = timezone_olson_regexp.match(zone)
1.88 - if match:
1.89 - if pytz is not None:
1.90 - tz = pytz.timezone(match.group("olson"))
1.91 -
1.92 - # NOTE: Using internal attribute.
1.93 -
1.94 - seconds = tz._utcoffset.seconds
1.95 - hours = seconds / 3600
1.96 - minutes = (seconds - hours * 3600) / 60
1.97 - return hours, minutes
1.98 -
1.99 return None
1.100
1.101 - def apply_location(self, location):
1.102 -
1.103 - """
1.104 - Apply 'location' information, setting the time zone if none has already
1.105 - been set.
1.106 - """
1.107 -
1.108 - if not self.time_zone():
1.109 - zone = getTimeZone(location)
1.110 - if zone:
1.111 - self.set_time_zone(zone)
1.112 -
1.113 -def getTimeZone(location):
1.114 -
1.115 - "Find a time zone for the specified 'location'."
1.116 -
1.117 - # Only try and find a time zone if pytz is present and able to suggest one.
1.118 -
1.119 - if pytz is None:
1.120 - return None
1.121 -
1.122 - code = getCountry(location)
1.123 -
1.124 - if code is None:
1.125 - return None
1.126 -
1.127 - try:
1.128 - zones = pytz.country_timezones(code)
1.129 - except KeyError:
1.130 - return None
1.131 -
1.132 - # No zones...
1.133 -
1.134 - if not zones:
1.135 - return None
1.136 -
1.137 - # Many potential zones.
1.138 -
1.139 - if len(zones) > 1:
1.140 - for zone in zones:
1.141 - continent, city = zone.split("/")
1.142 -
1.143 - # If the specific city is mentioned, choose the
1.144 - # zone.
1.145 -
1.146 - if location.find(city) != -1:
1.147 - return zone
1.148 -
1.149 - # Otherwise choose the first or only zone.
1.150 -
1.151 - return zones[0]
1.152 -
1.153 def getCountry(s):
1.154
1.155 "Find a country code in the given string 's'."