Mercurial > libervia-backend
comparison libervia/cli/xml_tools.py @ 4270:0d7bb4df2343
Reformatted code base using black.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 19 Jun 2024 18:44:57 +0200 |
parents | 47401850dec6 |
children |
comparison
equal
deleted
inserted
replaced
4269:64a85ce8be70 | 4270:0d7bb4df2343 |
---|---|
17 # You should have received a copy of the GNU Affero General Public License | 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/>. | 18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | 19 |
20 from libervia.backend.core.i18n import _ | 20 from libervia.backend.core.i18n import _ |
21 from libervia.cli.constants import Const as C | 21 from libervia.cli.constants import Const as C |
22 | |
22 | 23 |
23 def etree_parse(cmd, raw_xml, reraise=False): | 24 def etree_parse(cmd, raw_xml, reraise=False): |
24 """import lxml and parse raw XML | 25 """import lxml and parse raw XML |
25 | 26 |
26 @param cmd(CommandBase): current command instance | 27 @param cmd(CommandBase): current command instance |
44 else: | 45 else: |
45 element = etree.parse(raw_xml).getroot() | 46 element = etree.parse(raw_xml).getroot() |
46 except Exception as e: | 47 except Exception as e: |
47 if reraise: | 48 if reraise: |
48 raise e | 49 raise e |
49 cmd.parser.error( | 50 cmd.parser.error(_("Can't parse the payload XML in input: {msg}").format(msg=e)) |
50 _("Can't parse the payload XML in input: {msg}").format(msg=e) | |
51 ) | |
52 return element, etree | 51 return element, etree |
52 | |
53 | 53 |
54 def get_payload(cmd, element): | 54 def get_payload(cmd, element): |
55 """Retrieve payload element and exit with and error if not found | 55 """Retrieve payload element and exit with and error if not found |
56 | 56 |
57 @param element(etree.Element): root element | 57 @param element(etree.Element): root element |
58 @return element(etree.Element): payload element | 58 @return element(etree.Element): payload element |
59 """ | 59 """ |
60 if element.tag in ("item", "{http://jabber.org/protocol/pubsub}item"): | 60 if element.tag in ("item", "{http://jabber.org/protocol/pubsub}item"): |
61 if len(element) > 1: | 61 if len(element) > 1: |
62 cmd.disp(_("<item> can only have one child element (the payload)"), | 62 cmd.disp( |
63 error=True) | 63 _("<item> can only have one child element (the payload)"), error=True |
64 ) | |
64 cmd.host.quit(C.EXIT_DATA_ERROR) | 65 cmd.host.quit(C.EXIT_DATA_ERROR) |
65 element = element[0] | 66 element = element[0] |
66 return element | 67 return element |