示例#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
文件: idol_py.py 项目: corps/idol
    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
示例#3
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))
示例#4
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
示例#5
0
文件: idol_py.py 项目: corps/idol
    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
示例#6
0
文件: idol_mar.py 项目: corps/idol
    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
示例#7
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()))
示例#8
0
文件: idol_mar.py 项目: corps/idol
    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
示例#9
0
文件: idol_mar.py 项目: corps/idol
    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)
示例#10
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)
示例#11
0
文件: idol_py.py 项目: corps/idol
    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
示例#12
0
文件: idol_py.py 项目: corps/idol
 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)
示例#13
0
文件: idol_mar.py 项目: corps/idol
 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())
示例#14
0
文件: idol_py.py 项目: corps/idol
    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)
示例#15
0
文件: idol_mar.py 项目: corps/idol
 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())
示例#16
0
文件: idol_mar.py 项目: corps/idol
    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())
示例#17
0
文件: idol_py.py 项目: corps/idol
 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())
示例#18
0
文件: idol_mar.py 项目: corps/idol
 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)
示例#19
0
文件: idol_py.py 项目: corps/idol
    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)
示例#20
0
文件: idol_py.py 项目: corps/idol
 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)
示例#21
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
示例#22
0
文件: idol_py.py 项目: corps/idol
 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)
示例#23
0
文件: idol_py.py 项目: corps/idol
    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
示例#24
0
文件: idol_mar.py 项目: corps/idol
 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)
示例#25
0
文件: idol_mar.py 项目: corps/idol
 def enum(self) -> "Alt[IdolMarCodegenEnum]":
     return Alt(
         self.IdolMarCodegenEnum(self, options)
         for options in self.t_decon.get_enum())
示例#26
0
文件: idol_mar.py 项目: corps/idol
 def typestruct(self) -> "Alt[IdolMarCodegenTypeStructDeclaration]":
     return Alt(
         self.IdolMarCodegenTypeStructDeclaration(self, ts_decon)
         for ts_decon in self.t_decon.get_typestruct())
示例#27
0
 def default_factory_expr(self) -> Alt[Expression]:
     return Alt(
         import_expr(dc)
         for dc in self.codegen_file.declared_field_constructor)
示例#28
0
文件: idol_mar.py 项目: corps/idol
 def reference_import_field_expr_composer(
         self) -> Alt[FieldExpressionComposer]:
     return Alt(
         self.FieldExpressionComposer(import_expr(declared_field))
         for ref in self.scalar_dec.get_alias() for declared_field in
         self.idol_mar.codegen_file(ref).declared_field)
示例#29
0
文件: idol_mar.py 项目: corps/idol
 def scalar_field_composer(self) -> Alt["FieldExpressionComposer"]:
     return Alt(composer for scalar in self.inner_scalar
                for composer in scalar.field_expr_composer
                if self.ts_decon.get_scalar())
示例#30
0
文件: idol_mar.py 项目: corps/idol
 def inner_scalar(self) -> "Alt[IdolMarCodegenScalar]":
     return Alt(
         self.IdolMarCodegenScalar(self.idol_mar, scalar_decon)
         for scalar_decon in (self.ts_decon.get_scalar()
                              ^ self.ts_decon.get_map()
                              ^ self.ts_decon.get_repeated()))