-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
edi_xml_oca: replace xmlschema by lxml
- Loading branch information
1 parent
162d74d
commit d1d9eb3
Showing
2 changed files
with
40 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,11 @@ | |
# @author: Simone Orsi <[email protected]> | ||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). | ||
|
||
import io | ||
from contextlib import closing | ||
|
||
import xmlschema | ||
import xmltodict | ||
from lxml import etree | ||
|
||
from odoo import modules | ||
from odoo.tools import DotDict | ||
from odoo.tools import validate_xml_from_attachment | ||
|
||
from odoo.addons.component.core import Component | ||
|
||
|
@@ -28,35 +26,39 @@ def __init__(self, work_context): | |
if not hasattr(work_context, key): | ||
raise AttributeError(f"`{key}` is required for this component!") | ||
|
||
self.schema = xmlschema.XMLSchema(self._get_xsd_schema_path()) | ||
self.schema_path, self.schema = self._get_xsd_schema() | ||
|
||
def _get_xsd_schema_path(self): | ||
"""Lookup for XSD schema.""" | ||
def _get_xsd_schema(self): | ||
"""Lookup and parse the XSD schema.""" | ||
try: | ||
mod_name, path = self.work.schema_path.split(":") | ||
except ValueError as exc: | ||
raise ValueError("Path must be in the form `module:path`") from exc | ||
return modules.get_resource_path(mod_name, path) | ||
|
||
def _parse_xml(self, file_obj, **kw): | ||
schema_path = modules.get_resource_path(mod_name, path) | ||
with open(schema_path, "rb") as schema_file: | ||
return schema_path, etree.XMLSchema(etree.parse(schema_file)) | ||
|
||
def _xml_string_to_dict(self, xml_string): | ||
"""Read xml_content and return a data dict. | ||
:param file_obj: file obj of XML file | ||
:param xml_string: str of XML file | ||
""" | ||
return DotDict(self.schema.to_dict(file_obj, **kw)) | ||
return xmltodict.parse(xml_string)["xs:element"] | ||
|
||
def parse_xml(self, file_content, **kw): | ||
def parse_xml(self, file_content): | ||
"""Read XML content. | ||
:param file_content: str of XML file | ||
:param file_content: unicode str of XML file | ||
:return: dict with final data | ||
""" | ||
with closing(io.StringIO(file_content)) as fd: | ||
return self._parse_xml(fd) | ||
tree = etree.XML(file_content.encode("utf-8")) | ||
xml_string = etree.tostring(tree).decode("utf-8") | ||
return self._xml_string_to_dict(xml_string) | ||
|
||
def validate(self, xml_content, raise_on_fail=False): | ||
"""Validate XML content against XSD schema. | ||
Raises `XMLSchemaValidationError` if `raise_on_fail` is True. | ||
Raises `etree.DocumentInvalid` if `raise_on_fail` is True. | ||
:param xml_content: str containing xml data to validate | ||
:raise_on_fail: turn on/off validation error exception on fail | ||
|
@@ -65,15 +67,11 @@ def validate(self, xml_content, raise_on_fail=False): | |
* None if validation is ok | ||
* error string if `raise_on_fail` is False | ||
""" | ||
try: | ||
return self.schema.validate(xml_content) | ||
except self._validate_swallable_exceptions() as err: | ||
if raise_on_fail: | ||
raise | ||
return str(err) | ||
|
||
def _validate_swallable_exceptions(self): | ||
return ( | ||
xmlschema.exceptions.XMLSchemaValueError, | ||
xmlschema.validators.exceptions.XMLSchemaValidationError, | ||
xsd_name = self.schema_path | ||
xml_content = ( | ||
xml_content.encode("utf-8") if isinstance(xml_content, str) else xml_content | ||
) | ||
res = validate_xml_from_attachment(self.env, xml_content, xsd_name) | ||
if raise_on_fail and res: | ||
raise ValueError(res) | ||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters