root / kamaki / clients / utils / __init__.py @ b8220825
History | View | Annotate | Download (3.4 kB)
1 | b8220825 | Stavros Sachtouris | # Copyright 2013-2014 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 | 480585cd | Stavros Sachtouris | return (val1.lower() == val2.lower()) if ( |
39 | 480585cd | Stavros Sachtouris | exactMath) else val1.lower().startswith(val2.lower())
|
40 | 0f25af9f | Stavros Sachtouris | |
41 | 3dabe5d2 | Stavros Sachtouris | |
42 | 3dabe5d2 | Stavros Sachtouris | def filter_out(d, prefix, exactMatch=False): |
43 | 4cbe00ba | Stavros Sachtouris | """Remove entries that are prefixed with prefix (case insensitive)
|
44 | 4cbe00ba | Stavros Sachtouris |
|
45 | 4cbe00ba | Stavros Sachtouris | :param d: (dict) input
|
46 | 4cbe00ba | Stavros Sachtouris |
|
47 | 4cbe00ba | Stavros Sachtouris | :param prefix: (str) prefix to match input keys against
|
48 | 4cbe00ba | Stavros Sachtouris |
|
49 | 4cbe00ba | Stavros Sachtouris | :param exactMatch: (bool) key should fully match if True, just prefixed
|
50 | 4cbe00ba | Stavros Sachtouris | with prefix if False
|
51 | 4cbe00ba | Stavros Sachtouris |
|
52 | 4cbe00ba | Stavros Sachtouris | :returns: (dict) the updated d
|
53 | e6b39366 | Stavros Sachtouris | """
|
54 | 4067cdaf | Stavros Sachtouris | |
55 | 480585cd | Stavros Sachtouris | ret = dict()
|
56 | 0f25af9f | Stavros Sachtouris | for key, val in d.items(): |
57 | 4cbe00ba | Stavros Sachtouris | if not _matches(key, prefix, exactMath=exactMatch): |
58 | 0f25af9f | Stavros Sachtouris | ret[key] = val |
59 | 0f25af9f | Stavros Sachtouris | return ret
|
60 | e6b39366 | Stavros Sachtouris | |
61 | 3dabe5d2 | Stavros Sachtouris | |
62 | 3dabe5d2 | Stavros Sachtouris | def filter_in(d, prefix, exactMatch=False): |
63 | 4cbe00ba | Stavros Sachtouris | """Keep only entries of d prefixed with prefix
|
64 | 4067cdaf | Stavros Sachtouris |
|
65 | 4cbe00ba | Stavros Sachtouris | :param d: (dict) input
|
66 | 3dabe5d2 | Stavros Sachtouris |
|
67 | 4cbe00ba | Stavros Sachtouris | :param prefix: (str) prefix to match input keys against
|
68 | 3dabe5d2 | Stavros Sachtouris |
|
69 | 4cbe00ba | Stavros Sachtouris | :param exactMatch: (bool) key should fully match if True, just prefixed
|
70 | 4cbe00ba | Stavros Sachtouris | with prefix if False
|
71 | 4067cdaf | Stavros Sachtouris |
|
72 | 4cbe00ba | Stavros Sachtouris | :returns: (dict) the updated d
|
73 | 4cbe00ba | Stavros Sachtouris | """
|
74 | 480585cd | Stavros Sachtouris | ret = dict()
|
75 | 0f25af9f | Stavros Sachtouris | for key, val in d.items(): |
76 | 4cbe00ba | Stavros Sachtouris | if _matches(key, prefix, exactMath=exactMatch):
|
77 | 4cbe00ba | Stavros Sachtouris | ret[key] = val |
78 | 0f25af9f | Stavros Sachtouris | return ret
|
79 | 0e4bec91 | Stavros Sachtouris | |
80 | 3dabe5d2 | Stavros Sachtouris | |
81 | af3b2b36 | Stavros Sachtouris | def path4url(*args): |
82 | 4cbe00ba | Stavros Sachtouris | """
|
83 | 4cbe00ba | Stavros Sachtouris | :param args: (list of str)
|
84 | 4cbe00ba | Stavros Sachtouris |
|
85 | 4cbe00ba | Stavros Sachtouris | :returns: (str) a path in the form /args[0]/args[1]/...
|
86 | af3b2b36 | Stavros Sachtouris | """
|
87 | 4067cdaf | Stavros Sachtouris | |
88 | 3f931d41 | Stavros Sachtouris | r = '/'.join([''] + [arg.decode('utf-8') if ( |
89 | 4de960c7 | Stavros Sachtouris | isinstance(arg, str)) else '%s' % arg for arg in args]) |
90 | 3f931d41 | Stavros Sachtouris | while '//' in r: |
91 | 3f931d41 | Stavros Sachtouris | r = r.replace('//', '/') |
92 | a9e3df67 | Stavros Sachtouris | return ('/%s' % r.strip('/')) if r else '' |
93 | 3ddb2ade | Stavros Sachtouris | |
94 | 3ddb2ade | Stavros Sachtouris | |
95 | 3ddb2ade | Stavros Sachtouris | def readall(openfile, size, retries=7): |
96 | 3ddb2ade | Stavros Sachtouris | """Read a file until size is reached"""
|
97 | 3ddb2ade | Stavros Sachtouris | remains = size if size > 0 else 0 |
98 | 3ddb2ade | Stavros Sachtouris | buf = ''
|
99 | 3ddb2ade | Stavros Sachtouris | for i in range(retries): |
100 | d58df7c0 | Stavros Sachtouris | tmp_buf = openfile.read(remains) |
101 | d58df7c0 | Stavros Sachtouris | if tmp_buf:
|
102 | d58df7c0 | Stavros Sachtouris | buf += tmp_buf |
103 | d58df7c0 | Stavros Sachtouris | remains -= len(tmp_buf)
|
104 | d58df7c0 | Stavros Sachtouris | if remains > 0: |
105 | d58df7c0 | Stavros Sachtouris | continue
|
106 | 3ddb2ade | Stavros Sachtouris | return buf
|
107 | 3ddb2ade | Stavros Sachtouris | raise IOError('Failed to read %s bytes from file' % size) |