|
| 1 | +from inspect import isclass |
| 2 | +from typing import List, Tuple |
| 3 | + |
| 4 | +from .base import GenericModelCodeGenerator, KWAGRS_TEMPLATE, METADATA_FIELD_NAME, sort_kwargs, template |
| 5 | +from ..dynamic_typing import DDict, DList, DOptional, ImportPathList, MetaData, ModelMeta, StringSerializable |
| 6 | + |
| 7 | +DEFAULT_ORDER = ( |
| 8 | + ("default", "default_factory"), |
| 9 | + "*", |
| 10 | + ("metadata",) |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +class DataclassModelCodeGenerator(GenericModelCodeGenerator): |
| 15 | + DC_DECORATOR = template("dataclass" |
| 16 | + "{% if kwargs %}" |
| 17 | + f"({KWAGRS_TEMPLATE})" |
| 18 | + "{% endif %}") |
| 19 | + DC_FIELD = template(f"field({KWAGRS_TEMPLATE})") |
| 20 | + |
| 21 | + def __init__(self, model: ModelMeta, meta=False, post_init_converters=False, dataclass_kwargs: dict = None, |
| 22 | + **kwargs): |
| 23 | + """ |
| 24 | + :param model: ModelMeta instance |
| 25 | + :param meta: Enable generation of metadata as attrib argument |
| 26 | + :param post_init_converters: Enable generation of type converters in __post_init__ methods |
| 27 | + :param dataclass_kwargs: kwargs for @dataclass() decorators |
| 28 | + :param kwargs: |
| 29 | + """ |
| 30 | + super().__init__(model, **kwargs) |
| 31 | + self.post_init_converters = post_init_converters |
| 32 | + self.no_meta = not meta |
| 33 | + self.dataclass_kwargs = dataclass_kwargs or {} |
| 34 | + |
| 35 | + def generate(self, nested_classes: List[str] = None) -> Tuple[ImportPathList, str]: |
| 36 | + """ |
| 37 | + :param nested_classes: list of strings that contains classes code |
| 38 | + :return: list of import data, class code |
| 39 | + """ |
| 40 | + imports, code = super().generate(nested_classes) |
| 41 | + imports.append(('dataclasses', ['dataclass, field'])) |
| 42 | + return imports, code |
| 43 | + |
| 44 | + @property |
| 45 | + def decorators(self) -> List[str]: |
| 46 | + """ |
| 47 | + :return: List of decorators code (without @) |
| 48 | + """ |
| 49 | + return [self.DC_DECORATOR.render(kwargs=self.dataclass_kwargs)] |
| 50 | + |
| 51 | + def field_data(self, name: str, meta: MetaData, optional: bool) -> Tuple[ImportPathList, dict]: |
| 52 | + """ |
| 53 | + Form field data for template |
| 54 | +
|
| 55 | + :param name: Field name |
| 56 | + :param meta: Field metadata |
| 57 | + :param optional: Is field optional |
| 58 | + :return: imports, field data |
| 59 | + """ |
| 60 | + imports, data = super().field_data(name, meta, optional) |
| 61 | + body_kwargs = {} |
| 62 | + if optional: |
| 63 | + meta: DOptional |
| 64 | + if isinstance(meta.type, DList): |
| 65 | + body_kwargs["default_factory"] = "list" |
| 66 | + elif isinstance(meta.type, DDict): |
| 67 | + body_kwargs["default_factory"] = "dict" |
| 68 | + else: |
| 69 | + body_kwargs["default"] = "None" |
| 70 | + if isclass(meta.type) and issubclass(meta.type, StringSerializable): |
| 71 | + pass |
| 72 | + elif isclass(meta) and issubclass(meta, StringSerializable): |
| 73 | + pass |
| 74 | + |
| 75 | + if not self.no_meta: |
| 76 | + body_kwargs["metadata"] = {METADATA_FIELD_NAME: name} |
| 77 | + if len(body_kwargs) == 1 and next(iter(body_kwargs.keys())) == "default": |
| 78 | + data["body"] = body_kwargs["default"] |
| 79 | + elif body_kwargs: |
| 80 | + data["body"] = self.DC_FIELD.render(kwargs=sort_kwargs(body_kwargs, DEFAULT_ORDER)) |
| 81 | + return imports, data |
0 commit comments