PEP8
This commit is contained in:
parent
ba565a3eaa
commit
c2a607198c
|
@ -24,7 +24,7 @@ class Input(dict):
|
||||||
|
|
||||||
def pm(msg):
|
def pm(msg):
|
||||||
conn.msg(nick, msg)
|
conn.msg(nick, msg)
|
||||||
|
|
||||||
def set_nick(nick):
|
def set_nick(nick):
|
||||||
conn.set_nick(nick)
|
conn.set_nick(nick)
|
||||||
|
|
||||||
|
@ -137,7 +137,7 @@ def match_command(command):
|
||||||
return prefix[0]
|
return prefix[0]
|
||||||
elif prefix and command not in prefix:
|
elif prefix and command not in prefix:
|
||||||
return prefix
|
return prefix
|
||||||
|
|
||||||
return command
|
return command
|
||||||
|
|
||||||
|
|
||||||
|
@ -166,7 +166,7 @@ def main(conn, out):
|
||||||
|
|
||||||
if isinstance(command, list): # multiple potential matches
|
if isinstance(command, list): # multiple potential matches
|
||||||
input = Input(conn, *out)
|
input = Input(conn, *out)
|
||||||
input.reply("did you mean %s or %s?" %
|
input.reply("did you mean %s or %s?" %
|
||||||
(', '.join(command[:-1]), command[-1]))
|
(', '.join(command[:-1]), command[-1]))
|
||||||
elif command in bot.commands:
|
elif command in bot.commands:
|
||||||
input = Input(conn, *out)
|
input = Input(conn, *out)
|
||||||
|
|
|
@ -18,7 +18,7 @@ split_re = re.compile(r'([\d+-]*)d?(F|\d*)', re.I)
|
||||||
def nrolls(count, n):
|
def nrolls(count, n):
|
||||||
"roll an n-sided die count times"
|
"roll an n-sided die count times"
|
||||||
if n == "F":
|
if n == "F":
|
||||||
return [random.randint(-1,1) for x in xrange(count)]
|
return [random.randint(-1, 1) for x in xrange(min(count, 100))]
|
||||||
if n < 2: # it's a coin
|
if n < 2: # it's a coin
|
||||||
if count < 5000:
|
if count < 5000:
|
||||||
return [random.randint(0, 1) for x in xrange(count)]
|
return [random.randint(0, 1) for x in xrange(count)]
|
||||||
|
@ -38,13 +38,15 @@ def nrolls(count, n):
|
||||||
def dice(inp):
|
def dice(inp):
|
||||||
".dice <diceroll> -- simulates dicerolls, e.g. .dice 2d20-d5+4 roll 2 " \
|
".dice <diceroll> -- simulates dicerolls, e.g. .dice 2d20-d5+4 roll 2 " \
|
||||||
"D20s, subtract 1D5, add 4"
|
"D20s, subtract 1D5, add 4"
|
||||||
desc = None
|
|
||||||
try: # if inp is a re.match object...
|
try: # if inp is a re.match object...
|
||||||
(inp, desc) = inp.groups()
|
(inp, desc) = inp.groups()
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
pass # we got called via hook.command, inp is already the roll
|
(inp, desc) = valid_diceroll_re.match(inp).groups()
|
||||||
if desc == None: (inp, desc) = valid_diceroll_re.match(inp).groups()
|
|
||||||
if "d" not in inp: return
|
if "d" not in inp:
|
||||||
|
return
|
||||||
|
|
||||||
spec = whitespace_re.sub('', inp)
|
spec = whitespace_re.sub('', inp)
|
||||||
if not valid_diceroll_re.match(spec):
|
if not valid_diceroll_re.match(spec):
|
||||||
return "Invalid diceroll"
|
return "Invalid diceroll"
|
||||||
|
@ -56,7 +58,7 @@ def dice(inp):
|
||||||
for roll in groups:
|
for roll in groups:
|
||||||
count, side = split_re.match(roll).groups()
|
count, side = split_re.match(roll).groups()
|
||||||
count = int(count) if count not in " +-" else 1
|
count = int(count) if count not in " +-" else 1
|
||||||
if side.upper() == "F": # fudge dice are basically 1d3-2
|
if side.upper() == "F": # fudge dice are basically 1d3-2
|
||||||
for fudge in nrolls(count, "F"):
|
for fudge in nrolls(count, "F"):
|
||||||
if fudge == 1:
|
if fudge == 1:
|
||||||
rolls.append("\x033+\x0F")
|
rolls.append("\x033+\x0F")
|
||||||
|
|
|
@ -7,7 +7,7 @@ from util import hook, http
|
||||||
@hook.command
|
@hook.command
|
||||||
def urban(inp):
|
def urban(inp):
|
||||||
'''.u/.urban <phrase> -- looks up <phrase> on urbandictionary.com'''
|
'''.u/.urban <phrase> -- looks up <phrase> on urbandictionary.com'''
|
||||||
|
|
||||||
url = 'http://www.urbandictionary.com/define.php'
|
url = 'http://www.urbandictionary.com/define.php'
|
||||||
page = http.get_html(url, term=inp)
|
page = http.get_html(url, term=inp)
|
||||||
words = page.xpath("//td[@class='word']")
|
words = page.xpath("//td[@class='word']")
|
||||||
|
@ -44,7 +44,7 @@ def define(inp):
|
||||||
|
|
||||||
def format_output(show_examples):
|
def format_output(show_examples):
|
||||||
result = '%s: ' % h.xpath('//dt[@class="title-word"]/a/text()')[0]
|
result = '%s: ' % h.xpath('//dt[@class="title-word"]/a/text()')[0]
|
||||||
|
|
||||||
correction = h.xpath('//span[@class="correct-word"]/text()')
|
correction = h.xpath('//span[@class="correct-word"]/text()')
|
||||||
if correction:
|
if correction:
|
||||||
result = 'definition for "%s": ' % correction[0]
|
result = 'definition for "%s": ' % correction[0]
|
||||||
|
@ -70,7 +70,7 @@ def define(inp):
|
||||||
synonyms = h.xpath('//dd[@class="synonyms"]')
|
synonyms = h.xpath('//dd[@class="synonyms"]')
|
||||||
if synonyms:
|
if synonyms:
|
||||||
result += synonyms[0].text_content()
|
result += synonyms[0].text_content()
|
||||||
|
|
||||||
result = re.sub(r'\s+', ' ', result)
|
result = re.sub(r'\s+', ' ', result)
|
||||||
result = re.sub('\xb0', '', result)
|
result = re.sub('\xb0', '', result)
|
||||||
return result
|
return result
|
||||||
|
@ -101,7 +101,7 @@ def etymology(inp):
|
||||||
return 'No etymology found for ' + inp
|
return 'No etymology found for ' + inp
|
||||||
|
|
||||||
etym = etym[0].text_content()
|
etym = etym[0].text_content()
|
||||||
|
|
||||||
etym = ' '.join(etym.split())
|
etym = ' '.join(etym.split())
|
||||||
|
|
||||||
if len(etym) > 400:
|
if len(etym) > 400:
|
||||||
|
|
|
@ -12,7 +12,7 @@ ed_url = "http://encyclopediadramatica.com/"
|
||||||
def drama(inp):
|
def drama(inp):
|
||||||
'''.drama <phrase> -- gets first paragraph of Encyclopedia Dramatica ''' \
|
'''.drama <phrase> -- gets first paragraph of Encyclopedia Dramatica ''' \
|
||||||
'''article on <phrase>'''
|
'''article on <phrase>'''
|
||||||
|
|
||||||
j = http.get_json(api_url, search=inp)
|
j = http.get_json(api_url, search=inp)
|
||||||
if not j[1]:
|
if not j[1]:
|
||||||
return 'no results found'
|
return 'no results found'
|
||||||
|
|
|
@ -37,14 +37,13 @@ def google(inp):
|
||||||
|
|
||||||
result = parsed['responseData']['results'][0]
|
result = parsed['responseData']['results'][0]
|
||||||
|
|
||||||
|
|
||||||
title = result['titleNoFormatting']
|
title = result['titleNoFormatting']
|
||||||
content = result['content']
|
content = result['content']
|
||||||
|
|
||||||
if len(content) == 0:
|
if len(content) == 0:
|
||||||
content = "No description available"
|
content = "No description available"
|
||||||
else:
|
else:
|
||||||
content = http.html.fromstring(content).text_content()
|
content = http.html.fromstring(content).text_content()
|
||||||
|
|
||||||
out = '%s -- \x02%s\x02: "%s"' % (result['unescapedUrl'], title, content)
|
out = '%s -- \x02%s\x02: "%s"' % (result['unescapedUrl'], title, content)
|
||||||
out = ' '.join(out.split())
|
out = ' '.join(out.split())
|
||||||
|
|
|
@ -48,7 +48,7 @@ def version(inp, notice=None):
|
||||||
p = subprocess.Popen(['git', 'log', '--oneline'], stdout=subprocess.PIPE)
|
p = subprocess.Popen(['git', 'log', '--oneline'], stdout=subprocess.PIPE)
|
||||||
stdout, _ = p.communicate()
|
stdout, _ = p.communicate()
|
||||||
p.wait()
|
p.wait()
|
||||||
|
|
||||||
revnumber = len(stdout.splitlines())
|
revnumber = len(stdout.splitlines())
|
||||||
|
|
||||||
ret = stdout.split(None, 1)[0]
|
ret = stdout.split(None, 1)[0]
|
||||||
|
|
|
@ -6,6 +6,6 @@ from util import hook
|
||||||
@hook.command
|
@hook.command
|
||||||
def profile(inp):
|
def profile(inp):
|
||||||
".profile <username> -- links to <username>'s profile on SA"
|
".profile <username> -- links to <username>'s profile on SA"
|
||||||
|
|
||||||
return 'http://forums.somethingawful.com/member.php?action=getinfo' + \
|
return 'http://forums.somethingawful.com/member.php?action=getinfo' + \
|
||||||
'&username=' + '+'.join(inp.split())
|
'&username=' + '+'.join(inp.split())
|
||||||
|
|
|
@ -4,6 +4,7 @@ from util import hook, http
|
||||||
thread_re = r"(?i)forums\.somethingawful\.com/\S+threadid=(\d+)"
|
thread_re = r"(?i)forums\.somethingawful\.com/\S+threadid=(\d+)"
|
||||||
showthread = "http://forums.somethingawful.com/showthread.php?noseen=1"
|
showthread = "http://forums.somethingawful.com/showthread.php?noseen=1"
|
||||||
|
|
||||||
|
|
||||||
def login(user, password):
|
def login(user, password):
|
||||||
http.jar.clear_expired_cookies()
|
http.jar.clear_expired_cookies()
|
||||||
if any(cookie.domain == 'forums.somethingawful.com' and
|
if any(cookie.domain == 'forums.somethingawful.com' and
|
||||||
|
@ -13,7 +14,7 @@ def login(user, password):
|
||||||
return
|
return
|
||||||
assert("malformed cookie jar")
|
assert("malformed cookie jar")
|
||||||
http.get("http://forums.somethingawful.com/account.php", cookies=True,
|
http.get("http://forums.somethingawful.com/account.php", cookies=True,
|
||||||
post_data="action=login&username=%s&password=%s" % (user, password))
|
post_data="action=login&username=%s&password=%s" % (user, password))
|
||||||
|
|
||||||
|
|
||||||
@hook.regex(thread_re)
|
@hook.regex(thread_re)
|
||||||
|
@ -24,7 +25,7 @@ def forum_link(inp, bot=None):
|
||||||
|
|
||||||
login(bot.config['sa_user'], bot.config['sa_password'])
|
login(bot.config['sa_user'], bot.config['sa_password'])
|
||||||
|
|
||||||
thread = http.get_html(showthread, threadid=inp.group(1), perpage='1',
|
thread = http.get_html(showthread, threadid=inp.group(1), perpage='1',
|
||||||
cookies=True)
|
cookies=True)
|
||||||
|
|
||||||
breadcrumbs = thread.xpath('//div[@class="breadcrumbs"]//a/text()')
|
breadcrumbs = thread.xpath('//div[@class="breadcrumbs"]//a/text()')
|
||||||
|
@ -46,8 +47,8 @@ def forum_link(inp, bot=None):
|
||||||
num_posts = int(num_posts[0].rsplit('=', 1)[1])
|
num_posts = int(num_posts[0].rsplit('=', 1)[1])
|
||||||
|
|
||||||
return '\x02%s\x02 > \x02%s\x02 by \x02%s\x02, %s post%s' % (
|
return '\x02%s\x02 > \x02%s\x02 by \x02%s\x02, %s post%s' % (
|
||||||
forum_title, thread_title, poster, num_posts,
|
forum_title, thread_title, poster, num_posts,
|
||||||
's' if num_posts > 1 else '')
|
's' if num_posts > 1 else '')
|
||||||
|
|
||||||
|
|
||||||
forum_abbrevs = {
|
forum_abbrevs = {
|
||||||
|
|
|
@ -8,7 +8,7 @@ from util import hook, http
|
||||||
@hook.command
|
@hook.command
|
||||||
def suggest(inp, inp_unstripped=''):
|
def suggest(inp, inp_unstripped=''):
|
||||||
".suggest [#n] <phrase> -- gets a random/the nth suggested google search"
|
".suggest [#n] <phrase> -- gets a random/the nth suggested google search"
|
||||||
|
|
||||||
inp = inp_unstripped
|
inp = inp_unstripped
|
||||||
m = re.match('^#(\d+) (.+)$', inp)
|
m = re.match('^#(\d+) (.+)$', inp)
|
||||||
if m:
|
if m:
|
||||||
|
|
|
@ -15,7 +15,7 @@ api_key = "D1EBA6781E2572BB"
|
||||||
@hook.command
|
@hook.command
|
||||||
def tv_next(inp):
|
def tv_next(inp):
|
||||||
".tv_next <series> -- get the next episode of <series> from thetvdb.com"
|
".tv_next <series> -- get the next episode of <series> from thetvdb.com"
|
||||||
|
|
||||||
# http://thetvdb.com/wiki/index.php/API:GetSeries
|
# http://thetvdb.com/wiki/index.php/API:GetSeries
|
||||||
query = http.get_xml(base_url + 'GetSeries.php', seriesname=inp)
|
query = http.get_xml(base_url + 'GetSeries.php', seriesname=inp)
|
||||||
series_id = query.xpath('//seriesid/text()')
|
series_id = query.xpath('//seriesid/text()')
|
||||||
|
@ -34,19 +34,19 @@ def tv_next(inp):
|
||||||
|
|
||||||
next_eps = []
|
next_eps = []
|
||||||
today = datetime.date.today()
|
today = datetime.date.today()
|
||||||
|
|
||||||
for episode in reversed(series.xpath('//Episode')):
|
for episode in reversed(series.xpath('//Episode')):
|
||||||
first_aired = episode.findtext("FirstAired")
|
first_aired = episode.findtext("FirstAired")
|
||||||
try:
|
try:
|
||||||
airdate = datetime.date(*map(int, first_aired.split('-')))
|
airdate = datetime.date(*map(int, first_aired.split('-')))
|
||||||
except (ValueError, TypeError):
|
except (ValueError, TypeError):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
episode_name = episode.findtext("EpisodeName") or "No Title Yet"
|
episode_name = episode.findtext("EpisodeName") or "No Title Yet"
|
||||||
episode_num = "S%02dE%02d" % (int(episode.findtext("SeasonNumber")),
|
episode_num = "S%02dE%02d" % (int(episode.findtext("SeasonNumber")),
|
||||||
int(episode.findtext("EpisodeNumber")))
|
int(episode.findtext("EpisodeNumber")))
|
||||||
episode_desc = '%s "%s"' % (episode_num, episode_name)
|
episode_desc = '%s "%s"' % (episode_num, episode_name)
|
||||||
|
|
||||||
if airdate > today:
|
if airdate > today:
|
||||||
next_eps = ['%s (%s)' % (first_aired, episode_desc)]
|
next_eps = ['%s (%s)' % (first_aired, episode_desc)]
|
||||||
elif airdate == today:
|
elif airdate == today:
|
||||||
|
@ -55,11 +55,12 @@ def tv_next(inp):
|
||||||
#we're iterating in reverse order with newest episodes last
|
#we're iterating in reverse order with newest episodes last
|
||||||
#so, as soon as we're past today, break out of loop
|
#so, as soon as we're past today, break out of loop
|
||||||
break
|
break
|
||||||
|
|
||||||
if not next_eps:
|
if not next_eps:
|
||||||
return "there are no new episodes scheduled for %s" % series_name
|
return "there are no new episodes scheduled for %s" % series_name
|
||||||
|
|
||||||
if len(next_eps) == 1:
|
if len(next_eps) == 1:
|
||||||
return "the next episode of %s airs %s" % (series_name, next_eps[0])
|
return "the next episode of %s airs %s" % (series_name, next_eps[0])
|
||||||
else:
|
else:
|
||||||
return "the next episodes of %s: %s" % (series_name, ", ".join(next_eps))
|
next_eps = ', '.join(next_eps)
|
||||||
|
return "the next episodes of %s: %s" % (series_name, next_eps)
|
||||||
|
|
|
@ -122,7 +122,7 @@ def twitter(inp):
|
||||||
reply_user = tweet.find(reply_user).text
|
reply_user = tweet.find(reply_user).text
|
||||||
if reply_name is not None and (reply_id is not None or
|
if reply_name is not None and (reply_id is not None or
|
||||||
reply_user is not None):
|
reply_user is not None):
|
||||||
add_reply(reply_name, reply_id if reply_id else -1)
|
add_reply(reply_name, reply_id or -1)
|
||||||
|
|
||||||
time = strftime('%Y-%m-%d %H:%M:%S',
|
time = strftime('%Y-%m-%d %H:%M:%S',
|
||||||
strptime(time.text,
|
strptime(time.text,
|
||||||
|
|
|
@ -23,13 +23,16 @@ inspired by:
|
||||||
|
|
||||||
__license__ = "Python"
|
__license__ = "Python"
|
||||||
|
|
||||||
import re, unicodedata, urlparse
|
import re
|
||||||
|
import unicodedata
|
||||||
|
import urlparse
|
||||||
from urllib import quote, unquote
|
from urllib import quote, unquote
|
||||||
|
|
||||||
default_port = {
|
default_port = {
|
||||||
'http': 80,
|
'http': 80,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class Normalizer(object):
|
class Normalizer(object):
|
||||||
def __init__(self, regex, normalize_func):
|
def __init__(self, regex, normalize_func):
|
||||||
self.regex = regex
|
self.regex = regex
|
||||||
|
@ -37,84 +40,92 @@ class Normalizer(object):
|
||||||
|
|
||||||
normalizers = ( Normalizer( re.compile(r'(?:https?://)?(?:[a-zA-Z0-9\-]+\.)?(?:amazon|amzn){1}\.(?P<tld>[a-zA-Z\.]{2,})\/(gp/(?:product|offer-listing|customer-media/product-gallery)/|exec/obidos/tg/detail/-/|o/ASIN/|dp/|(?:[A-Za-z0-9\-]+)/dp/)?(?P<ASIN>[0-9A-Za-z]{10})'),
|
normalizers = ( Normalizer( re.compile(r'(?:https?://)?(?:[a-zA-Z0-9\-]+\.)?(?:amazon|amzn){1}\.(?P<tld>[a-zA-Z\.]{2,})\/(gp/(?:product|offer-listing|customer-media/product-gallery)/|exec/obidos/tg/detail/-/|o/ASIN/|dp/|(?:[A-Za-z0-9\-]+)/dp/)?(?P<ASIN>[0-9A-Za-z]{10})'),
|
||||||
lambda m: r'http://amazon.%s/dp/%s' % (m.group('tld'), m.group('ASIN'))),
|
lambda m: r'http://amazon.%s/dp/%s' % (m.group('tld'), m.group('ASIN'))),
|
||||||
Normalizer( re.compile(r'.*waffleimages\.com.*/([0-9a-fA-F]{40})'),
|
Normalizer( re.compile(r'.*waffleimages\.com.*/([0-9a-fA-F]{40})'),
|
||||||
lambda m: r'http://img.waffleimages.com/%s' % m.group(1) ),
|
lambda m: r'http://img.waffleimages.com/%s' % m.group(1) ),
|
||||||
Normalizer( re.compile(r'(?:youtube.*?(?:v=|/v/)|youtu\.be/|yooouuutuuube.*?id=)([-_a-z0-9]+)'),
|
Normalizer( re.compile(r'(?:youtube.*?(?:v=|/v/)|youtu\.be/|yooouuutuuube.*?id=)([-_a-z0-9]+)'),
|
||||||
lambda m: r'http://youtube.com/watch?v=%s' % m.group(1) ),
|
lambda m: r'http://youtube.com/watch?v=%s' % m.group(1) ),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def normalize(url):
|
def normalize(url):
|
||||||
"""Normalize a URL."""
|
"""Normalize a URL."""
|
||||||
|
|
||||||
scheme, auth, path, query, fragment = urlparse.urlsplit(url.strip())
|
scheme, auth, path, query, fragment = urlparse.urlsplit(url.strip())
|
||||||
userinfo, host, port=re.search('([^@]*@)?([^:]*):?(.*)', auth).groups()
|
userinfo, host, port = re.search('([^@]*@)?([^:]*):?(.*)', auth).groups()
|
||||||
|
|
||||||
# Always provide the URI scheme in lowercase characters.
|
# Always provide the URI scheme in lowercase characters.
|
||||||
scheme = scheme.lower()
|
scheme = scheme.lower()
|
||||||
|
|
||||||
# Always provide the host, if any, in lowercase characters.
|
# Always provide the host, if any, in lowercase characters.
|
||||||
host = host.lower()
|
host = host.lower()
|
||||||
if host and host[-1] == '.': host = host[:-1]
|
if host and host[-1] == '.':
|
||||||
if host and host.startswith("www."):
|
host = host[:-1]
|
||||||
if not scheme: scheme = "http"
|
if host and host.startswith("www."):
|
||||||
|
if not scheme:
|
||||||
|
scheme = "http"
|
||||||
host = host[4:]
|
host = host[4:]
|
||||||
elif path and path.startswith("www."):
|
elif path and path.startswith("www."):
|
||||||
if not scheme: scheme = "http"
|
if not scheme:
|
||||||
|
scheme = "http"
|
||||||
path = path[4:]
|
path = path[4:]
|
||||||
|
|
||||||
# Only perform percent-encoding where it is essential.
|
# Only perform percent-encoding where it is essential.
|
||||||
# Always use uppercase A-through-F characters when percent-encoding.
|
# Always use uppercase A-through-F characters when percent-encoding.
|
||||||
# All portions of the URI must be utf-8 encoded NFC from Unicode strings
|
# All portions of the URI must be utf-8 encoded NFC from Unicode strings
|
||||||
def clean(string):
|
def clean(string):
|
||||||
string=unicode(unquote(string), 'utf-8', 'replace')
|
string = unicode(unquote(string), 'utf-8', 'replace')
|
||||||
return unicodedata.normalize('NFC', string).encode('utf-8')
|
return unicodedata.normalize('NFC', string).encode('utf-8')
|
||||||
path=quote(clean(path), "~:/?#[]@!$&'()*+,;=")
|
path = quote(clean(path), "~:/?#[]@!$&'()*+,;=")
|
||||||
fragment=quote(clean(fragment), "~")
|
fragment = quote(clean(fragment), "~")
|
||||||
|
|
||||||
# note care must be taken to only encode & and = characters as values
|
# note care must be taken to only encode & and = characters as values
|
||||||
query="&".join(["=".join([quote(clean(t), "~:/?#[]@!$'()*+,;=")
|
query = "&".join(["=".join([quote(clean(t), "~:/?#[]@!$'()*+,;=")
|
||||||
for t in q.split("=", 1)]) for q in query.split("&")])
|
for t in q.split("=", 1)]) for q in query.split("&")])
|
||||||
|
|
||||||
# Prevent dot-segments appearing in non-relative URI paths.
|
# Prevent dot-segments appearing in non-relative URI paths.
|
||||||
if scheme in ["", "http", "https", "ftp", "file"]:
|
if scheme in ["", "http", "https", "ftp", "file"]:
|
||||||
output=[]
|
output = []
|
||||||
for input in path.split('/'):
|
for input in path.split('/'):
|
||||||
if input=="":
|
if input == "":
|
||||||
if not output: output.append(input)
|
if not output:
|
||||||
elif input==".":
|
output.append(input)
|
||||||
|
elif input == ".":
|
||||||
pass
|
pass
|
||||||
elif input=="..":
|
elif input == "..":
|
||||||
if len(output)>1: output.pop()
|
if len(output) > 1:
|
||||||
|
output.pop()
|
||||||
else:
|
else:
|
||||||
output.append(input)
|
output.append(input)
|
||||||
if input in ["", ".", ".."]: output.append("")
|
if input in ["", ".", ".."]:
|
||||||
path='/'.join(output)
|
output.append("")
|
||||||
|
path = '/'.join(output)
|
||||||
|
|
||||||
# For schemes that define a default authority, use an empty authority if
|
# For schemes that define a default authority, use an empty authority if
|
||||||
# the default is desired.
|
# the default is desired.
|
||||||
if userinfo in ["@", ":@"]: userinfo=""
|
if userinfo in ["@", ":@"]:
|
||||||
|
userinfo = ""
|
||||||
|
|
||||||
# For schemes that define an empty path to be equivalent to a path of "/",
|
# For schemes that define an empty path to be equivalent to a path of "/",
|
||||||
# use "/".
|
# use "/".
|
||||||
if path=="" and scheme in ["http", "https", "ftp", "file"]:
|
if path == "" and scheme in ["http", "https", "ftp", "file"]:
|
||||||
path="/"
|
path = "/"
|
||||||
|
|
||||||
# For schemes that define a port, use an empty port if the default is
|
# For schemes that define a port, use an empty port if the default is
|
||||||
# desired
|
# desired
|
||||||
if port and scheme in default_port.keys():
|
if port and scheme in default_port.keys():
|
||||||
if port.isdigit():
|
if port.isdigit():
|
||||||
port=str(int(port))
|
port = str(int(port))
|
||||||
if int(port)==default_port[scheme]:
|
if int(port) == default_port[scheme]:
|
||||||
port = ''
|
port = ''
|
||||||
|
|
||||||
# Put it all back together again
|
# Put it all back together again
|
||||||
auth=(userinfo or "") + host
|
auth = (userinfo or "") + host
|
||||||
if port:
|
if port:
|
||||||
auth+=":"+port
|
auth += ":" + port
|
||||||
if url.endswith("#") and query == "" and fragment == "":
|
if url.endswith("#") and query == "" and fragment == "":
|
||||||
path += "#"
|
path += "#"
|
||||||
normal_url = urlparse.urlunsplit((scheme, auth, path, query, fragment)).replace(
|
normal_url = urlparse.urlunsplit((scheme, auth, path, query,
|
||||||
"http:///", "http://")
|
fragment)).replace("http:///", "http://")
|
||||||
for norm in normalizers:
|
for norm in normalizers:
|
||||||
m = norm.regex.match(normal_url)
|
m = norm.regex.match(normal_url)
|
||||||
if m:
|
if m:
|
||||||
|
|
Loading…
Reference in New Issue