Skip to content

Commit

Permalink
refactor: generate generator set schema
Browse files Browse the repository at this point in the history
  • Loading branch information
jsolaas committed Sep 12, 2023
1 parent 71a07f5 commit e8442c0
Show file tree
Hide file tree
Showing 9 changed files with 386 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from typing import Any, Dict, List, Type

from libecalc.dto.base import ConsumerUserDefinedCategoryType
from libecalc.input.yaml_types import YamlBase
from libecalc.input.yaml_types.components.category import CategoryField
from libecalc.input.yaml_types.components.legacy.yaml_electricity_consumer import (
YamlElectricityConsumer,
)
from libecalc.input.yaml_types.placeholder_type import PlaceholderType
from libecalc.input.yaml_types.schema_helpers import (
replace_temporal_placeholder_property_with_legacy_ref,
)
from libecalc.input.yaml_types.temporal_model import TemporalModel
from pydantic import Field


class YamlGeneratorSet(YamlBase):
class Config:
title = "GENERATORSET"

@staticmethod
def schema_extra(schema: Dict[str, Any], model: Type["YamlGeneratorSet"]) -> None:
replace_temporal_placeholder_property_with_legacy_ref(
schema=schema,
property_key="ELECTRICITY2FUEL",
property_ref="$SERVER_NAME/api/v1/schema-validation/energy-usage-model-common.json#definitions/number_or_string",
)

