Skip to content

Commit e988c77

Browse files
committed
Quick-fix for already extracted ecoinvent releases
1 parent c715ec9 commit e988c77

2 files changed

Lines changed: 20 additions & 10 deletions

File tree

activity_browser/actions/database/database_import_from_ecoinvent.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import re
2+
import os
23
from logging import getLogger
34
from copy import deepcopy
45

@@ -223,7 +224,7 @@ def run_safely(self, release: ei.release, version: str, model: str):
223224
fix_version=False
224225
)
225226
path = str(path)
226-
if not path.endswith(".7z"):
227+
if not path.endswith(".7z") and os.path.exists(path + ".7z"):
227228
path = path + ".7z"
228229
self.download_ready.emit(path)
229230

activity_browser/bwutils/io/ecoinvent_importer.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,21 @@ class Ecoinvent7zImporter:
4242

4343
def __init__(self, archive_path: str) -> None:
4444
self.archive_path = archive_path
45+
self.is_compressed = archive_path.endswith('.7z')
4546

4647
def install_biosphere(self, biosphere_name: str = "biosphere3") -> None:
4748
"""
4849
Installs the biosphere that is bundled in the ecoinvent .7z. Simple extraction and installing through the
4950
standard bw2io importer is quick enough because we're talking about a single file.
5051
"""
51-
# extract the elementary exchanges to a temporary location
52-
with py7zr.SevenZipFile(self.archive_path, mode='r') as archive:
53-
temp = tempfile.gettempdir()
54-
archive.extract(temp, ["MasterData/ElementaryExchanges.xml"])
55-
bio_file = os.path.join(temp, "MasterData/ElementaryExchanges.xml")
52+
if self.is_compressed:
53+
# extract the elementary exchanges to a temporary location
54+
with py7zr.SevenZipFile(self.archive_path, mode='r') as archive:
55+
temp = tempfile.gettempdir()
56+
archive.extract(temp, ["MasterData/ElementaryExchanges.xml"])
57+
bio_file = os.path.join(temp, "MasterData", "ElementaryExchanges.xml")
58+
else:
59+
bio_file = os.path.join(self.archive_path, "MasterData", "ElementaryExchanges.xml")
5660

5761
# initiate the importer with the elementary exchanges file
5862
importer = Ecospold2BiosphereImporter(biosphere_name, None, bio_file)
@@ -71,11 +75,16 @@ def install_ecoinvent(self, db_name, biosphere_name: str = "biosphere3"):
7175
log.warning(f"Database already exists, overwriting {db_name}")
7276
bd.Database(db_name).delete(warn=False)
7377

74-
# load the spold files into memory
75-
spold_bytes = self.read_archive_to_bytes()
78+
if self.is_compressed:
79+
# load the spold files into memory
80+
spold_bytes = self.read_archive_to_bytes()
81+
82+
# process the in-memory data to a dict format and applying strategies
83+
db_data = self.process_bytes(spold_bytes, db_name)
84+
else:
85+
# if the archive is not compressed, we can use the standard importer
86+
db_data = Ecospold2DataExtractor.extract(os.path.join(self.archive_path, "datasets"), db_name)
7687

77-
# process the in-memory data to a dict format and applying strategies
78-
db_data = self.process_bytes(spold_bytes, db_name)
7988
db_data = self.apply_strategies(db_data, biosphere_name)
8089

8190
# rewrite into an ingestible format

0 commit comments

Comments
 (0)