<s> """<STR_LIT>""" <EOL> import re <EOL> from urlparse import urlsplit <EOL> from docutils . nodes import literal , strong , Text <EOL> from docutils . parsers . rst import directives <EOL> from sphinx . locale import l_ , _ <EOL> from sphinx . directives import ObjectDescription <EOL> from sphinx . util . docfields import TypedField <EOL> from sphinx_http_domain . docfields import NoArgGroupedField , ResponseField <EOL> from sphinx_http_domain . nodes import ( desc_http_method , desc_http_url , <EOL> desc_http_path , desc_http_patharg , <EOL> desc_http_query , desc_http_queryparam , <EOL> desc_http_fragment , desc_http_response ) <EOL> from sphinx_http_domain . utils import slugify , slugify_url <EOL> try : <EOL> from urlparse import parse_qsl <EOL> except ImportError : <EOL> from cgi import parse_qsl <EOL> class HTTPDescription ( ObjectDescription ) : <EOL> def get_anchor ( self , name , sig ) : <EOL> """<STR_LIT>""" <EOL> return self . typ + '<STR_LIT:->' + self . get_id ( name , sig ) <EOL> def get_entry ( self , name , sig ) : <EOL> """<STR_LIT>""" <EOL> return name <EOL> def get_id ( self , name , sig ) : <EOL> """<STR_LIT>""" <EOL> return name <EOL> def add_target_and_index ( self , name , sig , signode ) : <EOL> """<STR_LIT>""" <EOL> anchor = self . get_anchor ( name , sig ) <EOL> id = self . get_id ( name , sig ) <EOL> self . add_target ( anchor = anchor , entry = self . get_entry ( name , sig ) , <EOL> id = id , sig = sig , signode = signode ) <EOL> self . add_index ( anchor = anchor , name = name , sig = sig ) <EOL> def add_target ( self , anchor , id , entry , sig , signode ) : <EOL> """<STR_LIT>""" <EOL> if anchor not in self . state . document . ids : <EOL> signode [ '<STR_LIT>' ] . append ( anchor ) <EOL> signode [ '<STR_LIT>' ] . append ( anchor ) <EOL> signode [ '<STR_LIT>' ] = ( not self . names ) <EOL> self . state . document . note_explicit_target ( signode ) <EOL> data = self . env . domaindata [ '<STR_LIT:http>' ] [ self . typ ] <EOL> if id in data : <EOL> otherdocname = data [ id ] [ <NUM_LIT:0> ] <EOL> self . env . warn ( <EOL> self . env . docname , <EOL> '<STR_LIT>' % sig + <EOL> '<STR_LIT>' + <EOL> self . env . doc2path ( otherdocname ) + <EOL> '<STR_LIT>' , <EOL> self . lineno <EOL> ) <EOL> data [ id ] = entry <EOL> def add_index ( self , anchor , name , sig ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplemented <EOL> class HTTPMethod ( HTTPDescription ) : <EOL> """<STR_LIT>""" <EOL> typ = '<STR_LIT>' <EOL> nodetype = literal <EOL> option_spec = { <EOL> '<STR_LIT>' : directives . flag , <EOL> '<STR_LIT:title>' : directives . unchanged , <EOL> '<STR_LIT>' : directives . unchanged , <EOL> } <EOL> doc_field_types = [ <EOL> TypedField ( '<STR_LIT>' , label = l_ ( '<STR_LIT>' ) , <EOL> names = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> typenames = ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> can_collapse = True ) , <EOL> TypedField ( '<STR_LIT>' , label = l_ ( '<STR_LIT>' ) , <EOL> names = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> typenames = ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> typerolename = '<STR_LIT>' , <EOL> can_collapse = True ) , <EOL> TypedField ( '<STR_LIT>' , label = l_ ( '<STR_LIT>' ) , <EOL> names = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> typenames = ( '<STR_LIT>' , ) , <EOL> can_collapse = True ) , <EOL> TypedField ( '<STR_LIT>' , label = l_ ( '<STR_LIT>' ) , <EOL> names = ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> typenames = ( '<STR_LIT>' , ) , <EOL> can_collapse = True ) , <EOL> ResponseField ( '<STR_LIT>' , label = l_ ( '<STR_LIT>' ) , <EOL> names = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> typerolename = '<STR_LIT>' , <EOL> can_collapse = True ) <EOL> ] <EOL> sig_re = re . compile ( <EOL> ( <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> ) , <EOL> re . IGNORECASE <EOL> ) <EOL> path_re = re . compile ( <EOL> ( <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> ) , <EOL> re . VERBOSE <EOL> ) <EOL> def node_from_method ( self , method ) : <EOL> """<STR_LIT>""" <EOL> if method is None : <EOL> method = '<STR_LIT:GET>' <EOL> return desc_http_method ( method , method . upper ( ) ) <EOL> def node_from_url ( self , url ) : <EOL> """<STR_LIT>""" <EOL> if url is None : <EOL> raise ValueError <EOL> path , query , fragment = self . split_url ( url ) <EOL> urlnode = desc_http_url ( ) <EOL> urlnode += self . node_from_path ( path ) <EOL> node = self . node_from_query ( query ) <EOL> if node : <EOL> urlnode += node <EOL> node = self . node_from_fragment ( fragment ) <EOL> if node : <EOL> urlnode += node <EOL> return urlnode <EOL> def node_from_path ( self , path ) : <EOL> """<STR_LIT>""" <EOL> if path : <EOL> pathnode = desc_http_path ( path ) <EOL> path_segments = self . path_re . findall ( path ) [ : - <NUM_LIT:1> ] <EOL> for text , arg in path_segments : <EOL> pathnode += Text ( text ) <EOL> if arg : <EOL> arg = arg [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> pathnode += desc_http_patharg ( arg , arg ) <EOL> return pathnode <EOL> else : <EOL> raise ValueError <EOL> def node_from_query ( self , query ) : <EOL> """<STR_LIT>""" <EOL> if query : <EOL> querynode = desc_http_query ( query ) <EOL> query_params = query . split ( '<STR_LIT:&>' ) <EOL> for p in query_params : <EOL> querynode += desc_http_queryparam ( p , p ) <EOL> return querynode <EOL> def node_from_fragment ( self , fragment ) : <EOL> """<STR_LIT>""" <EOL> if fragment : <EOL> return desc_http_fragment ( fragment , fragment ) <EOL> def split_url ( self , url ) : <EOL> """<STR_LIT>""" <EOL> _ , _ , path , query , fragment = urlsplit ( url ) <EOL> return ( path , query , fragment ) <EOL> def handle_signature ( self , sig , signode ) : <EOL> """<STR_LIT>""" <EOL> m = self . sig_re . match ( sig ) <EOL> if m is None : <EOL> raise ValueError <EOL> method , url = m . groups ( ) <EOL> signode += self . node_from_method ( method ) <EOL> signode += self . node_from_url ( url ) <EOL> name = self . options . get ( '<STR_LIT>' , <EOL> slugify_url ( method . lower ( ) + '<STR_LIT:->' + url ) ) <EOL> title = self . options . get ( '<STR_LIT:title>' , sig ) <EOL> return ( method . upper ( ) , url , name , title ) <EOL> def get_entry ( self , name , sig ) : <EOL> """<STR_LIT>""" <EOL> method , _ , _ , title = name <EOL> return ( self . env . docname , sig , title , method ) <EOL> def get_id ( self , name , sig ) : <EOL> """<STR_LIT>""" <EOL> return name [ <NUM_LIT:2> ] <EOL> def add_index ( self , anchor , name , sig ) : <EOL> """<STR_LIT>""" <EOL> method , url , id , title = name <EOL> if title != sig : <EOL> self . indexnode [ '<STR_LIT>' ] . append ( ( '<STR_LIT>' , <EOL> _ ( "<STR_LIT>" ) % title , <EOL> anchor , anchor ) ) <EOL> self . indexnode [ '<STR_LIT>' ] . append ( <EOL> ( '<STR_LIT>' , <EOL> _ ( "<STR_LIT>" ) % { '<STR_LIT>' : method , <EOL> '<STR_LIT:url>' : url } , <EOL> anchor , anchor ) <EOL> ) <EOL> class HTTPResponse ( HTTPDescription ) : <EOL> """<STR_LIT>""" <EOL> typ = '<STR_LIT>' <EOL> nodetype = strong <EOL> option_spec = { <EOL> '<STR_LIT>' : directives . flag , <EOL> } <EOL> doc_field_types = [ <EOL> TypedField ( '<STR_LIT:data>' , label = l_ ( '<STR_LIT>' ) , <EOL> names = ( '<STR_LIT:data>' , ) , <EOL> typenames = ( '<STR_LIT>' , '<STR_LIT:type>' ) , <EOL> typerolename = '<STR_LIT>' , <EOL> can_collapse = True ) , <EOL> NoArgGroupedField ( '<STR_LIT>' , label = l_ ( '<STR_LIT>' ) , <EOL> names = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> can_collapse = True ) , <EOL> ] <EOL> def handle_signature ( self , sig , signode ) : <EOL> """<STR_LIT>""" <EOL> name = slugify ( sig ) <EOL> signode += desc_http_response ( name , sig ) <EOL> return name <EOL> def get_entry ( self , name , sig ) : <EOL> return ( self . env . docname , sig , sig ) <EOL> def add_index ( self , anchor , name , sig ) : <EOL> """<STR_LIT>""" <EOL> self . indexnode [ '<STR_LIT>' ] . append ( ( '<STR_LIT>' , <EOL> _ ( "<STR_LIT>" ) % sig , <EOL> anchor , anchor ) ) <EOL> self . indexnode [ '<STR_LIT>' ] . append ( ( '<STR_LIT>' , <EOL> _ ( "<STR_LIT>" ) % sig , <EOL> anchor , anchor ) ) </s>
