Mercurial > libervia-backend
comparison sat/tools/common/data_format.py @ 3028:ab2696e34d29
Python 3 port:
/!\ this is a huge commit
/!\ starting from this commit, SàT is needs Python 3.6+
/!\ SàT maybe be instable or some feature may not work anymore, this will improve with time
This patch port backend, bridge and frontends to Python 3.
Roughly this has been done this way:
- 2to3 tools has been applied (with python 3.7)
- all references to python2 have been replaced with python3 (notably shebangs)
- fixed files not handled by 2to3 (notably the shell script)
- several manual fixes
- fixed issues reported by Python 3 that where not handled in Python 2
- replaced "async" with "async_" when needed (it's a reserved word from Python 3.7)
- replaced zope's "implements" with @implementer decorator
- temporary hack to handle data pickled in database, as str or bytes may be returned,
to be checked later
- fixed hash comparison for password
- removed some code which is not needed anymore with Python 3
- deactivated some code which needs to be checked (notably certificate validation)
- tested with jp, fixed reported issues until some basic commands worked
- ported Primitivus (after porting dependencies like urwid satext)
- more manual fixes
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 13 Aug 2019 19:08:41 +0200 |
parents | 003b8b4b56a7 |
children | 9d0df638c8b4 |
comparison
equal
deleted
inserted
replaced
3027:ff5bcb12ae60 | 3028:ab2696e34d29 |
---|---|
1 #!/usr/bin/env python2 | 1 #!/usr/bin/env python3 |
2 # -*- coding: utf-8 -*- | 2 # -*- coding: utf-8 -*- |
3 | 3 |
4 # SAT: a jabber client | 4 # SAT: a jabber client |
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) | 5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) |
6 | 6 |
48 return | 48 return |
49 else: | 49 else: |
50 idx = 1 | 50 idx = 1 |
51 while True: | 51 while True: |
52 try: | 52 try: |
53 yield get(dict_, u"{}#{}".format(name, idx)) | 53 yield get(dict_, "{}#{}".format(name, idx)) |
54 except KeyError: | 54 except KeyError: |
55 return | 55 return |
56 else: | 56 else: |
57 idx += 1 | 57 idx += 1 |
58 | 58 |
71 get = lambda d, k: d[k] | 71 get = lambda d, k: d[k] |
72 for idx, main_value in enumerate(dict2iter(name, dict_, pop=pop)): | 72 for idx, main_value in enumerate(dict2iter(name, dict_, pop=pop)): |
73 ret = {name: main_value} | 73 ret = {name: main_value} |
74 for k in extra_keys: | 74 for k in extra_keys: |
75 ret[k] = get( | 75 ret[k] = get( |
76 dict_, u"{}{}_{}".format(name, (u"#" + unicode(idx)) if idx else u"", k) | 76 dict_, "{}{}_{}".format(name, ("#" + str(idx)) if idx else "", k) |
77 ) | 77 ) |
78 yield ret | 78 yield ret |
79 | 79 |
80 | 80 |
81 def iter2dict(name, iter_, dict_=None, check_conflict=True): | 81 def iter2dict(name, iter_, dict_=None, check_conflict=True): |
96 dict_ = {} | 96 dict_ = {} |
97 for idx, value in enumerate(iter_): | 97 for idx, value in enumerate(iter_): |
98 if idx == 0: | 98 if idx == 0: |
99 key = name | 99 key = name |
100 else: | 100 else: |
101 key = u"{}#{}".format(name, idx) | 101 key = "{}#{}".format(name, idx) |
102 if check_conflict and key in dict_: | 102 if check_conflict and key in dict_: |
103 raise exceptions.ConflictError | 103 raise exceptions.ConflictError |
104 dict_[key] = value | 104 dict_[key] = value |
105 return dict | 105 return dict |
106 | 106 |
107 | 107 |
108 def getSubDict(name, dict_, sep=u"_"): | 108 def getSubDict(name, dict_, sep="_"): |
109 """get a sub dictionary from a serialised dictionary | 109 """get a sub dictionary from a serialised dictionary |
110 | 110 |
111 look for keys starting with name, and create a dict with it | 111 look for keys starting with name, and create a dict with it |
112 eg.: if "key" is looked for, {'html': 1, 'key_toto': 2, 'key_titi': 3} will return: | 112 eg.: if "key" is looked for, {'html': 1, 'key_toto': 2, 'key_titi': 3} will return: |
113 {None: 1, toto: 2, titi: 3} | 113 {None: 1, toto: 2, titi: 3} |
114 @param name(unicode): name of the key | 114 @param name(unicode): name of the key |
115 @param dict_(dict): dictionary with the serialised list | 115 @param dict_(dict): dictionary with the serialised list |
116 @param sep(unicode): separator used between name and subkey | 116 @param sep(unicode): separator used between name and subkey |
117 @return iter: iterate through the deserialised items | 117 @return iter: iterate through the deserialised items |
118 """ | 118 """ |
119 for k, v in dict_.iteritems(): | 119 for k, v in dict_.items(): |
120 if k.startswith(name): | 120 if k.startswith(name): |
121 if k == name: | 121 if k == name: |
122 yield None, v | 122 yield None, v |
123 else: | 123 else: |
124 if k[len(name)] != sep: | 124 if k[len(name)] != sep: |
129 def serialise(data): | 129 def serialise(data): |
130 """Serialise data so it can be sent to bridge | 130 """Serialise data so it can be sent to bridge |
131 | 131 |
132 @return(unicode): serialised data, can be transmitted as string to the bridge | 132 @return(unicode): serialised data, can be transmitted as string to the bridge |
133 """ | 133 """ |
134 return json.dumps(data, ensure_ascii=False, default=unicode) | 134 return json.dumps(data, ensure_ascii=False, default=str) |
135 | 135 |
136 def deserialise(serialised_data, default=None, type_check=dict): | 136 def deserialise(serialised_data, default=None, type_check=dict): |
137 """Deserialize data from bridge | 137 """Deserialize data from bridge |
138 | 138 |
139 @param serialised_data(unicode): data to deserialise | 139 @param serialised_data(unicode): data to deserialise |
140 @default (object): value to use when serialised data is empty string | 140 @default (object): value to use when serialised data is empty string |
141 @param type_check(type): if not None, the deserialised data must be of this type | 141 @param type_check(type): if not None, the deserialised data must be of this type |
142 @return(object): deserialised data | 142 @return(object): deserialised data |
143 @raise ValueError: serialised_data is of wrong type | 143 @raise ValueError: serialised_data is of wrong type |
144 """ | 144 """ |
145 if serialised_data == u"": | 145 if serialised_data == "": |
146 return default | 146 return default |
147 ret = json.loads(serialised_data) | 147 ret = json.loads(serialised_data) |
148 if type_check is not None and not isinstance(ret, type_check): | 148 if type_check is not None and not isinstance(ret, type_check): |
149 raise ValueError(u"Bad data type, was expecting {type_check}, got {real_type}" | 149 raise ValueError("Bad data type, was expecting {type_check}, got {real_type}" |
150 .format(type_check=type_check, real_type=type(ret))) | 150 .format(type_check=type_check, real_type=type(ret))) |
151 return ret | 151 return ret |