32 |
32 |
# or implied, of GRNET S.A.
|
33 |
33 |
|
34 |
34 |
|
35 |
|
def matches(val1, val2, exactMath=True):
|
36 |
|
"""Case Insenstive match"""
|
|
35 |
def _matches(val1, val2, exactMath=True):
|
|
36 |
"""Case Insensitive match"""
|
37 |
37 |
|
38 |
38 |
if exactMath:
|
39 |
39 |
return True if val1.lower() == val2.lower() else False
|
... | ... | |
42 |
42 |
|
43 |
43 |
|
44 |
44 |
def filter_out(d, prefix, exactMatch=False):
|
45 |
|
"""@return a dict that contains the entries of d
|
46 |
|
that are NOT prefixed with prefic
|
|
45 |
"""Remove entries that are prefixed with prefix (case insensitive)
|
|
46 |
|
|
47 |
:param d: (dict) input
|
|
48 |
|
|
49 |
:param prefix: (str) prefix to match input keys against
|
|
50 |
|
|
51 |
:param exactMatch: (bool) key should fully match if True, just prefixed
|
|
52 |
with prefix if False
|
|
53 |
|
|
54 |
:returns: (dict) the updated d
|
47 |
55 |
"""
|
48 |
56 |
|
49 |
57 |
ret = {}
|
50 |
58 |
for key, val in d.items():
|
51 |
|
if not matches(key, prefix, exactMath=exactMatch):
|
|
59 |
if not _matches(key, prefix, exactMath=exactMatch):
|
52 |
60 |
ret[key] = val
|
53 |
61 |
return ret
|
54 |
62 |
|
55 |
63 |
|
56 |
64 |
def filter_in(d, prefix, exactMatch=False):
|
57 |
|
"""@return a dict that contains only the entries of d
|
58 |
|
that are prefixed with prefix
|
59 |
|
"""
|
|
65 |
"""Keep only entries of d prefixed with prefix
|
60 |
66 |
|
61 |
|
ret = {}
|
62 |
|
for key, val in d.items():
|
63 |
|
if matches(key, prefix, exactMath=exactMatch):
|
64 |
|
ret[key] = val
|
65 |
|
return ret
|
|
67 |
:param d: (dict) input
|
66 |
68 |
|
|
69 |
:param prefix: (str) prefix to match input keys against
|
67 |
70 |
|
68 |
|
def prefix_keys(d, prefix):
|
69 |
|
"""@return a sallow copy of d with all its keys prefixed with prefix
|
70 |
|
"""
|
|
71 |
:param exactMatch: (bool) key should fully match if True, just prefixed
|
|
72 |
with prefix if False
|
71 |
73 |
|
|
74 |
:returns: (dict) the updated d
|
|
75 |
"""
|
72 |
76 |
ret = {}
|
73 |
77 |
for key, val in d.items():
|
74 |
|
ret[prefix + key] = val
|
|
78 |
if _matches(key, prefix, exactMath=exactMatch):
|
|
79 |
ret[key] = val
|
75 |
80 |
return ret
|
76 |
81 |
|
77 |
82 |
|
78 |
83 |
def path4url(*args):
|
79 |
|
"""@return a string with all args in the form /arg1/arg2/...
|
80 |
|
@param args must be strings
|
|
84 |
"""
|
|
85 |
:param args: (list of str)
|
|
86 |
|
|
87 |
:returns: (str) a path in the form /args[0]/args[1]/...
|
81 |
88 |
"""
|
82 |
89 |
|
83 |
90 |
path = ''
|
... | ... | |
96 |
103 |
|
97 |
104 |
|
98 |
105 |
def params4url(params):
|
99 |
|
"""@return a string with all params in the form ?key1=val1&key2=val2&...
|
100 |
|
@param should be a dict.
|
|
106 |
"""{'key1':'val1', 'key2':None, 'key3':15} --> "?key1=val1&key2&key3=15"
|
|
107 |
|
|
108 |
:param params: (dict) request parameters in the form key:val
|
|
109 |
|
|
110 |
:returns: (str) http-request friendly in the form ?key1=val1&key2=val2&...
|
101 |
111 |
"""
|
102 |
112 |
|
103 |
113 |
assert(type(params) is dict)
|
... | ... | |
110 |
120 |
return result
|
111 |
121 |
|
112 |
122 |
|
113 |
|
def list2str(alist, seperator=','):
|
114 |
|
"""@return a string of comma seperated elements of the list"""
|
|
123 |
def list2str(alist, separator=','):
|
|
124 |
"""[val1, val2, val3] --> "val1,val2,val3"
|
|
125 |
|
|
126 |
:param separator: (str)
|
|
127 |
|
|
128 |
:returns: (str) all list elements separated by separator
|
|
129 |
"""
|
115 |
130 |
|
116 |
131 |
ret = ''
|
117 |
132 |
slist = sorted(alist)
|
... | ... | |
119 |
134 |
if 0 == slist.index(item):
|
120 |
135 |
ret = unicode(item)
|
121 |
136 |
else:
|
122 |
|
ret += seperator + unicode(item)
|
|
137 |
ret += separator + unicode(item)
|
123 |
138 |
return ret
|
124 |
|
|
125 |
|
|
126 |
|
def dict2file(d, f, depth=0):
|
127 |
|
for k, v in d.items():
|
128 |
|
f.write('%s%s: ' % ('\t' * depth, k))
|
129 |
|
if type(v) is dict:
|
130 |
|
f.write('\n')
|
131 |
|
dict2file(v, f, depth + 1)
|
132 |
|
elif type(v) is list:
|
133 |
|
f.write('\n')
|
134 |
|
list2file(v, f, depth + 1)
|
135 |
|
else:
|
136 |
|
f.write(' %s\n' % unicode(v))
|
137 |
|
|
138 |
|
|
139 |
|
def list2file(l, f, depth=1):
|
140 |
|
for item in l:
|
141 |
|
if type(item) is dict:
|
142 |
|
dict2file(item, f, depth + 1)
|
143 |
|
elif type(item) is list:
|
144 |
|
list2file(item, f, depth + 1)
|
145 |
|
else:
|
146 |
|
f.write('%s%s\n' % ('\t' * depth, unicode(item)))
|