From 019c08e0e97daf2c8a8d14654c1559344fda68a7 Mon Sep 17 00:00:00 2001 From: Edward Powell Date: Tue, 21 Oct 2014 12:43:29 -0400 Subject: [PATCH] [various] Remaining PEP8 fixes checkstyle.sh now checks most PEP8 problems, so they damn well better be resolved before pushing from now on. Closes #125, finally. --- calc.py | 3 +-- clock.py | 50 ++++++++++++++++++++++---------------------------- currency.py | 5 +++-- dice.py | 6 +++--- github.py | 9 +++++---- help.py | 20 +++++++++++--------- ip.py | 10 +++++----- isup.py | 4 ++-- remind.py | 1 + rss.py | 2 +- safety.py | 2 +- search.py | 4 ++-- url.py | 13 +++++++++---- wikipedia.py | 18 ++++++++---------- xkcd.py | 2 +- 15 files changed, 75 insertions(+), 74 deletions(-) diff --git a/calc.py b/calc.py index 0c9931029e..4008aa4ea6 100644 --- a/calc.py +++ b/calc.py @@ -40,8 +40,7 @@ def c(bot, trigger): except ZeroDivisionError: result = "Division by zero is not supported in this universe." except Exception as e: - result = "{error}: {msg}".format( - error=type(e), msg=e) + result = "{error}: {msg}".format(error=type(e), msg=e) bot.reply(result) diff --git a/clock.py b/clock.py index 9e86b5b634..bd13d73b81 100644 --- a/clock.py +++ b/clock.py @@ -65,18 +65,17 @@ def update_user(bot, trigger): tz = trigger.group(2) if not tz: bot.reply("What timezone do you want to set? Try one from " - "http://dft.ba/-tz") + "http://dft.ba/-tz") return if tz not in pytz.all_timezones: bot.reply("I don't know that time zone. Try one from " - "http://dft.ba/-tz") + "http://dft.ba/-tz") return bot.db.preferences.update(trigger.nick, {'tz': tz}) if len(tz) < 7: - bot.say("Okay, " + trigger.nick + - ", but you should use one from http://dft.ba/-tz if " - "you use DST.") + bot.say("Okay, {}, but you should use one from http://dft.ba/-tz " + "if you use DST.".format(trigger.nick)) else: bot.reply('I now have you in the %s time zone.' % tz) @@ -92,10 +91,9 @@ def update_user_format(bot, trigger): tformat = trigger.group(2) if not tformat: bot.reply("What format do you want me to use? Try using" - " http://strftime.net to make one.") + " http://strftime.net to make one.") - tz = get_timezone(bot.db, bot.config, None, None, - trigger.sender) + tz = get_timezone(bot.db, bot.config, None, None, trigger.sender) # Get old format as back-up old_format = bot.db.preferences.get(trigger.nick, 'time_format') @@ -104,16 +102,15 @@ def update_user_format(bot, trigger): bot.db.preferences.update(trigger.nick, {'time_format': tformat}) try: - timef = format_time(db = bot.db, zone=tz, nick=trigger.nick) + timef = format_time(db=bot.db, zone=tz, nick=trigger.nick) except: bot.reply("That format doesn't work. Try using" - " http://strftime.net to make one.") + " http://strftime.net to make one.") # New format doesn't work. Revert save in database. bot.db.preferences.update(trigger.nick, {'time_format': old_format}) return - bot.reply("Got it. Your time will now appear as %s. (If the " - "timezone is wrong, you might try the settz command)" - % timef) + bot.reply("Got it. Your time will now appear as {}. (If the timezone " + "is wrong, you might try the settz command)".format(timef)) else: bot.reply("I can't remember that; I don't have a database.") @@ -134,21 +131,20 @@ def update_channel(bot, trigger): tz = trigger.group(2) if not tz: bot.reply("What timezone do you want to set? Try one from " - "http://dft.ba/-tz") + "http://dft.ba/-tz") return if tz not in pytz.all_timezones: bot.reply("I don't know that time zone. Try one from " - "http://dft.ba/-tz") + "http://dft.ba/-tz") return bot.db.preferences.update(trigger.sender, {'tz': tz}) if len(tz) < 7: - bot.say("Okay, " + trigger.nick + - ", but you should use one from http://dft.ba/-tz if " - "you use DST.") + bot.say("Okay, {}, but you should use one from http://dft.ba/-tz " + "if you use DST.".format(trigger.nick)) else: bot.reply( - 'I now have {} in the {} time zone.'.format(trigger.sender,tz)) + 'I now have {} in the {} time zone.'.format(trigger.sender, tz)) @commands('setchanneltimeformat', 'setctf') @@ -166,19 +162,17 @@ def update_channel_format(bot, trigger): tformat = trigger.group(2) if not tformat: bot.reply("What format do you want me to use? Try using" - " http://strftime.net to make one.") + " http://strftime.net to make one.") - tz = get_timezone(bot.db, bot.config, None, None, - trigger.sender) + tz = get_timezone(bot.db, bot.config, None, None, trigger.sender) try: timef = format_time(zone=tz) except: bot.reply("That format doesn't work. Try using" - " http://strftime.net to make one.") + " http://strftime.net to make one.") return bot.db.preferences.update(trigger.sender, {'time_format': tformat}) - bot.reply("Got it. Times in this channel will now appear as %s " - "unless a user has their own format set. (If the timezone" - " is wrong, you might try the settz and channeltz " - "commands)" % timef) - + bot.reply("Got it. Times in this channel will now appear as {} " + "unless a user has their own format set. (If the timezone" + " is wrong, you might try the settz and channeltz " + "commands)".format(timef)) diff --git a/currency.py b/currency.py index 34eee176b1..714d74456c 100644 --- a/currency.py +++ b/currency.py @@ -54,7 +54,7 @@ def get_rate(code): def exchange(bot, trigger): """Show the exchange rate between two currencies""" if not trigger.group(2): - return bot.reply("No search term. An example: .cur 20 EUR in USD") + return bot.reply("No search term. An example: .cur 20 EUR in USD") match = regex.match(trigger.group(2)) if not match: # It's apologetic, because it's using Canadian data. @@ -68,6 +68,7 @@ def exchange(bot, trigger): bot.reply("Sorry, I didn't understand the input.") display(bot, amount, of, to) + def display(bot, amount, of, to): if not amount: bot.reply("Zero is zero, no matter what country you're in.") @@ -87,7 +88,7 @@ def display(bot, amount, of, to): result = amount / of_rate * to_rate bot.say("{} {} ({}) = {} {} ({})".format(amount, of, of_name, - result, to, to_name)) + result, to, to_name)) @commands('btc', 'bitcoin') diff --git a/dice.py b/dice.py index b55e062152..f50e1bef5d 100644 --- a/dice.py +++ b/dice.py @@ -134,14 +134,14 @@ def _roll_dice(bot, dice_expression): # Dice can't have zero or a negative number of sides. if dice_type <= 0: bot.reply("I don't have any dice with %d sides. =(" % dice_type) - return None # Signal there was a problem + return None # Signal there was a problem # Upper limit for dice should be at most a million. Creating a dict with # more than a million elements already takes a noticeable amount of time # on a fast computer and ~55kB of memory. if dice_num > 1000: bot.reply('I only have 1000 dice. =(') - return None # Signal there was a problem + return None # Signal there was a problem dice = DicePouch(dice_num, dice_type, 0) @@ -184,7 +184,7 @@ def roll(bot, trigger): arg_str = arg_str.replace("%", "%%") arg_str = re.sub(dice_regexp, "%s", arg_str) - f = lambda dice_expr: _roll_dice (bot, dice_expr) + f = lambda dice_expr: _roll_dice(bot, dice_expr) dice = list(map(f, dice_expressions)) if None in dice: diff --git a/github.py b/github.py index 1daab6e76c..a935140b87 100644 --- a/github.py +++ b/github.py @@ -21,11 +21,12 @@ import re issueURL = (r'https?://(?:www\.)?github.com/' - '([A-z0-9\-]+/[A-z0-9\-]+)/' - '(?:issues|pull)/' - '([\d]+)') + '([A-z0-9\-]+/[A-z0-9\-]+)/' + '(?:issues|pull)/' + '([\d]+)') regex = re.compile(issueURL) + def checkConfig(bot): if not bot.config.has_option('github', 'oauth_token') or not bot.config.has_option('github', 'repo'): return False @@ -195,6 +196,7 @@ def findIssue(bot, trigger): bot.reply('[#%s]\x02title:\x02 %s \x02|\x02 %s' % (data['number'], data['title'], body)) bot.say(data['html_url']) + @rule('.*%s.*' % issueURL) def issue_info(bot, trigger, match=None): match = match or trigger @@ -215,4 +217,3 @@ def issue_info(bot, trigger, match=None): bot.say('The API says this is an invalid issue. Please report this if you know it\'s a correct link!') return NOLIMIT bot.say('[#%s]\x02title:\x02 %s \x02|\x02 %s' % (data['number'], data['title'], body)) - diff --git a/help.py b/help.py index 4d9d691a79..6f235fa6e9 100644 --- a/help.py +++ b/help.py @@ -17,10 +17,12 @@ def setup(bot=None): if not bot: return - if bot.config.has_option('help', 'threshold') and not bot.config.help.threshold.isdecimal():#non-negative integer + if (bot.config.has_option('help', 'threshold') and not + bot.config.help.threshold.isdecimal()): # non-negative integer from willie.config import ConfigurationError raise ConfigurationError("Attribute threshold of section [help] must be a nonnegative integer") + @rule('$nick' '(?i)(help|doc) +([A-Za-z]+)(?:\?+)?$') @example('.help tell') @commands('help') @@ -32,19 +34,19 @@ def help(bot, trigger): else: name = trigger.group(2) name = name.lower() - + if bot.config.has_option('help', 'threshold'): - threshold=int(bot.config.help.threshold) + threshold = int(bot.config.help.threshold) else: - threshold=3 - + threshold = 3 + if name in bot.doc: if len(bot.doc[name][0]) + (1 if bot.doc[name][1] else 0) > threshold: - if trigger.nick != trigger.sender: #don't say that if asked in private + if trigger.nick != trigger.sender: # don't say that if asked in private bot.reply('The documentation for this command is too long; I\'m sending it to you in a private message.') - msgfun=lambda l: bot.msg(trigger.nick,l) + msgfun = lambda l: bot.msg(trigger.nick, l) else: - msgfun=bot.reply + msgfun = bot.reply for line in bot.doc[name][0]: msgfun(line) @@ -61,7 +63,7 @@ def commands(bot, trigger): bot.reply("I am sending you a private message of all my commands!") bot.msg(trigger.nick, 'Commands I recognise: ' + names + '.', max_messages=10) bot.msg(trigger.nick, ("For help, do '%s: help example' where example is the " + - "name of the command you want help for.") % bot.nick) + "name of the command you want help for.") % bot.nick) @rule('$nick' r'(?i)help(?:[?!]+)?$') diff --git a/ip.py b/ip.py index 048df03430..ca823cb8c4 100644 --- a/ip.py +++ b/ip.py @@ -87,9 +87,9 @@ def _find_geoip_db(bot): @commands('iplookup', 'ip') @example('.ip 8.8.8.8', - r'[IP/Host Lookup] Hostname: google-public-dns-a.google.com | Location: United States | Region: CA | ISP: Google Inc.', - re=True, - ignore='Downloading GeoIP database, please wait...') + r'[IP/Host Lookup] Hostname: google-public-dns-a.google.com | Location: United States | Region: CA | ISP: Google Inc.', + re=True, + ignore='Downloading GeoIP database, please wait...') def ip(bot, trigger): """IP Lookup tool""" if not trigger.group(2): @@ -107,7 +107,7 @@ def ip(bot, trigger): host = socket.getfqdn(query) response = "[IP/Host Lookup] Hostname: %s" % host response += " | Location: %s" % gi_city.country_name_by_name(query) - + region_data = gi_city.region_by_name(query) try: region = region_data['region_code'] # pygeoip >= 0.3.0 @@ -115,7 +115,7 @@ def ip(bot, trigger): region = region_data['region_name'] # pygeoip < 0.3.0 if region: response += " | Region: %s" % region - + isp = gi_org.org_by_name(query) if isp is not None: isp = re.sub('^AS\d+ ', '', isp) diff --git a/isup.py b/isup.py index 8a914ee5b4..e183fba1a3 100644 --- a/isup.py +++ b/isup.py @@ -25,10 +25,10 @@ def isup(bot, trigger): return bot.reply("Try it again without the %s" % protocol) else: site = 'http://' + site - + if not '.' in site: site += ".com" - + try: response = web.get(site) except Exception: diff --git a/remind.py b/remind.py index e4bcd5abae..d34541e677 100644 --- a/remind.py +++ b/remind.py @@ -23,6 +23,7 @@ except: pytz = None + def filename(self): name = self.nick + '-' + self.config.host + '.reminders.db' return os.path.join(self.config.dotdir, name) diff --git a/rss.py b/rss.py index e772b30581..53163b7f61 100644 --- a/rss.py +++ b/rss.py @@ -412,7 +412,7 @@ def disable_feed(): bot.debug( __file__, "Got HTTP 301 (Moved Permanently) on {0}, updating URI to {1}".format( - feed.name, fp.href), 'warning') + feed.name, fp.href), 'warning') c.execute(''' UPDATE rss_feeds SET feed_url = {0} WHERE channel = {0} AND feed_name = {0} diff --git a/safety.py b/safety.py index 66fc43ac7d..8da127d167 100644 --- a/safety.py +++ b/safety.py @@ -116,7 +116,7 @@ def url_handler(bot, trigger): 'scan': '1'} if trigger not in bot.memory['safety_cache']: - result = web.post(vt_base_api_url+'report', payload) + result = web.post(vt_base_api_url + 'report', payload) if sys.version_info.major > 2: result = result.decode('utf-8') result = json.loads(result) diff --git a/search.py b/search.py index f3ab03972e..49f68f6ef2 100644 --- a/search.py +++ b/search.py @@ -127,7 +127,7 @@ def duck_search(query): query = query.replace('!', '') uri = 'http://duckduckgo.com/html/?q=%s&kl=uk-en' % query bytes = web.get(uri) - if 'web-result"' in bytes: #filter out the adds on top of the page + if 'web-result"' in bytes: # filter out the adds on top of the page bytes = bytes.split('web-result"')[1] m = r_duck.search(bytes) if m: @@ -208,7 +208,7 @@ def suggest(bot, trigger): return bot.reply("No query term.") query = trigger.group(2) uri = 'http://websitedev.de/temp-bin/suggest.pl?q=' - answer = web.get(uri+query.replace('+', '%2B')) + answer = web.get(uri + query.replace('+', '%2B')) if answer: bot.say(answer) else: diff --git a/url.py b/url.py index ffb093179c..7b900728ab 100644 --- a/url.py +++ b/url.py @@ -43,9 +43,14 @@ def configure(config): if config.option('Exclude certain URLs from automatic title display', False): if not config.has_section('url'): config.add_section('url') - config.add_list('url', 'exclude', 'Enter regular expressions for each URL you would like to exclude.', + config.add_list( + 'url', + 'exclude', + 'Enter regular expressions for each URL you would like to exclude.', 'Regex:') - config.interactive_add('url', 'exclusion_char', + config.interactive_add( + 'url', + 'exclusion_char', 'Prefix to suppress URL titling', '!') @@ -83,7 +88,7 @@ def setup(bot=None): exclusion_char = bot.config.url.exclusion_char url_finder = re.compile(r'(?u)(%s?(?:http|https|ftp)(?:://\S+))' % - (exclusion_char)) + (exclusion_char)) @commands('title') @@ -210,7 +215,7 @@ def find_title(url): try: content, headers = web.get(url, return_headers=True, limit_bytes=max_bytes) except UnicodeDecodeError: - return # Fail silently when data can't be decoded + return # Fail silently when data can't be decoded # Some cleanup that I don't really grok, but was in the original, so # we'll keep it (with the compiled regexes made global) for now. diff --git a/wikipedia.py b/wikipedia.py index a9b4f3f402..977e30eb6c 100644 --- a/wikipedia.py +++ b/wikipedia.py @@ -14,6 +14,7 @@ REDIRECT = re.compile(r'^REDIRECT (.*)') + def configure(config): """ | [wikipedia] | example | purpose | @@ -28,6 +29,7 @@ def configure(config): c = 'Enter #channel:lang, one at time. When done, hit enter again.' config.add_list('wikipedia', 'lang_per_channel', c, 'Channel:') + def mw_search(server, query, num): """ Searches the specified MediaWiki server for the given query, and returns @@ -44,12 +46,13 @@ def mw_search(server, query, num): else: return None + def mw_snippet(server, query): """ Retrives a snippet of the specified length from the given page on the given server. """ - snippet_url = ('https://'+server+'/w/api.php?format=json' + snippet_url = ('https://' + server + '/w/api.php?format=json' '&action=query&prop=extracts&exintro&explaintext' '&exchars=300&redirects&titles=') snippet_url += query @@ -74,15 +77,10 @@ def wikipedia(bot, trigger): lang = bot.config.wikipedia.default_lang #change lang if channel has custom language set - if ( - trigger.sender and - not trigger.sender.is_nick() and - bot.config.has_option('wikipedia', 'lang_per_channel') - ): - customlang = re.search( - '('+trigger.sender+'):(\w+)', - bot.config.wikipedia.lang_per_channel - ) + if (trigger.sender and not trigger.sender.is_nick() and + bot.config.has_option('wikipedia', 'lang_per_channel')): + customlang = re.search('(' + trigger.sender + '):(\w+)', + bot.config.wikipedia.lang_per_channel) if customlang is not None: lang = customlang.group(2) diff --git a/xkcd.py b/xkcd.py index e73a59bb04..5d112aec66 100644 --- a/xkcd.py +++ b/xkcd.py @@ -88,7 +88,7 @@ def xkcd(bot, trigger): elif abs(query) == 0: requested = latest elif query == 404 or max_int + query == 404: - bot.say("404 - Not Found") # don't error on that one + bot.say("404 - Not Found") # don't error on that one return elif query > 0: requested = get_info(query)