root / kamaki / clients / utils.py @ 379cd4bb
History | View | Annotate | Download (3.6 kB)
1 |
# Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
def matches(val1, val2, exactMath=True): |
35 |
"""Case Insenstive match"""
|
36 |
if exactMath:
|
37 |
return True if val1.lower() == val2.lower() else False |
38 |
else:
|
39 |
return True if val1.lower().startswith(val2.lower()) else False |
40 |
|
41 |
def filter_out(d, prefix, exactMatch = False): |
42 |
"""@return a dict that contains the entries of d that are NOT prefixed with prefic
|
43 |
"""
|
44 |
ret = {} |
45 |
for key, val in d.items(): |
46 |
if not matches(key, prefix, exactMath = exactMatch): |
47 |
ret[key] = val |
48 |
return ret
|
49 |
|
50 |
def filter_in(d, prefix, exactMatch = False): |
51 |
"""@return a dict that contains only the entries of d that are prefixed with prefix
|
52 |
"""
|
53 |
ret = {} |
54 |
for key, val in d.items(): |
55 |
if matches(key, prefix, exactMath = exactMatch):
|
56 |
ret[key] = val |
57 |
return ret
|
58 |
|
59 |
def prefix_keys(d, prefix): |
60 |
"""@return a sallow copy of d with all its keys prefixed with prefix
|
61 |
"""
|
62 |
ret = {} |
63 |
for key, val in d.items(): |
64 |
ret[prefix+key] = val |
65 |
return ret
|
66 |
|
67 |
def path4url(*args): |
68 |
"""@return a string with all args in the form /arg1/arg2/...
|
69 |
@param args must be strings
|
70 |
"""
|
71 |
path = ''
|
72 |
for arg in args: |
73 |
path = path + '/' + unicode(arg) |
74 |
return path
|
75 |
|
76 |
def params4url(params): |
77 |
"""@return a string with all params in the form ?key1=val1&key2=val2&...
|
78 |
e.g. input
|
79 |
{'key1':'val1', 'key2':None, 'key3':'val3'}
|
80 |
will return
|
81 |
?key1=val1&key2&key3=val3
|
82 |
@param should be a dict.
|
83 |
Use params['somekey']=None for params that will apear without
|
84 |
a value at the final string
|
85 |
"""
|
86 |
assert(type(params) is dict) |
87 |
result = ''
|
88 |
dlmtr = '?'
|
89 |
for name in params: |
90 |
result = result + dlmtr + name |
91 |
result = result + '=' + unicode(params[name]) if params[name] is not None else result |
92 |
dlmtr = '&'
|
93 |
return result
|
94 |
|
95 |
def list2str(alist, seperator=','): |
96 |
"""@return a string of comma seperated elements of the list"""
|
97 |
ret = ''
|
98 |
for item in sorted(alist): |
99 |
if 0 == alist.index(item): |
100 |
ret = unicode(item)
|
101 |
else:
|
102 |
ret += seperator+unicode(item)
|
103 |
return ret
|
104 |
|