<s> """<STR_LIT>""" <EOL> import re <EOL> from docutils import nodes <EOL> from docutils . parsers . rst . roles import set_classes <EOL> from pygments . lexer import RegexLexer , bygroups <EOL> from pygments . lexers import get_lexer_by_name <EOL> from pygments . token import Literal , Text , Operator , Keyword , Name , Number <EOL> from pygments . util import ClassNotFound <EOL> from sphinx import addnodes <EOL> from sphinx . roles import XRefRole <EOL> from sphinx . domains import Domain , ObjType , Index <EOL> from sphinx . directives import ObjectDescription <EOL> from sphinx . util . nodes import make_refnode <EOL> from sphinx . util . docfields import GroupedField , TypedField <EOL> class DocRef ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , base_url , anchor , section ) : <EOL> """<STR_LIT>""" <EOL> self . base_url = base_url <EOL> self . anchor = anchor <EOL> self . section = section <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' . format ( self . base_url , self . anchor , self . section ) <EOL> RFC2616 = '<STR_LIT>' <EOL> RFC2616ANCHOR = '<STR_LIT>' <EOL> RFC5789 = '<STR_LIT>' <EOL> RFC5789ANCHOR = '<STR_LIT>' <EOL> DOCREFS = { <EOL> '<STR_LIT>' : DocRef ( RFC5789 , RFC5789ANCHOR , <NUM_LIT:2> ) , <EOL> '<STR_LIT>' : DocRef ( RFC2616 , RFC2616ANCHOR , <NUM_LIT> ) , <EOL> '<STR_LIT>' : DocRef ( RFC2616 , RFC2616ANCHOR , <NUM_LIT> ) , <EOL> '<STR_LIT>' : DocRef ( RFC2616 , RFC2616ANCHOR , <NUM_LIT> ) , <EOL> '<STR_LIT>' : DocRef ( RFC2616 , RFC2616ANCHOR , <NUM_LIT> ) , <EOL> '<STR_LIT>' : DocRef ( RFC2616 , RFC2616ANCHOR , <NUM_LIT> ) , <EOL> '<STR_LIT>' : DocRef ( RFC2616 , RFC2616ANCHOR , <NUM_LIT> ) , <EOL> '<STR_LIT>' : DocRef ( RFC2616 , RFC2616ANCHOR , <NUM_LIT> ) , <EOL> '<STR_LIT>' : DocRef ( RFC2616 , RFC2616ANCHOR , <NUM_LIT> ) <EOL> } <EOL> HTTP_STATUS_CODES = { <EOL> <NUM_LIT:100> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT:200> : '<STR_LIT:OK>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' <EOL> } <EOL> http_sig_param_re = re . compile ( r'<STR_LIT>' , <EOL> re . VERBOSE ) <EOL> def http_resource_anchor ( method , path ) : <EOL> path = re . sub ( r'<STR_LIT>' , '<STR_LIT:->' , path ) <EOL> return method . lower ( ) + '<STR_LIT:->' + path <EOL> class HTTPResource ( ObjectDescription ) : <EOL> doc_field_types = [ <EOL> TypedField ( '<STR_LIT>' , label = '<STR_LIT>' , <EOL> names = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> typerolename = '<STR_LIT>' , typenames = ( '<STR_LIT>' , '<STR_LIT:type>' ) ) , <EOL> TypedField ( '<STR_LIT>' , label = '<STR_LIT>' , <EOL> names = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> typerolename = '<STR_LIT>' , typenames = ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> TypedField ( '<STR_LIT>' , label = '<STR_LIT>' , <EOL> names = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> typerolename = '<STR_LIT>' , typenames = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> GroupedField ( '<STR_LIT>' , label = '<STR_LIT>' , <EOL> names = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> GroupedField ( '<STR_LIT>' , label = '<STR_LIT>' , <EOL> rolename = '<STR_LIT>' , <EOL> names = ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> GroupedField ( '<STR_LIT>' , label = '<STR_LIT>' , <EOL> rolename = '<STR_LIT>' , <EOL> names = ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> GroupedField ( '<STR_LIT>' , label = '<STR_LIT>' , <EOL> rolename = '<STR_LIT>' , <EOL> names = ( '<STR_LIT>' , '<STR_LIT:status>' , '<STR_LIT:code>' ) ) <EOL> ] <EOL> method = NotImplemented <EOL> def handle_signature ( self , sig , signode ) : <EOL> method = self . method . upper ( ) + '<STR_LIT:U+0020>' <EOL> signode += addnodes . desc_name ( method , method ) <EOL> offset = <NUM_LIT:0> <EOL> for match in http_sig_param_re . finditer ( sig ) : <EOL> path = sig [ offset : match . start ( ) ] <EOL> signode += addnodes . desc_name ( path , path ) <EOL> params = addnodes . desc_parameterlist ( ) <EOL> typ = match . group ( '<STR_LIT:type>' ) <EOL> if typ : <EOL> typ = typ + '<STR_LIT>' <EOL> params += addnodes . desc_annotation ( typ , typ ) <EOL> name = match . group ( '<STR_LIT:name>' ) <EOL> params += addnodes . desc_parameter ( name , name ) <EOL> signode += params <EOL> offset = match . end ( ) <EOL> if offset < len ( sig ) : <EOL> path = sig [ offset : len ( sig ) ] <EOL> signode += addnodes . desc_name ( path , path ) <EOL> fullname = self . method . upper ( ) + '<STR_LIT:U+0020>' + path <EOL> signode [ '<STR_LIT>' ] = self . method <EOL> signode [ '<STR_LIT:path>' ] = sig <EOL> signode [ '<STR_LIT>' ] = fullname <EOL> return ( fullname , self . method , sig ) <EOL> def needs_arglist ( self ) : <EOL> return False <EOL> def add_target_and_index ( self , name_cls , sig , signode ) : <EOL> signode [ '<STR_LIT>' ] . append ( http_resource_anchor ( * name_cls [ <NUM_LIT:1> : ] ) ) <EOL> self . env . domaindata [ '<STR_LIT:http>' ] [ self . method ] [ sig ] = ( self . env . docname , '<STR_LIT>' ) <EOL> def get_index_text ( self , modname , name ) : <EOL> return '<STR_LIT>' <EOL> class HTTPOptions ( HTTPResource ) : <EOL> method = '<STR_LIT>' <EOL> class HTTPHead ( HTTPResource ) : <EOL> method = '<STR_LIT>' <EOL> class HTTPPatch ( HTTPResource ) : <EOL> method = '<STR_LIT>' <EOL> class HTTPPost ( HTTPResource ) : <EOL> method = '<STR_LIT>' <EOL> class HTTPGet ( HTTPResource ) : <EOL> method = '<STR_LIT>' <EOL> class HTTPPut ( HTTPResource ) : <EOL> method = '<STR_LIT>' <EOL> class HTTPDelete ( HTTPResource ) : <EOL> method = '<STR_LIT>' <EOL> class HTTPTrace ( HTTPResource ) : <EOL> method = '<STR_LIT>' <EOL> def http_statuscode_role ( name , rawtext , text , lineno , inliner , <EOL> options = { } , content = [ ] ) : <EOL> if text . isdigit ( ) : <EOL> code = int ( text ) <EOL> try : <EOL> status = HTTP_STATUS_CODES [ code ] <EOL> except KeyError : <EOL> msg = inliner . reporter . error ( '<STR_LIT>' <EOL> % code , lineno = lineno ) <EOL> prb = inliner . problematic ( rawtext , rawtext , msg ) <EOL> return [ prb ] , [ msg ] <EOL> else : <EOL> try : <EOL> code , status = re . split ( r'<STR_LIT>' , text . strip ( ) , <NUM_LIT:1> ) <EOL> code = int ( code ) <EOL> except ValueError : <EOL> msg = inliner . reporter . error ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> text , <EOL> line = lineno <EOL> ) <EOL> prb = inliner . problematic ( rawtext , rawtext , msg ) <EOL> return [ prb ] , [ msg ] <EOL> nodes . reference ( rawtext ) <EOL> if code == <NUM_LIT> : <EOL> url = '<STR_LIT>' <EOL> if code == <NUM_LIT> : <EOL> url = '<STR_LIT>' <EOL> if code == <NUM_LIT> : <EOL> url = '<STR_LIT>' '<STR_LIT>' <EOL> elif code in HTTP_STATUS_CODES : <EOL> url = '<STR_LIT>' '<STR_LIT>' + ( '<STR_LIT>' % ( code // <NUM_LIT:100> , <NUM_LIT:1> + code % <NUM_LIT:100> ) ) <EOL> else : <EOL> url = '<STR_LIT>' <EOL> set_classes ( options ) <EOL> node = nodes . reference ( rawtext , '<STR_LIT>' % ( code , status ) , <EOL> refuri = url , ** options ) <EOL> return [ node ] , [ ] <EOL> def http_method_role ( name , rawtext , text , lineno , inliner , <EOL> options = { } , content = [ ] ) : <EOL> method = str ( text ) . lower ( ) <EOL> if method not in DOCREFS : <EOL> msg = inliner . reporter . error ( '<STR_LIT>' % method , <EOL> lineno = lineno ) <EOL> prb = inliner . problematic ( rawtext , rawtext , msg ) <EOL> return [ prb ] , [ msg ] <EOL> url = str ( DOCREFS [ method ] ) <EOL> node = nodes . reference ( rawtext , method . upper ( ) , refuri = url , ** options ) <EOL> return [ node ] , [ ] <EOL> class HTTPXRefRole ( XRefRole ) : <EOL> def __init__ ( self , method , ** kwargs ) : <EOL> XRefRole . __init__ ( self , ** kwargs ) <EOL> self . method = method <EOL> def process_link ( self , env , refnode , has_explicit_title , title , target ) : <EOL> if not target . startswith ( '<STR_LIT:/>' ) : <EOL> pass <EOL> if not has_explicit_title : <EOL> title = self . method . upper ( ) + '<STR_LIT:U+0020>' + title <EOL> return title , target <EOL> class HTTPIndex ( Index ) : <EOL> name = '<STR_LIT>' <EOL> localname = '<STR_LIT>' <EOL> shortname = '<STR_LIT>' <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( HTTPIndex , self ) . __init__ ( * args , ** kwargs ) <EOL> self . ignore = [ [ l for l in x . split ( '<STR_LIT:/>' ) if l ] <EOL> for x in self . domain . env . config [ '<STR_LIT>' ] ] <EOL> self . ignore . sort ( key = lambda x : - len ( x ) ) <EOL> def grouping_prefix ( self , path ) : <EOL> letters = [ x for x in path . split ( '<STR_LIT:/>' ) if x ] <EOL> for prefix in self . ignore : <EOL> if letters [ : len ( prefix ) ] == prefix : <EOL> return '<STR_LIT:/>' + '<STR_LIT:/>' . join ( letters [ : len ( prefix ) + <NUM_LIT:1> ] ) <EOL> return '<STR_LIT>' % ( letters [ <NUM_LIT:0> ] if letters else '<STR_LIT>' , ) <EOL> def generate ( self , docnames = None ) : <EOL> content = { } <EOL> items = ( ( method , path , info ) <EOL> for method , routes in self . domain . routes . items ( ) <EOL> for path , info in routes . items ( ) ) <EOL> items = sorted ( items , key = lambda item : item [ <NUM_LIT:1> ] ) <EOL> for method , path , info in items : <EOL> entries = content . setdefault ( self . grouping_prefix ( path ) , [ ] ) <EOL> entries . append ( [ <EOL> method . upper ( ) + '<STR_LIT:U+0020>' + path , <NUM_LIT:0> , info [ <NUM_LIT:0> ] , <EOL> http_resource_anchor ( method , path ) , '<STR_LIT>' , '<STR_LIT>' , info [ <NUM_LIT:1> ] <EOL> ] ) <EOL> content = sorted ( content . items ( ) , key = lambda k : k [ <NUM_LIT:0> ] ) <EOL> return ( content , True ) <EOL> class HTTPDomain ( Domain ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT:http>' <EOL> label = '<STR_LIT>' <EOL> object_types = { <EOL> '<STR_LIT>' : ObjType ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ObjType ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ObjType ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ObjType ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ObjType ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ObjType ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ObjType ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ObjType ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> } <EOL> directives = { <EOL> '<STR_LIT>' : HTTPOptions , <EOL> '<STR_LIT>' : HTTPHead , <EOL> '<STR_LIT>' : HTTPPost , <EOL> '<STR_LIT>' : HTTPGet , <EOL> '<STR_LIT>' : HTTPPut , <EOL> '<STR_LIT>' : HTTPPatch , <EOL> '<STR_LIT>' : HTTPDelete , <EOL> '<STR_LIT>' : HTTPTrace <EOL> } <EOL> roles = { <EOL> '<STR_LIT>' : HTTPXRefRole ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : HTTPXRefRole ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : HTTPXRefRole ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : HTTPXRefRole ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : HTTPXRefRole ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : HTTPXRefRole ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : HTTPXRefRole ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : HTTPXRefRole ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : http_statuscode_role , <EOL> '<STR_LIT>' : http_method_role <EOL> } <EOL> initial_data = { <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } <EOL> } <EOL> indices = [ ] <EOL> @ property <EOL> def routes ( self ) : <EOL> return dict ( ( key , self . data [ key ] ) for key in self . object_types ) <EOL> def clear_doc ( self , docname ) : <EOL> for typ , routes in self . routes . items ( ) : <EOL> for path , info in list ( routes . items ( ) ) : <EOL> if info [ <NUM_LIT:0> ] == docname : <EOL> del routes [ path ] <EOL> def resolve_xref ( self , env , fromdocname , builder , typ , target , <EOL> node , contnode ) : <EOL> try : <EOL> info = self . data [ str ( typ ) ] [ target ] <EOL> except KeyError : <EOL> return <EOL> else : <EOL> anchor = http_resource_anchor ( typ , target ) <EOL> title = typ . upper ( ) + '<STR_LIT:U+0020>' + target <EOL> return make_refnode ( builder , fromdocname , info [ <NUM_LIT:0> ] , anchor , <EOL> contnode , title ) <EOL> def get_objects ( self ) : <EOL> for method , routes in self . routes . items ( ) : <EOL> for path , info in routes . items ( ) : <EOL> anchor = http_resource_anchor ( method , path ) <EOL> yield ( path , path , method , info [ <NUM_LIT:0> ] , anchor , <NUM_LIT:1> ) <EOL> class HTTPLexer ( RegexLexer ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> aliases = [ '<STR_LIT:http>' ] <EOL> flags = re . DOTALL <EOL> def header_callback ( self , match ) : <EOL> if match . group ( <NUM_LIT:1> ) . lower ( ) == '<STR_LIT>' : <EOL> content_type = match . group ( <NUM_LIT:5> ) . strip ( ) <EOL> if '<STR_LIT:;>' in content_type : <EOL> content_type = content_type [ : content_type . find ( '<STR_LIT:;>' ) ] . strip ( ) <EOL> self . content_type = content_type <EOL> yield match . start ( <NUM_LIT:1> ) , Name . Attribute , match . group ( <NUM_LIT:1> ) <EOL> yield match . start ( <NUM_LIT:2> ) , Text , match . group ( <NUM_LIT:2> ) <EOL> yield match . start ( <NUM_LIT:3> ) , Operator , match . group ( <NUM_LIT:3> ) <EOL> yield match . start ( <NUM_LIT:4> ) , Text , match . group ( <NUM_LIT:4> ) <EOL> yield match . start ( <NUM_LIT:5> ) , Literal , match . group ( <NUM_LIT:5> ) <EOL> yield match . start ( <NUM_LIT:6> ) , Text , match . group ( <NUM_LIT:6> ) <EOL> def continuous_header_callback ( self , match ) : <EOL> yield match . start ( <NUM_LIT:1> ) , Text , match . group ( <NUM_LIT:1> ) <EOL> yield match . start ( <NUM_LIT:2> ) , Literal , match . group ( <NUM_LIT:2> ) <EOL> yield match . start ( <NUM_LIT:3> ) , Text , match . group ( <NUM_LIT:3> ) <EOL> def content_callback ( self , match ) : <EOL> content_type = getattr ( self , '<STR_LIT>' , None ) <EOL> content = match . group ( ) <EOL> offset = match . start ( ) <EOL> if content_type : <EOL> from pygments . lexers import get_lexer_for_mimetype <EOL> try : <EOL> lexer = get_lexer_for_mimetype ( content_type ) <EOL> except ClassNotFound : <EOL> pass <EOL> else : <EOL> for idx , token , value in lexer . get_tokens_unprocessed ( content ) : <EOL> yield offset + idx , token , value <EOL> return <EOL> yield offset , Text , content <EOL> tokens = { <EOL> '<STR_LIT:root>' : [ <EOL> ( r'<STR_LIT>' <EOL> r'<STR_LIT>' , <EOL> bygroups ( Name . Function , Text , Name . Namespace , Text , <EOL> Keyword . Reserved , Operator , Number , Text ) , <EOL> '<STR_LIT>' ) , <EOL> ( r'<STR_LIT>' , <EOL> bygroups ( Keyword . Reserved , Operator , Number , Text , Number , <EOL> Text , Name . Exception , Text ) , <EOL> '<STR_LIT>' ) , <EOL> ] , <EOL> '<STR_LIT>' : [ <EOL> ( r'<STR_LIT>' , header_callback ) , <EOL> ( r'<STR_LIT>' , continuous_header_callback ) , <EOL> ( r'<STR_LIT>' , Text , '<STR_LIT:content>' ) <EOL> ] , <EOL> '<STR_LIT:content>' : [ <EOL> ( r'<STR_LIT>' , content_callback ) <EOL> ] <EOL> } <EOL> def setup ( app ) : <EOL> app . add_domain ( HTTPDomain ) <EOL> try : <EOL> get_lexer_by_name ( '<STR_LIT:http>' ) <EOL> except ClassNotFound : <EOL> app . add_lexer ( '<STR_LIT:http>' , HTTPLexer ( ) ) <EOL> app . add_config_value ( '<STR_LIT>' , [ ] , None ) </s>
