Revision b10f66b9 snf-common/synnefo/util/keypath.py

b/snf-common/synnefo/util/keypath.py
36 36
integer_re = re.compile('-?[0-9]+')
37 37

  
38 38

  
39
def join_path(sep, path):
40
    iterable = ((str(n) if isinstance(n, (int, long)) else n) for n in path)
41
    return sep.join(iterable)
39
def unpack(pathstr, sep='.'):
40
    """
41
    >>> unpack('a.-2.x')
42
    ['a', -2, 'x']
43
    """
44
    names = pathstr.split(sep)
45
    parse = lambda x: int(x) if integer_re.match(x) else x
46
    return [parse(x) for x in names]
42 47

  
43 48

  
44
def lookup_path(container, path, sep='.', createpath=False):
49
def lookup_path(container, path, createpath=False):
45 50
    """
46 51
    return (['a','b'],
47 52
            [container['a'], container['a']['b']],
48
            'c')  where path=sep.join(['a','b','c'])
53
            'c')  where path=['a','b','c']
49 54

  
50 55
    """
51
    names = path.split(sep)
52
    dirnames = names[:-1]
53
    basename = names[-1]
54
    if integer_re.match(basename):
55
        basename = int(basename)
56
    dirnames = path[:-1]
57
    basename = path[-1]
56 58

  
57 59
    node = container
58 60
    name_path = []
59 61
    node_path = [node]
60 62
    for name in dirnames:
61 63
        name_path.append(name)
62

  
63
        if integer_re.match(name):
64
            name = int(name)
65

  
66 64
        try:
67 65
            node = node[name]
68 66
        except KeyError as e:
69 67
            if not createpath:
70
                m = "'{0}': path not found".format(join_path(sep, name_path))
68
                m = "{0}: path not found".format(name_path)
71 69
                raise KeyError(m)
72 70
            node[name] = {}
73 71
            node = node[name]
74 72
        except IndexError as e:
75 73
            if not createpath:
76
                m = "'{0}': path not found: {1}".format(
77
                    join_path(sep, name_path), e)
74
                m = "{0}: path not found: {1}".format(name_path, e)
78 75
                raise KeyError(m)
79 76
            size = name if name > 0 else -name
80 77
            node += (dict() for _ in xrange(len(node), size))
81 78
            node = node[name]
82 79
        except TypeError as e:
83
            m = "'{0}': cannot traverse path beyond this node: {1}"
84
            m = m.format(join_path(sep, name_path), str(e))
80
            m = "{0}: cannot traverse path beyond this node: {1}"
81
            m = m.format(name_path, str(e))
85 82
            raise ValueError(m)
86 83
        node_path.append(node)
87 84

  
......
97 94
                yield [name] + names, [node] + nodes
98 95

  
99 96

  
100
def list_paths(container, sep='.'):
97
def list_paths(container):
101 98
    """
102 99
    >>> sorted(list_paths({'a': {'b': {'c': 'd'}}}))
103
    [('a.b.c', 'd')]
100
    [(['a', 'b', 'c'], 'd')]
104 101
    >>> sorted(list_paths({'a': {'b': {'c': 'd'}, 'e': 3}}))
105
    [('a.b.c', 'd'), ('a.e', 3)]
102
    [(['a', 'b', 'c'], 'd'), (['a', 'e'], 3)]
106 103
    >>> sorted(list_paths({'a': {'b': {'c': 'd'}, 'e': {'f': 3}}}))
107
    [('a.b.c', 'd'), ('a.e.f', 3)]
104
    [(['a', 'b', 'c'], 'd'), (['a', 'e', 'f'], 3)]
108 105
    >>> sorted(list_paths({'a': [{'b': 3}, 2]}))
109
    [('a', [{'b': 3}, 2])]
106
    [(['a'], [{'b': 3}, 2])]
110 107
    >>> list_paths({})
111 108
    []
112 109

  
113 110
    """
