Skip to content

Commit

Permalink
[various] Remaining PEP8 fixes
Browse files Browse the repository at this point in the history
checkstyle.sh now checks most PEP8 problems, so they damn well better be
resolved before pushing from now on.

Closes sopel-irc#125, finally.
  • Loading branch information
embolalia committed Oct 21, 2014
1 parent 864e1c6 commit 019c08e
Show file tree
Hide file tree
Showing 15 changed files with 75 additions and 74 deletions.
3 changes: 1 addition & 2 deletions calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
50 changes: 22 additions & 28 deletions clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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')
Expand All @@ -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.")

Expand All @@ -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')
Expand All @@ -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))
5 changes: 3 additions & 2 deletions currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.")
Expand All @@ -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')
Expand Down
6 changes: 3 additions & 3 deletions dice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand Down
9 changes: 5 additions & 4 deletions github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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))

20 changes: 11 additions & 9 deletions help.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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)
Expand All @@ -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(?:[?!]+)?$')
Expand Down
10 changes: 5 additions & 5 deletions ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -107,15 +107,15 @@ 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
except KeyError:
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)
Expand Down
4 changes: 2 additions & 2 deletions isup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions remind.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion rss.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion safety.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
13 changes: 9 additions & 4 deletions url.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', '!')


Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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.
Expand Down
Loading

0 comments on commit 019c08e

Please sign in to comment.