Skip to content

Commit

Permalink
fix: ensure unique names in system v2 (#238)
Browse files Browse the repository at this point in the history
Also fix error message when names are not unique
  • Loading branch information
jsolaas authored Oct 16, 2023
1 parent 851e831 commit 3634a9e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
19 changes: 16 additions & 3 deletions src/libecalc/dto/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,20 @@ def validate_unique_names(cls, values):
names.append(fuel_consumer.name)
if isinstance(fuel_consumer, GeneratorSet):
for electricity_consumer in fuel_consumer.consumers:
names.append(electricity_consumer.name)
if isinstance(electricity_consumer, (CompressorSystem, PumpSystem)):
if isinstance(electricity_consumer, CompressorSystem):
consumers = electricity_consumer.compressors
else:
consumers = electricity_consumer.pumps
for consumer in consumers:
names.append(consumer.name)
elif isinstance(fuel_consumer, (CompressorSystem, PumpSystem)):
if isinstance(fuel_consumer, CompressorSystem):
consumers = fuel_consumer.compressors
else:
consumers = fuel_consumer.pumps
for consumer in consumers:
names.append(consumer.name)
if fuel_consumer.fuel is not None:
for fuel_type in fuel_consumer.fuel.values():
# Need to verify that it is a different fuel
Expand All @@ -520,8 +533,8 @@ def validate_unique_names(cls, values):

if len(duplicated_names) > 0:
raise ValueError(
"Component names must be unique. Components include asset/ecalc-model, installations,"
" generator sets, electricity consumers, fuel consumers and direct emitters."
"Component names must be unique. Components include the main model, installations,"
" generator sets, electricity consumers, fuel consumers, systems and its consumers and direct emitters."
f" Duplicated names are: {', '.join(duplicated_names)}"
)

Expand Down
2 changes: 1 addition & 1 deletion src/libecalc/input/validation_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def __init__(
error_location_info = " -> ".join(
[str(s).capitalize().replace("__root__", "General error").replace("_", " ") for s in error_loc]
)
if component_name := data.get(EcalcYamlKeywords.name):
if data is not None and (component_name := data.get(EcalcYamlKeywords.name)):
messages.append(f"{component_name} - {error_location_info}:\n\t{error_message}\n")
else:
messages.append(f"{error_location_info}:\n\t{error_message}\n")
Expand Down

0 comments on commit 3634a9e

Please sign in to comment.