<s> from __future__ import absolute_import <EOL> from . utils import jr_split , jp_split <EOL> from . getter import UrlGetter , LocalGetter <EOL> import six <EOL> import os <EOL> import inspect <EOL> import logging <EOL> logger = logging . getLogger ( __name__ ) <EOL> class SwaggerResolver ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , url_load_hook ) : <EOL> """<STR_LIT:U+0020>""" <EOL> self . __cache = { } <EOL> self . __url_load_hook = url_load_hook <EOL> def resolve ( self , jref , getter = None ) : <EOL> """<STR_LIT:U+0020>""" <EOL> url , jp = jr_split ( jref ) <EOL> local_url = self . __url_load_hook ( url ) if self . __url_load_hook else url <EOL> logger . info ( '<STR_LIT>' . format ( url , local_url ) ) <EOL> obj = self . __cache . get ( url , None ) <EOL> if not obj : <EOL> if not getter : <EOL> getter = UrlGetter <EOL> p = six . moves . urllib . parse . urlparse ( local_url ) <EOL> if p . scheme == '<STR_LIT:file>' and p . path : <EOL> getter = LocalGetter ( os . path . join ( p . netloc , p . path ) ) <EOL> if inspect . isclass ( getter ) : <EOL> getter = getter ( local_url ) <EOL> obj = six . advance_iterator ( getter ) <EOL> self . __cache [ url ] = obj if obj else None <EOL> if obj : <EOL> ts = jp_split ( jp ) [ <NUM_LIT:1> : ] <EOL> while len ( ts ) > <NUM_LIT:0> : <EOL> t = ts . pop ( <NUM_LIT:0> ) <EOL> if isinstance ( obj , list ) : <EOL> obj = obj [ int ( t ) ] <EOL> elif isinstance ( obj , dict ) : <EOL> obj = obj [ t ] <EOL> else : <EOL> raise Exception ( '<STR_LIT>' . format ( str ( type ( obj ) ) ) ) <EOL> else : <EOL> raise Exception ( '<STR_LIT>' . format ( jref ) ) <EOL> return obj </s>
