Пример #1
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())
Пример #2
0
 def default_factory_expr(self) -> Alt[Expression]:
     return (Alt(expr for struct in self.struct
                 for expr in struct.default_factory_expr)
             ^ Alt(expr for enum in self.enum
                   for expr in enum.default_factory_expr)
             ^ Alt(expr for ts in self.typestruct
                   for expr in ts.default_factory_expr))
Пример #3
0
    def container_constructor_expr(self) -> Alt[Expression]:
        container_con: Alt[Expression] = self.ts_decon.get_repeated(
        ) and Alt.lift(import_expr(self.idol_py.idol_py_file.list))
        container_con ^= self.ts_decon.get_map() and Alt.lift(
            import_expr(self.idol_py.idol_py_file.map))

        def container_expression(container_expr: Expression,
                                 scalar_expression: Expression) -> Expression:
            def inner(state: GeneratorAcc, path: Path):
                return scripter.invocation(
                    scripter.prop_access(container_expr(state, path), "of"),
                    scalar_expression(state, path),
                    scripter.invocation(
                        "dict",
                        atleast_one=scripter.literal(
                            includes_tag(self.ts_decon.context.type_tags,
                                         "atleast_one")),
                    ),
                )

            return inner

        return Alt(
            container_expression(container_con_expr, scalar_con_expr)
            for container_con_expr in container_con
            for scalar in self.inner_scalar
            for scalar_con_expr in scalar.constructor_expr)
Пример #4
0
 def map_field_composer(self) -> Alt["FieldExpressionComposer"]:
     if self.ts_decon.get_map():
         return Alt.lift(
             FieldExpressionComposer(
                 import_expr(
                     self.idol_mar.marshmallow_field_exported("Dict"))))
     return Alt.empty()
Пример #5
0
    def prim_constructor_expr(self) -> Alt[Expression]:
        def prim_con_expr(prim_expr: Expression) -> Expression:
            def inner(state: GeneratorAcc, path: Path) -> str:
                return scripter.invocation(
                    scripter.prop_access(
                        state.import_ident(
                            path, self.idol_py.idol_py_file.primitive), "of"),
                    prim_expr(state, path),
                )

            return inner

        def literal_con_expr(value: Any) -> Expression:
            def inner(state: GeneratorAcc, path: Path) -> str:
                return scripter.invocation(
                    scripter.prop_access(
                        state.import_ident(path,
                                           self.idol_py.idol_py_file.literal),
                        "of"),
                    scripter.literal(value),
                )

            return inner

        constructor_expr: Alt[Expression] = Alt(
            prim_con_expr(prim_expr) for prim_expr in self.prim_typing_expr
            if self.scalar_dec.get_primitive())

        constructor_expr ^= Alt(
            literal_con_expr(value)
            for _, value in self.scalar_dec.get_literal())

        return constructor_expr
Пример #6
0
    def declared_constructor(self) -> Alt[Exported]:
        result: Alt[Exported] = Alt(v for typestruct in self.typestruct
                                    for v in typestruct.declared_field_typing)

        result ^= Alt(v for enum in self.enum for v in enum.declared_enum)
        result ^= Alt(v for struct in self.struct
                      for v in struct.declared_dataclass)

        return result
Пример #7
0
    def declared_type_ident(self) -> Alt[Exported]:
        type_ident: Alt[Exported] = Alt(v for typestruct in self.typestruct
                                        for v in typestruct.declared_ident)

        type_ident ^= Alt(v for enum in self.enum for v in enum.declared_ident)
        type_ident ^= Alt(v for struct in self.struct
                          for v in struct.declared_ident)

        return type_ident
Пример #8
0
    def default_factory_expr(self) -> Alt[Expression]:
        if includes_tag(self.ts_decon.context.field_tags, "optional"):
            return Alt.lift(as_expression(scripter.thunk("None")))

        return (
            Alt(as_expression("list") for _ in self.ts_decon.get_repeated())
            ^ Alt(as_expression("dict") for _ in self.ts_decon.get_map())
            ^ Alt(expr for scalar in self.inner_scalar
                  for expr in scalar.default_factory_expr
                  if self.ts_decon.get_scalar()))
