Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
8389051
feat Initial commit rtc files, generated from XsDate
RuudHurkmans Sep 23, 2022
11cf986
autoformat: isort & black
RuudHurkmans Sep 23, 2022
fcc69ce
#226: Update generated models with missing root model.
priscavdsluis Sep 23, 2022
2f010dd
#226: Remove factory for now.
priscavdsluis Sep 23, 2022
3c00fef
#226: Update rtcDataConfig/models.py and add corresponding JSON.
priscavdsluis Sep 26, 2022
7c70834
#226: Add initial serializer for a rtcDataConfig file. Serializer sho…
priscavdsluis Sep 26, 2022
18ded4c
#226: Cast values to strings in Serializer
priscavdsluis Sep 26, 2022
84c269d
#226: Add initial parser for a rtcDataConfig file. Parser should stil…
priscavdsluis Sep 26, 2022
a760d11
#226: Create an RtcBaseModel to try to move validation outside of gen…
priscavdsluis Sep 27, 2022
00852e0
#226: Refactor RtcBaseModel a little bit.
priscavdsluis Sep 27, 2022
7205a37
#226: Add rtcToolsConfig.json
priscavdsluis Sep 27, 2022
9e71187
#226: Move files around
priscavdsluis Sep 27, 2022
a3fe4bd
#226: Update generated models
priscavdsluis Sep 27, 2022
b1c309a
#226: Add `allow_population_by_field_name` as True to RtcBaseModel
priscavdsluis Sep 27, 2022
d59316a
#226: Remove `allow_population_by_field_name` from generated models
priscavdsluis Sep 27, 2022
48baac8
#226: Add auto-generated file for rtcRuntimeConfig
priscavdsluis Sep 27, 2022
7596eb8
#226: Update generated data
priscavdsluis Sep 27, 2022
07354c6
#226: Add temporary bat script.
priscavdsluis Sep 27, 2022
96696de
#226: Add temporary bat script.
priscavdsluis Sep 27, 2022
474622a
#226: Update namspaces
priscavdsluis Sep 27, 2022
82cdce1
#226: Update script
priscavdsluis Sep 27, 2022
0bc621e
#226: Update generated models.
priscavdsluis Sep 27, 2022
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
79 changes: 79 additions & 0 deletions hydrolib/core/io/rtc/basemodel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from pydantic.class_validators import root_validator
from hydrolib.core.basemodel import BaseModel
from typing import get_origin
from hydrolib.core.utils import to_list
from pydantic.fields import ModelField

class RtcBaseModel(BaseModel):
class Config:
allow_population_by_field_name = True

@root_validator(pre=True)
def validate(cls, data) -> dict:
if not isinstance(data, dict):
return data

data = cls._get_root_data(data)
data = cls._get_list_data(data)

return data

@classmethod
def _get_root_data(cls, data: dict) -> dict:
"""Puts the provided dictionary `data` in a new dictionary with the key `__root__`.
This is only performed when this `cls` has a `__root__` attributes but is not delivered in the provided `data`.
Otherwise, the original data is returned.

Args:
data (dict): The data to validate that is used to initialize the class.

Returns:
dict: A dictionary with a `__root__` key if needed, otherwise the original data.
"""

root_field_name = "__root__"
root_field = cls.__fields__.get(root_field_name)

if root_field and root_field_name not in data:
return {root_field_name: data}
return data

@classmethod
def _get_list_data(cls, data: dict) -> dict:
for key, value in data.items():

field = cls.__fields__.get(key)
if not field:
continue

field_type = cls._get_field_type(field)

if field_type is list:
data[key] = to_list(value)

return data

@classmethod
def _get_field_type(cls, field: ModelField):
"""Gets the non-generic field type of a model field.
For example, if the model field type is `List[str]`,
the returned type will be `list`.

Args:
field (ModelField): The model field.

Returns:
The non-generic type of the model field for example `list`.
"""
# `outer_type_` returns e.g. List[str]
field_type = field.outer_type_

# `origin` returns e.g. list
origin = get_origin(field_type)
if origin:
return origin

if hasattr(field_type, "__origin__"):
return field_type.__origin__

return None
16 changes: 16 additions & 0 deletions hydrolib/core/io/rtc/generate_models.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CALL :CONVERT pi_state
CALL :CONVERT pi_timeseries
CALL :CONVERT rtcDataConfig
CALL :CONVERT rtcObjectiveConfig
CALL :CONVERT rtcRuntimeConfig
CALL :CONVERT rtcToolsConfig

pause
EXIT /B

:CONVERT
SET file=%~1
SET folder=%file%\generated
datamodel-codegen --input %folder%\%file%.json --input-file-type jsonschema --output %folder% --field-constraints --use-subclass-enum --use-schema-description --base-class hydrolib.core.io.rtc.basemodel.RtcBaseModel

EXIT /B
18 changes: 18 additions & 0 deletions hydrolib/core/io/rtc/pi_state/generated/_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# generated by datamodel-codegen:
# filename: pi_state.json
# timestamp: 2022-09-27T18:48:30+00:00

from __future__ import annotations

from pydantic import Field

from hydrolib.core.io.rtc.basemodel import RtcBaseModel

from . import StateComplexType


class State(RtcBaseModel):
__root__: StateComplexType = Field(
...,
description='A state consists of a number of locations.\n from which FFS reads and writes the model state data.\n A such reading is reading by the FFS and writing is writing by the FFS. ',
)
Loading