root / kamaki / clients / utils.py @ 4ff9d6e2
History | View | Annotate | Download (3.6 kB)
1 | 0e4bec91 | Stavros Sachtouris | # Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | 0e4bec91 | Stavros Sachtouris | #
|
3 | 0e4bec91 | Stavros Sachtouris | # Redistribution and use in source and binary forms, with or
|
4 | 0e4bec91 | Stavros Sachtouris | # without modification, are permitted provided that the following
|
5 | 0e4bec91 | Stavros Sachtouris | # conditions are met:
|
6 | 0e4bec91 | Stavros Sachtouris | #
|
7 | 0e4bec91 | Stavros Sachtouris | # 1. Redistributions of source code must retain the above
|
8 | 0e4bec91 | Stavros Sachtouris | # copyright notice, this list of conditions and the following
|
9 | 0e4bec91 | Stavros Sachtouris | # disclaimer.
|
10 | 0e4bec91 | Stavros Sachtouris | #
|
11 | 0e4bec91 | Stavros Sachtouris | # 2. Redistributions in binary form must reproduce the above
|
12 | 0e4bec91 | Stavros Sachtouris | # copyright notice, this list of conditions and the following
|
13 | 0e4bec91 | Stavros Sachtouris | # disclaimer in the documentation and/or other materials
|
14 | 0e4bec91 | Stavros Sachtouris | # provided with the distribution.
|
15 | 0e4bec91 | Stavros Sachtouris | #
|
16 | 0e4bec91 | Stavros Sachtouris | # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 | 0e4bec91 | Stavros Sachtouris | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | 0e4bec91 | Stavros Sachtouris | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 | 0e4bec91 | Stavros Sachtouris | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 | 0e4bec91 | Stavros Sachtouris | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 | 0e4bec91 | Stavros Sachtouris | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 | 0e4bec91 | Stavros Sachtouris | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 | 0e4bec91 | Stavros Sachtouris | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 | 0e4bec91 | Stavros Sachtouris | # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | 0e4bec91 | Stavros Sachtouris | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 | 0e4bec91 | Stavros Sachtouris | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 | 0e4bec91 | Stavros Sachtouris | # POSSIBILITY OF SUCH DAMAGE.
|
28 | 0e4bec91 | Stavros Sachtouris | #
|
29 | 0e4bec91 | Stavros Sachtouris | # The views and conclusions contained in the software and
|
30 | 0e4bec91 | Stavros Sachtouris | # documentation are those of the authors and should not be
|
31 | 0e4bec91 | Stavros Sachtouris | # interpreted as representing official policies, either expressed
|
32 | 0e4bec91 | Stavros Sachtouris | # or implied, of GRNET S.A.
|
33 | 0e4bec91 | Stavros Sachtouris | |
34 | 3dabe5d2 | Stavros Sachtouris | |
35 | 4cbe00ba | Stavros Sachtouris | def _matches(val1, val2, exactMath=True): |
36 | 4cbe00ba | Stavros Sachtouris | """Case Insensitive match"""
|
37 | 4067cdaf | Stavros Sachtouris | |
38 | 0f25af9f | Stavros Sachtouris | if exactMath:
|
39 | 0f25af9f | Stavros Sachtouris | return True if val1.lower() == val2.lower() else False |
40 | 0f25af9f | Stavros Sachtouris | else:
|
41 | 0f25af9f | Stavros Sachtouris | return True if val1.lower().startswith(val2.lower()) else False |
42 | 0f25af9f | Stavros Sachtouris | |
43 | 3dabe5d2 | Stavros Sachtouris | |
44 | 3dabe5d2 | Stavros Sachtouris | def filter_out(d, prefix, exactMatch=False): |
45 | 4cbe00ba | Stavros Sachtouris | """Remove entries that are prefixed with prefix (case insensitive)
|
46 | 4cbe00ba | Stavros Sachtouris |
|
47 | 4cbe00ba | Stavros Sachtouris | :param d: (dict) input
|
48 | 4cbe00ba | Stavros Sachtouris |
|
49 | 4cbe00ba | Stavros Sachtouris | :param prefix: (str) prefix to match input keys against
|
50 | 4cbe00ba | Stavros Sachtouris |
|
51 | 4cbe00ba | Stavros Sachtouris | :param exactMatch: (bool) key should fully match if True, just prefixed
|
52 | 4cbe00ba | Stavros Sachtouris | with prefix if False
|
53 | 4cbe00ba | Stavros Sachtouris |
|
54 | 4cbe00ba | Stavros Sachtouris | :returns: (dict) the updated d
|
55 | e6b39366 | Stavros Sachtouris | """
|
56 | 4067cdaf | Stavros Sachtouris | |
57 | 0f25af9f | Stavros Sachtouris | ret = {} |
58 | 0f25af9f | Stavros Sachtouris | for key, val in d.items(): |
59 | 4cbe00ba | Stavros Sachtouris | if not _matches(key, prefix, exactMath=exactMatch): |
60 | 0f25af9f | Stavros Sachtouris | ret[key] = val |
61 | 0f25af9f | Stavros Sachtouris | return ret
|
62 | e6b39366 | Stavros Sachtouris | |
63 | 3dabe5d2 | Stavros Sachtouris | |
64 | 3dabe5d2 | Stavros Sachtouris | def filter_in(d, prefix, exactMatch=False): |
65 | 4cbe00ba | Stavros Sachtouris | """Keep only entries of d prefixed with prefix
|
66 | 4067cdaf | Stavros Sachtouris |
|
67 | 4cbe00ba | Stavros Sachtouris | :param d: (dict) input
|
68 | 3dabe5d2 | Stavros Sachtouris |
|
69 | 4cbe00ba | Stavros Sachtouris | :param prefix: (str) prefix to match input keys against
|
70 | 3dabe5d2 | Stavros Sachtouris |
|
71 | 4cbe00ba | Stavros Sachtouris | :param exactMatch: (bool) key should fully match if True, just prefixed
|
72 | 4cbe00ba | Stavros Sachtouris | with prefix if False
|
73 | 4067cdaf | Stavros Sachtouris |
|
74 | 4cbe00ba | Stavros Sachtouris | :returns: (dict) the updated d
|
75 | 4cbe00ba | Stavros Sachtouris | """
|
76 | 0f25af9f | Stavros Sachtouris | ret = {} |
77 | 0f25af9f | Stavros Sachtouris | for key, val in d.items(): |
78 | 4cbe00ba | Stavros Sachtouris | if _matches(key, prefix, exactMath=exactMatch):
|
79 | 4cbe00ba | Stavros Sachtouris | ret[key] = val |
80 | 0f25af9f | Stavros Sachtouris | return ret
|
81 | 0e4bec91 | Stavros Sachtouris | |
82 | 3dabe5d2 | Stavros Sachtouris | |
83 | af3b2b36 | Stavros Sachtouris | def path4url(*args): |
84 | 4cbe00ba | Stavros Sachtouris | """
|
85 | 4cbe00ba | Stavros Sachtouris | :param args: (list of str)
|
86 | 4cbe00ba | Stavros Sachtouris |
|
87 | 4cbe00ba | Stavros Sachtouris | :returns: (str) a path in the form /args[0]/args[1]/...
|
88 | af3b2b36 | Stavros Sachtouris | """
|
89 | 4067cdaf | Stavros Sachtouris | |
90 | af3b2b36 | Stavros Sachtouris | path = ''
|
91 | af3b2b36 | Stavros Sachtouris | for arg in args: |
92 | a517ff50 | Stavros Sachtouris | suffix = '%s' % arg
|
93 | 6f1ec797 | Stavros Sachtouris | try:
|
94 | 6f1ec797 | Stavros Sachtouris | while suffix[0] == '/': |
95 | 3dabe5d2 | Stavros Sachtouris | suffix = suffix[1:]
|
96 | 6f1ec797 | Stavros Sachtouris | except IndexError: |
97 | 6f1ec797 | Stavros Sachtouris | continue
|
98 | 6f1ec797 | Stavros Sachtouris | if len(path) > 0 and path[-1] == '/': |
99 | 6f1ec797 | Stavros Sachtouris | path += suffix |
100 | 6f1ec797 | Stavros Sachtouris | else:
|
101 | 3dabe5d2 | Stavros Sachtouris | path += '/' + suffix
|
102 | af3b2b36 | Stavros Sachtouris | return path
|
103 | af3b2b36 | Stavros Sachtouris | |
104 | 3dabe5d2 | Stavros Sachtouris | |
105 | af3b2b36 | Stavros Sachtouris | def params4url(params): |
106 | 4cbe00ba | Stavros Sachtouris | """{'key1':'val1', 'key2':None, 'key3':15} --> "?key1=val1&key2&key3=15"
|
107 | 4cbe00ba | Stavros Sachtouris |
|
108 | 4cbe00ba | Stavros Sachtouris | :param params: (dict) request parameters in the form key:val
|
109 | 4cbe00ba | Stavros Sachtouris |
|
110 | 4cbe00ba | Stavros Sachtouris | :returns: (str) http-request friendly in the form ?key1=val1&key2=val2&...
|
111 | af3b2b36 | Stavros Sachtouris | """
|
112 | 4067cdaf | Stavros Sachtouris | |
113 | af3b2b36 | Stavros Sachtouris | assert(type(params) is dict) |
114 | af3b2b36 | Stavros Sachtouris | result = ''
|
115 | e742badc | Stavros Sachtouris | dlmtr = '?'
|
116 | af3b2b36 | Stavros Sachtouris | for name in params: |
117 | e742badc | Stavros Sachtouris | result = result + dlmtr + name |
118 | 3dabe5d2 | Stavros Sachtouris | result += '=%s' % params[name] if params[name] else result |
119 | e742badc | Stavros Sachtouris | dlmtr = '&'
|
120 | af3b2b36 | Stavros Sachtouris | return result |