diff --git a/SupyMisc/plugin.py b/SupyMisc/plugin.py index d616d42..e189f6f 100644 --- a/SupyMisc/plugin.py +++ b/SupyMisc/plugin.py @@ -28,6 +28,8 @@ ### import random +import re +import json try: from itertools import izip except ImportError: @@ -176,11 +178,32 @@ class SupyMisc(callbacks.Plugin): irc.reply(len(world.ircs)) netcount = wrap(netcount) - def supyplugins(self, irc, msg, args): + def supyplugins(self, irc, msg, args, text): """takes no arguments. Returns a URL for the source of this plugin. """ - irc.reply("SupyPlugins source is available at: https://github.com/GLolol/SupyPlugins") - supyplugins = wrap(supyplugins) + base = 'https://github.com/GLolol/SupyPlugins' + if not text: + irc.reply(format("SupyPlugins source is available at: %u", base)) + return + apiurl = 'https://api.github.com/repos/GLolol/SupyPlugins/contents/' + text = re.sub("\/+", "/", text) + try: + text, line = text.split("#") + except ValueError: + line = '' + try: + fd = utils.web.getUrl(apiurl + text) + data = json.loads(fd.decode("utf-8")) + if type(data) == list: + s = "%s/tree/master/%s" % (base, text) + else: + s = data['html_url'] + except (AttributeError, utils.web.Error): + irc.error('Not found.', Raise=True) + if line: + s += "#%s" % line + irc.reply(format('%u', s)) + supyplugins = wrap(supyplugins, [additional('text')]) def chancount(self, irc, msg, args): """takes no arguments. diff --git a/SupyMisc/test.py b/SupyMisc/test.py index 8a2209f..5c38420 100644 --- a/SupyMisc/test.py +++ b/SupyMisc/test.py @@ -64,4 +64,13 @@ class SupyMiscTestCase(PluginTestCase): self.assertResponse('mreplace hi,there hello,ok hmm, hi there everyone', 'hmm, hello ok everyone') + def testSPsourceFetch(self): + self.assertNotError('supyplugins') + self.assertRegexp('supyplugins SupyMisc//plugin.py', \ + '.*?blob\/master\/SupyMisc\/plugin\.py.*?') + self.assertRegexp('supyplugins SupyMisc/', \ + '.*?tree\/master\/SupyMisc.*?') + self.assertError('supyplugins asfswfuiahfawfawefawe') + + # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: