PEP8 clean

This commit is contained in:
nemunaire 2014-11-09 14:11:54 +01:00
commit e17996d858
23 changed files with 496 additions and 331 deletions

View file

@ -18,14 +18,16 @@
import imp
def intToIP(n):
ip = ""
for i in range(0,4):
for i in range(0, 4):
mod = n % 256
ip = "%d.%s" % (mod, ip)
n = (n - mod) / 256
return ip[:len(ip) - 1]
def ipToInt(ip):
sum = 0
for b in ip.split("."):

View file

@ -19,6 +19,7 @@
from datetime import datetime, timezone
import time
def countdown(delta, resolution=5):
sec = delta.seconds
hours, remainder = divmod(sec, 3600)
@ -31,7 +32,7 @@ def countdown(delta, resolution=5):
if resolution > 0 and (force or an > 0):
force = True
sentence += " %i an"%(an)
sentence += " %i an" % an
if an > 1:
sentence += "s"
@ -42,7 +43,7 @@ def countdown(delta, resolution=5):
if resolution > 1 and (force or days > 0):
force = True
sentence += " %i jour"%(days)
sentence += " %i jour" % days
if days > 1:
sentence += "s"
@ -53,7 +54,7 @@ def countdown(delta, resolution=5):
if resolution > 2 and (force or hours > 0):
force = True
sentence += " %i heure"%(hours)
sentence += " %i heure" % hours
if hours > 1:
sentence += "s"
if resolution > 4:
@ -63,7 +64,7 @@ def countdown(delta, resolution=5):
if resolution > 3 and (force or minutes > 0):
force = True
sentence += " %i minute"%(minutes)
sentence += " %i minute" % minutes
if minutes > 1:
sentence += "s"
if resolution > 4:
@ -71,20 +72,21 @@ def countdown(delta, resolution=5):
if resolution > 4 and (force or seconds > 0):
force = True
sentence += " %i seconde"%(seconds)
sentence += " %i seconde" % seconds
if seconds > 1:
sentence += "s"
return sentence[1:]
def countdown_format(date, msg_before, msg_after, tz=None):
"""Replace in a text %s by a sentence incidated the remaining time before/after an event"""
if tz != None:
"""Replace in a text %s by a sentence incidated the remaining time
before/after an event"""
if tz is not None:
oldtz = os.environ['TZ']
os.environ['TZ'] = tz
time.tzset()
#Calculate time before the date
# Calculate time before the date
try:
if datetime.now(timezone.utc) > date:
sentence_c = msg_after
@ -100,8 +102,7 @@ def countdown_format(date, msg_before, msg_after, tz=None):
sentence_c = msg_before
delta = date - datetime.now()
if tz != None:
if tz is not None:
os.environ['TZ'] = oldtz
return sentence_c % countdown(delta)

View file

@ -29,24 +29,37 @@ xtrdt = re.compile(r'''^.*? (?P<day>[0-9]{1,4}) .+?
(?:[^0-9]*[m\":][^0-9]*(?P<second>[0-9]{1,2}))?)?)?.*?
$''', re.X)
def extractDate(msg):
"""Parse a message to extract a time and date"""
result = xtrdt.match(msg.lower())
if result is not None:
day = result.group("day")
month = result.group("month")
if month == "janvier" or month == "january" or month == "januar": month = 1
elif month == "fevrier" or month == "février" or month == "february": month = 2
elif month == "mars" or month == "march": month = 3
elif month == "avril" or month == "april": month = 4
elif month == "mai" or month == "may" or month == "maï": month = 5
elif month == "juin" or month == "juni" or month == "junni": month = 6
elif month == "juillet" or month == "jully" or month == "july": month = 7
elif month == "aout" or month == "août" or month == "august": month = 8
elif month == "september" or month == "septembre": month = 9
elif month == "october" or month == "october" or month == "oktober": month = 10
elif month == "november" or month == "novembre": month = 11
elif month == "december" or month == "decembre" or month == "décembre": month = 12
if month == "janvier" or month == "january" or month == "januar":
month = 1
elif month == "fevrier" or month == "février" or month == "february":
month = 2
elif month == "mars" or month == "march":
month = 3
elif month == "avril" or month == "april":
month = 4
elif month == "mai" or month == "may" or month == "maï":
month = 5
elif month == "juin" or month == "juni" or month == "junni":
month = 6
elif month == "juillet" or month == "jully" or month == "july":
month = 7
elif month == "aout" or month == "août" or month == "august":
month = 8
elif month == "september" or month == "septembre":
month = 9
elif month == "october" or month == "october" or month == "oktober":
month = 10
elif month == "november" or month == "novembre":
month = 11
elif month == "december" or month == "decembre" or month == "décembre":
month = 12
year = result.group("year")
@ -57,9 +70,12 @@ def extractDate(msg):
minute = result.group("minute")
second = result.group("second")
if year is None: year = date.today().year
if hour is None: hour = 0
if minute is None: minute = 0
if year is None:
year = date.today().year
if hour is None:
hour = 0
if minute is None:
minute = 0
if second is None:
second = 1
else:
@ -68,6 +84,7 @@ def extractDate(msg):
minute = int(minute) + 1
second = 0
return datetime(int(year), int(month), int(day), int(hour), int(minute), int(second))
return datetime(int(year), int(month), int(day),
int(hour), int(minute), int(second))
else:
return None