name: str = Field(
...,
title="NAME",
description="Name of the generator set.\n\n$ECALC_DOCS_KEYWORDS_URL/NAME",
)
category: ConsumerUserDefinedCategoryType = CategoryField(...)
fuel: TemporalModel[str] = Field(
None,
title="FUEL",
description="The fuel used by the generator set." "\n\n$ECALC_DOCS_KEYWORDS_URL/FUEL",
)
electricity2fuel: TemporalModel[PlaceholderType] = Field(
...,
title="ELECTRICITY2FUEL",
description="Specifies the correlation between the electric power delivered and the fuel burned by a "
"generator set.\n\n$ECALC_DOCS_KEYWORDS_URL/ELECTRICITY2FUEL",
)
consumers: List[YamlElectricityConsumer] = Field(
...,
title="CONSUMERS",
description="Consumers getting electrical power from the generator set.\n\n$ECALC_DOCS_KEYWORDS_URL/CONSUMERS",
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from libecalc.input.yaml_types import YamlBase
from libecalc.input.yaml_types.components.category import CategoryField
from libecalc.input.yaml_types.components.compressor_system import CompressorSystem
from libecalc.input.yaml_types.components.generator_set import YamlGeneratorSet
from libecalc.input.yaml_types.components.legacy.yaml_fuel_consumer import (
YamlFuelConsumer,
)
Expand All @@ -23,11 +24,6 @@ class Config:

@staticmethod
def schema_extra(schema: Dict[str, Any], model: Type["YamlInstallation"]) -> None:
replace_placeholder_property_with_legacy_ref(
schema=schema,
property_key="GENERATORSETS",
property_ref="$SERVER_NAME/api/v1/schema-validation/generator-sets.json#properties/GENERATORSETS",
)
replace_placeholder_property_with_legacy_ref(
schema=schema,
property_key="DIRECTEMITTERS",
Expand Down Expand Up @@ -55,7 +51,7 @@ def schema_extra(schema: Dict[str, Any], model: Type["YamlInstallation"]) -> Non
title="REGULARITY",
description="Regularity of the installation can be specified by a single number or as an expression. USE WITH CARE.\n\n$ECALC_DOCS_KEYWORDS_URL/REGULARITY",
)
generatorsets: PlaceholderType = Field(
generatorsets: List[YamlGeneratorSet] = Field(
None,
title="GENERATORSETS",
description="Defines one or more generator sets.\n\n$ECALC_DOCS_KEYWORDS_URL/GENERATORSETS",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from typing import Any, Dict, Type

from libecalc.dto.base import ConsumerUserDefinedCategoryType
from libecalc.input.yaml_types import YamlBase
from libecalc.input.yaml_types.components.category import CategoryField
from libecalc.input.yaml_types.placeholder_type import PlaceholderType
from libecalc.input.yaml_types.schema_helpers import (
replace_temporal_placeholder_property_with_legacy_ref,
)
from libecalc.input.yaml_types.temporal_model import TemporalModel
from pydantic import Field


class YamlElectricityConsumer(YamlBase):
class Config:
title = "ELECTRICITY_CONSUMER"

@staticmethod
def schema_extra(schema: Dict[str, Any], model: Type["YamlElectricityConsumer"]) -> None:
replace_temporal_placeholder_property_with_legacy_ref(
schema=schema,
property_key="ENERGY_USAGE_MODEL",
property_ref="$SERVER_NAME/api/v1/schema-validation/energy-usage-model.json#properties/ENERGY_USAGE_MODEL",
)

name: str = Field(
...,
title="NAME",
description="Name of the consumer.\n\n$ECALC_DOCS_KEYWORDS_URL/NAME",
)
category: ConsumerUserDefinedCategoryType = CategoryField(...)
energy_usage_model: TemporalModel[PlaceholderType] = Field(
...,
title="ENERGY_USAGE_MODEL",
description="Definition of the energy usage model for the consumer."
"\n\n$ECALC_DOCS_KEYWORDS_URL/ENERGY_USAGE_MODEL",
)
Original file line number Diff line number Diff line change
@@ -1,37 +1,14 @@
from typing import Any, Dict, Type

from libecalc.dto.base import ConsumerUserDefinedCategoryType
from libecalc.input.yaml_types import YamlBase
from libecalc.input.yaml_types.components.category import CategoryField
from libecalc.input.yaml_types.placeholder_type import PlaceholderType
from libecalc.input.yaml_types.components.legacy.yaml_electricity_consumer import (
YamlElectricityConsumer,
)
from libecalc.input.yaml_types.temporal_model import TemporalModel
from pydantic import Field


class YamlFuelConsumer(YamlBase):
class YamlFuelConsumer(YamlElectricityConsumer):
class Config:
title = "FUEL_CONSUMER"

@staticmethod
def schema_extra(schema: Dict[str, Any], model: Type["YamlFuelConsumer"]) -> None:
for energy_usage_model in schema["properties"]["ENERGY_USAGE_MODEL"]["anyOf"]:
del energy_usage_model["type"]
energy_usage_model[
"$ref"
] = "$SERVER_NAME/api/v1/schema-validation/energy-usage-model.json#properties/ENERGY_USAGE_MODEL"

name: str = Field(
...,
title="NAME",
description="Name of the fuel consumer.\n\n$ECALC_DOCS_KEYWORDS_URL/NAME",
)
category: ConsumerUserDefinedCategoryType = CategoryField(...)
energy_usage_model: TemporalModel[PlaceholderType] = Field(
...,
title="ENERGY_USAGE_MODEL",
description="Definition of the energy usage model for the consumer."
"\n\n$ECALC_DOCS_KEYWORDS_URL/ENERGY_USAGE_MODEL",
)
fuel: TemporalModel[str] = Field(
None,
title="FUEL",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ def replace_placeholder_property_with_legacy_ref(schema: dict, property_key: str
"""
Replace a property with a $ref to an existing json-schema file. This method exists so that we can gradually replace
all json-schema files with pydantic classes that can generate the schema. Instead of converting all schemas at once,
we can use this method to inject the old schemas for some of the properties in the class we are converting.
we can use this method to inject the old schemas for some properties in the class we are converting.
This method will delete the type property, and inject the given $ref.
Expand All @@ -13,3 +13,17 @@ def replace_placeholder_property_with_legacy_ref(schema: dict, property_key: str
"""
del schema["properties"][property_key]["type"]
schema["properties"][property_key]["$ref"] = property_ref


def replace_temporal_placeholder_property_with_legacy_ref(schema: dict, property_key: str, property_ref: str) -> None:
for value in schema["properties"][property_key]["anyOf"]:
if "additionalProperties" in value:
# Replace type in additional properties (temporal)
# This will also replace for patternProperties since it is the same object used for both.
additional_properties = value["additionalProperties"]
del additional_properties["type"]
additional_properties["$ref"] = property_ref
else:
# Replace type when not temporal
del value["type"]
value["$ref"] = property_ref
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from datetime import datetime
from typing import MutableMapping, TypeVar, Union
from typing import Dict, TypeVar, Union

from pydantic import ConstrainedStr


class DatetimeString(ConstrainedStr):
regex = "^\\d{4}\\-(0?[1-9]|1[012])\\-(0?[1-9]|[12][0-9]|3[01])$"


TModel = TypeVar("TModel")
TemporalModel = Union[TModel, MutableMapping[datetime, TModel]]
TemporalModel = Union[TModel, Dict[DatetimeString, TModel]]
Loading

0 comments on commit e8442c0

Please sign in to comment.