Exemple #1
0
 def declared_dataclass(self) -> Alt[Exported]:
     return Alt.lift(
         self.export(
             self.default_dataclass_name,
             scripter.nameable_class_dec(
                 [],
                 [
                     line for field_name, field in self.fields
                     for typing_expr in field.typing_expr
                     for default_factory_expr in field.default_factory_expr
                     for line in (scripter.comments(
                         get_tag_values(field.ts_decon.context.field_tags,
                                        "description")
                     ) + [
                         scripter.assignment(
                             get_safe_ident(field_name),
                             self.apply_expr(
                                 self.dataclass_field_expr(
                                     default_factory=default_factory_expr)),
                             typing=self.apply_expr(typing_expr),
                         )
                     ])
                 ],
                 doc_str=get_tag_values(self.codegen_file.t.tags,
                                        "description"),
                 decorators=[
                     self.import_ident(
                         Exported(Path("dataclasses"), "dataclass"))
                 ],
             ),
         ))
Exemple #2
0
    def declared_dataclass(self) -> Alt[Exported]:
        type_decon = get_material_type_deconstructor(
            self.idol_data.config.params.all_types, self.t)

        codegen_ident: Alt[str] = Alt(
            self.import_ident(codegen_schema, self.default_dataclass_name +
                              "Codegen") for codegen_schema in
            self.idol_data.codegen_file(self.t.named).declared_constructor
            if type_decon.get_struct() or type_decon.get_enum())

        return Alt(
            self.export(
                self.default_dataclass_name,
                scripter.nameable_class_dec(
                    [ident],
                    [],
                ),
            ) for ident in codegen_ident if type_decon.get_struct()) + Alt(
                self.export(
                    self.default_dataclass_name,
                    scripter.assignable(
                        ident,
                        scripter.index_access(
                            self.import_ident(Exported(Path("typing"),
                                                       "Type")), ident)),
                ) for ident in codegen_ident if type_decon.get_enum())
Exemple #3
0
 def inner(state: GeneratorAcc, path: Path) -> str:
     return scripter.index_access(
         state.import_ident(path, Exported(Path("typing"),
                                           "Mapping")),
         "str",
         scalar_typing_expr(state, path),
     )
Exemple #4
0
        def expr(state: GeneratorAcc, path: Path) -> str:
            kwds = {}
            if default:
                kwds["default"] = default(state, path)
            if default_factory:
                kwds["default_factory"] = default_factory(state, path)

            return scripter.invocation(
                state.import_ident(path, Exported(Path("dataclasses"),
                                                  "field")), **kwds)
Exemple #5
0
 def declared_enum(self) -> Alt[Exported]:
     return Alt.lift(
         self.export(
             self.default_enum_name,
             scripter.nameable_class_dec(
                 [self.import_ident(Exported(Path("enum"), "Enum"))],
                 [
                     scripter.assignment(name.upper(),
                                         scripter.literal(name))
                     for name in self.options
                 ],
                 doc_str=get_tag_values(self.codegen_file.t.tags,
                                        "description"),
             ),
         ))
Exemple #6
0
 def declared_field(self) -> Alt[Exported]:
     return Alt(
         self.export(
             self.codegen_file.default_field_name,
             scripter.assignable(
                 self.apply_expr(
                     FieldExpressionComposer(
                         import_expr(
                             Exported(Path("marshmallow_enum"),
                                      "EnumField")),
                         [self.import_ident(declared_enum)],
                         dict(by_value=scripter.literal(True)),
                     ).curried_field_declaration_expr(
                         self.codegen_file.default_field_name,
                         self.codegen_file.idol_mar))),
         ) for declared_enum in self.declared_enum)
Exemple #7
0
                    def lazy_import(state: GeneratorAcc, path: Path) -> str:
                        import_module = state.import_ident(
                            path, Exported(Path("importlib"), "import_module"))

                        return scripter.prop_access(
                            scripter.invocation(
                                import_module,
                                scripter.literal(
                                    ImportPath.as_python_module_path(
                                        path.import_path_to(
                                            scaffold_declared_schema.path).
                                        rel_path)),
                                "__package__",
                            ),
                            scaffold_declared_schema.ident,
                        )
Exemple #8
0
 def declared_schema(self) -> Alt[Exported]:
     return Alt.lift(
         self.export(
             self.codegen_file.default_schema_name,
             scripter.nameable_class_dec(
                 [
                     self.import_ident(
                         Exported(Path("marshmallow"), "Schema"))
                 ],
                 [
                     line for field_name, field in self.fields
                     for composer in field.field_composer
                     for line in (scripter.comments(
                         get_tag_values(field.ts_decon.context.field_tags,
                                        "description")
                     ) + [
                         scripter.assignment(
                             get_safe_ident(field_name),
                             self.apply_expr(
                                 composer.with_more_kwds(
                                     dict(
                                         dump_to=scripter.literal(
                                             field_name),
                                         load_from=scripter.literal(
                                             field_name),
                                         allow_none=scripter.literal(
                                             includes_tag(
                                                 field.ts_decon.context.
                                                 field_tags,
                                                 "optional",
                                             )),
                                     )).field_instance_expr()),
                         )
                     ])
                 ],
                 doc_str=get_tag_values(self.codegen_file.t.tags,
                                        "description"),
             ),
         ))
Exemple #9
0
 def marshmallow_field_exported(cls, ident: str) -> Exported:
     return Exported(Path("marshmallow.fields"), ident)
Exemple #10
0
 def inner(state: GeneratorAcc, path: Path) -> str:
     return scripter.index_access(
         state.import_ident(path,
                            Exported(Path("typing"), "Optional")),
         inner_expr(state, path),
     )
Exemple #11
0
 def any_expr(state: GeneratorAcc, path: Path) -> str:
     return state.import_ident(path, Exported(Path("typing"), "Any"))
Exemple #12
0
from typing import Callable, List, Dict, Any

from idol import scripter
from idol.cli import start, CliConfig
from idol.functional import OrderedObj, Alt
from idol.generator import (
    GeneratorParams, GeneratorConfig, build, GeneratorAcc, TypeDeconstructor,
    Path, get_material_type_deconstructor, TypeStructDeconstructor,
    ScalarDeconstructor, GeneratorContext, GeneratorFileContext, Exported,
    Expression, import_expr, get_safe_ident, get_tag_values, includes_tag,
    ExternFileContext, AbstractGeneratorFileContext, check)
from idol.py.schema.primitive_type import PrimitiveType
from idol.py.schema.reference import Reference
from idol.py.schema.type import Type

ExportedAny: Exported = Exported(Path("typing"), "Any")


class IdolPy(GeneratorContext):
    scaffolds: Dict[str, "IdolPyScaffoldFile"]
    codegens: Dict[str, "IdolPyCodegenFile"]
    scaffold_impl: "Callable[[IdolPy, Type, Path], IdolPyScaffoldFile]"
    codegen_impl: "Callable[[IdolPy, Type, Path], IdolPyCodegenFile]"

    def __init__(
        self,
        config: GeneratorConfig,
        scaffold_impl:
        "Callable[[IdolPy, Type, Path], IdolPyScaffoldFile]" = None,
        codegen_impl:
        "Callable[[IdolPy, Type, Path], IdolPyCodegenFile]" = None,