-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add quick project install capability
- Loading branch information
Showing
5 changed files
with
128 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
from pathlib import Path | ||
from typing import Optional | ||
|
||
import bw2data as bd | ||
import requests | ||
from platformdirs import user_data_dir | ||
|
||
from .backup import restore_project_directory | ||
from .download_utils import download_with_progressbar | ||
|
||
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", | ||
} | ||
|
||
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", | ||
} | ||
|
||
cache_dir = Path( | ||
user_data_dir(appname="bw2io-project-cache", appauthor="brightway-team") | ||
) | ||
cache_dir.mkdir(exist_ok=True) | ||
|
||
|
||
def get_projects(update_config: Optional[bool] = True) -> 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 update_config: | ||
try: | ||
projects = requests.get(URL + FILENAME).json() | ||
except: | ||
pass | ||
return projects | ||
|
||
|
||
def install_project( | ||
project_key: str, | ||
project_name: Optional[str] = None, | ||
projects_config: Optional[dict] = get_projects(), | ||
url: Optional[str] = "https://files.brightway.dev/", | ||
overwrite_existing: Optional[bool] = False, | ||
): | ||
""" | ||
Install an existing Brightway project archive. | ||
By default uses ``https://files.brightway.dev/`` as the file repository, but you can run your own. | ||
Parameters | ||
---------- | ||
project_key: str | ||
A string uniquely identifying a project, e.g. ``ecoinvent-3.8-biosphere``. | ||
project_name: str, optional | ||
The name of the new project to create. If not provided will be taken from the archive file. | ||
projects_config: dict, optional | ||
A dictionary that maps ``project_key`` values to filenames at the repository | ||
url: str, optional | ||
The URL, with trailing slash ``/``, where the file can be found. | ||
overwrite_existing: bool, optional | ||
Allow overwriting an existing project | ||
Returns | ||
------- | ||
str | ||
The name of the created project. | ||
""" | ||
try: | ||
filename = projects_config[project_key] | ||
except KeyError: | ||
raise KeyError(f"Project key {project_key} not in `project_config`") | ||
|
||
fp = cache_dir / filename | ||
if not fp.exists(): | ||
download_with_progressbar( | ||
url=url + filename, filename=filename, dirpath=cache_dir | ||
) | ||
|
||
return restore_project_directory( | ||
fp=fp, project_name=project_name, overwrite_existing=overwrite_existing | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
"mrio_common_metadata", | ||
"numpy", | ||
"openpyxl", | ||
"platformdirs", | ||
"pyprind", | ||
"requests", | ||
"scipy", | ||
|