Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / utils.py @ 4f01cc99

History | View | Annotate | Download (3.2 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 filter_out(d, prefix, exactMatch = False):
35
    """@return a dict that contains the entries of d that are NOT prefixed with prefic
36
    """
37
    if exactMatch:
38
        return {key:d[key] for key in d if not key.lower() == prefix.lower()}
39
    return {key:d[key] for key in d if not key.lower().startswith(prefix.lower())}
40

    
41
def filter_in(d, prefix, exactMatch = False):
42
    """@return a dict that contains only the entries of d that are prefixed with prefix
43
    """
44
    if exactMatch:
45
        return {key:d[key] for key in d if key.lower() == prefix.lower()}
46
    return {key:d[key] for key in d if key.lower().startswith(prefix.lower())}
47
    
48
def prefix_keys(d, prefix):
49
    """@return a sallow copy of d with all its keys prefixed with prefix
50
    """
51
    return {prefix+key:d[key] for key in d.keys()}
52

    
53
def path4url(*args):
54
    """@return a string with all args in the form /arg1/arg2/...
55
       @param args must be strings
56
    """
57
    path = ''
58
    for arg in args:
59
        path = path + '/' + unicode(arg)
60
    return path
61

    
62
def params4url(params):
63
    """@return a string with all params in the form ?key1=val1&key2=val2&...
64
            e.g. input
65
                {'key1':'val1', 'key2':None, 'key3':'val3'}
66
            will return
67
                ?key1=val1&key2&key3=val3
68
       @param should be a dict.
69
            Use params['somekey']=None for params that will apear without 
70
            a value at the final string
71
    """
72
    assert(type(params) is dict)
73
    result = ''
74
    dlmtr = '?'
75
    for name in params:
76
        result = result + dlmtr + name
77
        result = result + '=' + params[name] if params[name] is not None else result
78
        dlmtr = '&'
79
    return result
80