-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change the file format of the blitz file and add internal (need to be…
… renamed) fields with their `_` preffix (#6) * Change the file format of the blitz file and add internal (need to be renamed) fields with their `_` preffix * Update blitz/models/base.py * Fix small typos in README.md + add .idea/ to .gitignore (#4) * Add .idea/ to .gitignore * Fix small typos in readme * Create demo option on create command. (#7) * Create demo option on create command. * Update blitz/models/blitz/field.py Co-authored-by: mde-pach <[email protected]> * Remove folder and add in gitignore. * Fix MYPY --------- Co-authored-by: mde-pach <[email protected]> * Add exception that handle error message printing in the cli (temporary fatorization) and use the only existing blitz app if there is only one and no blitz app name is specified (#5) * Add exception that handle error message printing in the cli (temporary fatorization) and use the only existing blitz app if there is only one and no blitz app name is specified * Update blitz/cli/commands/start.py --------- Co-authored-by: pbrochar <[email protected]> * reformat code --------- Co-authored-by: pbrochar <[email protected]> Co-authored-by: Ivan Prunier <[email protected]>
- Loading branch information
1 parent
d3f3104
commit c8161f9
Showing
13 changed files
with
216 additions
and
120 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
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
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 |
---|---|---|
@@ -1,27 +1,94 @@ | ||
from typing import Any | ||
from pydantic import BaseModel, Field | ||
from typing import Any, ClassVar, NoReturn | ||
from pydantic import BaseModel, Field, field_serializer | ||
from blitz.models.blitz.config import BlitzAppConfig | ||
from blitz.models.blitz.resource import BlitzResourceConfig | ||
from pathlib import Path | ||
from enum import StrEnum | ||
import json | ||
import yaml | ||
|
||
|
||
class FileType(StrEnum): | ||
JSON = "json" | ||
YAML = "yaml" | ||
def _get_data_from_json(file: Path) -> dict[str, dict[str, Any]]: | ||
with open(file, "r") as f: | ||
return dict(json.load(f)) | ||
|
||
|
||
def _get_data_from_yaml(file: Path) -> dict[str, dict[str, Any]]: | ||
with open(file, "r") as f: | ||
return dict(yaml.safe_load(f)) | ||
|
||
|
||
def _no_parser_for_suffix(file: Path) -> NoReturn: | ||
raise ValueError(f"No parser for {file}") | ||
|
||
|
||
class BlitzFile(BaseModel): | ||
""" | ||
The Blitz file is the configuration file for a Blitz app. It contains the BlitzAppConfig and a list of BlitzResourceConfig. | ||
""" | ||
|
||
path: Path | None = Field(default=None, exclude=True) | ||
file_type: FileType | None = Field(default=None, exclude=True) | ||
class FileType(StrEnum): | ||
JSON = "json" | ||
YAML = "yaml" | ||
|
||
CONFIG_FIELD_NAME: ClassVar[str] = "config" | ||
RESOURCES_FIELD_NAME: ClassVar[str] = "resources" | ||
|
||
config: BlitzAppConfig | ||
resources_configs: list[BlitzResourceConfig] = Field([], serialization_alias="resources") | ||
resources_configs: list[BlitzResourceConfig] = Field(default=[], serialization_alias=RESOURCES_FIELD_NAME) | ||
raw_file: dict[str, Any] = Field(exclude=True) | ||
path: Path | None = Field(default=None, exclude=True) | ||
file_type: FileType | None = Field(default=None, exclude=True) | ||
|
||
# def write(self) -> None: | ||
# with open(self.path, "w") as blitz_file: | ||
# blitz_file.write(self.model_dump_json) | ||
|
||
@field_serializer("resources_configs") | ||
def _serialize_resources_configs(self, resources_configs: list[BlitzResourceConfig], _info: Any) -> dict[str, Any]: | ||
serialized_resources_configs = {} | ||
for resource_config in resources_configs: | ||
serialized_resources_configs[resource_config.name] = resource_config.model_dump() | ||
|
||
return serialized_resources_configs | ||
|
||
@classmethod | ||
def from_file(cls, file_path: Path) -> "BlitzFile": | ||
blitz_file = { | ||
cls.FileType.JSON.value: _get_data_from_json, | ||
cls.FileType.YAML.value: _get_data_from_yaml, | ||
}.get(file_path.suffix[1:], _no_parser_for_suffix)(file_path) | ||
|
||
return cls.from_dict( | ||
blitz_file, | ||
path=file_path.absolute(), | ||
file_type=cls.FileType(file_path.suffix.removeprefix(".")), | ||
) | ||
|
||
@classmethod | ||
def from_dict( | ||
cls, | ||
blitz_file: dict[str, dict[str, Any]], | ||
path: Path | None = None, | ||
file_type: FileType | None = None, | ||
) -> "BlitzFile": | ||
resources_configs: list[BlitzResourceConfig] = [] | ||
resource_name: str | ||
resource_config: dict[str, Any] | ||
for resource_name, resource_config in blitz_file.get(cls.RESOURCES_FIELD_NAME, {}).items(): | ||
settings_fields = {} | ||
fields = {} | ||
for field_name, field_value in resource_config.items(): | ||
if field_name.startswith(BlitzResourceConfig.Settings.FIELD_PREFIX): | ||
settings_fields[field_name[len(BlitzResourceConfig.Settings.FIELD_PREFIX) :]] = field_value | ||
else: | ||
fields[field_name] = field_value | ||
resources_configs.append(BlitzResourceConfig(name=resource_name, fields=fields, settings=settings_fields)) | ||
|
||
return cls( | ||
config=blitz_file.get(cls.CONFIG_FIELD_NAME), | ||
resources_configs=resources_configs, | ||
raw_file=blitz_file, | ||
path=path, | ||
file_type=file_type, | ||
) |
Oops, something went wrong.