Пример #9
0
    def declared_field(self) -> Alt[Exported]:
        declared_field: Alt[Exported] = Alt(v for typestruct in self.typestruct
                                            for v in typestruct.declared_field)

        declared_field ^= Alt(v for enum in self.enum
                              for v in enum.declared_field)
        declared_field ^= Alt(v for struct in self.struct
                              for v in struct.declared_field)

        return declared_field
Пример #10
0
    def prim_field_expr(self) -> Alt[FieldExpressionComposer]:
        scalar_prim: Alt[PrimitiveType] = self.scalar_dec.get_primitive()

        return Alt(
            FieldExpressionComposer(
                import_expr(self.scalar_marshmallow_field_mappings[prim_type]))
            for prim_type in scalar_prim
            if prim_type in self.scalar_marshmallow_field_mappings) ^ Alt(
                FieldExpressionComposer(
                    import_expr(self.idol_mar.idol_mar_file.any))
                for prim_type in scalar_prim if prim_type == PrimitiveType.ANY)
Пример #11
0
    def declared_schema(self) -> Alt[Exported]:
        declared_schema: Alt[Exported] = Alt(
            v for typestruct in self.typestruct
            for v in typestruct.declared_schema)

        declared_schema ^= Alt(v for enum in self.enum
                               for v in enum.declared_schema)
        declared_schema ^= Alt(v for struct in self.struct
                               for v in struct.declared_schema)

        return declared_schema
Пример #12
0
    def default_factory_expr(self) -> Alt[Expression]:
        for declared_enum in self.codegen_file.declared_field_constructor:

            def inner(state: GeneratorAcc, path: Path) -> str:
                return scripter.thunk(
                    scripter.invocation(
                        "next",
                        scripter.invocation(
                            "iter", state.import_ident(path, declared_enum))))

            return Alt.lift(inner)

        return Alt.empty()
Пример #13
0
    def default_factory_expr(self) -> Alt[Expression]:
        for type_decon in self.material_type_decon:
            codegen_file = self.idol_data.codegen_file(type_decon.t.named)
            return codegen_file.default_factory_expr

        return Alt(
            as_expression(scripter.thunk(scripter.literal(val)))
            for _, val in self.scalar_dec.get_literal()) ^ Alt(
                as_expression(
                    scripter.thunk(
                        scripter.literal(
                            self.scalar_primitive_default_map[prim])))
                for prim in self.scalar_dec.get_primitive()
                if prim in self.scalar_primitive_default_map)
Пример #14
0
    def typing_expr(self) -> Alt[Expression]:
        def repeated_expr(scalar_typing_expr: Expression) -> Expression:
            def inner(state: GeneratorAcc, path: Path) -> str:
                return scripter.index_access(
                    state.import_ident(
                        path, Exported(Path("typing"), "MutableSequence")),
                    scalar_typing_expr(state, path),
                )

            return inner

        def map_expr(scalar_typing_expr: Expression) -> Expression:
            def inner(state: GeneratorAcc, path: Path) -> str:
                return scripter.index_access(
                    state.import_ident(
                        path, Exported(Path("typing"), "MutableMapping")),
                    "str",
                    scalar_typing_expr(state, path),
                )

            return inner

        def optional_expr(inner_expr: Expression) -> Expression:
            def inner(state: GeneratorAcc, path: Path) -> str:
                return scripter.index_access(
                    state.import_ident(path,
                                       Exported(Path("typing"), "Optional")),
                    inner_expr(state, path),
                )

            return inner

        container_typing: Alt[Expression] = Alt(
            repeated_expr(scalar_typing_expr) for scalar in self.inner_scalar
            for scalar_typing_expr in scalar.typing_expr
            if self.ts_decon.get_repeated())

        container_typing ^= Alt(
            map_expr(scalar_typing_expr) for scalar in self.inner_scalar
            for scalar_typing_expr in scalar.typing_expr
            if self.ts_decon.get_map())

        typing = container_typing + Alt(expr for scalar in self.inner_scalar
                                        for expr in scalar.typing_expr)

        if includes_tag(self.ts_decon.context.field_tags, "optional"):
            return Alt(optional_expr(typing_expr) for typing_expr in typing)

        return typing
