-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathbuilder.py
More file actions
57 lines (50 loc) · 1.23 KB
/
builder.py
File metadata and controls
57 lines (50 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# coding: utf-8
'''
Created on 2012-03-28
@author: Tomasz 'Doppler' Najdek
Updated 2012-04-01 Bernard 'berni' Kobos
'''
try:
# for Python3
import urllib.parse as urllib
except ImportError:
# for Python2
import urllib
try:
unicode
except NameError:
# for Python3
unicode = str
def build(item, encoding=None):
def recursion(item, base=None):
pairs = list()
if(hasattr(item, 'values')):
for key, value in item.items():
if encoding:
quoted_key = urllib.quote(unicode(key).encode(encoding))
else:
quoted_key = urllib.quote(unicode(key))
if(base):
new_base = "%s[%s]" % (base, quoted_key)
pairs += recursion(value, new_base)
else:
new_base = quoted_key
pairs += recursion(value, new_base)
elif(isinstance(item, list)):
for (index, value) in enumerate(item):
if(base):
new_base = "%s" % (base)
pairs += recursion(value, new_base)
else:
pairs += recursion(value)
else:
if encoding:
quoted_item = urllib.quote(unicode(item).encode(encoding))
else:
quoted_item = urllib.quote(unicode(item))
if(base):
pairs.append("%s=%s" % (base, quoted_item))
else:
pairs.append(quoted_item)
return pairs
return '&'.join(recursion(item))