Skip to content

Commit

Permalink
Add get_excel_lcia_file_for_version
Browse files Browse the repository at this point in the history
  • Loading branch information
cmutel committed Nov 8, 2023
1 parent 6081172 commit 451d563
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
3 changes: 2 additions & 1 deletion ecoinvent_interface/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
"ProcessMapping",
"ReleaseType",
"Settings",
"get_excel_lcia_file_for_version",
]

__version__ = "2.3"

from .storage import CachedStorage
from .settings import Settings, permanent_setting
from .release import EcoinventRelease, ReleaseType
from .release import EcoinventRelease, ReleaseType, get_excel_lcia_file_for_version
from .process_interface import EcoinventProcess, ProcessFileType
from .mapping import ProcessMapping
60 changes: 60 additions & 0 deletions ecoinvent_interface/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,63 @@ def _download_and_cache(
"""
logger.debug(message)
return filepath


def get_excel_lcia_file_for_version(release: EcoinventRelease, version: str) -> Path:
"""
The Excel LCIA file has varying names depending on the version. This
function download the LCIA file, if necessary, and returns the filepath
of the Excel file for further use.
Parameters
----------
release
An instance of `EcoinventRelease` with valid settings
version
The ecoinvent version for which the LCIA file should be found
Returns
-------
A `pathlib.Path` filepath
"""
if not isinstance(release, EcoinventRelease):
raise ValueError("`release` must be an instance of `EcoinventRelease`")
if version not in release.list_versions():
raise ValueError("Invalid version")

filelist = release.list_extra_files(version)
guess = f"ecoinvent {version}_LCIA_implementation.7z"

possibles = sorted(
[
(damerau_levenshtein(given, guess), given)
for given in release.list_extra_files(version)
if "lcia" in given.lower() and version in given
]
)
if not possibles:
raise ValueError(f"Can't find LCIA file close to {guess} among {filelist}")
elif possibles[0][0] > 10:
raise ValueError(
f"Closest LCIA filename match to {guess} is {filelist[0][1]},"
+ "but this is too different"
)
dirpath = release.get_extra(
version=version,
filename=possibles[0][1],
)

guess = f"LCIA_implementation_{version}.xlsx"
filelist = list(dirpath.iterdir())
possibles = sorted(
[
(damerau_levenshtein(filepath.name, guess), filepath)
for filepath in filelist
if filepath.suffix.lower() == ".xlsx" and version in filepath.name
]
)
if possibles and possibles[0][0] < 10:
return possibles[0][1]
else:
raise ValueError(f"Can't find LCIA Excel file like {guess} in {filelist}")
24 changes: 24 additions & 0 deletions tests/test_release_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import pytest

from ecoinvent_interface import EcoinventRelease, Settings
from ecoinvent_interface import get_excel_lcia_file_for_version as gelffv


@pytest.fixture
def release(tmp_path):
settings = Settings(output_path=str(tmp_path))
custom_headers = {"ecoinvent-api-client-library-is-test": "true"}
return EcoinventRelease(settings=settings, custom_headers=custom_headers)


def test_gelffv_basic(release):
assert gelffv(release, "3.7.1").name == "LCIA_implementation_3.7.1.xlsx"
assert gelffv(release, "3.8").name == "LCIA Implementation v3.8.xlsx"
assert gelffv(release, "3.9.1").name == "LCIA Implementation 3.9.1.xlsx"


def test_gelffv_input_errors(release):
with pytest.raises(ValueError):
gelffv(release, "3.17.1")
with pytest.raises(ValueError):
gelffv(None, "3.7.1")

0 comments on commit 451d563

Please sign in to comment.