-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: implement YamlModel specific errors
YamlError gives additional info about the error in yaml, in addition to ensuring a consistent interface across different yaml readers.
- Loading branch information
Showing
9 changed files
with
206 additions
and
83 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from dataclasses import dataclass | ||
from typing import Optional | ||
|
||
from typing_extensions import Self | ||
|
||
from libecalc.presentation.yaml.yaml_entities import YamlDict | ||
|
||
|
||
@dataclass | ||
class FileMark: | ||
line_number: int | ||
column_number: int | ||
|
||
def __str__(self) -> str: | ||
return f" line {self.line_number}, column {self.column_number}" | ||
|
||
|
||
@dataclass | ||
class FileContext: | ||
start: FileMark | ||
end: Optional[FileMark] = None | ||
name: Optional[str] = None | ||
|
||
def __str__(self) -> str: | ||
file_context_string = "" | ||
if self.name is not None: | ||
file_context_string += f" in '{self.name}'," | ||
|
||
file_context_string += str(self.start) | ||
|
||
return file_context_string | ||
|
||
@classmethod | ||
def from_yaml_dict(cls, data: YamlDict) -> Self: | ||
if not hasattr(data, "end_mark") or not hasattr(data, "start_mark"): | ||
return None | ||
|
||
# This only works with our implementation of pyyaml read | ||
# In the future, we can move this logic into PyYamlYamlModel with a better interface in YamlValidator. | ||
# Specifically with our own definition of the returned data in each property in YamlValidator. | ||
start_mark = data.start_mark | ||
end_mark = data.end_mark | ||
return FileContext( | ||
start=FileMark( | ||
line_number=start_mark.line + 1, | ||
column_number=start_mark.column, | ||
), | ||
end=FileMark( | ||
line_number=end_mark.line + 1, | ||
column_number=end_mark.column, | ||
), | ||
) |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from typing import Optional | ||
|
||
from libecalc.presentation.yaml.file_context import FileContext | ||
|
||
|
||
class YamlError(Exception): | ||
def __init__(self, problem: str, file_context: Optional[FileContext] = None): | ||
self.problem = problem | ||
self.file_context = file_context | ||
message = f"{problem}" | ||
if file_context is not None: | ||
message += str(file_context) | ||
|
||
super().__init__(message) | ||
|
||
|
||
class DuplicateKeyError(YamlError): | ||
def __init__(self, key: str, file_context: FileContext): | ||
self.key = key | ||
super().__init__(f"Duplicate key {key!r} is found", file_context) |
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
Oops, something went wrong.