diff options
author | kongr45gpen <electrovesta@gmail.com> | 2014-12-31 17:05:05 +0200 |
---|---|---|
committer | kongr45gpen <electrovesta@gmail.com> | 2014-12-31 17:05:05 +0200 |
commit | afdb8a96e0b4adb612560ad241f288c7761af73a (patch) | |
tree | 0a3924c9785e0872d707175cf44aee8fdecedc30 | |
parent | d7e9c2b03f552193e2d8ccbcfc44987c659fc469 (diff) | |
download | supybot_github-afdb8a96e0b4adb612560ad241f288c7761af73a.tar.gz supybot_github-afdb8a96e0b4adb612560ad241f288c7761af73a.tar.bz2 supybot_github-afdb8a96e0b4adb612560ad241f288c7761af73a.zip |
Add support for different themes
-rw-r--r-- | .gitignore | 4 | ||||
-rw-r--r-- | config.py | 3 | ||||
-rw-r--r-- | local/handler/GithubHandler.py | 36 | ||||
-rw-r--r-- | local/handler/IssueCommentHandler.py | 56 | ||||
-rw-r--r-- | local/handler/IssueHandler.py | 51 | ||||
-rw-r--r-- | local/handler/PushHandler.py | 126 | ||||
-rw-r--r-- | local/handler/StatusHandler.py | 18 | ||||
-rw-r--r-- | local/handler/TravisHandler.py | 26 | ||||
-rw-r--r-- | local/handler/WikiHandler.py | 55 | ||||
-rw-r--r-- | local/theme/DefaultTheme.py | 146 | ||||
-rw-r--r-- | local/theme/Theme.py | 6 | ||||
-rw-r--r-- | local/theme/__init__.py | 7 | ||||
-rw-r--r-- | local/utility.py | 17 |
13 files changed, 332 insertions, 219 deletions
@@ -36,7 +36,11 @@ nosetests.xml .pydevproject # Supybot test files +backup conf +data logs test-conf +test-data test-logs +tmp @@ -57,4 +57,7 @@ conf.registerGlobalValue(Github, 'port', registry.Integer(8093, """The port where Github will send HTTP requests""")) +conf.registerGlobalValue(Github, 'theme', + registry.String('default', """The name of the theme that will be used to style messages""")) + # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: diff --git a/local/handler/GithubHandler.py b/local/handler/GithubHandler.py index f095ec8..dfa024c 100644 --- a/local/handler/GithubHandler.py +++ b/local/handler/GithubHandler.py @@ -29,6 +29,8 @@ import StatusHandler import TravisHandler import IssueCommentHandler +from .. import theme as themes + class GithubHandler(BaseHTTPServer.BaseHTTPRequestHandler): def do_POST(s): """Respond to a POST request.""" @@ -90,31 +92,47 @@ class GithubHandler(BaseHTTPServer.BaseHTTPRequestHandler): s.wfile.write("The password is wrong") return - irc = {} - # Handle different event types + themeName = configValue('theme') + + alphanumericPattern = re.compile('[\W_]+') + themeClass = ''.join([alphanumericPattern.sub('', themeName).title(), 'Theme']) + try: + mod = getattr(themes, themeClass) + klass = getattr(mod, themeClass) + except AttributeError: + # The theme was not found + log.error("The '%s' theme was not found" % themeName) + klass = themes.DefaultTheme.DefaultTheme + + theme = klass() + + # + # Handle different event types + # msgs = [] + theme.msgs = msgs if 'matrix' in data: - msgs = TravisHandler.handle(data) + TravisHandler.handle(data, theme) elif 'pages' in data: - msgs = WikiHandler.handle(data) + WikiHandler.handle(data, theme) elif 'state' in data: - msgs = StatusHandler.handle(data) + StatusHandler.handle(data, theme) elif 'commits' in data: - msgs = PushHandler.handle(data) + PushHandler.handle(data, theme) elif 'issue' in data: if 'comment' in data: - msgs = IssueCommentHandler.handle(data) + IssueCommentHandler.handle(data, theme) else: - msgs = IssueHandler.handle(data) + IssueHandler.handle(data, theme) else: msgs.append("Something happened") + theme.finalize() saveMessages(msgs) - #msgs.append( ircmsgs.privmsg("#main", "%s" % ()) ) if not world.testing: for msg in msgs: for irc in world.ircs: diff --git a/local/handler/IssueCommentHandler.py b/local/handler/IssueCommentHandler.py index ba6d846..e0084f3 100644 --- a/local/handler/IssueCommentHandler.py +++ b/local/handler/IssueCommentHandler.py @@ -1,39 +1,23 @@ from ..utility import * -def handle(data): - msgs = [] - - url = getShortURL(data['comment']['html_url']) - - creator = '' - if data['sender']['login'] != data['issue']['user']['login']: - creator = " by %s" % (ircutils.mircColor(data['issue']['user']['login'],"green"),) - +def handle(data, theme): milestone = '' - if data['issue']['milestone'] and configValue("showMilestone"): - milestone = ircutils.mircColor(" (%s" % (data['issue']['milestone']['title']),"brown") - - if milestone: - oparen = '- ' - else: - oparen = '(' - - lines = data['comment']['body'].splitlines() - line = lines[0] - if len(line) > 70: - line = "%s..." % (line[0:67]) - elif len(lines) > 1: - line += "..." - - msgs.append( "%s: %s commented on issue %s \"%s\"%s%s %s%s): %s" % ( - ircutils.bold(data['repository']['name']), - ircutils.mircColor(data['comment']['user']['login'], "green"), - ''.join(["#",str(data['issue']['number'])]), - ircutils.bold(data['issue']['title']), - creator, - milestone, - oparen, url, - line - )) - - return msgs + if configValue("showMilestone") and 'milestone' in data['issue'] and data['issue']['milestone']: + milestone = data['issue']['milestone']['title'] + + assignee = '' + if 'assignee' in data['issue'] and data['issue']['assignee']: + assignee = data['issue']['assignee']['login'] + + theme.issue( + repo = data['repository']['name'], + actor = data['comment']['user']['login'], + action = 'commented on', + comment = data['comment']['body'], + issueNo = data['issue']['number'], + issueTitle = data['issue']['title'], + creator = data['issue']['user']['login'], + milestone = milestone, + url = getShortURL(data['comment']['html_url']), + assignee = assignee + ) diff --git a/local/handler/IssueHandler.py b/local/handler/IssueHandler.py index 1ae9a6d..df07a9c 100644 --- a/local/handler/IssueHandler.py +++ b/local/handler/IssueHandler.py @@ -1,37 +1,22 @@ from ..utility import * -def handle(data): - msgs = [] - - url = data['issue']['url'] - - if data['issue']['assignee'] and data['sender']['login'] == data['issue']['assignee']['login']: - senderColor = "green" - else: - senderColor = "dark grey" - - creator = '' - if data['sender']['login'] != data['issue']['user']['login']: - creator = " by %s" % (ircutils.mircColor(data['issue']['user']['login'],"green"),) - +def handle(data, theme): milestone = '' if data['issue']['milestone'] and configValue("showMilestone"): - milestone = ircutils.mircColor(" (%s" % (data['issue']['milestone']['title']),"brown") - - if milestone: - oparen = '- ' - else: - oparen = '(' - - msgs.append( "%s: %s %s issue %s \"%s\"%s%s %s%s)" % ( - ircutils.bold(data['repository']['name']), - ircutils.mircColor(data['sender']['login'], senderColor), - colorAction(data['action']), - ''.join(["#",str(data['issue']['number'])]), - ircutils.bold(data['issue']['title']), - creator, - milestone, - oparen, url - )) - - return msgs + milestone = data['issue']['milestone']['title'] + + assignee = '' + if 'assignee' in data['issue'] and data['issue']['assignee']: + assignee = data['issue']['assignee']['login'] + + theme.issue( + repo = data['repository']['name'], + actor = data['sender']['login'], + action = data['action'], + issueNo = data['issue']['number'], + issueTitle = data['issue']['title'], + creator = data['issue']['user']['login'], + milestone = milestone, + url = getShortURL(data['issue']['url']), + assignee = assignee + ) diff --git a/local/handler/PushHandler.py b/local/handler/PushHandler.py index 96b9339..19bc436 100644 --- a/local/handler/PushHandler.py +++ b/local/handler/PushHandler.py @@ -1,16 +1,10 @@ from ..utility import * -def handle(data): - msgs = [] - +def handle(data, theme): commitno = len(data['commits']) ref = data['ref'].split('/',2) branch = ref[2] - colon = '' - if data['commits']: - colon = ':' - isTag = False isMerge = False @@ -24,14 +18,11 @@ def handle(data): if ref[1] == 'tags': isTag = True - urls = ' (%s)' % (getShortURL(data['compare']),) + urls = getShortURL(data['compare']) if 'base_ref' in data: base_ref = data['base_ref'].split('/',2) baseBranch = base_ref[2] - if isTag: - branchFrom = '%s %s ' % (baseBranch, ircutils.bold('*')) - else: - branchFrom = ' from %s' % (ircutils.bold(ircutils.mircColor(baseBranch, "blue")), ) + branchFrom = baseBranch if (data['created'] and data['deleted']) or (not data['created'] and not data['deleted'] and data['forced']): if isTag: @@ -67,58 +58,47 @@ def handle(data): isMerge = True if configValue("hidePush",None) == False and not branched: - msgs.append( "%s @ %s: %s pushed %s %s (%s)%s" % ( - ircutils.bold(ircutils.mircColor(branch, "blue")), - ircutils.bold(data['repository']['name']), - ircutils.mircColor(data['pusher']['name'], "green"), - ircutils.bold(str(commitno)), - plural(commitno, "commit", "commits"), - getShortURL(data['compare']), - colon - )) + theme.push( + repo = data['repository']['name'], + branch = branch, + actor = data['pusher']['name'], + url = getShortURL(data['compare']), + count = commitno + ) elif branched: if isTag: - if onlyDeleted: - commitInfo = "" - else: - commitMsg = "" - if configValue("tagShowCommitMsg"): - commitMsg = ircutils.mircColor(" (%s)" % (maxLen(data['head_commit']['message'].splitlines()[0],75)),"brown") - commitInfo = " %s %s%s as" % (branchFrom, ircutils.bold(data['head_commit']['id'][0:6]), commitMsg) - msgs.append("%s: %s %s%s %s%s" % ( - ircutils.bold(data['repository']['name']), - ircutils.mircColor(data['pusher']['name'], "green"), - colorAction(action), - commitInfo, - ircutils.bold(ircutils.mircColor(branch, "blue")), - urls, - )) + theme.tag( + repo = data['repository']['name'], + actor = data['pusher']['name'], + action = action, + base = branchFrom, + to = branch, + onlyDeleted = onlyDeleted, + headMsg = data['head_commit']['message'], + headId = data['head_commit']['id'], + url = urls + ) elif isMerge: - distinctMessage = "" - if configValue("hidePush",None) == False and regularCommitCount > 0: - distinctMessage = " and %s %s %s" % ( colorAction("pushed"), regularCommitCount, plural(regularCommitCount, 'commit', 'commits')) - - msgs.append( "%s: %s %s %s %s%s%s into %s%s" % ( - ircutils.bold(data['repository']['name']), - ircutils.mircColor(data['pusher']['name'], "green"), - colorAction(action), - mergedCommitCount, - plural(mergedCommitCount, 'commit', 'commits'), - branchFrom, - distinctMessage, - ircutils.bold(ircutils.mircColor(branch, "blue")), - urls - )) + theme.merge( + repo = data['repository']['name'], + actor = data['pusher']['name'], + action = action, + mergeCount = mergedCommitCount, + regularCount = regularCommitCount, + base = branchFrom, + to = branch, + url = urls + ) else: - msgs.append( "%s: %s %s branch %s%s%s%s" % ( - ircutils.bold(data['repository']['name']), - ircutils.mircColor(data['pusher']['name'], "green"), - colorAction(action), - ircutils.bold(ircutils.mircColor(branch, "blue")), - branchFrom, - urls, - colon - )) + theme.branch( + repo = data['repository']['name'], + actor = data['pusher']['name'], + action = action, + count = commitno, + base = branchFrom, + to = branch, + url = urls + ) for commit in data['commits']: if 'username' in commit['author']: @@ -134,21 +114,11 @@ def handle(data): if isMerge and not commit['distinct']: commitBranch = "%s -> %s" % ( baseBranch, branch ) - msgs.append("%s @ %s: %s * %s (%s)" % ( - ircutils.bold(ircutils.mircColor(commitBranch, "blue")), - ircutils.bold(data['repository']['name']), - ircutils.mircColor(author, "green"), - ircutils.bold(commit['id'][0:6]), - getShortURL(commit['url']), - )) - - commitlines = commit['message'].splitlines() - for rawline in commitlines: - line = maxLen(rawline, 400) - msgs.append( "%s @ %s: %s" % ( - ircutils.bold(ircutils.mircColor(commitBranch, "blue")), - ircutils.bold(data['repository']['name']), - line, - )) - - return msgs + theme.commit( + branch = commitBranch, + repo = data['repository']['name'], + author = author, + id = commit['id'], + message = commit['message'], + url = getShortURL(commit['url']) + ) diff --git a/local/handler/StatusHandler.py b/local/handler/StatusHandler.py index 46e2e67..dd4bbd9 100644 --- a/local/handler/StatusHandler.py +++ b/local/handler/StatusHandler.py @@ -1,13 +1,9 @@ from ..utility import * -def handle(data): - msgs = [] - - msgs.append( "%s: %s - %s (%s)" % ( - ircutils.bold(data['repository']['name']), - colorAction(data['state']), - data['description'], - data['target_url'] - )) - - return msgs +def handle(data, theme): + theme.status( + repo = data['repository']['name'], + status = data['state'], + description = data['description'], + url = data['target_url'] + ) diff --git a/local/handler/TravisHandler.py b/local/handler/TravisHandler.py index e69ab9d..bdfb484 100644 --- a/local/handler/TravisHandler.py +++ b/local/handler/TravisHandler.py @@ -1,18 +1,12 @@ from ..utility import * -def handle(data): - msgs = [] - - status = data['status_message'].lower() - - if isStatusVisible(data['repository']['url'], status): - msgs.append( "%s: Build status: %s * %s by %s (%s - %s)" % ( - ircutils.bold(data['repository']['name']), - colorAction(status), - ircutils.bold(data['commit'][0:6]), - ircutils.mircColor(data['author_name'], "green"), - ircutils.mircColor(maxLen(data['message'].splitlines()[0], 50), "dark gray"), - getShortURL(data['build_url']) - )) - - return msgs +def handle(data, theme): + if isStatusVisible(data['repository']['url'], data['status_message'].lower()): + theme.travis( + repo = data['repository']['name'], + status = data['status_message'], + commitId = data['commit'], + commitMessage = data['message'], + commitAuthor = data['author_name'], + buildUrl = data['build_url'] + ) diff --git a/local/handler/WikiHandler.py b/local/handler/WikiHandler.py index 1f93718..59bb37e 100644 --- a/local/handler/WikiHandler.py +++ b/local/handler/WikiHandler.py @@ -1,40 +1,31 @@ from ..utility import * -def handle(data): - msgs = [] - +def handle(data, theme): pageno = len(data['pages']) url = getShortURL("%s/wiki/_compare/%s" % ( data['repository']['html_url'], data['pages'][0]['sha'] )) if configValue("hidePush",None) is False: - msgs.append( "%s: %s modified %s wiki %s (%s):" % ( - ircutils.bold(data['repository']['name']), - ircutils.mircColor(data['sender']['login'], "green"), - ircutils.bold(str(pageno)), - plural(pageno, "page", "pages"), - url - )) - - urlShown = False; - + theme.wikiPush( + repo = data['repository']['name'], + actor = data['sender']['login'], + count = pageno, + url = url + ) + + pages = [] for page in data['pages']: - if configValue("hidePush") and urlShown is False: - pageurl = "(%s)" % (url,) - urlShown = True - elif configValue("hidePush"): - pageurl = "" - else: - pageurl = "(%s)" % (page['html_url'],) - - # Unfortunately github doesn't support edit summaries :( - msgs.append( "%s: %s %s %s * %s %s" % ( - ircutils.bold(data['repository']['name']), - ircutils.mircColor(data['sender']['login'], "green"), - colorAction(page['action']), - ircutils.bold(ircutils.mircColor(page['page_name'], "blue")), - ircutils.bold(page['sha'][0:6]), - pageurl, - )) - - return msgs + pages.append({ + 'action': page['action'], + 'name' : page['page_name'], + 'hash' : page['sha'], + 'url' : page['html_url'] + }) + + # Unfortunately github doesn't support edit summaries :( + theme.wikiPages( + repo = data['repository']['name'], + actor = data['sender']['login'], + pages = pages, + url = url + ) diff --git a/local/theme/DefaultTheme.py b/local/theme/DefaultTheme.py new file mode 100644 index 0000000..18982de --- /dev/null +++ b/local/theme/DefaultTheme.py @@ -0,0 +1,146 @@ +from Theme import Theme + +from ..utility import * + +class DefaultTheme(Theme): + def push(self, branch, repo, actor, count, url): + self.msgs.append( "%s @ %s: %s pushed %s %s (%s)%s" % ( + ircutils.bold(ircutils.mircColor(branch, "blue")), + ircutils.bold(repo), + ircutils.mircColor(actor, "green"), + ircutils.bold(str(count)), + plural(count, "commit", "commits"), + url, + ':' if count else '' + )) + + def commit(self, branch, repo, author, message, id, url): + self.msgs.append("%s @ %s: %s * %s (%s)" % ( + ircutils.bold(ircutils.mircColor(branch, "blue")), + ircutils.bold(repo), + ircutils.mircColor(author, "green"), + ircutils.bold(id[0:6]), + url + )) + + commitlines = message.splitlines() + for line in commitlines: + self.msgs.append( "%s @ %s: %s" % ( + ircutils.bold(ircutils.mircColor(branch, "blue")), + ircutils.bold(repo), + maxLen(line, 400), + )) + + def merge(self, repo, actor, action, mergeCount, regularCount, base, to, url): + distinctMessage = "" + if configValue("hidePush",None) == False and regularCount > 0: + distinctMessage = " and %s %s %s" % ( colorAction("pushed"), regularCount, plural(regularCount, 'commit', 'commits')) + + self.msgs.append( "%s: %s %s %s %s from %s%s into %s%s" % ( + ircutils.bold(repo), + ircutils.mircColor(actor, "green"), + colorAction(action), + mergeCount, + plural(mergeCount, 'commit', 'commits'), + ircutils.bold(ircutils.mircColor(base, "blue")), + distinctMessage, + ircutils.bold(ircutils.mircColor(to, "blue")), + ' (%s)' % url if url else '' + )) + + def branch(self, repo, actor, action, count, base, to, url): + self.msgs.append( "%s: %s %s branch %s from %s%s%s" % ( + ircutils.bold(repo), + ircutils.mircColor(actor, "green"), + colorAction(action), + ircutils.bold(ircutils.mircColor(to, "blue")), + ircutils.bold(ircutils.mircColor(base, "blue")), + ' (%s)' % url if url else '', + ':' if count else '' + )) + + def tag(self, repo, actor, action, base, to, onlyDeleted, headMsg, headId, url): + if onlyDeleted: + commitInfo = "" + else: + commitMsg = "" + if configValue("tagShowCommitMsg"): + commitMsg = ircutils.mircColor(" (%s)" % (maxLen(headMsg.splitlines()[0],75)),"brown") + commitInfo = " %s %s %s%s as" % (base, ircutils.bold('*'), ircutils.bold(headId[0:6]), commitMsg) + + self.msgs.append("%s: %s %s%s %s%s" % ( + ircutils.bold(repo), + ircutils.mircColor(actor, "green"), + colorAction(action), + commitInfo, + ircutils.bold(ircutils.mircColor(to, "blue")), + ' (%s)' % url if url else '' + )) + + def issue(self, repo, actor, action, issueNo, issueTitle, creator, milestone, url, assignee = None, comment = None): + formattedActor = ircutils.mircColor(actor, "green") + + if actor == assignee: + formattedActor = ircutils.bold(formattedActor) + + self.msgs.append( "%s: %s %s issue #%s \"%s\"%s%s%s %s%s)%s" % ( + ircutils.bold(repo), + formattedActor, + colorAction(action), + issueNo, + ircutils.bold(issueTitle), + " by %s" % ircutils.mircColor(creator,"green") if creator != actor else '', + " to %s" % ircutils.bold(ircutils.mircColor(assignee, "green")) if action == 'assigned' else '', + " (%s" % ircutils.mircColor(milestone, "brown") if milestone else '', + '- ' if milestone else '(', + url, + ": %s" % maxLen(comment, 70) if comment else '' + )) + + def wikiPush(self, repo, actor, count, url): + self.msgs.append( "%s: %s modified %s wiki %s (%s):" % ( + ircutils.bold(repo), + ircutils.mircColor(actor, "green"), + ircutils.bold(str(count)), + plural(count, "page", "pages"), + url + )) + + def wikiPages(self, repo, actor, pages, url): + urlShown = False; + + for page in pages: + if configValue("hidePush") and urlShown is False: + pageurl = "(%s)" % (url,) + urlShown = True + elif configValue("hidePush"): + pageurl = "" + else: + pageurl = "(%s)" % (page['url'],) + + self.msgs.append( "%s: %s %s %s * %s %s" % ( + ircutils.bold(repo), + ircutils.mircColor(actor, "green"), + colorAction(page['action']), + ircutils.bold(ircutils.mircColor(page['name'], "blue")), + ircutils.bold(page['hash'][0:6]), + pageurl, + )) + + def travis(self, repo, status, commitId, commitMessage, commitAuthor, buildUrl): + self.msgs.append( "%s: Build status: %s * %s by %s (%s - %s)" % ( + ircutils.bold(repo), + colorAction(status.lower()), + ircutils.bold(commitId[0:6]), + ircutils.mircColor(commitAuthor, "green"), + ircutils.mircColor(maxLen(commitMessage, 50), "dark gray"), + buildUrl + )) + + def status(self, repo, status, description, url): + self.msgs.append( "%s: %s - %s (%s)" % ( + ircutils.bold(repo), + colorAction(status), + description, + url + )) diff --git a/local/theme/Theme.py b/local/theme/Theme.py new file mode 100644 index 0000000..a1c565d --- /dev/null +++ b/local/theme/Theme.py @@ -0,0 +1,6 @@ +class Theme(object): + def __init__(self): + self.msgs = [] + + def finalize(self): + pass diff --git a/local/theme/__init__.py b/local/theme/__init__.py new file mode 100644 index 0000000..c877d71 --- /dev/null +++ b/local/theme/__init__.py @@ -0,0 +1,7 @@ +# Import all files in the current directory +import os +for module in os.listdir(os.path.dirname(__file__)): + if module == '__init__.py' or module[-3:] != '.py': + continue + __import__(module[:-3], locals(), globals()) +del module diff --git a/local/utility.py b/local/utility.py index 1f73fbd..b7d99ee 100644 --- a/local/utility.py +++ b/local/utility.py @@ -40,10 +40,19 @@ def plural(number, s, p): return p return s -def maxLen(msg, maxn=400): +def maxLen(msg, maxn=400, splitLines=True): """Cut down a string if its longer than `maxn` chars""" - if len(msg) > maxn: - ret = "%s..." % (msg[0:(maxn-3)]) + + if splitLines is True: + lines = msg.splitlines() + line = lines[0] + else: + line = msg + + if len(line) > maxn: + ret = "%s..." % (line[0:(maxn-3)]) + elif splitLines is True and len(lines) > 1: + ret = "%s..." % (line) else: ret = msg return ret @@ -56,7 +65,7 @@ def colorAction(action): "failed", "errored", "failure", "still failing", "broken" ]: return ircutils.bold(ircutils.mircColor(action, "red")) - if action in [ "merged" ]: + if action in [ "assigned", "merged" ]: return ircutils.bold(ircutils.mircColor(action, "light blue")) if action in [ "reopened", "pending" ]: return ircutils.bold(ircutils.mircColor(action, "blue")) |