114
    return [(join_path(sep, name_path), node_path[-1])
111
    return [(name_path, node_path[-1])
115 112
            for name_path, node_path in walk_paths(container)]
116 113

  
117 114

  
118
def del_path(container, path, sep='.', collect=True):
115
def del_path(container, path, collect=True):
119 116
    """
120
    del container['a']['b']['c'] where path=sep.join(['a','b','c'])
117
    del container['a']['b']['c'] where path=['a','b','c']
121 118

  
122
    >>> d = {'a': {'b': {'c': 'd'}}}; del_path(d, 'a.b.c'); d
119
    >>> d = {'a': {'b': {'c': 'd'}}}; del_path(d, ['a', 'b', 'c']); d
123 120
    {}
124
    >>> d = {'a': {'b': {'c': 'd'}}}; del_path(d, 'a.b.c', collect=False); d
121
    >>> d = {'a': {'b': {'c': 'd'}}}; del_path(d, ['a', 'b', 'c'],\
122
 collect=False); d
125 123
    {'a': {'b': {}}}
126
    >>> d = {'a': {'b': {'c': 'd'}}}; del_path(d, 'a.b.c.d')
124
    >>> d = {'a': {'b': {'c': 'd'}}}; del_path(d, ['a', 'b', 'c', 'd'])
127 125
    Traceback (most recent call last):
128
    ValueError: 'a.b.c': cannot traverse path beyond this node:\
126
    ValueError: ['a', 'b', 'c']: cannot traverse path beyond this node:\
129 127
 'str' object does not support item deletion
130 128
    """
131 129

  
132 130
    name_path, node_path, basename = \
133
            lookup_path(container, path, sep=sep, createpath=False)
131
        lookup_path(container, path, createpath=False)
134 132

  
135 133
    lastnode = node_path.pop()
136
    lastname = basename
137 134
    try:
138 135
        if basename in lastnode:
139 136
            del lastnode[basename]
140 137
    except (TypeError, KeyError) as e:
141
        m = "'{0}': cannot traverse path beyond this node: {1}"
142
        m = m.format(join_path(sep, name_path), str(e))
138
        m = "{0}: cannot traverse path beyond this node: {1}"
139
        m = m.format(name_path, str(e))
143 140
        raise ValueError(m)
144 141

  
145 142
    if collect:
......
149 146
            del lastnode[basename]
150 147

  
151 148

  
152
def get_path(container, path, sep='.'):
149
def get_path(container, path):
153 150
    """
154
    return container['a']['b']['c'] where path=sep.join(['a','b','c'])
151
    return container['a']['b']['c'] where path=['a','b','c']
155 152

  
156
    >>> get_path({'a': {'b': {'c': 'd'}}}, 'a.b.c.d')
153
    >>> get_path({'a': {'b': {'c': 'd'}}}, ['a', 'b', 'c', 'd'])
157 154
    Traceback (most recent call last):
158
    ValueError: 'a.b.c.d': cannot traverse path beyond this node:\
155
    ValueError: ['a', 'b', 'c', 'd']: cannot traverse path beyond this node:\
159 156
 string indices must be integers, not str
160
    >>> get_path({'a': {'b': {'c': 1}}}, 'a.b.c.d')
157
    >>> get_path({'a': {'b': {'c': 1}}}, ['a', 'b', 'c', 'd'])
161 158
    Traceback (most recent call last):
162
    ValueError: 'a.b.c.d': cannot traverse path beyond this node:\
163
 'int' object is unsubscriptable
164
    >>> get_path({'a': {'b': {'c': 1}}}, 'a.b.c')
159
    ValueError: ['a', 'b', 'c', 'd']: cannot traverse path beyond this node:\
160
 'int' object has no attribute '__getitem__'
161
    >>> get_path({'a': {'b': {'c': 1}}}, ['a', 'b', 'c'])
165 162
    1
166
    >>> get_path({'a': {'b': {'c': 1}}}, 'a.b')
163
    >>> get_path({'a': {'b': {'c': 1}}}, ['a', 'b'])
167 164
    {'c': 1}
168
    >>> get_path({'a': [{'z': 1}]}, 'a.0')
165
    >>> get_path({'a': [{'z': 1}]}, ['a', 'b'])
166
    Traceback (most recent call last):
167
    ValueError: ['a', 'b']: cannot traverse path beyond this node:\
168
 list indices must be integers, not str
169
    >>> get_path({'a': [{'z': 1}]}, ['a', 0])
169 170
    {'z': 1}
170
    >>> get_path({'a': [{'z': 1}]}, 'a.0.z')
171
    >>> get_path({'a': [{'z': 1}]}, ['a', 1])
172
    Traceback (most recent call last):
173
    KeyError: "['a', 1]: path not found: list index out of range"
174
    >>> get_path({'a': [{'z': 1}]}, ['a', 0, 'z'])
171 175
    1
172
    >>> get_path({'a': [{'z': 1}]}, 'a.-1.z')
176
    >>> get_path({'a': [{'z': 1}]}, ['a', -1, 'z'])
173 177
    1
174 178

  
175 179
    """
176 180
    name_path, node_path, basename = \
177
            lookup_path(container, path, sep=sep, createpath=False)
181
        lookup_path(container, path, createpath=False)
178 182
    name_path.append(basename)
179 183
    node = node_path[-1]
180 184

  
181 185
    try:
182 186
        return node[basename]
183 187
    except TypeError as e:
184
        m = "'{0}': cannot traverse path beyond this node: {1}"
185
        m = m.format(join_path(sep, name_path), str(e))
188
        m = "{0}: cannot traverse path beyond this node: {1}"
189
        m = m.format(name_path, str(e))
186 190
        raise ValueError(m)
187 191
    except KeyError as e:
188
        m = "'{0}': path not found: {1}"
189
        m = m.format(join_path(sep, name_path), str(e))
192
        m = "{0}: path not found: {1}"
193
        m = m.format(name_path, str(e))
194
        raise KeyError(m)
195
    except IndexError as e:
196
        m = "{0}: path not found: {1}"
197
        m = m.format(name_path, str(e))
190 198
        raise KeyError(m)
191 199

  
192 200

  
193
def set_path(container, path, value, sep='.',
194
             createpath=False, overwrite=True):
201
def set_path(container, path, value, createpath=False, overwrite=True):
195 202
    """
196
    container['a']['b']['c'] = value where path=sep.join(['a','b','c'])
203
    container['a']['b']['c'] = value where path=['a','b','c']
197 204

  
198
    >>> set_path({'a': {'b': {'c': 'd'}}}, 'a.b.c.d', 1)
199
    Traceback (most recent call last):
200
    ValueError: 'a.b.c.d': cannot index non-object node with string
201
    >>> set_path({'a': {'b': {'c': 'd'}}}, 'a.b.x.d', 1)
205
    >>> set_path({'a': {'b': {'c': 'd'}}}, ['a', 'b', 'c', 'd'], 1)
202 206
    Traceback (most recent call last):
203
    KeyError: "'a.b.x': path not found"
204
    >>> set_path({'a': {'b': {'c': 'd'}}}, 'a.b.x.d', 1, createpath=True)
205
    
206
    >>> set_path({'a': {'b': {'c': 'd'}}}, 'a.b.c', 1)
207
    
208
    >>> set_path({'a': {'b': {'c': 'd'}}}, 'a.b.c', 1, overwrite=False)
207
    ValueError: ['a', 'b', 'c', 'd']: cannot index, node is neither dict nor\
208
 list
209
    >>> set_path({'a': {'b': {'c': 'd'}}}, ['a', 'b', 'x', 'd'], 1)
209 210
    Traceback (most recent call last):
210
    ValueError: will not overwrite path 'a.b.c'
211
    >>> d = {'a': [{'z': 1}]}; set_path(d, 'a.-2.1', 2, createpath=False)
211
    KeyError: "['a', 'b', 'x']: path not found"
212
    >>> set_path({'a': {'b': {'c': 'd'}}}, ['a', 'b', 'x', 'd'], 1,\
213
 createpath=True)
214

  
215
    >>> set_path({'a': {'b': {'c': 'd'}}}, ['a', 'b', 'c'], 1)
216

  
217
    >>> set_path({'a': {'b': {'c': 'd'}}}, ['a', 'b', 'c'], 1, overwrite=False)
212 218
    Traceback (most recent call last):
213
    KeyError: "'a.-2': path not found: list index out of range"
214
    >>> d = {'a': [{'z': 1}]}; set_path(d, 'a.-2.1', 2, createpath=True)
219
    ValueError: will not overwrite path ['a', 'b', 'c']
220
    >>> d = {'a': [{'z': 1}]}; set_path(d, ['a', -2, 1], 2, createpath=False)
215 221
    Traceback (most recent call last):
216
    ValueError: 'a.-2.1': will not index object node with integer
217
    >>> d = {'a': [{'z': 1}]}; set_path(d, 'a.-2.z', 2, createpath=True); \
218
 d['a'][-2]['z']
222
    KeyError: "['a', -2]: path not found: list index out of range"
223
    >>> d = {'a': [{'z': 1}]}; set_path(d, ['a', -2, 1], 2, createpath=True); \
224
 d['a'][-2][1]
219 225
    2
220 226

  
221 227
    """
222 228
    name_path, node_path, basename = \
223
            lookup_path(container, path, sep=sep, createpath=createpath)
229
        lookup_path(container, path, createpath=createpath)
224 230
    name_path.append(basename)
225 231
    node = node_path[-1]
226 232

  
227 233
    if basename in node and not overwrite:
228
        m = "will not overwrite path '{0}'".format(path)
234
        m = "will not overwrite path {0}".format(path)
229 235
        raise ValueError(m)
230 236

  
231 237
    is_object_node = hasattr(node, 'keys')
232
    is_string_name = isinstance(basename, basestring)
233
    if not is_string_name and is_object_node:
234
        m = "'{0}': will not index object node with integer"
235
        m = m.format(join_path(sep, name_path))
238
    is_list_node = isinstance(node, list)
239
    if not is_object_node and not is_list_node:
240
        m = "{0}: cannot index, node is neither dict nor list"
241
        m = m.format(name_path)
236 242
        raise ValueError(m)
237
    if is_string_name and not is_object_node:
238
        m = "'{0}': cannot index non-object node with string"
239
        m = m.format(join_path(sep, name_path))
243

  
244
    is_integer = isinstance(basename, (int, long))
245
    if is_list_node and not is_integer:
246
        m = "{0}: cannot index list node without an integer"
247
        m = m.format(name_path)
240 248
        raise ValueError(m)
241 249
    try:
242 250
        node[basename] = value
243 251
    except TypeError as e:
244
        m = "'{0}': cannot traverse path beyond this node: {1}"
245
        m = m.format(join_path(sep, name_path), str(e))
252
        m = "{0}: cannot traverse path beyond this node: {1}"
253
        m = m.format(name_path, str(e))
246 254
        raise ValueError(m)
247 255

  
248 256

  

Also available in: Unified diff