<s> import os <EOL> import logging <EOL> import docutils <EOL> from docutils . core import publish_doctree <EOL> from . parameter import Param , List <EOL> from . utility import rst_to_html <EOL> log = logging . getLogger ( __name__ ) <EOL> class Element ( object ) : <EOL> """<STR_LIT>""" <EOL> title = None <EOL> description = None <EOL> response = None <EOL> plugin = None <EOL> param_list = None <EOL> param_dict = None <EOL> param_index = None <EOL> param_default = None <EOL> param_not_null = None <EOL> param_allow_null = None <EOL> param_types = None <EOL> code_index = None <EOL> code_dict = None <EOL> def __init__ ( self , app , view , method ) : <EOL> self . title = '<STR_LIT>' <EOL> self . plugins = [ ] <EOL> self . plugin_keys = [ ] <EOL> self . param_list = [ ] <EOL> self . param_dict = { } <EOL> self . param_index = [ ] <EOL> self . param_default = { } <EOL> self . param_not_null = [ ] <EOL> self . param_allow_null = [ ] <EOL> self . param_types = { } <EOL> self . code_index = [ ] <EOL> self . code_list = [ ] <EOL> self . code_dict = { } <EOL> self . __element_code_set ( '<STR_LIT:success>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . __element_code_set ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . __element_code_set ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not method . __doc__ : <EOL> raise Exception ( '<STR_LIT>' % ( view . __name__ , method . __name__ ) ) <EOL> try : <EOL> doc_field = publish_doctree ( method . __doc__ ) <EOL> self . __parse_doc_tree ( app , view , method , doc_field ) <EOL> self . __clear_up ( app ) <EOL> except Exception : <EOL> log . exception ( '<STR_LIT>' ) <EOL> def __clear_up ( self , app ) : <EOL> if self . param_not_null : <EOL> self . code_index . insert ( <NUM_LIT:2> , '<STR_LIT>' ) <EOL> self . code_list . insert ( <NUM_LIT:2> , ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . code_dict [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . plugins = tuple ( self . plugins ) <EOL> self . plugin_keys = tuple ( self . plugin_keys ) <EOL> self . param_list = tuple ( self . param_list ) <EOL> self . param_not_null = tuple ( self . param_not_null ) <EOL> self . param_allow_null = tuple ( self . param_allow_null ) <EOL> self . param_index = self . param_not_null + self . param_allow_null <EOL> self . code_index = tuple ( self . code_index ) <EOL> self . code_list = tuple ( self . code_list ) <EOL> self . response = self . response or app . config [ '<STR_LIT>' ] [ '<STR_LIT:default>' ] <EOL> self . description = self . description or '<STR_LIT>' <EOL> def __element_code_set ( self , error_code , error_message , category = '<STR_LIT>' ) : <EOL> if error_code not in self . code_index : <EOL> self . code_index . append ( error_code ) <EOL> self . code_list . append ( ( error_code , error_message , category ) ) <EOL> self . code_dict [ error_code ] = error_message <EOL> def __parse_doc_field ( self , app , view , method , doc_field ) : <EOL> name = doc_field . children [ <NUM_LIT:0> ] . astext ( ) <EOL> content = rst_to_html ( doc_field . children [ <NUM_LIT:1> ] . rawsource ) <EOL> if name == '<STR_LIT>' : <EOL> self . response = app . config [ '<STR_LIT>' ] [ content ] <EOL> elif name == '<STR_LIT>' : <EOL> for item in content . split ( ) : <EOL> plugin = app . config [ '<STR_LIT>' ] . get ( item ) <EOL> if not plugin : <EOL> raise Exception ( '<STR_LIT>' % ( view . __name__ , method . __name__ , item ) ) <EOL> self . plugins . append ( plugin ) <EOL> self . plugin_keys . append ( item ) <EOL> for error_code , error_message in plugin . error_codes . items ( ) : <EOL> self . __element_code_set ( error_code , error_message , '<STR_LIT>' + item ) <EOL> elif name . startswith ( '<STR_LIT>' ) : <EOL> self . __element_code_set ( name [ <NUM_LIT:6> : ] , content ) <EOL> elif name . startswith ( '<STR_LIT>' ) : <EOL> items = name [ <NUM_LIT:6> : ] . split ( ) <EOL> param_type = items [ <NUM_LIT:0> ] <EOL> if param_type . endswith ( '<STR_LIT:]>' ) : <EOL> sub_type = app . config [ '<STR_LIT>' ] . get ( param_type . split ( '<STR_LIT:[>' ) [ <NUM_LIT:1> ] [ : - <NUM_LIT:1> ] ) <EOL> param_type = app . config [ '<STR_LIT>' ] . get ( param_type . split ( '<STR_LIT:[>' ) [ <NUM_LIT:0> ] ) <EOL> param_type = param_type ( sub_type ) <EOL> else : <EOL> param_type = app . config [ '<STR_LIT>' ] . get ( param_type ) <EOL> if not param_type : <EOL> raise Exception ( '<STR_LIT>' % ( self . __name__ , method . __name__ , items [ <NUM_LIT:0> ] ) ) <EOL> if method . __name__ . upper ( ) not in param_type . support : <EOL> raise Exception ( '<STR_LIT>' % <EOL> ( self . __name__ , method . __name__ , param_type . __name__ , method . __name__ . upper ( ) ) ) <EOL> param = { '<STR_LIT:name>' : items [ - <NUM_LIT:1> ] , '<STR_LIT:type>' : param_type , '<STR_LIT>' : False , '<STR_LIT:description>' : content } <EOL> if len ( items ) > <NUM_LIT:2> and items [ <NUM_LIT:1> ] == '<STR_LIT:*>' : <EOL> param [ '<STR_LIT>' ] = True <EOL> self . param_not_null . append ( items [ - <NUM_LIT:1> ] ) <EOL> else : <EOL> self . param_allow_null . append ( items [ - <NUM_LIT:1> ] ) <EOL> self . param_list . append ( param ) <EOL> self . param_dict [ items [ - <NUM_LIT:1> ] ] = param <EOL> self . param_default [ items [ - <NUM_LIT:1> ] ] = None <EOL> self . param_types [ items [ - <NUM_LIT:1> ] ] = param_type <EOL> if isinstance ( param [ '<STR_LIT:type>' ] , List ) : <EOL> self . __element_code_set ( param_type . type . error_code , param_type . type . description , '<STR_LIT:type>' ) <EOL> self . __element_code_set ( param_type . error_code , param_type . description % param_type . type . __name__ , '<STR_LIT:type>' ) <EOL> elif param [ '<STR_LIT:type>' ] != Param : <EOL> self . __element_code_set ( param_type . error_code , param_type . description , '<STR_LIT:type>' ) <EOL> else : <EOL> setattr ( self , name , content ) <EOL> def __parse_doc_tree ( self , app , view , method , doc_tree ) : <EOL> if type ( doc_tree ) == docutils . nodes . term : <EOL> self . title = rst_to_html ( doc_tree . rawsource ) <EOL> return <EOL> if type ( doc_tree ) == docutils . nodes . paragraph : <EOL> if self . description is None : <EOL> self . title = self . title + rst_to_html ( doc_tree . rawsource ) <EOL> self . description = '<STR_LIT>' <EOL> elif self . description == '<STR_LIT>' : <EOL> self . description = rst_to_html ( doc_tree . rawsource ) <EOL> else : <EOL> self . description = self . description + os . linesep * <NUM_LIT:2> + rst_to_html ( doc_tree . rawsource ) <EOL> return <EOL> if type ( doc_tree ) == docutils . nodes . field : <EOL> self . __parse_doc_field ( app , view , method , doc_tree ) <EOL> return <EOL> for item in doc_tree . children : <EOL> self . __parse_doc_tree ( app , view , method , item ) </s>
