acapy_plugin_toolbox/util.py
def generate_model_schema(
        name: str,
        handler: str,
        msg_type: str,
        schema: dict
        ):
    """
    Generate a Message model class and schema class programmatically.

    The following would result in a class named XYZ inheriting from AgentMessage
    and XYZSchema inheriting from AgentMessageSchema.

    XYZ, XYZSchema = generate_model_schema(
        'XYZ',
        'aries_cloudagent.admin.handlers.XYZHandler',
        '{}/xyz'.format(PROTOCOL),
        {}
    )

    The attributes of XYZ are determined by schema_dict's keys. The actual
    schema of XYZSchema is defined by the field-value combinations of
    schema_dict, similar to marshmallow's Schema.from_dict() (can't actually
    use that here as the model_class must be set in the Meta inner-class of
    AgentMessageSchema's).
    """
    if isinstance(schema, dict):
        slots = list(schema.keys())
        schema_dict = schema
    elif hasattr(schema, '_declared_fields'):
        slots = list(schema._declared_fields.keys())
        schema_dict = schema._declared_fields
    else:
        raise TypeError('Schema must be dict or class defining _declared_fields')

    Model = type(
        name,
        (AgentMessage,),
        {
            'Meta': type(
                'Meta', (), {
                    '__qualname__': name + '.Meta',
                    'handler_class': handler,
                    'message_type': msg_type,
                    'schema_class': name + 'Schema',
                }
            ),
            '__init__': generic_init,
            '__slots__': slots
        }
    )
    Model.__module__ = sys._getframe(1).f_globals['__name__']
    Schema = type(
        name + 'Schema',
        (AgentMessageSchema,),
        {
            'Meta': type(
                'Meta', (), {
                    '__qualname__': name + 'Schema.Meta',
                    'model_class': Model,
                }
            ),
            **schema_dict
        }
    )
    Schema.__module__ = sys._getframe(1).f_globals['__name__']
    return Model, Schema

    Generate a Message model class and schema class programmatically.

    The following would result in a class named XYZ inheriting from AgentMessage
    and XYZSchema inheriting from AgentMessageSchema.

    XYZ, XYZSchema = generate_model_schema(
        'XYZ',
        'aries_cloudagent.admin.handlers.XYZHandler',
        '{}/xyz'.format(PROTOCOL),
        {}
    )

    The attributes of XYZ are determined by schema_dict's keys. The actual
    schema of XYZSchema is defined by the field-value combinations of
    schema_dict, similar to marshmallow's Schema.from_dict() (can't actually
    use that here as the model_class must be set in the Meta inner-class of
    AgentMessageSchema's).
    
Generate a Message model class and schema class programmatically.
The following would result in a class named XYZ inheriting from AgentMessage
and XYZSchema inheriting from AgentMessageSchema.



The attributes of XYZ are determined by schema_dict's keys. The actual
schema of XYZSchema is defined by the field-value combinations of
schema_dict, similar to marshmallow's Schema.from_dict() (can't actually
use that here as the model_class must be set in the Meta inner-class of
AgentMessageSchema's).
Generate a Message model class and schema class programmatically . The following would result in a class named XYZ inheriting from AgentMessage and XYZSchema inheriting from AgentMessageSchema . The attributes of XYZ are determined by schema_dict ' s keys . The actual schema of XYZSchema is defined by the field - value combinations of schema_dict similar to marshmallow ' s Schema . from_dict () ( can ' t actually use that here as the model_class must be set in the Meta inner - class of AgentMessageSchema ' s ) .
def generate_model_schema(
        name: str,
        handler: str,
        msg_type: str,
        schema: dict
        ):
    if isinstance(schema, dict):
        slots = list(schema.keys())
        schema_dict = schema
    elif hasattr(schema, '_declared_fields'):
        slots = list(schema._declared_fields.keys())
        schema_dict = schema._declared_fields
    else:
        raise TypeError('Schema must be dict or class defining _declared_fields')
    Model = type(
        name,
        (AgentMessage,),
        {
            'Meta': type(
                'Meta', (), {
                    '__qualname__': name + '.Meta',
                    'handler_class': handler,
                    'message_type': msg_type,
                    'schema_class': name + 'Schema',
                }
            ),
            '__init__': generic_init,
            '__slots__': slots
        }
    )
    Model.__module__ = sys._getframe(1).f_globals['__name__']
    Schema = type(
        name + 'Schema',
        (AgentMessageSchema,),
        {
            'Meta': type(
                'Meta', (), {
                    '__qualname__': name + 'Schema.Meta',
                    'model_class': Model,
                }
            ),
            **schema_dict
        }
    )
    Schema.__module__ = sys._getframe(1).f_globals['__name__']
    return Model, Schema
def generate_model_schema ( name : str , handler : str , msg_type : str , schema : dict ) : if isinstance ( schema , dict ) : slots = list ( schema . keys ( ) ) schema_dict = schema elif hasattr ( schema , '_declared_fields' ) : slots = list ( schema . _declared_fields . keys ( ) ) schema_dict = schema . _declared_fields else : raise TypeError ( 'Schema must be dict or class defining _declared_fields' ) Model = type ( name , ( AgentMessage , ) , { 'Meta' : type ( 'Meta' , ( ) , { '__qualname__' : name + '.Meta' , 'handler_class' : handler , 'message_type' : msg_type , 'schema_class' : name + 'Schema' , } ) , '__init__' : generic_init , '__slots__' : slots } ) Model . __module__ = sys . _getframe ( 1 ) . f_globals [ '__name__' ] Schema = type ( name + 'Schema' , ( AgentMessageSchema , ) , { 'Meta' : type ( 'Meta' , ( ) , { '__qualname__' : name + 'Schema.Meta' , 'model_class' : Model , } ) , ** schema_dict } ) Schema . __module__ = sys . _getframe ( 1 ) . f_globals [ '__name__' ] return Model , Schema
Generate a Message model class and schema class programmatically.
Generate a Message model class and schema class programmatically .
"""
    Generate a Message model class and schema class programmatically.

    The following would result in a class named XYZ inheriting from AgentMessage
    and XYZSchema inheriting from AgentMessageSchema.

    XYZ, XYZSchema = generate_model_schema(
        'XYZ',
        'aries_cloudagent.admin.handlers.XYZHandler',
        '{}/xyz'.format(PROTOCOL),
        {}
    )

    The attributes of XYZ are determined by schema_dict's keys. The actual
    schema of XYZSchema is defined by the field-value combinations of
    schema_dict, similar to marshmallow's Schema.from_dict() (can't actually
    use that here as the model_class must be set in the Meta inner-class of
    AgentMessageSchema's).
    """
name
str
handler
str
msg_type
str
schema
dict