forked from enviPath/enviPy
[Feature] Minimal IUCLID export (#338)
This is an initial implementation that creates a working minimal .i6z document. It passes schema validation and can be imported into IUCLID. Caveat: IUCLID files target individual compounds. Pathway is not actually covered by the format. It can be added in either soil or water and soil OECD endpoints. **I currently only implemented the soil endpoint for all data.** This sort of works, and I can report all degradation products in a pathway (not a nice view, but we can report many transformation products and add a diagram attachment in the future). Adding additional information is an absolute pain, as we need to explicitly map each type of information to the relevant OECD field. I use the XSD scheme for validation, but unfortunately the IUCLID parser is not fully compliant and requires a specific order, etc. The workflow is: finding the AI structure from the XSD scheme -> make the scheme validation pass -> upload to IUCLID to get obscure error messages -> guess what could be wrong -> repeat 💣 New specifications get released once per year, so we will have to update accordingly. I believe that this should be a more expensive feature, as it requires significant effort to uphold. Currently implemented for root compound only in SOIL: - Soil Texture 2 - Soil Texture 1 - pH value - Half-life per soil sample / scenario (mapped to disappearance; not sure about that). - CEC - Organic Matter (only Carbon) - Moisture content - Humidity <img width="2123" alt="image.png" src="attachments/d29830e1-65ef-4136-8939-1825e0959c62"> <img width="2124" alt="image.png" src="attachments/ac9de2ac-bf68-4ba4-b40b-82f810a9de93"> <img width="2139" alt="image.png" src="attachments/5674c7e6-865e-420e-974a-6b825b331e6c"> Reviewed-on: enviPath/enviPy#338 Co-authored-by: Tobias O <tobias.olenyi@envipath.com> Co-committed-by: Tobias O <tobias.olenyi@envipath.com>
This commit is contained in:
105
epiuclid/builders/base.py
Normal file
105
epiuclid/builders/base.py
Normal file
@ -0,0 +1,105 @@
|
||||
import xml.etree.ElementTree as ET
|
||||
from datetime import datetime, timezone
|
||||
|
||||
# IUCLID 6 XML namespaces
|
||||
NS_PLATFORM_CONTAINER = "http://iuclid6.echa.europa.eu/namespaces/platform-container/v2"
|
||||
NS_PLATFORM_METADATA = "http://iuclid6.echa.europa.eu/namespaces/platform-metadata/v1"
|
||||
NS_PLATFORM_FIELDS = "http://iuclid6.echa.europa.eu/namespaces/platform-fields/v1"
|
||||
NS_XLINK = "http://www.w3.org/1999/xlink"
|
||||
|
||||
# Register namespace prefixes for clean output
|
||||
ET.register_namespace("i6c", NS_PLATFORM_CONTAINER)
|
||||
ET.register_namespace("i6m", NS_PLATFORM_METADATA)
|
||||
ET.register_namespace("i6", NS_PLATFORM_FIELDS)
|
||||
ET.register_namespace("xlink", NS_XLINK)
|
||||
|
||||
IUCLID_VERSION = "6.0.0"
|
||||
DEFINITION_VERSION = "10.0"
|
||||
CREATION_TOOL = "enviPath"
|
||||
|
||||
|
||||
def _tag(ns: str, local: str) -> str:
|
||||
return f"{{{ns}}}{local}"
|
||||
|
||||
|
||||
def _sub(parent: ET.Element, ns: str, local: str, text: str | None = None) -> ET.Element:
|
||||
"""Create a sub-element under parent. Only sets text if not None."""
|
||||
elem = ET.SubElement(parent, _tag(ns, local))
|
||||
if text is not None:
|
||||
elem.text = str(text)
|
||||
return elem
|
||||
|
||||
|
||||
def _sub_if(parent: ET.Element, ns: str, local: str, text: str | None = None) -> ET.Element | None:
|
||||
"""Create a sub-element only when text is not None."""
|
||||
if text is None:
|
||||
return None
|
||||
return _sub(parent, ns, local, text)
|
||||
|
||||
|
||||
def build_platform_metadata(
|
||||
document_key: str,
|
||||
document_type: str,
|
||||
name: str,
|
||||
document_sub_type: str | None = None,
|
||||
parent_document_key: str | None = None,
|
||||
order_in_section_no: int | None = None,
|
||||
) -> ET.Element:
|
||||
"""Build the <i6c:PlatformMetadata> element for an i6d document."""
|
||||
pm = ET.Element(_tag(NS_PLATFORM_CONTAINER, "PlatformMetadata"))
|
||||
|
||||
now = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
|
||||
|
||||
_sub(pm, NS_PLATFORM_METADATA, "iuclidVersion", IUCLID_VERSION)
|
||||
_sub(pm, NS_PLATFORM_METADATA, "documentKey", document_key)
|
||||
_sub(pm, NS_PLATFORM_METADATA, "documentType", document_type)
|
||||
_sub(pm, NS_PLATFORM_METADATA, "definitionVersion", DEFINITION_VERSION)
|
||||
_sub(pm, NS_PLATFORM_METADATA, "creationDate", now)
|
||||
_sub(pm, NS_PLATFORM_METADATA, "lastModificationDate", now)
|
||||
_sub(pm, NS_PLATFORM_METADATA, "name", name)
|
||||
if document_sub_type:
|
||||
_sub(pm, NS_PLATFORM_METADATA, "documentSubType", document_sub_type)
|
||||
if parent_document_key:
|
||||
_sub(pm, NS_PLATFORM_METADATA, "parentDocumentKey", parent_document_key)
|
||||
if order_in_section_no is not None:
|
||||
_sub(pm, NS_PLATFORM_METADATA, "orderInSectionNo", str(order_in_section_no))
|
||||
_sub(pm, NS_PLATFORM_METADATA, "i5Origin", "false")
|
||||
_sub(pm, NS_PLATFORM_METADATA, "creationTool", CREATION_TOOL)
|
||||
|
||||
return pm
|
||||
|
||||
|
||||
def build_document(
|
||||
document_key: str,
|
||||
document_type: str,
|
||||
name: str,
|
||||
content_element: ET.Element,
|
||||
document_sub_type: str | None = None,
|
||||
parent_document_key: str | None = None,
|
||||
order_in_section_no: int | None = None,
|
||||
) -> str:
|
||||
"""Build a complete i6d document XML string."""
|
||||
root = ET.Element(_tag(NS_PLATFORM_CONTAINER, "Document"))
|
||||
|
||||
pm = build_platform_metadata(
|
||||
document_key=document_key,
|
||||
document_type=document_type,
|
||||
name=name,
|
||||
document_sub_type=document_sub_type,
|
||||
parent_document_key=parent_document_key,
|
||||
order_in_section_no=order_in_section_no,
|
||||
)
|
||||
root.append(pm)
|
||||
|
||||
content_wrapper = _sub(root, NS_PLATFORM_CONTAINER, "Content")
|
||||
content_wrapper.append(content_element)
|
||||
|
||||
_sub(root, NS_PLATFORM_CONTAINER, "Attachments")
|
||||
_sub(root, NS_PLATFORM_CONTAINER, "ModificationHistory")
|
||||
|
||||
return ET.tostring(root, encoding="unicode", xml_declaration=True)
|
||||
|
||||
|
||||
def document_key(uuid) -> str:
|
||||
"""Format a UUID as an IUCLID document key (uuid/0 for raw data)."""
|
||||
return f"{uuid}/0"
|
||||
Reference in New Issue
Block a user