<s> """<STR_LIT>""" <EOL> from docutils import nodes <EOL> from docutils . parsers . rst . directives . tables import Table <EOL> from docutils . statemachine import ViewList <EOL> from sphinx . util . compat import Directive <EOL> import yaml <EOL> def full_name ( cls ) : <EOL> return cls . __module__ + '<STR_LIT:.>' + cls . __name__ <EOL> class rest_method ( nodes . Part , nodes . Element ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class RestMethodDirective ( Directive ) : <EOL> has_content = True <EOL> def run ( self ) : <EOL> lineno = self . state_machine . abs_line_number ( ) <EOL> target = nodes . target ( ) <EOL> section = nodes . section ( classes = [ "<STR_LIT>" ] ) <EOL> node = rest_method ( ) <EOL> method , sep , url = self . content [ <NUM_LIT:0> ] . partition ( '<STR_LIT:U+0020>' ) <EOL> node [ '<STR_LIT>' ] = method <EOL> node [ '<STR_LIT:url>' ] = url <EOL> node [ '<STR_LIT:target>' ] = self . state . parent . attributes [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> temp_target = "<STR_LIT>" % node [ '<STR_LIT:target>' ] <EOL> target = nodes . target ( ids = [ temp_target ] ) <EOL> self . state . add_target ( temp_target , '<STR_LIT>' , target , lineno ) <EOL> section += node <EOL> return [ target , section ] <EOL> class RestParametersDirective ( Table ) : <EOL> headers = [ "<STR_LIT:Name>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> def yaml_from_file ( self , fpath ) : <EOL> """<STR_LIT>""" <EOL> with open ( fpath , '<STR_LIT:r>' ) as stream : <EOL> try : <EOL> lookup = yaml . load ( stream ) <EOL> except yaml . YAMLError as exc : <EOL> self . app . warn ( exc ) <EOL> raise <EOL> content = "<STR_LIT:\n>" . join ( self . content ) <EOL> parsed = yaml . load ( content ) <EOL> new_content = list ( ) <EOL> for paramlist in parsed : <EOL> for name , ref in paramlist . items ( ) : <EOL> new_content . append ( ( name , lookup [ ref ] ) ) <EOL> self . yaml = new_content <EOL> def run ( self ) : <EOL> env = self . state . document . settings . env <EOL> self . app = env . app <EOL> if not self . content : <EOL> error = self . state_machine . reporter . error ( <EOL> '<STR_LIT>' , <EOL> nodes . literal_block ( self . block_text , self . block_text ) , <EOL> line = self . lineno ) <EOL> return [ error ] <EOL> if not len ( self . arguments ) >= <NUM_LIT:1> : <EOL> self . state_machine . reporter . error ( <EOL> '<STR_LIT>' , <EOL> nodes . literal_block ( self . block_text , self . block_text ) , <EOL> line = self . lineno ) <EOL> return [ error ] <EOL> rel_fpath , fpath = env . relfn2path ( self . arguments . pop ( ) ) <EOL> self . yaml_from_file ( fpath ) <EOL> self . max_cols = len ( self . headers ) <EOL> self . options [ '<STR_LIT>' ] = ( <NUM_LIT:20> , <NUM_LIT:10> , <NUM_LIT:10> , <NUM_LIT> ) <EOL> self . col_widths = self . get_column_widths ( self . max_cols ) <EOL> title , messages = self . make_title ( ) <EOL> table_node = self . build_table ( ) <EOL> self . add_name ( table_node ) <EOL> if title : <EOL> table_node . insert ( <NUM_LIT:0> , title ) <EOL> return [ table_node ] + messages <EOL> def get_rows ( self , table_data ) : <EOL> rows = [ ] <EOL> groups = [ ] <EOL> trow = nodes . row ( ) <EOL> entry = nodes . entry ( ) <EOL> para = nodes . paragraph ( text = unicode ( table_data ) ) <EOL> entry += para <EOL> trow += entry <EOL> rows . append ( trow ) <EOL> return rows , groups <EOL> def collect_rows ( self ) : <EOL> def add_col ( value ) : <EOL> entry = nodes . entry ( ) <EOL> result = ViewList ( value . split ( '<STR_LIT:\n>' ) ) <EOL> self . state . nested_parse ( result , <NUM_LIT:0> , entry ) <EOL> return entry <EOL> rows = [ ] <EOL> groups = [ ] <EOL> try : <EOL> for key , values in self . yaml : <EOL> min_version = values . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> desc = values . get ( '<STR_LIT:description>' , '<STR_LIT>' ) <EOL> classes = [ ] <EOL> if min_version : <EOL> desc += ( "<STR_LIT>" % min_version ) <EOL> min_ver_css_name = ( "<STR_LIT>" + <EOL> str ( min_version ) . replace ( '<STR_LIT:.>' , '<STR_LIT:_>' ) ) <EOL> classes . append ( min_ver_css_name ) <EOL> trow = nodes . row ( classes = classes ) <EOL> name = key <EOL> if values . get ( '<STR_LIT>' ) is True : <EOL> name += "<STR_LIT>" <EOL> trow += add_col ( name ) <EOL> trow += add_col ( values . get ( '<STR_LIT>' ) ) <EOL> trow += add_col ( values . get ( '<STR_LIT:type>' ) ) <EOL> trow += add_col ( desc ) <EOL> rows . append ( trow ) <EOL> except AttributeError as exc : <EOL> self . app . warn ( "<STR_LIT>" % <EOL> ( key , values , exc ) ) <EOL> raise <EOL> return rows , groups <EOL> def build_table ( self ) : <EOL> table = nodes . table ( ) <EOL> tgroup = nodes . tgroup ( cols = len ( self . headers ) ) <EOL> table += tgroup <EOL> tgroup . extend ( <EOL> nodes . colspec ( colwidth = col_width , colname = '<STR_LIT:c>' + str ( idx ) ) <EOL> for idx , col_width in enumerate ( self . col_widths ) <EOL> ) <EOL> thead = nodes . thead ( ) <EOL> tgroup += thead <EOL> row_node = nodes . row ( ) <EOL> thead += row_node <EOL> row_node . extend ( nodes . entry ( h , nodes . paragraph ( text = h ) ) <EOL> for h in self . headers ) <EOL> tbody = nodes . tbody ( ) <EOL> tgroup += tbody <EOL> rows , groups = self . collect_rows ( ) <EOL> tbody . extend ( rows ) <EOL> table . extend ( groups ) <EOL> return table <EOL> def rest_method_html ( self , node ) : <EOL> tmpl = """<STR_LIT>""" <EOL> self . body . append ( tmpl % node ) <EOL> raise nodes . SkipNode <EOL> def resolve_rest_references ( app , doctree ) : <EOL> for node in doctree . traverse ( ) : <EOL> if isinstance ( node , rest_method ) : <EOL> rest_node = node <EOL> rest_method_section = node . parent <EOL> rest_section = rest_method_section . parent <EOL> gp = rest_section . parent <EOL> rest_section . attributes [ '<STR_LIT>' ] . append ( '<STR_LIT>' ) <EOL> rest_section . attributes [ '<STR_LIT>' ] . append ( '<STR_LIT>' ) <EOL> title = rest_section . children . pop ( <NUM_LIT:0> ) <EOL> rest_node [ '<STR_LIT>' ] = title . children [ <NUM_LIT:0> ] <EOL> rest_section . attributes [ '<STR_LIT>' ] [ <NUM_LIT:0> ] = ( <EOL> "<STR_LIT>" % rest_section . attributes [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ) <EOL> rest_method_section . attributes [ '<STR_LIT>' ] [ <NUM_LIT:0> ] = rest_node [ '<STR_LIT:target>' ] <EOL> idx = gp . children . index ( rest_section ) <EOL> rest_section . remove ( rest_method_section ) <EOL> gp . insert ( idx , rest_method_section ) <EOL> def setup ( app ) : <EOL> app . add_node ( rest_method , <EOL> html = ( rest_method_html , None ) ) <EOL> app . add_directive ( '<STR_LIT>' , RestParametersDirective ) <EOL> app . add_directive ( '<STR_LIT>' , RestMethodDirective ) <EOL> app . add_stylesheet ( '<STR_LIT>' ) <EOL> app . add_stylesheet ( '<STR_LIT>' ) <EOL> app . add_javascript ( '<STR_LIT>' ) <EOL> app . add_javascript ( '<STR_LIT>' ) <EOL> app . connect ( '<STR_LIT>' , resolve_rest_references ) <EOL> return { '<STR_LIT:version>' : '<STR_LIT>' } </s>
