comparison libervia/backend/tools/common/data_format.py @ 4071:4b842c1fb686

refactoring: renamed `sat` package to `libervia.backend`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 11:49:51 +0200
parents sat/tools/common/data_format.py@524856bd7b19
children 79c8a70e1813
comparison
equal deleted inserted replaced
4070:d10748475025 4071:4b842c1fb686
1 #!/usr/bin/env python3
2
3
4 # SAT: a jabber client
5 # Copyright (C) 2009-2021 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 #  FIXME: json may be more appropriate than manual serialising like done here
22
23 from typing import Any
24
25 from libervia.backend.core import exceptions
26 import json
27
28
29 def dict2iter(name, dict_, pop=False):
30 """iterate into a list serialised in a dict
31
32 name is the name of the key.
33 Serialisation is done with [name] [name#1] [name#2] and so on
34 e.g.: if name is 'group', keys are group, group#1, group#2, ...
35 iteration stop at first missing increment
36 Empty values are possible
37 @param name(unicode): name of the key
38 @param dict_(dict): dictionary with the serialised list
39 @param pop(bool): if True, remove the value from dict
40 @return iter: iterate through the deserialised list
41 """
42 if pop:
43 get = lambda d, k: d.pop(k)
44 else:
45 get = lambda d, k: d[k]
46
47 try:
48 yield get(dict_, name)
49 except KeyError:
50 return
51 else:
52 idx = 1
53 while True:
54 try:
55 yield get(dict_, "{}#{}".format(name, idx))
56 except KeyError:
57 return
58 else:
59 idx += 1
60
61
62 def dict2iterdict(name, dict_, extra_keys, pop=False):
63 """like dict2iter but yield dictionaries
64
65 params are like in [dict2iter], extra_keys is used for extra dict keys.
66 e.g. dict2iterdict(comments, mb_data, ('node', 'service')) will yield dicts like:
67 {u'comments': u'value1', u'node': u'value2', u'service': u'value3'}
68 """
69 #  FIXME: this format seem overcomplicated, it may be more appropriate to use json here
70 if pop:
71 get = lambda d, k: d.pop(k)
72 else:
73 get = lambda d, k: d[k]
74 for idx, main_value in enumerate(dict2iter(name, dict_, pop=pop)):
75 ret = {name: main_value}
76 for k in extra_keys:
77 ret[k] = get(
78 dict_, "{}{}_{}".format(name, ("#" + str(idx)) if idx else "", k)
79 )
80 yield ret
81
82
83 def iter2dict(name, iter_, dict_=None, check_conflict=True):
84 """Fill a dict with values from an iterable
85
86 name is used to serialise iter_, in the same way as in [dict2iter]
87 Build from the tags a dict using the microblog data format.
88
89 @param name(unicode): key to use for serialisation
90 e.g. "group" to have keys "group", "group#1", "group#2", ...
91 @param iter_(iterable): values to store
92 @param dict_(None, dict): dictionary to fill, or None to create one
93 @param check_conflict(bool): if True, raise an exception in case of existing key
94 @return (dict): filled dict, or newly created one
95 @raise exceptions.ConflictError: a needed key already exists
96 """
97 if dict_ is None:
98 dict_ = {}
99 for idx, value in enumerate(iter_):
100 if idx == 0:
101 key = name
102 else:
103 key = "{}#{}".format(name, idx)
104 if check_conflict and key in dict_:
105 raise exceptions.ConflictError
106 dict_[key] = value
107 return dict
108
109
110 def get_sub_dict(name, dict_, sep="_"):
111 """get a sub dictionary from a serialised dictionary
112
113 look for keys starting with name, and create a dict with it
114 eg.: if "key" is looked for, {'html': 1, 'key_toto': 2, 'key_titi': 3} will return:
115 {None: 1, toto: 2, titi: 3}
116 @param name(unicode): name of the key
117 @param dict_(dict): dictionary with the serialised list
118 @param sep(unicode): separator used between name and subkey
119 @return iter: iterate through the deserialised items
120 """
121 for k, v in dict_.items():
122 if k.startswith(name):
123 if k == name:
124 yield None, v
125 else:
126 if k[len(name)] != sep:
127 continue
128 else:
129 yield k[len(name) + 1 :], v
130
131 def serialise(data):
132 """Serialise data so it can be sent to bridge
133
134 @return(unicode): serialised data, can be transmitted as string to the bridge
135 """
136 return json.dumps(data, ensure_ascii=False, default=str)
137
138 def deserialise(serialised_data: str, default: Any = None, type_check: type = dict):
139 """Deserialize data from bridge
140
141 @param serialised_data(unicode): data to deserialise
142 @default (object): value to use when serialised data is empty string
143 @param type_check(type): if not None, the deserialised data must be of this type
144 @return(object): deserialised data
145 @raise ValueError: serialised_data is of wrong type
146 """
147 if serialised_data == "":
148 return default
149 ret = json.loads(serialised_data)
150 if type_check is not None and not isinstance(ret, type_check):
151 raise ValueError("Bad data type, was expecting {type_check}, got {real_type}"
152 .format(type_check=type_check, real_type=type(ret)))
153 return ret