<s> '''<STR_LIT>''' <EOL> from pyschema import core <EOL> from pyschema . types import Field , Boolean , Integer , Float <EOL> from pyschema . types import Text , Enum , List , Map , SubRecord <EOL> try : <EOL> from collections import OrderedDict <EOL> except ImportError : <EOL> from ordereddict import OrderedDict <EOL> try : <EOL> import simplejson as json <EOL> except ImportError : <EOL> import json <EOL> Boolean . jsonschema_type_name = '<STR_LIT>' <EOL> Integer . jsonschema_type_name = '<STR_LIT>' <EOL> Float . jsonschema_type_name = '<STR_LIT>' <EOL> Text . jsonschema_type_name = '<STR_LIT:string>' <EOL> Enum . jsonschema_type_name = '<STR_LIT:string>' <EOL> List . jsonschema_type_name = '<STR_LIT>' <EOL> Map . avro_type_name = '<STR_LIT:object>' <EOL> @ Field . mixin <EOL> class FieldMixin : <EOL> def jsonschema_type_schema ( self , state ) : <EOL> return { <EOL> '<STR_LIT:type>' : self . jsonschema_type_name , <EOL> } <EOL> @ Enum . mixin <EOL> class EnumMixin : <EOL> def jsonschema_type_schema ( self , state ) : <EOL> return { <EOL> '<STR_LIT:type>' : self . jsonschema_type_name , <EOL> '<STR_LIT>' : sorted ( list ( self . values ) ) , <EOL> } <EOL> @ List . mixin <EOL> class ListMixin : <EOL> def jsonschema_type_schema ( self , state ) : <EOL> return { <EOL> '<STR_LIT:type>' : self . jsonschema_type_name , <EOL> '<STR_LIT>' : self . field_type . jsonschema_type_schema ( state ) , <EOL> } <EOL> @ Map . mixin <EOL> class MapMixin : <EOL> def jsonschema_type_schema ( self , state ) : <EOL> return { <EOL> '<STR_LIT:type>' : '<STR_LIT:object>' , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : self . value_type . jsonschema_type_schema ( state ) , <EOL> } , <EOL> } <EOL> @ SubRecord . mixin <EOL> class SubRecordMixin : <EOL> def jsonschema_type_schema ( self , state ) : <EOL> cls = self . _schema <EOL> state . record_schemas [ cls . _schema_name ] = get_schema_dict ( cls , state ) <EOL> return { <EOL> '<STR_LIT>' : self . jsonschema_type_name , <EOL> } <EOL> @ property <EOL> def jsonschema_type_name ( self ) : <EOL> return '<STR_LIT>' . format ( self . _schema . _schema_name ) <EOL> class SchemaGeneratorState ( object ) : <EOL> def __init__ ( self ) : <EOL> self . record_schemas = dict ( ) <EOL> def get_schema_dict ( record , state = None ) : <EOL> """<STR_LIT>""" <EOL> state = state or SchemaGeneratorState ( ) <EOL> schema = OrderedDict ( [ <EOL> ( '<STR_LIT:type>' , '<STR_LIT:object>' ) , <EOL> ( '<STR_LIT:id>' , record . _schema_name ) , <EOL> ] ) <EOL> fields = dict ( ) <EOL> for field_name , field_type in record . _fields . iteritems ( ) : <EOL> fields [ field_name ] = field_type . jsonschema_type_schema ( state ) <EOL> required = set ( fields . keys ( ) ) <EOL> schema [ '<STR_LIT>' ] = fields <EOL> schema [ '<STR_LIT>' ] = sorted ( list ( required ) ) <EOL> schema [ '<STR_LIT>' ] = False <EOL> state . record_schemas [ record . _schema_name ] = schema <EOL> return schema <EOL> def get_root_schema_dict ( record ) : <EOL> """<STR_LIT>""" <EOL> state = SchemaGeneratorState ( ) <EOL> schema = get_schema_dict ( record , state ) <EOL> del state . record_schemas [ record . _schema_name ] <EOL> if state . record_schemas : <EOL> schema [ '<STR_LIT>' ] = dict ( ) <EOL> for name , sub_schema in state . record_schemas . iteritems ( ) : <EOL> schema [ '<STR_LIT>' ] [ name ] = sub_schema <EOL> return schema <EOL> def get_root_schema_string ( record ) : <EOL> return json . dumps ( get_root_schema_dict ( record ) ) <EOL> def dumps ( record ) : <EOL> return json . dumps ( core . to_json_compatible ( record ) ) <EOL> def loads ( s , record_store = None , schema = None ) : <EOL> return core . loads ( s , record_store , schema , core . from_json_compatible ) </s>