View file

@ -28,31 +28,39 @@ from urllib.request import urlopen
from exception import IRCException
import xmlparser
def isURL(url):
"""Return True if the URL can be parsed"""
o = urlparse(url)
return o.scheme == "" and o.netloc == "" and o.path == ""
def getScheme(url):
"""Return the protocol of a given URL"""
o = urlparse(url)
return o.scheme
def getHost(url):
"""Return the domain of a given URL"""
return urlparse(url).netloc
def getPort(url):
"""Return the port of a given URL"""
return urlparse(url).port
def getPath(url):
"""Return the page request of a given URL"""
return urlparse(url).path
def getUser(url):
"""Return the page request of a given URL"""
return urlparse(url).username
def getPassword(url):
"""Return the page request of a given URL"""
return urlparse(url).password
@ -67,16 +75,19 @@ def getURLContent(url, timeout=15):
o = urlparse("http://" + url)
if o.scheme == "http":
conn = http.client.HTTPConnection(o.netloc, port=o.port, timeout=timeout)
conn = http.client.HTTPConnection(o.netloc, port=o.port,
timeout=timeout)
elif o.scheme == "https":
conn = http.client.HTTPSConnection(o.netloc, port=o.port, timeout=timeout)
conn = http.client.HTTPSConnection(o.netloc, port=o.port,
timeout=timeout)
elif o.scheme is None or o.scheme == "":
conn = http.client.HTTPConnection(o.netloc, port=80, timeout=timeout)
else:
return None
try:
if o.query != '':
conn.request("GET", o.path + "?" + o.query, None, {"User-agent": "Nemubot v3"})
conn.request("GET", o.path + "?" + o.query,
None, {"User-agent": "Nemubot v3"})
else:
conn.request("GET", o.path, None, {"User-agent": "Nemubot v3"})
except socket.timeout:
@ -115,10 +126,14 @@ def getURLContent(url, timeout=15):
if res.status == http.client.OK or res.status == http.client.SEE_OTHER:
return data.decode(charset)
elif (res.status == http.client.FOUND or res.status == http.client.MOVED_PERMANENTLY) and res.getheader("Location") != url:
elif ((res.status == http.client.FOUND or
res.status == http.client.MOVED_PERMANENTLY) and
res.getheader("Location") != url):
return getURLContent(res.getheader("Location"), timeout)
else:
raise IRCException("A HTTP error occurs: %d - %s" % (res.status, http.client.responses[res.status]))
raise IRCException("A HTTP error occurs: %d - %s" %
(res.status, http.client.responses[res.status]))
def getXML(url, timeout=15):
"""Get content page and return XML parsed content"""
@ -128,6 +143,7 @@ def getXML(url, timeout=15):
else:
return xmlparser.parse_string(cnt.encode())
def getJSON(url, timeout=15):
"""Get content page and return JSON content"""
cnt = getURLContent(url, timeout)
@ -136,6 +152,7 @@ def getJSON(url, timeout=15):
else:
return json.loads(cnt.decode())
# Other utils
def htmlentitydecode(s):
@ -143,7 +160,11 @@ def htmlentitydecode(s):
return re.sub('&(%s);' % '|'.join(name2codepoint),
lambda m: chr(name2codepoint[m.group(1)]), s)
def striphtml(data):
"""Remove HTML tags from text"""
p = re.compile(r'<.*?>')
return htmlentitydecode(p.sub('', data).replace("&#x28;", "/(").replace("&#x29;", ")/").replace("&#x22;", "\""))
return htmlentitydecode(p.sub('', data)
.replace("&#x28;", "/(")
.replace("&#x29;", ")/")
.replace("&#x22;", "\""))