Skip to content

Commit

Permalink
Fixed an issue with creating text elements
Browse files Browse the repository at this point in the history
  • Loading branch information
sami-m-g committed Sep 12, 2023
1 parent 854d36f commit 97fc89c
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
>> <Element {http://lca.jrc.it/ILCD/Contact}contactDataSet at 0x1c85f20c780>

## 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.
Expand Down
113 changes: 113 additions & 0 deletions data/invalid/sample_contact_invalid.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet type="text/xsl" href="../../stylesheets/contact2html.xsl"?>
<contactDataSet xmlns="http://lca.jrc.it/ILCD/Contact"
xmlns:common="http://lca.jrc.it/ILCD/Common"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://lca.jrc.it/ILCD/Contact ../pyilcd/schemas/ILCD_ContactDataSet.xsd"
version="1.1">
<contactInformation>
<dataSetInformation>
<common:UUID>00000000-0000-0000-0000-000000000000</common:UUID>
<common:shortName xml:lang="en">shortName0</common:shortName>
<common:shortName xml:lang="de">shortName1</common:shortName>
<common:name xml:lang="en">name0</common:name>
<common:name xml:lang="de">name1</common:name>
<classificationInformation>
<common:classification>
<common:class level="a" classId="classId1">Organisations</common:class>
<common:class level="1" classId="classId3">Other organisations</common:class>
</common:classification>
<common:classification name="Custom" classes="../Example_Custom_Classification.xml">
<common:class level="0" classId="classId5">class2</common:class>
<common:class level="1" classId="classId7">class3</common:class>
</common:classification>
</classificationInformation>
<contactAddress xml:lang="en">contactAddress0</contactAddress>
<contactAddress xml:lang="de">contactAddress1</contactAddress>
<telephone>telephone0</telephone>
<telefax>telefax0</telefax>
<email>[email protected]</email>
<WWWAddress>http://www.acme.org/</WWWAddress>
<centralContactPoint xml:lang="en">centralContactPoint0</centralContactPoint>
<centralContactPoint xml:lang="de">centralContactPoint1</centralContactPoint>
<contactDescriptionOrComment xml:lang="en">contactDescriptionOrComment0</contactDescriptionOrComment>
<contactDescriptionOrComment xml:lang="de">contactDescriptionOrComment1</contactDescriptionOrComment>
<referenceToContact type="contact data set"
refObjectId="00000000-0000-0000-0000-000000000000"
version="00.00"
uri="">
<common:shortDescription xml:lang="en">shortDescription0</common:shortDescription>
<common:shortDescription xml:lang="de">shortDescription1</common:shortDescription>
<!--<common:other/>-->
</referenceToContact>
<referenceToContact type="contact data set"
refObjectId="00000000-0000-0000-0000-000000000000"
version="00.00"
uri="">
<common:shortDescription xml:lang="en">shortDescription2</common:shortDescription>
<common:shortDescription xml:lang="de">shortDescription3</common:shortDescription>
<!--<common:other/>-->
</referenceToContact>
<referenceToLogo type="source data set" refObjectId="00000000-0000-0000-0000-000000000000"
version="00.00"
uri="">
<common:shortDescription xml:lang="en">shortDescription4</common:shortDescription>
<common:shortDescription xml:lang="de">shortDescription5</common:shortDescription>
<!--<common:other/>-->
</referenceToLogo>
<!--<common:other/>-->
</dataSetInformation>
<!--<common:other/>-->
</contactInformation>
<administrativeInformation>
<dataEntryBy>
<common:timeStamp>2006-05-04T18:13:51.0Z</common:timeStamp>
<common:referenceToDataSetFormat type="source data set"
uri="../sources/ILCD_Format_1_1_a97a0155-0234-4b87-b4ce-a45da52f2a40.xml"
refObjectId="a97a0155-0234-4b87-b4ce-a45da52f2a40">
<common:shortDescription xml:lang="en">ILCD format 1.1</common:shortDescription>
<!--<common:other/>-->
</common:referenceToDataSetFormat>
<common:referenceToDataSetFormat type="source data set"
refObjectId="00000000-0000-0000-0000-000000000000"
version="00.00"
uri="">
<common:shortDescription xml:lang="en">shortDescription8</common:shortDescription>
<common:shortDescription xml:lang="de">shortDescription9</common:shortDescription>
<!--<common:other/>-->
</common:referenceToDataSetFormat>
<!--<common:other/>-->
</dataEntryBy>
<publicationAndOwnership>
<common:dataSetVersion>00.00</common:dataSetVersion>
<common:referenceToPrecedingDataSetVersion type="contact data set"
refObjectId="00000000-0000-0000-0000-000000000000"
version="00.00"
uri="">
<common:shortDescription xml:lang="en">shortDescription10</common:shortDescription>
<common:shortDescription xml:lang="de">shortDescription11</common:shortDescription>
<!--<common:other/>-->
</common:referenceToPrecedingDataSetVersion>
<common:referenceToPrecedingDataSetVersion type="contact data set"
refObjectId="00000000-0000-0000-0000-000000000000"
version="00.00"
uri="">
<common:shortDescription xml:lang="en">shortDescription12</common:shortDescription>
<common:shortDescription xml:lang="de">shortDescription13</common:shortDescription>
<!--<common:other/>-->
</common:referenceToPrecedingDataSetVersion>
<common:permanentDataSetURI>http://www.ilcd-network.org/data/processes/sample_contact.xml</common:permanentDataSetURI>
<common:referenceToOwnershipOfDataSet type="contact data set"
refObjectId="00000000-0000-0000-0000-000000000000"
version="00.00"
uri="">
<common:shortDescription xml:lang="en">shortDescription14</common:shortDescription>
<common:shortDescription xml:lang="de">shortDescription15</common:shortDescription>
<!--<common:other/>-->
</common:referenceToOwnershipOfDataSet>
<!--<common:other/>-->
</publicationAndOwnership>
<!--<common:other/>-->
</administrativeInformation>
<!--<common:other/>-->
</contactDataSet>
2 changes: 1 addition & 1 deletion pyilcd/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.0.0
6.1.0
3 changes: 2 additions & 1 deletion pyilcd/contact_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .helpers import (
create_attribute_contact_dataset,
create_attribute_list_contact_dataset,
create_element_text_contact_dataset,
)


Expand Down Expand Up @@ -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."""
Expand Down
4 changes: 2 additions & 2 deletions pyilcd/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 6 additions & 0 deletions pyilcd/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
1 change: 1 addition & 0 deletions tests/test_contact_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 0 additions & 8 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit 97fc89c

Please sign in to comment.