Skip to content

Commit 41de2e1

Browse files
committed
Pull the parts of m.m.reminders.AnnouncementConf shared with m.m.events into a new m.u.eventutil.CalendarConf (replacing m.u.eventutil.get_group_conf).
1 parent 921d1f5 commit 41de2e1

File tree

3 files changed

+38
-30
lines changed

3 files changed

+38
-30
lines changed

metabot/modules/events.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,27 @@ def moddispatch(ctx, msg, modconf): # pylint: disable=missing-docstring
3333
def group(ctx, msg):
3434
"""Handle /events in a group chat."""
3535

36-
group_id = '%s' % ctx.chat['id']
37-
groupconf = ctx.bot.config['issue37']['moderator'][group_id]
38-
calcodes, tzinfo, count, days, unused_hour, unused_dow = eventutil.get_group_conf(groupconf)
39-
if not calcodes or not tzinfo:
36+
group_id = f"{ctx.chat['id']}"
37+
calconf = eventutil.CalendarConf(ctx.bot.config['issue37']['moderator'][group_id])
38+
if not calconf.calcodes or not calconf.tzinfo:
4039
missing = []
41-
if not calcodes:
40+
if not calconf.calcodes:
4241
missing.append('choose one or more calendars')
43-
if not tzinfo:
42+
if not calconf.tzinfo:
4443
missing.append('set the time zone')
4544
return msg.add(
4645
"I'm not configured for this group! Ask a bot admin to go into the <b>moderator</b> "
4746
'module settings, group <b>%s</b>, and %s.', group_id, humanize.list(missing))
4847

49-
events, unused_alerts = eventutil.get_group_events(ctx.bot, calcodes, tzinfo, count, days)
48+
events, unused_alerts = eventutil.get_group_events(ctx.bot, calconf.calcodes, calconf.tzinfo,
49+
calconf.count, calconf.days)
5050
if not events:
51-
msg.add('No events in the next %s days!', days)
51+
msg.add('No events in the next %s days!', calconf.days)
5252
else:
5353
url = eventutil.get_image(events[0], ctx.bot.config)
5454
if url:
5555
msg.add('photo:' + url)
56-
msg.add(eventutil.format_events(ctx.bot, events, tzinfo))
56+
msg.add(eventutil.format_events(ctx.bot, events, calconf.tzinfo))
5757

5858

5959
def private(ctx, msg, modconf): # pylint: disable=too-many-branches,too-many-locals

metabot/modules/reminders.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,29 @@ def periodic():
5656
class AnnouncementConf: # pylint: disable=too-few-public-methods
5757
"""A group's announcement configuration."""
5858

59-
def __init__(self, groupconf):
60-
(self.calcodes, self.tzinfo, self.count, self.days, self.hour,
61-
self.dow) = eventutil.get_group_conf(groupconf)
62-
self.preambles = groupconf['daily'].get('text', '').splitlines()
59+
def __init__(self, calconf, dailyconf):
60+
self.calconf = calconf
61+
self.dow = dailyconf.get('dow', 0)
62+
self.hour = dailyconf.get('hour')
63+
self.preambles = dailyconf.get('text', '').splitlines()
6364

6465
def get_events(self, bot, eventtime, base, *, countdown=True):
6566
"""Get (and format) events for the given time."""
6667

67-
events, alerts = eventutil.get_group_events(bot, self.calcodes, self.tzinfo, self.count,
68-
self.days, eventtime)
68+
calconf = self.calconf
69+
events, alerts = eventutil.get_group_events(bot, calconf.calcodes, calconf.tzinfo,
70+
calconf.count, calconf.days, eventtime)
6971
if self.preambles:
7072
preamble = self.preambles[int(eventtime / (60 * 60 * 24)) % len(self.preambles)]
7173
else:
7274
preamble = ''
7375
text = _generate_preamble(preamble, events)
7476
if events:
75-
ev = eventutil.format_events(bot, events, self.tzinfo, base=base, countdown=countdown)
77+
ev = eventutil.format_events(bot,
78+
events,
79+
calconf.tzinfo,
80+
base=base,
81+
countdown=countdown)
7682
text = f'{text}\n\n{ev}'
7783
return events, alerts, text
7884

@@ -81,20 +87,21 @@ class Announcement(collections.namedtuple('Announcement', 'time events message t
8187
"""The most recent announcement."""
8288

8389

84-
def _daily_messages(multibot, records): # pylint: disable=too-many-branches,too-many-locals
90+
def _daily_messages(multibot, records): # pylint: disable=too-many-branches,too-many-locals,too-many-statements
8591
now = time.time()
8692
# If running at 11:22:33.444, act as if we're running at exactly 11:20:00.000.
8793
period = int(now // PERIOD * PERIOD)
8894
startofhour = now // 3600
8995

9096
for botuser, botconf in multibot.conf['bots'].items(): # pylint: disable=too-many-nested-blocks
9197
for groupid, groupconf in botconf['issue37']['moderator'].items():
92-
annconf = AnnouncementConf(groupconf)
93-
if not annconf.tzinfo or not isinstance(annconf.hour, int):
98+
calconf = eventutil.CalendarConf(groupconf)
99+
annconf = AnnouncementConf(calconf, groupconf['daily'])
100+
if not calconf.tzinfo or not isinstance(annconf.hour, int):
94101
continue
95102

96-
nowdt = datetime.datetime.fromtimestamp(now, annconf.tzinfo)
97-
perioddt = datetime.datetime.fromtimestamp(period, annconf.tzinfo)
103+
nowdt = datetime.datetime.fromtimestamp(now, calconf.tzinfo)
104+
perioddt = datetime.datetime.fromtimestamp(period, calconf.tzinfo)
98105
key = (botuser, groupid)
99106
if key in records:
100107
last = Announcement(*records[key])
@@ -123,7 +130,7 @@ def _daily_messages(multibot, records): # pylint: disable=too-many-branches,too
123130
if last:
124131
events, alerts, text = annconf.get_events(bot, last.time, perioddt)
125132
_handle_alerts(bot, records, groupid, alerts)
126-
edits = diff_events(multibot, annconf.tzinfo, perioddt, last.events, events)
133+
edits = diff_events(multibot, calconf.tzinfo, perioddt, last.events, events)
127134

128135
suffix = last.suffix
129136
if edits:

metabot/util/eventutil.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919
}
2020

2121

22-
def get_group_conf(groupconf):
23-
"""Pull calendar configuration from a raw group conf."""
24-
25-
timezone = groupconf.get('timezone')
26-
tzinfo = timezone and pytz.timezone(timezone)
27-
return (groupconf.get('calendars', '').split(), tzinfo, groupconf.get('maxeventscount', 10),
28-
groupconf.get('maxeventsdays',
29-
6), groupconf['daily'].get('hour'), groupconf['daily'].get('dow', 0))
22+
class CalendarConf: # pylint: disable=too-few-public-methods
23+
"""A group's calendar configuration."""
24+
25+
def __init__(self, groupconf):
26+
self.calcodes = groupconf.get('calendars', '').split()
27+
self.count = groupconf.get('maxeventscount', 10)
28+
self.days = groupconf.get('maxeventsdays', 6)
29+
timezone = groupconf.get('timezone')
30+
self.tzinfo = timezone and pytz.timezone(timezone)
3031

3132

3233
def get_group_events(bot, calcodes, tzinfo, count, days, now=None): # pylint: disable=too-many-arguments,too-many-locals

0 commit comments

Comments
 (0)