<s> import calendar <EOL> from datetime import datetime <EOL> import re <EOL> import aniso8601 <EOL> from flask import current_app , request <EOL> import six <EOL> from werkzeug . utils import cached_property <EOL> from flask_potion . utils import get_value , route_from <EOL> from flask_potion . reference import ResourceReference , ResourceBound , _bind_schema <EOL> from flask_potion . schema import Schema <EOL> class Raw ( Schema ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , schema , io = "<STR_LIT>" , default = None , attribute = None , nullable = False , title = None , description = None ) : <EOL> self . _schema = schema <EOL> self . _default = default <EOL> self . attribute = attribute <EOL> self . nullable = nullable <EOL> self . title = title <EOL> self . description = description <EOL> self . io = io <EOL> def _finalize_schema ( self , schema , io ) : <EOL> """<STR_LIT>""" <EOL> schema = dict ( schema ) <EOL> if self . io == "<STR_LIT:r>" and "<STR_LIT:r>" in io : <EOL> schema [ "<STR_LIT>" ] = True <EOL> if "<STR_LIT:null>" in schema . get ( "<STR_LIT:type>" , [ ] ) : <EOL> self . nullable = True <EOL> elif self . nullable : <EOL> if "<STR_LIT>" in schema and None not in schema [ "<STR_LIT>" ] : <EOL> schema [ "<STR_LIT>" ] . append ( None ) <EOL> if "<STR_LIT:type>" in schema : <EOL> type_ = schema [ "<STR_LIT:type>" ] <EOL> if isinstance ( type_ , ( str , dict ) ) : <EOL> schema [ "<STR_LIT:type>" ] = [ type_ , "<STR_LIT:null>" ] <EOL> else : <EOL> schema [ "<STR_LIT:type>" ] . append ( "<STR_LIT:null>" ) <EOL> if "<STR_LIT>" in schema : <EOL> if not any ( "<STR_LIT:null>" in choice . get ( "<STR_LIT:type>" , [ ] ) for choice in schema [ "<STR_LIT>" ] ) : <EOL> schema [ "<STR_LIT>" ] . append ( { "<STR_LIT:type>" : "<STR_LIT:null>" } ) <EOL> elif "<STR_LIT>" in schema : <EOL> if not any ( "<STR_LIT:null>" in choice . get ( "<STR_LIT:type>" , [ ] ) for choice in schema [ "<STR_LIT>" ] ) : <EOL> schema [ "<STR_LIT>" ] . append ( { "<STR_LIT:type>" : "<STR_LIT:null>" } ) <EOL> elif "<STR_LIT:type>" not in schema : <EOL> if len ( schema ) == <NUM_LIT:1> and "<STR_LIT>" in schema : <EOL> schema = { "<STR_LIT>" : [ schema , { "<STR_LIT:type>" : "<STR_LIT:null>" } ] } <EOL> else : <EOL> current_app . logger . warn ( '<STR_LIT>' . format ( self ) ) <EOL> for attr in ( "<STR_LIT:default>" , "<STR_LIT:title>" , "<STR_LIT:description>" ) : <EOL> value = getattr ( self , attr ) <EOL> if value is not None : <EOL> schema [ attr ] = value <EOL> return schema <EOL> @ property <EOL> def io ( self ) : <EOL> return self . _io <EOL> @ io . setter <EOL> def io ( self , value ) : <EOL> io = '<STR_LIT>' <EOL> if '<STR_LIT:w>' in value or '<STR_LIT:c>' in value : <EOL> io += '<STR_LIT:c>' <EOL> if '<STR_LIT:r>' in value : <EOL> io += '<STR_LIT:r>' <EOL> if '<STR_LIT:w>' in value or '<STR_LIT:u>' in value : <EOL> io += '<STR_LIT:u>' <EOL> self . _io = io <EOL> @ property <EOL> def default ( self ) : <EOL> if callable ( self . _default ) : <EOL> return self . _default ( ) <EOL> return self . _default <EOL> @ default . setter <EOL> def default ( self , value ) : <EOL> self . _default = value <EOL> def schema ( self ) : <EOL> """<STR_LIT>""" <EOL> schema = self . _schema <EOL> if callable ( schema ) : <EOL> schema = schema ( ) <EOL> if isinstance ( schema , Schema ) : <EOL> read_schema , write_schema = schema . response , schema . request <EOL> elif isinstance ( schema , tuple ) : <EOL> read_schema , write_schema = schema <EOL> else : <EOL> return self . _finalize_schema ( schema , "<STR_LIT:r>" ) , self . _finalize_schema ( schema , "<STR_LIT:w>" ) <EOL> return self . _finalize_schema ( read_schema , "<STR_LIT:r>" ) , self . _finalize_schema ( write_schema , "<STR_LIT:w>" ) <EOL> def format ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value is not None : <EOL> return self . formatter ( value ) <EOL> return value <EOL> def convert ( self , instance , update = False , validate = True ) : <EOL> """<STR_LIT>""" <EOL> if validate : <EOL> instance = super ( Raw , self ) . convert ( instance , update ) <EOL> if instance is not None : <EOL> return self . converter ( instance ) <EOL> return instance <EOL> def formatter ( self , value ) : <EOL> return value <EOL> def converter ( self , value ) : <EOL> return value <EOL> def output ( self , key , obj ) : <EOL> key = key if self . attribute is None else self . attribute <EOL> return self . format ( get_value ( key , obj , self . default ) ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' . format ( self . __class__ . __name__ , repr ( self . attribute ) ) <EOL> class Any ( Raw ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** kwargs ) : <EOL> super ( Any , self ) . __init__ ( { "<STR_LIT:type>" : [ "<STR_LIT:null>" , "<STR_LIT:string>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:object>" , "<STR_LIT>" ] } , ** kwargs ) <EOL> def _field_from_object ( parent , cls_or_instance ) : <EOL> if isinstance ( cls_or_instance , type ) : <EOL> container = cls_or_instance ( ) <EOL> else : <EOL> container = cls_or_instance <EOL> if not isinstance ( container , Schema ) : <EOL> raise RuntimeError ( '<STR_LIT>' . format ( parent , container . __class__ . __name__ ) ) <EOL> if not isinstance ( container , Raw ) : <EOL> container = Raw ( container ) <EOL> return container <EOL> class Custom ( Raw ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , schema , converter = None , formatter = None , ** kwargs ) : <EOL> super ( Custom , self ) . __init__ ( schema , ** kwargs ) <EOL> self . _converter = converter <EOL> self . _formatter = formatter <EOL> def format ( self , value ) : <EOL> if self . _formatter is None : <EOL> return value <EOL> return self . _formatter ( value ) <EOL> def converter ( self , value ) : <EOL> if self . _converter is None : <EOL> return value <EOL> return self . _converter ( value ) <EOL> class Array ( Raw , ResourceBound ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cls_or_instance , min_items = None , max_items = None , unique = None , ** kwargs ) : <EOL> self . container = container = _field_from_object ( self , cls_or_instance ) <EOL> schema_properties = [ ( '<STR_LIT:type>' , '<STR_LIT>' ) ] <EOL> schema_properties += [ ( k , v ) for k , v in [ ( '<STR_LIT>' , min_items ) , ( '<STR_LIT>' , max_items ) , ( '<STR_LIT>' , unique ) ] if v is not None ] <EOL> schema = lambda s : dict ( [ ( '<STR_LIT>' , s ) ] + schema_properties ) <EOL> super ( Array , self ) . __init__ ( lambda : ( schema ( container . response ) , schema ( container . request ) ) , <EOL> default = kwargs . pop ( '<STR_LIT:default>' , list ) , ** kwargs ) <EOL> def bind ( self , resource ) : <EOL> if isinstance ( self . container , ResourceBound ) : <EOL> self . container = self . container . bind ( resource ) <EOL> return self <EOL> def format ( self , value ) : <EOL> if value is not None : <EOL> return self . formatter ( value ) <EOL> if not self . nullable : <EOL> return [ ] <EOL> return value <EOL> def formatter ( self , value ) : <EOL> return [ self . container . format ( v ) for v in value ] <EOL> def converter ( self , value ) : <EOL> return [ self . container . convert ( v ) for v in value ] <EOL> List = Array <EOL> class Object ( Raw , ResourceBound ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , properties = None , pattern = None , pattern_properties = None , additional_properties = None , ** kwargs ) : <EOL> self . properties = None <EOL> self . pattern_properties = None <EOL> self . additional_properties = None <EOL> if isinstance ( properties , dict ) : <EOL> self . properties = properties <EOL> elif isinstance ( properties , ( type , Raw ) ) : <EOL> field = _field_from_object ( self , properties ) <EOL> if pattern : <EOL> self . pattern_properties = { pattern : field } <EOL> else : <EOL> self . additional_properties = field <EOL> if isinstance ( additional_properties , ( type , Raw ) ) : <EOL> self . additional_properties = _field_from_object ( self , additional_properties ) <EOL> elif additional_properties is True : <EOL> self . additional_properties = Any ( ) <EOL> if isinstance ( pattern_properties , ( type , Raw ) ) : <EOL> self . pattern_properties = _field_from_object ( self , pattern_properties ) <EOL> def schema ( ) : <EOL> request = { "<STR_LIT:type>" : "<STR_LIT:object>" } <EOL> response = { "<STR_LIT:type>" : "<STR_LIT:object>" } <EOL> for schema , attr in ( ( request , "<STR_LIT>" ) , ( response , "<STR_LIT>" ) ) : <EOL> if self . properties : <EOL> schema [ "<STR_LIT>" ] = { key : getattr ( field , attr ) for key , field in self . properties . items ( ) } <EOL> if self . pattern_properties : <EOL> schema [ "<STR_LIT>" ] = { pattern : getattr ( field , attr ) <EOL> for pattern , field in self . pattern_properties . items ( ) } <EOL> if self . additional_properties : <EOL> schema [ "<STR_LIT>" ] = getattr ( self . additional_properties , attr ) <EOL> else : <EOL> schema [ "<STR_LIT>" ] = False <EOL> return response , request <EOL> if self . pattern_properties and ( len ( self . pattern_properties ) > <NUM_LIT:1> or self . additional_properties ) : <EOL> raise NotImplementedError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> super ( Object , self ) . __init__ ( schema , ** kwargs ) <EOL> def bind ( self , resource ) : <EOL> if self . properties : <EOL> self . properties = { <EOL> key : _bind_schema ( value , resource ) <EOL> for key , value in self . properties . items ( ) } <EOL> if self . pattern_properties : <EOL> self . pattern_properties = { <EOL> key : _bind_schema ( value , resource ) <EOL> for key , value in self . pattern_properties . items ( ) } <EOL> if self . additional_properties : <EOL> self . additional_properties = _bind_schema ( self . additional_properties , resource ) <EOL> return self <EOL> @ cached_property <EOL> def _property_attributes ( self ) : <EOL> if not self . properties : <EOL> return ( ) <EOL> return [ field . attribute or key for key , field in self . properties . items ( ) ] <EOL> def formatter ( self , value ) : <EOL> output = { } <EOL> if self . properties : <EOL> output = { key : field . format ( get_value ( field . attribute or key , value , field . default ) ) for key , field in self . properties . items ( ) } <EOL> else : <EOL> output = { } <EOL> if self . pattern_properties : <EOL> pattern , field = next ( iter ( self . pattern_properties . items ( ) ) ) <EOL> if not self . additional_properties : <EOL> output . update ( { k : field . format ( v ) for k , v in value . items ( ) if k not in self . _property_attributes } ) <EOL> else : <EOL> raise NotImplementedError ( ) <EOL> elif self . additional_properties : <EOL> field = self . additional_properties <EOL> output . update ( { k : field . format ( v ) for k , v in value . items ( ) if k not in self . _property_attributes } ) <EOL> return output <EOL> def converter ( self , instance ) : <EOL> result = { } <EOL> if self . properties : <EOL> result = { field . attribute or key : field . convert ( instance . get ( key , field . default ) ) <EOL> for key , field in self . properties . items ( ) } <EOL> if self . pattern_properties : <EOL> pattern , field = next ( iter ( self . pattern_properties . items ( ) ) ) <EOL> if not self . additional_properties : <EOL> result . update ( { key : field . convert ( value ) <EOL> for key , value in instance . items ( ) if key not in result } ) <EOL> else : <EOL> raise NotImplementedError ( ) <EOL> elif self . additional_properties : <EOL> field = self . additional_properties <EOL> result . update ( { key : field . convert ( value ) for key , value in instance . items ( ) if key not in result } ) <EOL> return result <EOL> class AttributeMapped ( Object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cls_or_instance , mapping_attribute = None , ** kwargs ) : <EOL> self . mapping_attribute = mapping_attribute <EOL> super ( AttributeMapped , self ) . __init__ ( cls_or_instance , ** kwargs ) <EOL> def _set_mapping_attribute ( self , obj , value ) : <EOL> if isinstance ( obj , dict ) : <EOL> obj [ self . mapping_attribute ] = value <EOL> else : <EOL> setattr ( obj , self . mapping_attribute , value ) <EOL> return obj <EOL> def formatter ( self , value ) : <EOL> if self . pattern_properties : <EOL> pattern , field = next ( iter ( self . pattern_properties . items ( ) ) ) <EOL> return { get_value ( self . mapping_attribute , v , None ) : field . format ( v ) for v in value } <EOL> elif self . additional_properties : <EOL> return { get_value ( self . mapping_attribute , v , None ) : self . additional_properties . format ( v ) for v in value } <EOL> def converter ( self , value ) : <EOL> if self . pattern_properties : <EOL> pattern , field = next ( iter ( self . pattern_properties . items ( ) ) ) <EOL> return [ self . _set_mapping_attribute ( field . convert ( v ) , k ) for k , v in value . items ( ) ] <EOL> elif self . additional_properties : <EOL> return [ self . _set_mapping_attribute ( self . additional_properties . convert ( v ) , k ) for k , v in value . items ( ) ] <EOL> class String ( Raw ) : <EOL> """<STR_LIT>""" <EOL> url_rule_converter = '<STR_LIT:string>' <EOL> def __init__ ( self , min_length = None , max_length = None , pattern = None , enum = None , format = None , ** kwargs ) : <EOL> schema = { "<STR_LIT:type>" : "<STR_LIT:string>" } <EOL> if enum is not None : <EOL> enum = list ( enum ) <EOL> for v , k in ( ( min_length , '<STR_LIT>' ) , <EOL> ( max_length , '<STR_LIT>' ) , <EOL> ( pattern , '<STR_LIT>' ) , <EOL> ( enum , '<STR_LIT>' ) , <EOL> ( format , '<STR_LIT>' ) ) : <EOL> if v is not None : <EOL> schema [ k ] = v <EOL> super ( String , self ) . __init__ ( schema , ** kwargs ) <EOL> class UUID ( String ) : <EOL> """<STR_LIT>""" <EOL> url_rule_converter = '<STR_LIT:string>' <EOL> UUID_REGEX = "<STR_LIT>" <EOL> def __init__ ( self , ** kwargs ) : <EOL> super ( UUID , self ) . __init__ ( min_length = <NUM_LIT> , max_length = <NUM_LIT> , pattern = self . UUID_REGEX , ** kwargs ) <EOL> try : <EOL> from datetime import timezone <EOL> except ImportError : <EOL> from datetime import tzinfo , timedelta <EOL> class timezone ( tzinfo ) : <EOL> def __init__ ( self , utcoffset , name = None ) : <EOL> self . _utcoffset = utcoffset <EOL> self . _name = name <EOL> def utcoffset ( self , dt ) : <EOL> return self . _utcoffset <EOL> def tzname ( self , dt ) : <EOL> return self . _name <EOL> def dst ( self , dt ) : <EOL> return timedelta ( <NUM_LIT:0> ) <EOL> timezone . utc = timezone ( timedelta ( <NUM_LIT:0> ) , '<STR_LIT>' ) <EOL> class Date ( Raw ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** kwargs ) : <EOL> super ( Date , self ) . __init__ ( { <EOL> "<STR_LIT:type>" : "<STR_LIT:object>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT>" <EOL> } <EOL> } , <EOL> "<STR_LIT>" : False <EOL> } , ** kwargs ) <EOL> def formatter ( self , value ) : <EOL> return { "<STR_LIT>" : int ( calendar . timegm ( value . timetuple ( ) ) * <NUM_LIT:1000> ) } <EOL> def converter ( self , value ) : <EOL> return datetime . fromtimestamp ( value [ "<STR_LIT>" ] / <NUM_LIT:1000> , timezone . utc ) . date ( ) <EOL> class DateTime ( Date ) : <EOL> """<STR_LIT>""" <EOL> def formatter ( self , value ) : <EOL> return { "<STR_LIT>" : int ( calendar . timegm ( value . utctimetuple ( ) ) * <NUM_LIT:1000> ) } <EOL> def converter ( self , value ) : <EOL> return datetime . fromtimestamp ( value [ "<STR_LIT>" ] / <NUM_LIT:1000> , timezone . utc ) <EOL> class DateString ( Raw ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** kwargs ) : <EOL> super ( DateString , self ) . __init__ ( { "<STR_LIT:type>" : "<STR_LIT:string>" , "<STR_LIT>" : "<STR_LIT:date>" } , ** kwargs ) <EOL> def formatter ( self , value ) : <EOL> return value . strftime ( '<STR_LIT>' ) <EOL> def converter ( self , value ) : <EOL> return aniso8601 . parse_date ( value ) <EOL> class DateTimeString ( Raw ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** kwargs ) : <EOL> super ( DateTimeString , self ) . __init__ ( { "<STR_LIT:type>" : "<STR_LIT:string>" , "<STR_LIT>" : "<STR_LIT>" } , ** kwargs ) <EOL> def formatter ( self , value ) : <EOL> return value . isoformat ( ) <EOL> def converter ( self , value ) : <EOL> return aniso8601 . parse_datetime ( value ) <EOL> class Uri ( String ) : <EOL> def __init__ ( self , ** kwargs ) : <EOL> super ( Uri , self ) . __init__ ( format = "<STR_LIT>" , ** kwargs ) <EOL> class Email ( String ) : <EOL> def __init__ ( self , ** kwargs ) : <EOL> super ( Email , self ) . __init__ ( format = "<STR_LIT:email>" , ** kwargs ) <EOL> class Boolean ( Raw ) : <EOL> def __init__ ( self , ** kwargs ) : <EOL> super ( Boolean , self ) . __init__ ( { "<STR_LIT:type>" : "<STR_LIT>" } , ** kwargs ) <EOL> def format ( self , value ) : <EOL> return bool ( value ) <EOL> class Integer ( Raw ) : <EOL> url_rule_converter = '<STR_LIT:int>' <EOL> def __init__ ( self , minimum = None , maximum = None , default = None , ** kwargs ) : <EOL> schema = { "<STR_LIT:type>" : "<STR_LIT>" } <EOL> if minimum is not None : <EOL> schema [ '<STR_LIT>' ] = minimum <EOL> if maximum is not None : <EOL> schema [ '<STR_LIT>' ] = maximum <EOL> super ( Integer , self ) . __init__ ( schema , default = default , ** kwargs ) <EOL> def formatter ( self , value ) : <EOL> return int ( value ) <EOL> class PositiveInteger ( Integer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , maximum = None , ** kwargs ) : <EOL> super ( PositiveInteger , self ) . __init__ ( minimum = <NUM_LIT:1> , maximum = maximum , ** kwargs ) <EOL> class Number ( Raw ) : <EOL> def __init__ ( self , <EOL> minimum = None , <EOL> maximum = None , <EOL> exclusive_minimum = False , <EOL> exclusive_maximum = False , <EOL> ** kwargs ) : <EOL> schema = { "<STR_LIT:type>" : "<STR_LIT>" } <EOL> if minimum is not None : <EOL> schema [ '<STR_LIT>' ] = minimum <EOL> if exclusive_minimum : <EOL> schema [ '<STR_LIT>' ] = True <EOL> if maximum is not None : <EOL> schema [ '<STR_LIT>' ] = maximum <EOL> if exclusive_maximum : <EOL> schema [ '<STR_LIT>' ] = True <EOL> super ( Number , self ) . __init__ ( schema , ** kwargs ) <EOL> def formatter ( self , value ) : <EOL> return float ( value ) <EOL> class ToOne ( Raw , ResourceBound ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , resource , ** kwargs ) : <EOL> self . target_reference = ResourceReference ( resource ) <EOL> def schema ( ) : <EOL> target = self . target <EOL> key_converters = self . target . meta . key_converters <EOL> response_schema = self . formatter_key . response <EOL> if len ( key_converters ) > <NUM_LIT:1> : <EOL> request_schema = { <EOL> "<STR_LIT>" : [ nk . request for nk in key_converters ] <EOL> } <EOL> else : <EOL> request_schema = self . formatter_key . request <EOL> return response_schema , request_schema <EOL> super ( ToOne , self ) . __init__ ( schema , ** kwargs ) <EOL> def rebind ( self , resource ) : <EOL> if self . target_reference . value == '<STR_LIT>' : <EOL> return self . __class__ ( <EOL> '<STR_LIT>' , <EOL> default = self . default , <EOL> attribute = self . attribute , <EOL> nullable = self . nullable , <EOL> title = self . title , <EOL> description = self . description , <EOL> io = self . io <EOL> ) . bind ( resource ) <EOL> else : <EOL> return self <EOL> @ cached_property <EOL> def target ( self ) : <EOL> return self . target_reference . resolve ( self . resource ) <EOL> @ cached_property <EOL> def formatter_key ( self ) : <EOL> return self . target . meta . key_converters [ <NUM_LIT:0> ] <EOL> def formatter ( self , item ) : <EOL> return self . formatter_key . format ( item ) <EOL> def converter ( self , value ) : <EOL> for python_type , json_type in ( <EOL> ( dict , '<STR_LIT:object>' ) , <EOL> ( int , '<STR_LIT>' ) , <EOL> ( ( list , tuple ) , '<STR_LIT>' ) , <EOL> ( six . string_types , '<STR_LIT:string>' ) ) : <EOL> if isinstance ( value , python_type ) : <EOL> return self . target . meta . key_converters_by_type [ json_type ] . convert ( value ) <EOL> class ToMany ( Array ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , resource , ** kwargs ) : <EOL> super ( ToMany , self ) . __init__ ( ToOne ( resource , nullable = False ) , ** kwargs ) <EOL> class Inline ( Raw , ResourceBound ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , resource , patchable = False , ** kwargs ) : <EOL> self . target_reference = ResourceReference ( resource ) <EOL> self . patchable = patchable <EOL> def schema ( ) : <EOL> def _response_schema ( ) : <EOL> if self . resource == self . target : <EOL> return { "<STR_LIT>" : "<STR_LIT:#>" } <EOL> return { "<STR_LIT>" : self . resource . routes [ "<STR_LIT>" ] . rule_factory ( self . resource ) } <EOL> if not not self . patchable : <EOL> return _response_schema ( ) <EOL> else : <EOL> return _response_schema ( ) , self . target . schema . patchable . update <EOL> super ( Inline , self ) . __init__ ( schema , ** kwargs ) <EOL> def rebind ( self , resource ) : <EOL> if self . target_reference . value == '<STR_LIT>' : <EOL> return self . __class__ ( <EOL> '<STR_LIT>' , <EOL> patchable = self . patchable , <EOL> default = self . default , <EOL> attribute = self . attribute , <EOL> nullable = self . nullable , <EOL> title = self . title , <EOL> description = self . description , <EOL> io = self . io <EOL> ) . bind ( resource ) <EOL> else : <EOL> return self <EOL> @ cached_property <EOL> def target ( self ) : <EOL> return self . target_reference . resolve ( self . resource ) <EOL> def format ( self , item ) : <EOL> return self . target . schema . format ( item ) <EOL> def convert ( self , item , update = False ) : <EOL> return self . target . schema . convert ( item , update = update , patchable = self . patchable ) <EOL> class ItemType ( Raw ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , resource ) : <EOL> self . resource = resource <EOL> super ( ItemType , self ) . __init__ ( lambda : { <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" , <EOL> "<STR_LIT>" : [ self . resource . meta . name ] <EOL> } , io = "<STR_LIT:r>" ) <EOL> def format ( self , value ) : <EOL> return self . resource . meta . name <EOL> class ItemUri ( Raw ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , resource , attribute = None ) : <EOL> self . target_reference = ResourceReference ( resource ) <EOL> super ( ItemUri , self ) . __init__ ( lambda : { <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" , <EOL> "<STR_LIT>" : "<STR_LIT>" . format ( re . escape ( self . target . route_prefix ) ) <EOL> } , io = "<STR_LIT:r>" , attribute = attribute ) <EOL> @ cached_property <EOL> def target ( self ) : <EOL> return self . target_reference . resolve ( ) <EOL> def format ( self , value ) : <EOL> return '<STR_LIT>' . format ( self . target . route_prefix , value ) <EOL> def converter ( self , value ) : <EOL> try : <EOL> endpoint , args = route_from ( value , '<STR_LIT:GET>' ) <EOL> except Exception as e : <EOL> raise e <EOL> return self . target . manager . id_field . convert ( args [ '<STR_LIT:id>' ] ) </s>
