Skip to content
Merged
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
21 changes: 11 additions & 10 deletions appabuild/cli/lca.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,18 @@ def build(
An AppaBuild environment is initialized (background and foreground databases), unless --no-init is specified.

"""
try:
foreground_database = None
if init:
if not appabuild_config_path:
logger.error(
"AppaBuild configuration file and LCA config file are required for initialization",
exc_info=True,
)
raise ValueError()
foreground_database = setup.initialize(appabuild_config_path)
# try:
foreground_database = None
if init:
if not appabuild_config_path:
logger.error(
"AppaBuild configuration file and LCA config file are required for initialization",
exc_info=True,
)
raise ValueError()
foreground_database = setup.initialize(appabuild_config_path)

try:
setup.build(lca_config_path, foreground_database)
except (ValueError, ValidationError, BwDatabaseError):
sys.exit(1)
Expand Down
17 changes: 14 additions & 3 deletions appabuild/config/appa_lca.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from __future__ import annotations

import os
from typing import Dict
from typing import Dict, Optional, Union

import yaml
from pydantic import BaseModel, ValidationError, field_validator
Expand All @@ -14,7 +14,17 @@
from appabuild.logger import log_validation_error, logger


class DatabaseConfig(BaseModel):
class DatabasesConfig(BaseModel):
foreground: ForegroundDatabaseConfig
ecoinvent: Optional[EcoInventDatabaseConfig] = None


class EcoInventDatabaseConfig(BaseModel):
version: str
system_model: str


class ForegroundDatabaseConfig(BaseModel):
"""
Database entry in an Appa LCA configuration.

Expand Down Expand Up @@ -45,7 +55,8 @@ class AppaLCAConfig(BaseModel):
"""

project_name: str
databases: Dict[str, DatabaseConfig]
replace_bg: Optional[bool] = False
databases: DatabasesConfig

@field_validator("databases", mode="before")
@classmethod
Expand Down
27 changes: 13 additions & 14 deletions appabuild/database/bw_databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
import re
from typing import List, Optional, Union

import brightway2 as bw
import bw2data as bd
from pydantic import BaseModel

from appabuild.database.serialized_data import ActivityIdentifier
from appabuild.exceptions import BwDatabaseError
from appabuild.exceptions import BwDatabaseError, SerializedDataError
from appabuild.logger import logger


class BwDatabase(BaseModel):
Expand All @@ -27,7 +28,7 @@ def database(self):
Brightway database object
:return:
"""
return bw.Database(self.name)
return bd.Database(self.name)

def search_activity(
self, regexes: dict, must_find_only_one: Optional[bool] = False
Expand All @@ -48,14 +49,13 @@ def search_activity(
)
matching_activities = list(set.intersection(*map(set, matching_acts)))
if must_find_only_one and len(matching_activities) < 1:
raise BwDatabaseError(
f"Cannot find any activity resolving the following regexes: {regexes}."
)
e = f"Cannot find any activity resolving the following regexes: {regexes}."
logger.exception(e)
raise BwDatabaseError(e)
if must_find_only_one and len(matching_activities) > 1:
raise BwDatabaseError(
f"Too many activity matching the following regexes {regexes}. Matches "
f"are {matching_activities}."
)
e = f"Too many activity matching the following regexes {regexes}. Matches are {matching_activities}."
logger.exception(e)
raise BwDatabaseError(e)
if must_find_only_one:
return ActivityIdentifier(
database=matching_activities[0][0], uuid=matching_activities[0][1]
Expand All @@ -79,10 +79,9 @@ def resolve_activity_identifier(
regexes = unresolved_activity_identifier.model_dump()
database = regexes.pop("database")
if database != self.name:
raise ValueError(
f"Cannot search activity on a different database ({database} != "
f"{self.name})."
)
e = f"Cannot search activity on a different database ({database} != {self.name})."
logger.exception(e)
raise SerializedDataError(e)
activity_identifier = self.search_activity(
regexes=regexes, must_find_only_one=True
)
Expand Down
Loading
Loading