<s> import functools <EOL> import os <EOL> import six <EOL> from girder import constants <EOL> from girder . utility . webroot import WebrootBase <EOL> from . import docs , access <EOL> from . rest import Resource , RestException , getApiUrl <EOL> """<STR_LIT>""" <EOL> API_VERSION = constants . VERSION [ '<STR_LIT>' ] <EOL> SWAGGER_VERSION = '<STR_LIT>' <EOL> class Description ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , summary ) : <EOL> self . _summary = summary <EOL> self . _params = [ ] <EOL> self . _responses = [ ] <EOL> self . _consumes = [ ] <EOL> self . _responseClass = None <EOL> self . _notes = None <EOL> def asDict ( self ) : <EOL> """<STR_LIT>""" <EOL> resp = { <EOL> '<STR_LIT>' : self . _summary , <EOL> '<STR_LIT>' : self . _notes , <EOL> '<STR_LIT>' : self . _params , <EOL> '<STR_LIT>' : self . _responses , <EOL> '<STR_LIT>' : self . _responseClass <EOL> } <EOL> if self . _consumes : <EOL> resp [ '<STR_LIT>' ] = self . _consumes <EOL> return resp <EOL> def responseClass ( self , obj ) : <EOL> self . _responseClass = obj <EOL> return self <EOL> def param ( self , name , description , paramType = '<STR_LIT>' , dataType = '<STR_LIT:string>' , <EOL> required = True , enum = None , default = None ) : <EOL> """<STR_LIT>""" <EOL> if dataType == '<STR_LIT:int>' : <EOL> dataType = '<STR_LIT>' <EOL> param = { <EOL> '<STR_LIT:name>' : name , <EOL> '<STR_LIT:description>' : description , <EOL> '<STR_LIT>' : paramType , <EOL> '<STR_LIT:type>' : dataType , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : required <EOL> } <EOL> if enum : <EOL> param [ '<STR_LIT>' ] = enum <EOL> if default is not None : <EOL> if dataType == '<STR_LIT>' : <EOL> param [ '<STR_LIT>' ] = '<STR_LIT:true>' if default else '<STR_LIT:false>' <EOL> elif dataType == '<STR_LIT>' and default == <NUM_LIT:0> : <EOL> param [ '<STR_LIT>' ] = '<STR_LIT:0>' <EOL> else : <EOL> param [ '<STR_LIT>' ] = default <EOL> self . _params . append ( param ) <EOL> return self <EOL> def pagingParams ( self , defaultSort , defaultSortDir = <NUM_LIT:1> , defaultLimit = <NUM_LIT:50> ) : <EOL> """<STR_LIT>""" <EOL> self . param ( '<STR_LIT>' , '<STR_LIT>' , default = defaultLimit , <EOL> required = False , dataType = '<STR_LIT:int>' ) <EOL> self . param ( '<STR_LIT>' , '<STR_LIT>' , default = <NUM_LIT:0> , <EOL> required = False , dataType = '<STR_LIT:int>' ) <EOL> if defaultSort is not None : <EOL> self . param ( '<STR_LIT>' , '<STR_LIT>' , <EOL> default = defaultSort , required = False ) <EOL> self . param ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> required = False , dataType = '<STR_LIT:int>' , enum = ( <NUM_LIT:1> , - <NUM_LIT:1> ) , <EOL> default = defaultSortDir ) <EOL> return self <EOL> def consumes ( self , value ) : <EOL> self . _consumes . append ( value ) <EOL> return self <EOL> def notes ( self , notes ) : <EOL> self . _notes = notes <EOL> return self <EOL> def errorResponse ( self , reason = '<STR_LIT>' , code = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> self . _responses . append ( { <EOL> '<STR_LIT:message>' : reason , <EOL> '<STR_LIT:code>' : code <EOL> } ) <EOL> return self <EOL> class ApiDocs ( WebrootBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , templatePath = None ) : <EOL> if not templatePath : <EOL> templatePath = os . path . join ( constants . PACKAGE_DIR , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> super ( ApiDocs , self ) . __init__ ( templatePath ) <EOL> self . vars = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:title>' : '<STR_LIT>' <EOL> } <EOL> def _cmp ( a , b ) : <EOL> return ( a > b ) - ( a < b ) <EOL> class Describe ( Resource ) : <EOL> def __init__ ( self ) : <EOL> super ( Describe , self ) . __init__ ( ) <EOL> self . route ( '<STR_LIT:GET>' , ( ) , self . listResources , nodoc = True ) <EOL> self . route ( '<STR_LIT:GET>' , ( '<STR_LIT>' , ) , self . describeResource , nodoc = True ) <EOL> @ access . public <EOL> def listResources ( self , params ) : <EOL> return { <EOL> '<STR_LIT>' : API_VERSION , <EOL> '<STR_LIT>' : SWAGGER_VERSION , <EOL> '<STR_LIT>' : [ { '<STR_LIT:path>' : '<STR_LIT>' % resource } <EOL> for resource in sorted ( six . viewkeys ( docs . routes ) ) ] <EOL> } <EOL> def _compareRoutes ( self , routeOp1 , routeOp2 ) : <EOL> """<STR_LIT>""" <EOL> return _cmp ( routeOp1 [ <NUM_LIT:0> ] . replace ( '<STR_LIT:{>' , '<STR_LIT:U+0020>' ) , <EOL> routeOp2 [ <NUM_LIT:0> ] . replace ( '<STR_LIT:{>' , '<STR_LIT:U+0020>' ) ) <EOL> def _compareOperations ( self , op1 , op2 ) : <EOL> """<STR_LIT>""" <EOL> methodOrder = [ '<STR_LIT:GET>' , '<STR_LIT>' , '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> method1 = op1 . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> method2 = op2 . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if method1 in methodOrder and method2 in methodOrder : <EOL> return _cmp ( methodOrder . index ( method1 ) , methodOrder . index ( method2 ) ) <EOL> if method1 in methodOrder or method2 in methodOrder : <EOL> return _cmp ( method1 not in methodOrder , method2 not in methodOrder ) <EOL> return _cmp ( method1 , method2 ) <EOL> @ access . public <EOL> def describeResource ( self , resource , params ) : <EOL> if resource not in docs . routes : <EOL> raise RestException ( '<STR_LIT>' % resource ) <EOL> return { <EOL> '<STR_LIT>' : API_VERSION , <EOL> '<STR_LIT>' : SWAGGER_VERSION , <EOL> '<STR_LIT>' : getApiUrl ( ) , <EOL> '<STR_LIT>' : dict ( docs . models [ resource ] , ** docs . models [ None ] ) , <EOL> '<STR_LIT>' : [ { <EOL> '<STR_LIT:path>' : route , <EOL> '<STR_LIT>' : sorted ( <EOL> op , key = functools . cmp_to_key ( self . _compareOperations ) ) <EOL> } for route , op in sorted ( <EOL> six . viewitems ( docs . routes [ resource ] ) , <EOL> key = functools . cmp_to_key ( self . _compareRoutes ) ) <EOL> ] <EOL> } <EOL> class describeRoute ( object ) : <EOL> def __init__ ( self , description ) : <EOL> """<STR_LIT>""" <EOL> self . description = description <EOL> def __call__ ( self , fun ) : <EOL> fun . description = self . description <EOL> return fun </s>
