Changed indentation and wrote docstring for each function.
This commit is contained in:
@@ -3,7 +3,7 @@
|
|||||||
# @Author: KevinMidboe
|
# @Author: KevinMidboe
|
||||||
# @Date: 2017-11-01 15:57:23
|
# @Date: 2017-11-01 15:57:23
|
||||||
# @Last Modified by: KevinMidboe
|
# @Last Modified by: KevinMidboe
|
||||||
# @Last Modified time: 2017-12-22 11:16:09
|
# @Last Modified time: 2017-12-22 12:07:18
|
||||||
|
|
||||||
import re
|
import re
|
||||||
import logging
|
import logging
|
||||||
@@ -13,118 +13,137 @@ from datetime import datetime
|
|||||||
from colored import stylize
|
from colored import stylize
|
||||||
|
|
||||||
SYMBOLS = {
|
SYMBOLS = {
|
||||||
'customary' : ('B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'),
|
'customary' : ('B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'),
|
||||||
'customary_ext' : ('byte', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa',
|
'customary_ext' : ('byte', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa',
|
||||||
'zetta', 'iotta'),
|
'zetta', 'iotta'),
|
||||||
'iec' : ('BiB', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'),
|
'iec' : ('BiB', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'),
|
||||||
'iec_ext' : ('byte', 'kibi', 'mebi', 'gibi', 'tebi', 'pebi', 'exbi',
|
'iec_ext' : ('byte', 'kibi', 'mebi', 'gibi', 'tebi', 'pebi', 'exbi',
|
||||||
'zebi', 'yobi'),
|
'zebi', 'yobi'),
|
||||||
}
|
}
|
||||||
|
|
||||||
__all__ = ('ColorizeFilter', )
|
__all__ = ('ColorizeFilter', )
|
||||||
|
|
||||||
class ColorizeFilter(logging.Filter):
|
class ColorizeFilter(logging.Filter):
|
||||||
|
"""
|
||||||
|
Class for setting specific colors to levels of severity for log output
|
||||||
|
"""
|
||||||
|
|
||||||
color_by_level = {
|
color_by_level = {
|
||||||
logging.DEBUG: 'yellow',
|
logging.DEBUG: 'yellow',
|
||||||
logging.WARNING: 'red',
|
logging.WARNING: 'red',
|
||||||
logging.ERROR: 'red',
|
logging.ERROR: 'red',
|
||||||
logging.INFO: 'white'
|
logging.INFO: 'white'
|
||||||
}
|
}
|
||||||
|
|
||||||
def filter(self, record):
|
def filter(self, record):
|
||||||
record.raw_msg = record.msg
|
record.raw_msg = record.msg
|
||||||
color = self.color_by_level.get(record.levelno)
|
color = self.color_by_level.get(record.levelno)
|
||||||
if color:
|
if color:
|
||||||
record.msg = stylize(record.msg, colored.fg(color))
|
record.msg = stylize(record.msg, colored.fg(color))
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def sanitize(string, ignore_characters=None, replace_characters=None):
|
def sanitize(string, ignore_characters=None, replace_characters=None):
|
||||||
"""Sanitize a string to strip special characters.
|
"""
|
||||||
|
Sanitize a string to strip special characters.
|
||||||
|
|
||||||
:param str string: the string to sanitize.
|
:param str string: the string to sanitize.
|
||||||
:param set ignore_characters: characters to ignore.
|
:param set ignore_characters: characters to ignore.
|
||||||
:return: the sanitized string.
|
:return: the sanitized string.
|
||||||
:rtype: str
|
:rtype: str
|
||||||
|
|
||||||
"""
|
"""
|
||||||
# only deal with strings
|
# only deal with strings
|
||||||
if string is None:
|
if string is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
replace_characters = replace_characters or ''
|
replace_characters = replace_characters or ''
|
||||||
|
|
||||||
ignore_characters = ignore_characters or set()
|
ignore_characters = ignore_characters or set()
|
||||||
|
|
||||||
characters = ignore_characters
|
characters = ignore_characters
|
||||||
if characters:
|
if characters:
|
||||||
string = re.sub(r'[%s]' % re.escape(''.join(characters)), replace_characters, string)
|
string = re.sub(r'[%s]' % re.escape(''.join(characters)), replace_characters, string)
|
||||||
|
|
||||||
return string
|
return string
|
||||||
|
|
||||||
def return_re_match(string, re_statement):
|
def return_re_match(string, re_statement):
|
||||||
if string is None:
|
"""
|
||||||
return
|
Helper function for checking if a string contains a given regex statement
|
||||||
|
|
||||||
m = re.search(re_statement, string)
|
:param str string, str re_statement: string we want to check and regex string we
|
||||||
if 'Y-day' in m.group():
|
check for in string
|
||||||
return datetime.now().strftime('%m-%d %Y')
|
:return: sanitized string of the match we found
|
||||||
return sanitize(m.group(), '\xa0', ' ')
|
:rtype: str
|
||||||
|
"""
|
||||||
|
if string is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
m = re.search(re_statement, string)
|
||||||
# Can maybe be moved away from this class
|
# This is here for a edge case for piratebay torrent dates
|
||||||
# returns a number that is either the value of multiple_pages
|
if 'Y-day' in m.group():
|
||||||
# or if it exceeds total_pages, return total_pages.
|
return datetime.now().strftime('%m-%d %Y')
|
||||||
def pagesToCount(multiple, total):
|
return sanitize(m.group(), '\xa0', ' ')
|
||||||
if (multiple > total):
|
|
||||||
return total
|
|
||||||
return multiple
|
|
||||||
|
|
||||||
def representsInteger(str):
|
def representsInteger(str):
|
||||||
try:
|
"""
|
||||||
int(str)
|
Checks if a string only contains integers
|
||||||
return True
|
|
||||||
except ValueError:
|
:param str str: string we want to check only has integers
|
||||||
return False
|
:return: if string only contains integers
|
||||||
|
:rtype: boolean
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
int(str)
|
||||||
|
return True
|
||||||
|
except ValueError:
|
||||||
|
return False
|
||||||
|
|
||||||
def deHumansize(s):
|
def deHumansize(s):
|
||||||
"""
|
"""
|
||||||
Attempts to guess the string format based on default symbols
|
Attempts to guess the string format based on default symbols
|
||||||
set and return the corresponding bytes as an integer.
|
set and return the corresponding bytes as an integer.
|
||||||
When unable to recognize the format ValueError is raised.
|
When unable to recognize the format ValueError is raised.
|
||||||
|
|
||||||
:param str s: human file size that we want to convert
|
:param str s: human file size that we want to convert
|
||||||
:return: the guessed bytes in from the human file size
|
:return: the guessed bytes in from the human file size
|
||||||
:rtype: int
|
:rtype: int
|
||||||
"""
|
"""
|
||||||
|
|
||||||
init = s
|
init = s
|
||||||
num = ""
|
num = ""
|
||||||
while s and s[0:1].isdigit() or s[0:1] == '.':
|
while s and s[0:1].isdigit() or s[0:1] == '.':
|
||||||
num += s[0]
|
num += s[0]
|
||||||
s = s[1:]
|
s = s[1:]
|
||||||
num = float(num)
|
num = float(num)
|
||||||
letter = s.strip()
|
letter = s.strip()
|
||||||
for name, sset in SYMBOLS.items():
|
for name, sset in SYMBOLS.items():
|
||||||
if letter in sset:
|
if letter in sset:
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
if letter == 'k':
|
if letter == 'k':
|
||||||
# treat 'k' as an alias for 'K' as per: http://goo.gl/kTQMs
|
# treat 'k' as an alias for 'K' as per: http://goo.gl/kTQMs
|
||||||
sset = SYMBOLS['customary']
|
sset = SYMBOLS['customary']
|
||||||
letter = letter.upper()
|
letter = letter.upper()
|
||||||
else:
|
else:
|
||||||
raise ValueError("can't interpret %r" % init)
|
raise ValueError("can't interpret %r" % init)
|
||||||
prefix = {sset[0]:1}
|
prefix = {sset[0]:1}
|
||||||
for i, s in enumerate(sset[1:]):
|
for i, s in enumerate(sset[1:]):
|
||||||
prefix[s] = 1 << (i+1)*10
|
prefix[s] = 1 << (i+1)*10
|
||||||
return int(num * prefix[letter])
|
return int(num * prefix[letter])
|
||||||
|
|
||||||
def humansize(nbytes):
|
def humansize(nbytes):
|
||||||
suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
|
"""
|
||||||
i = 0
|
Translates a size in bytes to a human readable size format
|
||||||
while nbytes >= 1024 and i < len(suffixes)-1:
|
|
||||||
nbytes /= 1024.
|
:param int nbytes: integer of size of torrent file
|
||||||
i += 1
|
:return: size in bytes in a human readable format
|
||||||
f = ('%.2f' % nbytes).rstrip('0').rstrip('.')
|
:rtype: str
|
||||||
return '{} {}'.format(f, suffixes[i])
|
"""
|
||||||
|
suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
|
||||||
|
i = 0
|
||||||
|
while nbytes >= 1024 and i < len(suffixes)-1:
|
||||||
|
nbytes /= 1024.
|
||||||
|
i += 1
|
||||||
|
f = ('%.2f' % nbytes).rstrip('0').rstrip('.')
|
||||||
|
return '{} {}'.format(f, suffixes[i])
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user