comparison sat_frontends/jp/xml_tools.py @ 2804:710de41da2f2

jp (pubsub/node): new "import" command, to publish many nodes from an XML file
author Goffi <goffi@goffi.org>
date Fri, 15 Feb 2019 22:13:43 +0100
parents ff1b40823b07
children ab2696e34d29
comparison
equal deleted inserted replaced
2803:d4a9a60bc650 2804:710de41da2f2
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 sat.core.i18n import _ 20 from sat.core.i18n import _
21 from sat_frontends.jp.constants import Const as C 21 from sat_frontends.jp.constants import Const as C
22 22
23 def etreeParse(cmd, raw_xml): 23 def etreeParse(cmd, raw_xml, reraise=False):
24 """Import lxml and parse raw XML 24 """Import lxml and parse raw XML
25 25
26 @param cmd(CommandBase): current command instance 26 @param cmd(CommandBase): current command instance
27 @param raw_xml(file, str): an XML bytestring, string or file-like object 27 @param raw_xml(file, str): an XML bytestring, string or file-like object
28 @param reraise(bool): if True, re raise exception on parse error instead of doing a
29 parser.error (which terminate the execution)
28 @return (tuple(etree.Element, module): parsed element, etree module 30 @return (tuple(etree.Element, module): parsed element, etree module
29 """ 31 """
30 try: 32 try:
31 from lxml import etree 33 from lxml import etree
32 except ImportError: 34 except ImportError:
40 parser = etree.XMLParser(remove_blank_text=True) 42 parser = etree.XMLParser(remove_blank_text=True)
41 element = etree.fromstring(raw_xml, parser) 43 element = etree.fromstring(raw_xml, parser)
42 else: 44 else:
43 element = etree.parse(raw_xml).getroot() 45 element = etree.parse(raw_xml).getroot()
44 except Exception as e: 46 except Exception as e:
47 if reraise:
48 raise e
45 cmd.parser.error( 49 cmd.parser.error(
46 _(u"Can't parse the payload XML in input: {msg}").format(msg=e) 50 _(u"Can't parse the payload XML in input: {msg}").format(msg=e)
47 ) 51 )
48 return element, etree 52 return element, etree
49 53