classgen: use own Exception type
This commit is contained in:
@@ -1,12 +1,17 @@
|
|||||||
# ruff: noqa: RUF001
|
# ruff: noqa: RUF001
|
||||||
import argparse
|
import argparse
|
||||||
import json
|
import json
|
||||||
|
import sys
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
|
class Error(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
# Function to map JSON schemas and types to Python types
|
# Function to map JSON schemas and types to Python types
|
||||||
def map_json_type(
|
def map_json_type(
|
||||||
json_type: Any, nested_types: set[str] | None = None, parent: Any = None
|
json_type: Any, nested_types: set[str] | None = None, parent: Any = None
|
||||||
@@ -35,7 +40,7 @@ def map_json_type(
|
|||||||
if json_type == "null":
|
if json_type == "null":
|
||||||
return {"None"}
|
return {"None"}
|
||||||
msg = f"Python type not found for {json_type}"
|
msg = f"Python type not found for {json_type}"
|
||||||
raise ValueError(msg)
|
raise Error(msg)
|
||||||
|
|
||||||
|
|
||||||
known_classes = set()
|
known_classes = set()
|
||||||
@@ -98,7 +103,7 @@ def field_def_from_default_type(
|
|||||||
Or report this problem to the clan team. So the class generator can be improved.
|
Or report this problem to the clan team. So the class generator can be improved.
|
||||||
#################################################
|
#################################################
|
||||||
"""
|
"""
|
||||||
raise ValueError(msg)
|
raise Error(msg)
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@@ -148,7 +153,7 @@ def field_def_from_default_value(
|
|||||||
)
|
)
|
||||||
# Other default values unhandled yet.
|
# Other default values unhandled yet.
|
||||||
msg = f"Unhandled default value for field '{field_name}' - default value: {default_value}"
|
msg = f"Unhandled default value for field '{field_name}' - default value: {default_value}"
|
||||||
raise ValueError(msg)
|
raise Error(msg)
|
||||||
|
|
||||||
|
|
||||||
def get_field_def(
|
def get_field_def(
|
||||||
@@ -200,7 +205,7 @@ def generate_dataclass(schema: dict[str, Any], class_name: str = root_class) ->
|
|||||||
|
|
||||||
if (prop_type is None) and (not union_variants):
|
if (prop_type is None) and (not union_variants):
|
||||||
msg = f"Type not found for property {prop} {prop_info}"
|
msg = f"Type not found for property {prop} {prop_info}"
|
||||||
raise ValueError(msg)
|
raise Error(msg)
|
||||||
|
|
||||||
if union_variants:
|
if union_variants:
|
||||||
field_types = map_json_type(union_variants)
|
field_types = map_json_type(union_variants)
|
||||||
@@ -337,7 +342,11 @@ def main() -> None:
|
|||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
args.func(args)
|
try:
|
||||||
|
args.func(args)
|
||||||
|
except Error as e:
|
||||||
|
print(e)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user