Пример #15
0
 def declared_ident(self) -> Alt[Exported]:
     return Alt(
         self.export(
             self.codegen_file.default_type_name,
             scripter.commented(
                 get_tag_values(self.codegen_file.t_decon.t.tags,
                                "description"),
                 scripter.declare_and_shadow(
                     self.apply_expr(typing_expr),
                     self.apply_expr(constructor_expr)),
             ),
         ) for typing_expr in self.typing_expr
         for constructor_expr in self.container_constructor_expr) ^ Alt(
             declaration for scalar in self.declarable_scalar
             for declaration in scalar.declared_ident)
Пример #16
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"))
                 ],
             ),
         ))
Пример #17
0
 def literal_field_expr(self) -> Alt[FieldExpressionComposer]:
     return Alt(
         self.FieldExpressionComposer(
             import_expr(
                 self.idol_mar.marshmallow_field_exported("Constant")),
             [scripter.literal(val)],
         ) for _, val in self.scalar_dec.get_literal())
Пример #18
0
 def list_field_composer(self) -> Alt["FieldExpressionComposer"]:
     return Alt(
         FieldExpressionComposer(
             import_expr(self.idol_mar.marshmallow_field_exported("List")),
             [field_composer.field_instance_expr()],
         ) for scalar in self.inner_scalar
         for field_composer in scalar.field_expr_composer
         if self.ts_decon.get_repeated())
Пример #19
0
    def declared_schema(self) -> Alt[Exported]:
        type_decon = get_material_type_deconstructor(
            self.idol_mar.config.params.all_types, self.t)

        codegen_schema_ident: Alt[str] = Alt(
            self.import_ident(codegen_schema, self.default_schema_name +
                              "Codegen") for codegen_schema in
            self.idol_mar.codegen_file(self.t.named).declared_schema
            if type_decon.get_struct())

        return Alt(
            self.export(
                self.default_schema_name,
                scripter.nameable_class_dec([codegen_ident], [],
                                            doc_str=get_tag_values(
                                                self.t.tags, "description")),
            ) for codegen_ident in codegen_schema_ident
            if type_decon.get_struct())
Пример #20
0
 def struct(self) -> "Alt[IdolPyCodegenStruct]":
     return Alt(
         IdolPyCodegenStruct(
             self,
             OrderedObj.from_iterable(
                 OrderedObj(
                     {k: IdolPyCodegenTypeStruct(self.idol_py, ts_decon)})
                 for k, ts_decon in fields),
         ) for fields in self.t_decon.get_struct())
Пример #21
0
 def declared_alias_ident(self) -> Alt[Exported]:
     return Alt(
         self.export(
             self.codegen_file.default_type_name,
             scripter.commented(
                 get_tag_values(self.codegen_file.t_decon.t.tags,
                                "description"),
                 scripter.assignable(self.apply_expr(ref_import)),
             ),
         ) for ref_import in self.reference_import_expr)
Пример #22
0
    def declared_field(self) -> Alt[Exported]:
        for declared_schema in self.declared_schema:
            schema_expr: Expression = import_expr(declared_schema)

            if self.codegen_file.t.named.qualified_name in self.config.params.scaffold_types.obj:
                for scaffold_declared_schema in self.codegen_file.idol_mar.scaffold_file(
                        self.codegen_file.t.named).declared_schema:

                    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,
                        )

                    schema_expr = lazy_import

            return Alt.lift(
                self.export(
                    self.codegen_file.default_field_name,
                    scripter.assignable(
                        self.apply_expr(
                            FieldExpressionComposer(
                                import_expr(
                                    self.codegen_file.idol_mar.
                                    marshmallow_field_exported("Nested")),
                                [self.apply_expr(schema_expr)],
                            ).curried_field_declaration_expr(
                                self.codegen_file.default_field_name,
                                self.codegen_file.idol_mar))),
                ))

        return Alt.empty()
