Skip to content

Commit

Permalink
Update remote.py
Browse files Browse the repository at this point in the history
  • Loading branch information
cmutel committed Jun 16, 2024
1 parent 95626a8 commit e006e78
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions bw2io/remote.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from urllib.parse import urljoin
from pathlib import Path
from typing import Optional,Union

Expand All @@ -11,35 +12,40 @@
PROJECTS_BW2 = {
"ecoinvent-3.8-biosphere": "ecoinvent-3.8-biosphere.bw2.tar.gz",
"ecoinvent-3.9.1-biosphere": "ecoinvent-3.9.1-biosphere.bw2.tar.gz",
"ecoinvent-3.10-biosphere": "ecoinvent-3.10-biosphere.bw2.tar.gz",
}

PROJECTS_BW25 = {
"ecoinvent-3.8-biosphere": "ecoinvent-3.8-biosphere.tar.gz",
"ecoinvent-3.9.1-biosphere": "ecoinvent-3.9.1-biosphere.tar.gz",
"USEEIO-1.1": "USEEIO-1.1.tar.gz",
"forwast": "forwast.tar.gz",
}

cache_dir = Path(bd.projects._base_data_dir) / "bw2io_cache_dir"
cache_dir.mkdir(exist_ok=True)


def get_projects(update_config: Optional[bool] = True) -> dict:
def get_projects(update_config: bool = True, base_url: Optional[str] = None, filename: Optional[str] = None) -> dict:
BW2 = bd.__version__ < (4,)
projects = PROJECTS_BW2 if BW2 else PROJECTS_BW25
URL = "https://files.brightway.dev/"
FILENAME = "projects-config.bw2.json" if BW2 else "projects-config.json"
if base_url is None:
base_url = "https://files.brightway.dev/"
if filename is None:
filename = "projects-config.bw2.json" if BW2 else "projects-config.json"
if update_config:
try:
projects = requests.get(URL + FILENAME).json()
projects.update(requests.get(urljoin(base_url, filename)).json())
except:
print(f"Can't connect to {base_url}")
pass
return projects


def install_project(
project_key: str,
project_name: Optional[str] = None,
projects_config: Optional[dict] = get_projects(),
projects_config: Optional[dict] = None,
url: Optional[str] = "https://files.brightway.dev/",
overwrite_existing: Optional[bool] = False,
__recursive: Union[bool,None] = False
Expand Down Expand Up @@ -69,6 +75,9 @@ def install_project(
str
The name of the created project.
"""
if projects_config is None:
projects_config = get_projects(base_url=url)

try:
filename = projects_config[project_key]
except KeyError:
Expand All @@ -77,7 +86,7 @@ def install_project(
fp = cache_dir / filename
if not fp.exists():
download_with_progressbar(
url=url + filename, filename=filename, dirpath=cache_dir
url=urljoin(url, filename), filename=filename, dirpath=cache_dir
)

try:
Expand Down

0 comments on commit e006e78

Please sign in to comment.