Code cleanup

This commit is contained in:
Ruud
2014-01-19 19:15:58 +01:00
parent 3c03e400f0
commit 799299c7cc
214 changed files with 497 additions and 324 deletions
+37 -7
View File
@@ -11,8 +11,10 @@ import sys
log = CPLog(__name__)
def fnEscape(pattern):
return pattern.replace('[','[[').replace(']','[]]').replace('[[','[[]')
return pattern.replace('[', '[[').replace(']', '[]]').replace('[[', '[[]')
def link(src, dst):
if os.name == 'nt':
@@ -21,6 +23,7 @@ def link(src, dst):
else:
os.link(src, dst)
def symlink(src, dst):
if os.name == 'nt':
import ctypes
@@ -28,6 +31,7 @@ def symlink(src, dst):
else:
os.symlink(src, dst)
def getUserDir():
try:
import pwd
@@ -37,6 +41,7 @@ def getUserDir():
return os.path.expanduser('~')
def getDownloadDir():
user_dir = getUserDir()
@@ -49,6 +54,7 @@ def getDownloadDir():
return user_dir
def getDataDir():
# Windows
@@ -68,8 +74,10 @@ def getDataDir():
# Linux
return os.path.join(user_dir, '.couchpotato')
def isDict(object):
return isinstance(object, dict)
def isDict(obj):
return isinstance(obj, dict)
def mergeDicts(a, b, prepend_list = False):
assert isDict(a), isDict(b)
@@ -91,6 +99,7 @@ def mergeDicts(a, b, prepend_list = False):
current_dst[key] = current_src[key]
return dst
def removeListDuplicates(seq):
checked = []
for e in seq:
@@ -98,26 +107,32 @@ def removeListDuplicates(seq):
checked.append(e)
return checked
def flattenList(l):
if isinstance(l, list):
return sum(map(flattenList, l))
else:
return l
def md5(text):
return hashlib.md5(ss(text)).hexdigest()
def sha1(text):
return hashlib.sha1(text).hexdigest()
def isLocalIP(ip):
ip = ip.lstrip('htps:/')
regex = '/(^127\.)|(^192\.168\.)|(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^::1)$/'
return re.search(regex, ip) is not None or 'localhost' in ip or ip[:4] == '127.'
def getExt(filename):
return os.path.splitext(filename)[1][1:]
def cleanHost(host, protocol = True, ssl = False, username = None, password = None):
if not '://' in host and protocol:
@@ -129,7 +144,7 @@ def cleanHost(host, protocol = True, ssl = False, username = None, password = No
if protocol and username and password:
login = '%s:%s@' % (username, password)
if not login in host:
host = host.replace('://', '://' + login, 1)
host = host.replace('://', '://' + login, 1)
host = host.rstrip('/ ')
if protocol:
@@ -137,6 +152,7 @@ def cleanHost(host, protocol = True, ssl = False, username = None, password = No
return host
def getImdb(txt, check_inside = False, multiple = False):
if not check_inside:
@@ -161,10 +177,12 @@ def getImdb(txt, check_inside = False, multiple = False):
return False
def tryInt(s, default = 0):
try: return int(s)
except: return default
def tryFloat(s):
try:
if isinstance(s, str):
@@ -173,17 +191,24 @@ def tryFloat(s):
return float(s)
except: return 0
def natsortKey(s):
return map(tryInt, re.findall(r'(\d+|\D+)', s))
def natcmp(a, b):
return cmp(natsortKey(a), natsortKey(b))
a2 = natsortKey(a)
b2 = natsortKey(b)
return (a2 > b2) - (a2 < b2)
def toIterable(value):
if isinstance(value, collections.Iterable):
return value
return [value]
def getTitle(library_dict):
try:
try:
@@ -206,6 +231,7 @@ def getTitle(library_dict):
log.error('Could not get title for library item: %s', library_dict)
return None
def possibleTitles(raw_title):
titles = [
@@ -220,16 +246,20 @@ def possibleTitles(raw_title):
return list(set(titles))
def randomString(size = 8, chars = string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for x in range(size))
def splitString(str, split_on = ',', clean = True):
list = [x.strip() for x in str.split(split_on)] if str else []
return filter(None, list) if clean else list
l = [x.strip() for x in str.split(split_on)] if str else []
return filter(None, l) if clean else l
def dictIsSubset(a, b):
return all([k in b and b[k] == v for k, v in a.items()])
def isSubFolder(sub_folder, base_folder):
# Returns True if sub_folder is the same as or inside base_folder
return base_folder and sub_folder and os.path.normpath(base_folder).rstrip(os.path.sep) + os.path.sep in os.path.normpath(sub_folder).rstrip(os.path.sep) + os.path.sep