Переглянути джерело

cleaned up RC class a lot; get_pretty() -> prettify()

tags/v0.1^2
Ben Kurtovic 13 роки тому
джерело
коміт
ff7af4eb6b
2 змінених файлів з 51 додано та 35 видалено
  1. +50
    -34
      bot/classes/rc.py
  2. +1
    -1
      bot/watcher.py

+ 50
- 34
bot/classes/rc.py Переглянути файл

@@ -1,57 +1,73 @@
# -*- coding: utf-8 -*-

# A class to store data on an individual event received from our IRC watcher.

import re

class RC(object):
"""A class to store data on an event received from our IRC watcher."""
re_color = re.compile("\x03([0-9]{1,2}(,[0-9]{1,2})?)?")
re_edit = re.compile("\A\[\[(.*?)\]\]\s(.*?)\s(http://.*?)\s\*\s(.*?)\s\*\s(.*?)\Z")
re_log = re.compile("\A\[\[(.*?)\]\]\s(.*?)\s\*\s(.*?)\s\*\s(.*?)\Z")

def __init__(self, msg):
"""store data on an individual event received from our IRC watcher"""
self.msg = msg

def parse(self):
"""parse recent changes log into some variables"""
"""Parse a recent change event into some variables."""
# Strip IRC color codes; we don't want or need 'em:
self.msg = self.re_color.sub("", self.msg).strip()
msg = self.msg
msg = re.sub("\x03([0-9]{1,2}(,[0-9]{1,2})?)?", "", msg) # strip IRC color codes; we don't want/need 'em
msg = msg.strip()
self.msg = msg
self.is_edit = True

# flags: 'M' for minor edit, 'B' for bot edit, 'create' for a user creation log entry...
# Flags: 'M' for minor edit, 'B' for bot edit, 'create' for a user
# creation log entry, etc:
try:
page, flags, url, user, comment = re.findall("\A\[\[(.*?)\]\]\s(.*?)\s(http://.*?)\s\*\s(.*?)\s\*\s(.*?)\Z", msg)[0]
except IndexError: # we're probably missing the http:// part, because it's a log entry, which lacks a url
page, flags, user, comment = re.findall("\A\[\[(.*?)\]\]\s(.*?)\s\*\s(.*?)\s\*\s(.*?)\Z", msg)[0]
url = "http://en.wikipedia.org/wiki/{}".format(page)
flags = flags.strip() # flag tends to have a extraneous whitespace character at the end when it's a log entry
self.is_edit = False # this is a log entry, not edit
self.page, self.flags, self.url, self.user, self.comment = page, flags, url, user, comment

def get_pretty(self):
"""make a nice, colorful message from self.msg to send to the front-end"""
page, self.flags, url, user, comment = self.re_edit.findall(msg)[0]
except IndexError:
# We're probably missing the http:// part, because it's a log
# entry, which lacks a URL:
page, flags, user, comment = self.re_log.findall(msg)[0]
url = "".join(("http://en.wikipedia.org/wiki/", page))

self.is_edit = False # This is a log entry, not edit

# Flags tends to have extra whitespace at the end when they're
# log entries:
self.flags = flags.strip()

self.page, self.url, self.user, self.comment = page, url, user, comment

def prettify(self):
"""Make a nice, colorful message to send back to the IRC front-end."""
flags = self.flags
event_type = flags # "New <event>:" if we don't know exactly what happened
# "New <event>:" if we don't know exactly what happened:
event_type = flags
if "N" in flags:
event_type = "page" # "New page:"
event_type = "page" # "New page:"
elif flags == "delete":
event_type = "deletion" # "New deletion:"
event_type = "deletion" # "New deletion:"
elif flags == "protect":
event_type = "protection" # "New protection:"
event_type = "protection" # "New protection:"
elif flags == "create":
event_type = "user" # "New user:"
event_type = "user" # "New user:"
if self.page == "Special:Log/move":
event_type = "move" # New move:
event_type = "move" # New move:
else:
event_type = "edit" # "New edit:"
event_type = "edit" # "New edit:"
if "B" in flags:
event_type = "bot {}".format(event_type) # "New bot edit:"
# "New bot edit:"
event_type = "bot {}".format(event_type)
if "M" in flags:
event_type = "minor {}".format(event_type) # "New minor edit:" OR "New minor bot edit:"
# "New minor edit:" OR "New minor bot edit:"
event_type = "minor {}".format(event_type)

# Example formatting:
# New edit: [[Page title]] * User name * http://en... * edit summary
if self.is_edit:
pretty = "\x02New {0}\x0F: \x0314[[\x0307{1}\x0314]]\x0306 *\x0303 {2}\x0306 *\x0302 {3}\x0306 *\x0310 {4}".format(event_type, self.page, self.user, self.url, self.comment)
else:
pretty = "\x02New {0}\x0F: \x0303{1}\x0306 *\x0302 {2}\x0306 *\x0310 {3}".format(event_type, self.user, self.url, self.comment)
return pretty
return "".join(("\x02New ", event_type, "\x0F: \x0314[[\x0307",
self.page, "\x0314]]\x0306 *\x0303 ", self.user,
"\x0306 *\x0302 ", self.url, "\x0306 *\x0310 ",
self.comment))

return "".join(("\x02New ", event_type, "\x0F: \x0303", self.user,
"\x0306 *\x0302 ", self.url, "\x0306 *\x0310 ",
self.comment))

+ 1
- 1
bot/watcher.py Переглянути файл

@@ -86,6 +86,6 @@ def process_rc(rc):
"""
chans = rules.process(rc)
if chans and frontend_conn:
pretty = rc.get_pretty()
pretty = rc.prettify()
for chan in chans:
frontend_conn.say(chan, pretty)

Завантаження…
Відмінити
Зберегти