You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.0 KiB

import re
11 years ago
from couchpotato.core.helpers.encoding import toUnicode
11 years ago
from six.moves import urllib
11 years ago
from couchpotato.core.helpers.variable import natsortKey
11 years ago
def getParams(params):
reg = re.compile('^[a-z0-9_\.]+$')
# Sort keys
11 years ago
param_keys = list(params.keys())
param_keys.sort(key = natsortKey)
11 years ago
temp = {}
for param in param_keys:
value = params[param]
nest = re.split("([\[\]]+)", param)
if len(nest) > 1:
nested = []
for key in nest:
if reg.match(key):
nested.append(key)
current = temp
for item in nested:
if item is nested[-1]:
11 years ago
current[item] = toUnicode(urllib.parse.unquote(value))
else:
try:
current[item]
except:
current[item] = {}
current = current[item]
else:
11 years ago
temp[param] = toUnicode(urllib.parse.unquote(value))
if temp[param].lower() in ['true', 'false']:
temp[param] = temp[param].lower() != 'false'
return dictToList(temp)
non_decimal = re.compile(r'[^\d.]+')
11 years ago
def dictToList(params):
if type(params) is dict:
new = {}
11 years ago
for x, value in params.items():
try:
11 years ago
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
sorted_keys = sorted(value.keys(), key = alphanum_key)
all_ints = 0
for pnr in sorted_keys:
all_ints += 1 if non_decimal.sub('', pnr) == pnr else 0
if all_ints == len(sorted_keys):
new_value = [dictToList(value[k]) for k in sorted_keys]
else:
new_value = value
except:
new_value = value
new[x] = new_value
else:
new = params
return new