Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 291 #292

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bw2io/extractors/ecospold2.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,12 @@ def condense_multiline_comment(cls, element):
[
child.text
for child in element.iterchildren()
if child.tag == "{http://www.EcoInvent.org/EcoSpold02}text"
if child.tag == "{http://www.EcoInvent.org/EcoSpold02}text" and child.text
]
+ [
"Image: " + child.text
for child in element.iterchildren()
if child.tag == "{http://www.EcoInvent.org/EcoSpold02}imageUrl"
if child.tag == "{http://www.EcoInvent.org/EcoSpold02}imageUrl" and child.text
]
)
except:
Expand Down
60 changes: 57 additions & 3 deletions tests/ecospold2/ecospold2_extractor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from pathlib import Path
from lxml import objectify
import pytest

from bw2io.extractors.ecospold2 import Ecospold2DataExtractor
from bw2io.extractors.ecospold2 import Ecospold2DataExtractor, getattr2

FIXTURES = Path(__file__).resolve().parent.parent / "fixtures" / "ecospold2"

Expand All @@ -12,7 +14,7 @@ def test_extraction_without_synonyms():
"ei",
)
expected = {
"comment": "Things and stuff and whatnot\nIncluded activities start: Includes start stuff\nIncluded activities end: Includes end stuff\nTechnology: typical technology for ze Germans!",
"comment": "Things and stuff and whatnot\na Kikki comment\nIncluded activities start: Includes start stuff\nIncluded activities end: Includes end stuff\nTechnology: typical technology for ze Germans!",
"classifications": [
("EcoSpold01Categories", "construction materials/concrete"),
(
Expand Down Expand Up @@ -151,7 +153,7 @@ def test_extraction_with_synonyms():
"ei",
)
expected = {
"comment": "Things and stuff and whatnot\nIncluded activities end: Includes some stuff\nTechnology: typical technology for ze Germans!",
"comment": "Things and stuff and whatnot\na Kikki comment\nIncluded activities end: Includes some stuff\nTechnology: typical technology for ze Germans!",
"classifications": [
("EcoSpold01Categories", "construction materials/concrete"),
(
Expand Down Expand Up @@ -282,3 +284,55 @@ def test_extraction_with_synonyms():
}
print(data[0])
assert data[0] == expected


@pytest.fixture(
params=[
["Things and stuff and whatnot", "a Kiki comment", ""],
["Things and stuff and whatnot", "", "a Kiki comment"],
["Things and stuff and whatnot", "", "a Kiki comment", "a Bouba comment"],
[""],
]
)
def activity_multiline_gc(request):
# Define namespaces and schema location
ns = "http://www.EcoInvent.org/EcoSpold02"
xsi_ns = "http://www.w3.org/2001/XMLSchema-instance"

# Create the ecoSpold element with namespaces and schema location
eco_spold = objectify.Element(
"ecoSpold",
nsmap={
None: ns, # Default namespace
"xsi": xsi_ns,
},
)
eco_spold.set(
f"{{{xsi_ns}}}schemaLocation",
"http://www.EcoInvent.org/EcoSpold02 ../../schemas/datasets/EcoSpold02.xsd "
"http://www.EcoInvent.org/UsedUserMasterData ../../schemas/datasets/EcoSpold02UserMasterData.xsd",
)

# Create the activity element
activity = objectify.SubElement(eco_spold, "activity")

# Create the generalComment element
general_comment = objectify.SubElement(activity, "generalComment")

# Add text elements to generalComment
for i, a_text in enumerate(request.param):
text_e = objectify.SubElement(general_comment, f"{{{ns}}}text", index=f"{i}")
text_e.set("{http://www.w3.org/XML/1998/namespace}lang", "en")
text_e._setText(a_text)

return activity


def test_condense_multiline_comment(request, activity_multiline_gc):
expected = "\n".join(
[s for s in request.node.callspec.params.get("activity_multiline_gc") if s]
)
res = Ecospold2DataExtractor.condense_multiline_comment(
getattr2(activity_multiline_gc, "generalComment")
)
assert res == expected
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
<includedActivitiesStart xml:lang="en">Includes start stuff</includedActivitiesStart>
<includedActivitiesEnd xml:lang="en">Includes end stuff</includedActivitiesEnd>
<generalComment>
<text xml:lang="en" index="1">Things and stuff and whatnot</text>
<text xml:lang="en" index="0">Things and stuff and whatnot</text>
<text xml:lang="en" index="1">a Kikki comment</text>
<text xml:lang="en" index="2"></text>
</generalComment>
</activity>
<classification classificationId="a82c98c0-1e7e-495d-bad9-8b6d0d12e5c5">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
<includedActivitiesStart xml:lang="en" />
<includedActivitiesEnd xml:lang="en">Includes some stuff</includedActivitiesEnd>
<generalComment>
<text xml:lang="en" index="1">Things and stuff and whatnot</text>
<text xml:lang="en" index="0">Things and stuff and whatnot</text>
<text xml:lang="en" index="1">a Kikki comment</text>
<text xml:lang="en" index="2"></text>
</generalComment>
</activity>
<classification classificationId="a82c98c0-1e7e-495d-bad9-8b6d0d12e5c5">
Expand Down