Пример #23
0
    def prim_typing_expr(self) -> Alt[Expression]:
        scalar_prim: Alt[PrimitiveType] = self.scalar_dec.get_primitive()
        scalar_prim ^= Alt(prim_type
                           for prim_type, _ in self.scalar_dec.get_literal())

        def any_expr(state: GeneratorAcc, path: Path) -> str:
            return state.import_ident(path, Exported(Path("typing"), "Any"))

        def scalar_expr(prim_type: PrimitiveType) -> Expression:
            def inner(state: GeneratorAcc, path: Path) -> str:
                return self.scalar_type_name_mappings[prim_type]

            return inner

        return Alt(
            scalar_expr(prim_type) for prim_type in scalar_prim
            if prim_type in self.scalar_type_name_mappings) ^ Alt(
                any_expr
                for prim_type in scalar_prim if prim_type == PrimitiveType.ANY)
Пример #24
0
 def declared_field(self) -> Alt[Exported]:
     return Alt(
         self.export(
             self.codegen_file.default_field_name,
             scripter.assignable(
                 self.apply_expr(
                     composer.curried_field_declaration_expr(
                         self.codegen_file.default_field_name,
                         self.idol_mar))),
         ) for composer in self.field_composer)
Пример #25
0
    def declared_field_constructor(self) -> Alt[Exported]:
        """
        Used as an indirection that may either be the declared_constructor generated by this
        codegen file, or the scaffold wrapper generated for types that are included in
        the scaffold.  Used by embedded references to this type.
        """
        if self.t.named.qualified_name in self.idol_data.config.params.scaffold_types.obj:
            return Alt(con for con in self.idol_data.scaffold_file(
                self.t.named).declared_dataclass)

        return self.declared_constructor
Пример #26
0
    def declared_schema(self) -> Alt[Exported]:
        for scalar in self.ts_decon.get_scalar():
            for ref in scalar.get_alias():
                schema: Alt[Exported] = (
                    self.codegen_file.idol_mar.scaffold_file(
                        ref).declared_schema if ref.qualified_name
                    in self.config.params.scaffold_types.obj else self.
                    codegen_file.idol_mar.codegen_file(ref).declared_schema)

                for exported_schema in schema:
                    return Alt.lift(
                        self.export(
                            self.codegen_file.default_schema_name,
                            scripter.assignable(
                                self.import_ident(exported_schema)),
                        ))

        return Alt.lift(
            self.export(self.codegen_file.default_schema_name,
                        scripter.assignable(scripter.literal(None))))
Пример #27
0
 def declared_prim_ident(self) -> Alt[Exported]:
     return Alt(
         self.export(
             self.codegen_file.default_type_name,
             scripter.commented(
                 get_tag_values(self.codegen_file.t_decon.t.tags,
                                "description"),
                 scripter.declare_and_shadow(self.apply_expr(
                     prim_expr), self.apply_expr(prim_con_expr)),
             ),
         ) for prim_expr in self.prim_typing_expr
         for prim_con_expr in self.prim_constructor_expr)
Пример #28
0
    def reference_import_expr(self) -> Alt[Expression]:
        alias_codegen_file = Alt(
            self.idol_py.codegen_file(ref)
            for ref in self.scalar_dec.get_alias())

        alias_scaffold_file = Alt(
            self.idol_py.scaffold_file(ref)
            for ref in self.scalar_dec.get_alias()
            if ref.qualified_name in self.config.params.scaffold_types.obj)

        imported_alias_ident: Alt[Expression] = Alt(
            import_expr(codegen_type, "Codegen" + codegen_type.ident)
            for codegen_file in alias_codegen_file
            for codegen_type in codegen_file.declared_type_ident
            if not alias_scaffold_file)

        imported_alias_ident ^= Alt(
            import_expr(scaffold_type, "Scaffold" + scaffold_type.ident)
            for scaffold_file in alias_scaffold_file
            for scaffold_type in scaffold_file.declared_type_ident)

        return imported_alias_ident
Пример #29
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"),
             ),
         ))
Пример #30
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)