From 97fc89c0f12199751c28f6c4658b8bef049cdb68 Mon Sep 17 00:00:00 2001 From: sami-m-g Date: Tue, 12 Sep 2023 15:39:36 +0300 Subject: [PATCH] Fixed an issue with creating text elements --- CHANGES.md | 5 ++ README.md | 29 ++++++ data/invalid/sample_contact_invalid.xml | 113 ++++++++++++++++++++++++ pyilcd/VERSION | 2 +- pyilcd/contact_dataset.py | 3 +- pyilcd/core.py | 4 +- pyilcd/helpers.py | 6 ++ tests/test_contact_dataset.py | 1 + tests/test_core.py | 8 -- 9 files changed, 159 insertions(+), 12 deletions(-) create mode 100644 data/invalid/sample_contact_invalid.xml diff --git a/CHANGES.md b/CHANGES.md index b4b8eda..300a17d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [6.0.0] - 2023-09-12 + +### Fixed +- An issue with creating text elements + ## [6.0.0] - 2023-04-13 ### Fixed diff --git a/README.md b/README.md index 5a6cbe6..e098273 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,35 @@ You can install _pyilcd_ via [pip] from [PyPI]: $ pip install pyilcd ``` +## Usage + +```python +from pyilcd import parse_file_contact_dataset, validate_file_contact_dataset, save_ilcd_file, Defaults + +# Override defaults if needed, else skip. Defaults are already set. +Defaults.config_defaults("config.ini") # Replace with your own config file + +# Validate the ContactDataset class against the ContactDataset schema. +print(validate_file_contact_dataset("data/invalid/sample_contact_invalid.xml")) # Replace with your own XML file +>> data/contact/sample_contact_invalid.xml:17:0:ERROR:SCHEMASV:SCHEMAV_CVC_DATATYPE_VALID_1_2_1: Element '{http://lca.jrc.it/ILCD/Common}class', attribute 'level': 'a' is not a valid value of the atomic type '{http://lca.jrc.it/ILCD/Common}LevelType'. data/contact/sample_contact_invalid.xml:17:0:ERROR:SCHEMASV:SCHEMAV_CVC_IDC: Element '{http://lca.jrc.it/ILCD/Common}class', attribute 'level': Warning: No precomputed value available, the value was either invalid or something strange happened. + +# Parse the required XML file to ContactDataset class. +contactDataset = parse_file_contact_dataset("data/contact/sample_contact.xml") # Replace with your own XML file +contactDataset +>> + +## Change whatever attributes you need changing. +dataSetInformation = contactDataset.contactInformation.dataSetInformation +dataSetInformation.UUID +>> 00000000-0000-0000-0000-000000000000 +dataSetInformation.UUID = "10000000-0000-0000-0000-000000000000" +print(dataSetInformation.UUID) +>> 10000000-0000-0000-0000-000000000000 + +## Save final ContactDataset class as an XML file, make sure root directory exists. +save_ilcd_file(contactDataset, "out/sample_contact_new.xml") # Replace with your own path +``` + ## Contributing Contributions are very welcome. diff --git a/data/invalid/sample_contact_invalid.xml b/data/invalid/sample_contact_invalid.xml new file mode 100644 index 0000000..0b4de25 --- /dev/null +++ b/data/invalid/sample_contact_invalid.xml @@ -0,0 +1,113 @@ + + + + + + 00000000-0000-0000-0000-000000000000 + shortName0 + shortName1 + name0 + name1 + + + Organisations + Other organisations + + + class2 + class3 + + + contactAddress0 + contactAddress1 + telephone0 + telefax0 + email0@acme.org + http://www.acme.org/ + centralContactPoint0 + centralContactPoint1 + contactDescriptionOrComment0 + contactDescriptionOrComment1 + + shortDescription0 + shortDescription1 + + + + shortDescription2 + shortDescription3 + + + + shortDescription4 + shortDescription5 + + + + + + + + + 2006-05-04T18:13:51.0Z + + ILCD format 1.1 + + + + shortDescription8 + shortDescription9 + + + + + + 00.00 + + shortDescription10 + shortDescription11 + + + + shortDescription12 + shortDescription13 + + + http://www.ilcd-network.org/data/processes/sample_contact.xml + + shortDescription14 + shortDescription15 + + + + + + + + diff --git a/pyilcd/VERSION b/pyilcd/VERSION index 09b254e..dfda3e0 100644 --- a/pyilcd/VERSION +++ b/pyilcd/VERSION @@ -1 +1 @@ -6.0.0 +6.1.0 diff --git a/pyilcd/contact_dataset.py b/pyilcd/contact_dataset.py index 1296f4d..796fab7 100644 --- a/pyilcd/contact_dataset.py +++ b/pyilcd/contact_dataset.py @@ -13,6 +13,7 @@ from .helpers import ( create_attribute_contact_dataset, create_attribute_list_contact_dataset, + create_element_text_contact_dataset, ) @@ -62,7 +63,7 @@ def publicationAndOwnership(self) -> "PublicationAndOwnership": class DataSetInformation(etree.ElementBase): """Data set information.""" - commonUUID = create_attribute_contact_dataset("common:UUID", str) + UUID = create_element_text_contact_dataset("common:UUID", str) """Automatically generated Universally Unique Identifier of this data set. Together with the "Data set version", the UUID uniquely identifies each data set.""" diff --git a/pyilcd/core.py b/pyilcd/core.py index f8cec1a..ab825d2 100644 --- a/pyilcd/core.py +++ b/pyilcd/core.py @@ -200,8 +200,8 @@ def _check_common_lookup(name: str) -> type: try: return COMMON_LOOK_UP[name] - except KeyError as exc: - raise KeyError(f"Element {name} can't be found in custom lookup.") from exc + except KeyError: + return None class ProcessDatasetLookup(etree.CustomElementClassLookup): diff --git a/pyilcd/helpers.py b/pyilcd/helpers.py index ac689d6..0dc5fc5 100644 --- a/pyilcd/helpers.py +++ b/pyilcd/helpers.py @@ -76,6 +76,12 @@ def create_element_text_unit_group_dataset(name: str, element_type: type) -> pro return create_element_text(name, element_type, Defaults.SCHEMA_UNIT_GROUP_DATASET) +def create_element_text_contact_dataset(name: str, element_type: type) -> property: + """Helper wrapper method for creating setters and getters for an ilcd + Contact element text""" + return create_element_text(name, element_type, Defaults.SCHEMA_CONTACT_DATASET) + + def create_attribute_list_process_dataset(name: str, attr_type: type) -> property: """Helper wrapper method for creating setters and getters for an ilcd Process Dataset element text list""" diff --git a/tests/test_contact_dataset.py b/tests/test_contact_dataset.py index dda13a9..3b3358a 100644 --- a/tests/test_contact_dataset.py +++ b/tests/test_contact_dataset.py @@ -13,6 +13,7 @@ def test_contact_information(contact_dataset: ContactDataSet) -> None: ) assert isinstance(dataSetInformation.referenceToContact[0], GlobalReference) assert isinstance(dataSetInformation.referenceToLogo, GlobalReference) + assert dataSetInformation.UUID == "00000000-0000-0000-0000-000000000000" def test_administrative_information(contact_dataset: ContactDataSet) -> None: diff --git a/tests/test_core.py b/tests/test_core.py index b7fc35d..310f860 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -5,11 +5,9 @@ from pathlib import Path from typing import Callable, List, Tuple, Union -import pytest from lxml import etree from pyilcd.core import ( - _check_common_lookup, parse_directory_contact_dataset, parse_directory_flow_dataset, parse_directory_flow_property_dataset, @@ -27,12 +25,6 @@ ) -def test_check_common_lookup() -> None: - """It validates file successfully.""" - with pytest.raises(KeyError): - _check_common_lookup("undefined") - - def test_validate_file_process_dataset_success() -> None: """It validates file successfully.""" assert validate_file_process_dataset("data/process/sample_process.xml") is None