From 2cb95524ec859be61929339566701bf76e43d6ba Mon Sep 17 00:00:00 2001 From: Dennis Kaarsemaker Date: Thu, 29 Jun 2006 23:28:13 +0200 Subject: [PATCH] Rewrote parts of Webcal for semantic clean-ness, added 'ir' to the moster regex of Bugtracker --- Bugtracker/plugin.py | 2 +- Webcal/config.py | 2 + Webcal/plugin.py | 107 ++++++++++++++++++++----------------------- 3 files changed, 52 insertions(+), 59 deletions(-) diff --git a/Bugtracker/plugin.py b/Bugtracker/plugin.py index 2e390db..489d6e9 100644 --- a/Bugtracker/plugin.py +++ b/Bugtracker/plugin.py @@ -159,7 +159,7 @@ class Bugtracker(callbacks.PluginRegexp): list = wrap(list, [additional('text')]) def bugSnarfer(self, irc, msg, match): - r"""\b(?P(([a-z]+)?\s+bugs?|[a-z]+))\s+#?(?P\d+(?!\d*\.\d+)((,|\s*(and|en|et|und))\s*#?\d+(?!\d*\.\d+))*)""" + r"""\b(?P(([a-z]+)?\s+bugs?|[a-z]+))\s+#?(?P\d+(?!\d*\.\d+)((,|\s*(and|en|et|und|ir))\s*#?\d+(?!\d*\.\d+))*)""" if not self.registryValue('bugSnarfer', msg.args[0]): return # Don't double on commands diff --git a/Webcal/config.py b/Webcal/config.py index 7747305..7b85eac 100644 --- a/Webcal/config.py +++ b/Webcal/config.py @@ -22,6 +22,8 @@ def configure(advanced): Webcal = conf.registerPlugin('Webcal') conf.registerChannelValue(conf.supybot.plugins.Webcal, 'url', registry.String('',"""Webcal URL for the channel""")) +conf.registerChannelValue(conf.supybot.plugins.Webcal, 'filter', + registry.String('',"""What to filter on in the ical feed""")) conf.registerChannelValue(conf.supybot.plugins.Webcal, 'topic', registry.String('',"""Topic template""")) conf.registerGlobalValue(conf.supybot.plugins.Webcal, 'defaultChannel', diff --git a/Webcal/plugin.py b/Webcal/plugin.py index 08cacfb..e187285 100644 --- a/Webcal/plugin.py +++ b/Webcal/plugin.py @@ -66,63 +66,46 @@ class Webcal(callbacks.Plugin): self.cache.clear() def _filter(self, event, channel, now): - #channel = '#ubuntu-meeting' # Testing hack - if channel.lower() not in event.raw_data.lower(): + fword = self.registryValue('filter', channel) + if fword.lower() not in event.raw_data.lower(): return False delta = event.endDate - now return delta.days >= 0 or (delta.days == -1 and abs(delta).seconds < 30 * 60) - def _gettopic(self, url, channel, do_update=False, only_update=False, timezone = None, no_topic=False): - if do_update or url not in self.cache.keys(): - data = utils.web.getUrl(url) - parser = ical.ICalReader(data) - #parser = ical.ICalReader(ical.sample) - self.cache[url] = parser.events - if not only_update: - now = datetime.datetime.now(pytz.UTC) - events = filter(lambda x: self._filter(x,channel,now),self.cache[url])[:6] - preamble = '' - if len(events): - # The standard slack of 30 minutes after the meeting will be an - # error if there are 2 conscutive meetings. - if len(events) > 1 and events[1].startDate < now: - events = events[1:] - ev0 = events[0] - delta = abs(ev0.startDate - now) - if ev0.startDate < now or (delta.days == 0 and delta.seconds < 10 * 60): - preamble = 'Current meeting: %s | ' % ev0.summary.replace('Meeting','').strip() - events = events[1:] - events = map(lambda x: _event_to_string(x,timezone), events) - template = self.registryValue('topic', channel) - newtopic = ' | '.join(events).replace(' Meeting','') - if '%s' in template and not no_topic: - newtopic = template % str(newtopic) - return preamble + newtopic - - def _nextmeeting(self, url, channel, timezone): + def _gettopic(self, url, channel, timezone=None, no_topic=False, num_events=6): if url not in self.cache.keys(): - data = utils.web.getUrl(url) - parser = ical.ICalReader(data) - #parser = ical.ICalReader(ical.sample) - self.cache[url] = parser.events + self._refresh_cache(url) now = datetime.datetime.now(pytz.UTC) - events = filter(lambda x: self._filter(x,channel,now),self.cache[url])[:6] + events = filter(lambda x: self._filter(x,channel,now),self.cache[url])[:num_events] preamble = '' if len(events): # The standard slack of 30 minutes after the meeting will be an - # error if there are 2 conscutive meetings. + # error if there are 2 conscutive meetings, so remove the first + # one in that case if len(events) > 1 and events[1].startDate < now: events = events[1:] ev0 = events[0] delta = abs(ev0.startDate - now) if ev0.startDate < now or (delta.days == 0 and delta.seconds < 10 * 60): - return ' - Current meeting: %s ' % ev0.summary.replace('Meeting','').strip() - return ' - Next meeting: %s in %s' % (ev0.summary.replace('Meeting',''), diff(delta)) - return '' - + preamble = 'Current meeting: %s' % ev0.summary.replace('Meeting','').strip() + if num_events == 1: + return '%s in %s' % (preamble, diff(delta)) + events = events[1:] + preamble += ' | ' + # n_e = 1 -> next meeting + # n_t = T -> n_t + if num_events == 1: + if not events: + return "No meetings scheduled" + return 'Next meeting: %s in %s' % (events[0].summary.replace('Meeting','').strip(), diff(delta)) + events = map(lambda x: _event_to_string(x,timezone), events) + newtopic = ' | '.join(events).replace(' Meeting','') + template = self.registryValue('topic', channel) + if '%s' in template and not no_topic: + newtopic = template % str(newtopic) + return preamble + newtopic + def _autotopics(self): - if not self.irc: - return for c in self.irc.state.channels: url = self.registryValue('url', c) if url: @@ -130,19 +113,26 @@ class Webcal(callbacks.Plugin): if newtopic and not (newtopic.strip() == self.irc.state.getTopic(c).strip()): self.irc.queueMsg(ircmsgs.topic(c, newtopic)) - def _refresh_cache(self): - if not self.lastIrc: - return - for c in self.lastIrc.state.channels: - url = self.registryValue('url', c) - if url: - self._gettopic(url, c, True, true) + def _refresh_cache(self,url=None): + if url: + data = utils.web.getUrl(url) + parser = ical.ICalReader(data) + self.cache[url] = parser.events + else: + for c in self.irc.state.channels: + url = self.registryValue('url', c) + if url: + data = utils.web.getUrl(url) + parser = ical.ICalReader(data) + self.cache[url] = parser.events def topic(self, irc, msg, args): url = self.registryValue('url', msg.args[0]) if not url: return - newtopic = self._gettopic(url, msg.args[0], True) + self._refresh_cache(url) + newtopic = self._gettopic(url, msg.args[0]) + # Only change topic if it actually is different! if not (newtopic.strip() == irc.state.getTopic(msg.args[0]).strip()): irc.queueMsg(ircmsgs.topic(msg.args[0], newtopic)) topic = wrap(topic) @@ -182,7 +172,6 @@ class Webcal(callbacks.Plugin): now = datetime.datetime.now(pytz.UTC) if not tz: tz = 'utc' - #irc.reply('Current time in UTC: %s' % now.strftime("%B %d %Y, %H:%M:%S")) tzs = filter(lambda x: self._tzfilter(x.lower(),tz.lower()), pytz.all_timezones) if not tzs or 'gmt' in tz.lower(): irc.error('Unknown timezone: %s - Full list: http://bugbot.ubuntulinux.nl/timezones.html' % tz) @@ -193,20 +182,22 @@ class Webcal(callbacks.Plugin): c = self.registryValue('defaultChannel') if not c: return - #c = "#ubuntu-meeting" + meeting = '' url = self.registryValue('url', c) - if not url: - meeting = '' - else: - meeting = self._nextmeeting(url, c, tzs[0]) - irc.reply('Current time in %s: %s%s' % (tzs[0],now.astimezone(pytz.timezone(tzs[0])).strftime("%B %d %Y, %H:%M:%S"),meeting)) + if url: + meeting = self._gettopic(url, c, timezone=tzs[0], no_topic = True, num_events = 1) + if meeting: + meeting = ' - ' + meeting + irc.reply('Current time in %s: %s%s' % (tzs[0], + now.astimezone(pytz.timezone(tzs[0])).strftime("%B %d %Y, %H:%M:%S"),meeting)) now = wrap(now, [additional('text')]) time = now + # Warn people that you manage the topic def doTopic(self, irc, msg): url = self.registryValue('url', msg.args[0]) if not url: return - irc.queueMsg(ircmsgs.privmsg(msg.nick, "The topic of %s is managed by me and filled with the contents of %s - please don't change manually" % (msg.args[0],url))) + irc.reply("The topic of %s is managed by me and filled with the contents of %s - please don't change manually" % (msg.args[0],url), private=True) Class = Webcal