comparison src/tools/common/data_format.py @ 1919:d3354c80bd1f

core (tools): moved common to a separate package, and put data method in a data_format module
author Goffi <goffi@goffi.org>
date Tue, 22 Mar 2016 22:46:04 +0100
parents src/tools/common.py@d17772b0fe22
children 2daf7b4c6756
comparison
equal deleted inserted replaced
1918:01d56efd488b 1919:d3354c80bd1f
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # SAT: a jabber client
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 """ tools common to backend and frontends """
21
22 from sat.core import exceptions
23
24
25 def dict2iter(name, dict_, pop=False):
26 """iterate into a list serialised in a dict
27
28 name is the name of the key.
29 Serialisation is done with [name] [name#1] [name#2] and so on
30 e.g.: if name is 'group', keys are group, group#1, group#2, ...
31 iteration stop at first missing increment
32 Empty values are possible
33 @param name(unicode): name of the key
34 @param mb_data (dict): dictionary with the serialised list
35 @param pop(bool): if True, remove the value from dict
36 @return iter: iterate through the deserialised list
37 """
38 if pop:
39 get=lambda d,k: d.pop(k)
40 else:
41 get=lambda d,k: d[k]
42
43 try:
44 yield get(dict_,name)
45 except KeyError:
46 return
47 else:
48 idx = 1
49 while True:
50 try:
51 yield get(dict_,u'{}#{}'.format(name, idx))
52 except KeyError:
53 return
54 else:
55 idx += 1
56
57 def iter2dict(name, iter_, dict_=None, check_conflict=True):
58 """Fill a dict with values from an iterable
59
60 name is used to serialise iter_, in the same way as in [dict2iter]
61 Build from the tags a dict using the microblog data format.
62
63 @param name(unicode): key to use for serialisation
64 e.g. "group" to have keys "group", "group#1", "group#2", ...
65 @param iter_(iterable): values to store
66 @param dict_(None, dict): dictionary to fill, or None to create one
67 @param check_conflict(bool): if True, raise an exception in case of existing key
68 @return (dict): filled dict, or newly created one
69 @raise exceptions.ConflictError: a needed key already exists
70 """
71 if dict_ is None:
72 dict_ = {}
73 for idx, value in enumerate(iter_):
74 if idx == 0:
75 key = name
76 else:
77 key = u'{}#{}'.format(name, idx)
78 if check_conflict and key in dict_:
79 raise exceptions.ConflictError
80 dict_[key] = value
81 return dict