annotate sat/tools/common/data_format.py @ 4036:c4464d7ae97b

dpc (cli/info): update "disco" documentation to include new "external services": fix 418
author Goffi <goffi@goffi.org>
date Fri, 07 Apr 2023 15:18:39 +0200
parents 045af0eeda3f
children 524856bd7b19
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1 #!/usr/bin/env python3
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
1660
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
3
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SAT: a jabber client
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
1660
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
6
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
11
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
16
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
19
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """ tools common to backend and frontends """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
21 #  FIXME: json may be more appropriate than manual serialising like done here
1660
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
22
3893
045af0eeda3f core, CLI (base), tools (common/data_format): typing/core readability
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
23 from typing import Any
045af0eeda3f core, CLI (base), tools (common/data_format): typing/core readability
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
24
1660
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from sat.core import exceptions
2656
8cacf7929f3c tools (common/data_format): added serialise and deserialise methods (using json for now)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
26 import json
1660
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
27
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
28
1660
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
29 def dict2iter(name, dict_, pop=False):
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
30 """iterate into a list serialised in a dict
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
31
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
32 name is the name of the key.
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
33 Serialisation is done with [name] [name#1] [name#2] and so on
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
34 e.g.: if name is 'group', keys are group, group#1, group#2, ...
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
35 iteration stop at first missing increment
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
36 Empty values are possible
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
37 @param name(unicode): name of the key
2079
c8e561a5b2b6 core (tools/common): added getSubDict method in data_format to get sub dictionary from serialised one
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
38 @param dict_(dict): dictionary with the serialised list
1660
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
39 @param pop(bool): if True, remove the value from dict
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
40 @return iter: iterate through the deserialised list
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
41 """
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
42 if pop:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
43 get = lambda d, k: d.pop(k)
1660
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
44 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
45 get = lambda d, k: d[k]
1660
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
46
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
47 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
48 yield get(dict_, name)
1660
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
49 except KeyError:
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
50 return
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
51 else:
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
52 idx = 1
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
53 while True:
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
54 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
55 yield get(dict_, "{}#{}".format(name, idx))
1660
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
56 except KeyError:
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
57 return
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
58 else:
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
59 idx += 1
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
60
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
61
2153
f67434fd88d2 tools (common/data_format): added dict2iterdict to yield dictionary from serialised complex values
Goffi <goffi@goffi.org>
parents: 2079
diff changeset
62 def dict2iterdict(name, dict_, extra_keys, pop=False):
f67434fd88d2 tools (common/data_format): added dict2iterdict to yield dictionary from serialised complex values
Goffi <goffi@goffi.org>
parents: 2079
diff changeset
63 """like dict2iter but yield dictionaries
f67434fd88d2 tools (common/data_format): added dict2iterdict to yield dictionary from serialised complex values
Goffi <goffi@goffi.org>
parents: 2079
diff changeset
64
f67434fd88d2 tools (common/data_format): added dict2iterdict to yield dictionary from serialised complex values
Goffi <goffi@goffi.org>
parents: 2079
diff changeset
65 params are like in [dict2iter], extra_keys is used for extra dict keys.
f67434fd88d2 tools (common/data_format): added dict2iterdict to yield dictionary from serialised complex values
Goffi <goffi@goffi.org>
parents: 2079
diff changeset
66 e.g. dict2iterdict(comments, mb_data, ('node', 'service')) will yield dicts like:
f67434fd88d2 tools (common/data_format): added dict2iterdict to yield dictionary from serialised complex values
Goffi <goffi@goffi.org>
parents: 2079
diff changeset
67 {u'comments': u'value1', u'node': u'value2', u'service': u'value3'}
f67434fd88d2 tools (common/data_format): added dict2iterdict to yield dictionary from serialised complex values
Goffi <goffi@goffi.org>
parents: 2079
diff changeset
68 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
69 #  FIXME: this format seem overcomplicated, it may be more appropriate to use json here
2153
f67434fd88d2 tools (common/data_format): added dict2iterdict to yield dictionary from serialised complex values
Goffi <goffi@goffi.org>
parents: 2079
diff changeset
70 if pop:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
71 get = lambda d, k: d.pop(k)
2153
f67434fd88d2 tools (common/data_format): added dict2iterdict to yield dictionary from serialised complex values
Goffi <goffi@goffi.org>
parents: 2079
diff changeset
72 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
73 get = lambda d, k: d[k]
2153
f67434fd88d2 tools (common/data_format): added dict2iterdict to yield dictionary from serialised complex values
Goffi <goffi@goffi.org>
parents: 2079
diff changeset
74 for idx, main_value in enumerate(dict2iter(name, dict_, pop=pop)):
f67434fd88d2 tools (common/data_format): added dict2iterdict to yield dictionary from serialised complex values
Goffi <goffi@goffi.org>
parents: 2079
diff changeset
75 ret = {name: main_value}
f67434fd88d2 tools (common/data_format): added dict2iterdict to yield dictionary from serialised complex values
Goffi <goffi@goffi.org>
parents: 2079
diff changeset
76 for k in extra_keys:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
77 ret[k] = get(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
78 dict_, "{}{}_{}".format(name, ("#" + str(idx)) if idx else "", k)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
79 )
2153
f67434fd88d2 tools (common/data_format): added dict2iterdict to yield dictionary from serialised complex values
Goffi <goffi@goffi.org>
parents: 2079
diff changeset
80 yield ret
f67434fd88d2 tools (common/data_format): added dict2iterdict to yield dictionary from serialised complex values
Goffi <goffi@goffi.org>
parents: 2079
diff changeset
81
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
82
1660
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
83 def iter2dict(name, iter_, dict_=None, check_conflict=True):
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
84 """Fill a dict with values from an iterable
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
85
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
86 name is used to serialise iter_, in the same way as in [dict2iter]
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
87 Build from the tags a dict using the microblog data format.
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
88
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
89 @param name(unicode): key to use for serialisation
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
90 e.g. "group" to have keys "group", "group#1", "group#2", ...
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
91 @param iter_(iterable): values to store
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
92 @param dict_(None, dict): dictionary to fill, or None to create one
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
93 @param check_conflict(bool): if True, raise an exception in case of existing key
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
94 @return (dict): filled dict, or newly created one
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
95 @raise exceptions.ConflictError: a needed key already exists
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
96 """
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
97 if dict_ is None:
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
98 dict_ = {}
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
99 for idx, value in enumerate(iter_):
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
100 if idx == 0:
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
101 key = name
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
102 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
103 key = "{}#{}".format(name, idx)
1660
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
104 if check_conflict and key in dict_:
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
105 raise exceptions.ConflictError
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
106 dict_[key] = value
204c2a7fe68b core (tools): new tools.common module with tools used by backend and frontends. dict2iter and iter2dict method to serialise iterable in a dict (for bridge)
Goffi <goffi@goffi.org>
parents:
diff changeset
107 return dict
2079
c8e561a5b2b6 core (tools/common): added getSubDict method in data_format to get sub dictionary from serialised one
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
108
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
109
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
110 def getSubDict(name, dict_, sep="_"):
2079
c8e561a5b2b6 core (tools/common): added getSubDict method in data_format to get sub dictionary from serialised one
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
111 """get a sub dictionary from a serialised dictionary
c8e561a5b2b6 core (tools/common): added getSubDict method in data_format to get sub dictionary from serialised one
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
112
c8e561a5b2b6 core (tools/common): added getSubDict method in data_format to get sub dictionary from serialised one
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
113 look for keys starting with name, and create a dict with it
c8e561a5b2b6 core (tools/common): added getSubDict method in data_format to get sub dictionary from serialised one
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
114 eg.: if "key" is looked for, {'html': 1, 'key_toto': 2, 'key_titi': 3} will return:
c8e561a5b2b6 core (tools/common): added getSubDict method in data_format to get sub dictionary from serialised one
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
115 {None: 1, toto: 2, titi: 3}
c8e561a5b2b6 core (tools/common): added getSubDict method in data_format to get sub dictionary from serialised one
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
116 @param name(unicode): name of the key
c8e561a5b2b6 core (tools/common): added getSubDict method in data_format to get sub dictionary from serialised one
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
117 @param dict_(dict): dictionary with the serialised list
c8e561a5b2b6 core (tools/common): added getSubDict method in data_format to get sub dictionary from serialised one
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
118 @param sep(unicode): separator used between name and subkey
c8e561a5b2b6 core (tools/common): added getSubDict method in data_format to get sub dictionary from serialised one
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
119 @return iter: iterate through the deserialised items
c8e561a5b2b6 core (tools/common): added getSubDict method in data_format to get sub dictionary from serialised one
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
120 """
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
121 for k, v in dict_.items():
2079
c8e561a5b2b6 core (tools/common): added getSubDict method in data_format to get sub dictionary from serialised one
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
122 if k.startswith(name):
c8e561a5b2b6 core (tools/common): added getSubDict method in data_format to get sub dictionary from serialised one
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
123 if k == name:
c8e561a5b2b6 core (tools/common): added getSubDict method in data_format to get sub dictionary from serialised one
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
124 yield None, v
c8e561a5b2b6 core (tools/common): added getSubDict method in data_format to get sub dictionary from serialised one
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
125 else:
c8e561a5b2b6 core (tools/common): added getSubDict method in data_format to get sub dictionary from serialised one
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
126 if k[len(name)] != sep:
c8e561a5b2b6 core (tools/common): added getSubDict method in data_format to get sub dictionary from serialised one
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
127 continue
c8e561a5b2b6 core (tools/common): added getSubDict method in data_format to get sub dictionary from serialised one
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
128 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
129 yield k[len(name) + 1 :], v
2656
8cacf7929f3c tools (common/data_format): added serialise and deserialise methods (using json for now)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
130
8cacf7929f3c tools (common/data_format): added serialise and deserialise methods (using json for now)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
131 def serialise(data):
8cacf7929f3c tools (common/data_format): added serialise and deserialise methods (using json for now)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
132 """Serialise data so it can be sent to bridge
8cacf7929f3c tools (common/data_format): added serialise and deserialise methods (using json for now)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
133
8cacf7929f3c tools (common/data_format): added serialise and deserialise methods (using json for now)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
134 @return(unicode): serialised data, can be transmitted as string to the bridge
8cacf7929f3c tools (common/data_format): added serialise and deserialise methods (using json for now)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
135 """
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
136 return json.dumps(data, ensure_ascii=False, default=str)
2656
8cacf7929f3c tools (common/data_format): added serialise and deserialise methods (using json for now)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
137
3893
045af0eeda3f core, CLI (base), tools (common/data_format): typing/core readability
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
138 def deserialise(serialised_data: str, default: Any = None, type_check: type = dict):
2656
8cacf7929f3c tools (common/data_format): added serialise and deserialise methods (using json for now)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
139 """Deserialize data from bridge
8cacf7929f3c tools (common/data_format): added serialise and deserialise methods (using json for now)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
140
8cacf7929f3c tools (common/data_format): added serialise and deserialise methods (using json for now)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
141 @param serialised_data(unicode): data to deserialise
2697
fcc945537d5f tools (common/data_format): serialise now check types and return a default value when empty string is parsed.
Goffi <goffi@goffi.org>
parents: 2656
diff changeset
142 @default (object): value to use when serialised data is empty string
fcc945537d5f tools (common/data_format): serialise now check types and return a default value when empty string is parsed.
Goffi <goffi@goffi.org>
parents: 2656
diff changeset
143 @param type_check(type): if not None, the deserialised data must be of this type
2656
8cacf7929f3c tools (common/data_format): added serialise and deserialise methods (using json for now)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
144 @return(object): deserialised data
2697
fcc945537d5f tools (common/data_format): serialise now check types and return a default value when empty string is parsed.
Goffi <goffi@goffi.org>
parents: 2656
diff changeset
145 @raise ValueError: serialised_data is of wrong type
2656
8cacf7929f3c tools (common/data_format): added serialise and deserialise methods (using json for now)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
146 """
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
147 if serialised_data == "":
2697
fcc945537d5f tools (common/data_format): serialise now check types and return a default value when empty string is parsed.
Goffi <goffi@goffi.org>
parents: 2656
diff changeset
148 return default
fcc945537d5f tools (common/data_format): serialise now check types and return a default value when empty string is parsed.
Goffi <goffi@goffi.org>
parents: 2656
diff changeset
149 ret = json.loads(serialised_data)
fcc945537d5f tools (common/data_format): serialise now check types and return a default value when empty string is parsed.
Goffi <goffi@goffi.org>
parents: 2656
diff changeset
150 if type_check is not None and not isinstance(ret, type_check):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
151 raise ValueError("Bad data type, was expecting {type_check}, got {real_type}"
2697
fcc945537d5f tools (common/data_format): serialise now check types and return a default value when empty string is parsed.
Goffi <goffi@goffi.org>
parents: 2656
diff changeset
152 .format(type_check=type_check, real_type=type(ret)))
fcc945537d5f tools (common/data_format): serialise now check types and return a default value when empty string is parsed.
Goffi <goffi@goffi.org>
parents: 2656
diff changeset
153 return ret