<s> """<STR_LIT>""" <EOL> import inspect <EOL> import yaml <EOL> import re <EOL> from collections import defaultdict <EOL> def _sanitize ( comment ) : <EOL> return comment . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) if comment else comment <EOL> def _parse_docstring ( obj , process_doc ) : <EOL> first_line , other_lines , swag = None , None , None <EOL> full_doc = inspect . getdoc ( obj ) <EOL> if full_doc : <EOL> line_feed = full_doc . find ( '<STR_LIT:\n>' ) <EOL> if line_feed != - <NUM_LIT:1> : <EOL> first_line = process_doc ( full_doc [ : line_feed ] ) <EOL> yaml_sep = full_doc [ line_feed + <NUM_LIT:1> : ] . find ( '<STR_LIT>' ) <EOL> if yaml_sep != - <NUM_LIT:1> : <EOL> other_lines = process_doc ( full_doc [ line_feed + <NUM_LIT:1> : line_feed + yaml_sep ] ) <EOL> swag = yaml . load ( full_doc [ line_feed + yaml_sep : ] ) <EOL> else : <EOL> other_lines = process_doc ( full_doc [ line_feed + <NUM_LIT:1> : ] ) <EOL> else : <EOL> first_line = full_doc <EOL> return first_line , other_lines , swag <EOL> def _extract_definitions ( alist , level = None ) : <EOL> """<STR_LIT>""" <EOL> def _extract_array_defs ( source ) : <EOL> ret = [ ] <EOL> items = source . get ( '<STR_LIT>' ) <EOL> if items is not None and '<STR_LIT>' in items : <EOL> ret += _extract_definitions ( [ items ] , level + <NUM_LIT:1> ) <EOL> return ret <EOL> if level is None : <EOL> level = <NUM_LIT:0> <EOL> defs = list ( ) <EOL> if alist is not None : <EOL> for item in alist : <EOL> schema = item . get ( "<STR_LIT>" ) <EOL> if schema is not None : <EOL> schema_id = schema . get ( "<STR_LIT:id>" ) <EOL> if schema_id is not None : <EOL> defs . append ( schema ) <EOL> ref = { "<STR_LIT>" : "<STR_LIT>" . format ( schema_id ) } <EOL> if level == <NUM_LIT:0> : <EOL> item [ '<STR_LIT>' ] = ref <EOL> else : <EOL> item . update ( ref ) <EOL> del item [ '<STR_LIT>' ] <EOL> properties = schema . get ( '<STR_LIT>' ) <EOL> if properties is not None : <EOL> defs += _extract_definitions ( properties . values ( ) , level + <NUM_LIT:1> ) <EOL> defs += _extract_array_defs ( schema ) <EOL> defs += _extract_array_defs ( item ) <EOL> return defs <EOL> def swagger ( app , process_doc = _sanitize , template = None ) : <EOL> """<STR_LIT>""" <EOL> output = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:info>" : { <EOL> "<STR_LIT:version>" : "<STR_LIT>" , <EOL> "<STR_LIT:title>" : "<STR_LIT>" , <EOL> } <EOL> } <EOL> paths = defaultdict ( dict ) <EOL> definitions = defaultdict ( dict ) <EOL> if template is not None : <EOL> output . update ( template ) <EOL> for k , v in output . get ( '<STR_LIT>' , { } ) . items ( ) : <EOL> paths [ k ] = v <EOL> for k , v in output . get ( '<STR_LIT>' , { } ) . items ( ) : <EOL> definitions [ k ] = v <EOL> output [ "<STR_LIT>" ] = paths <EOL> output [ "<STR_LIT>" ] = definitions <EOL> ignore_verbs = { "<STR_LIT>" , "<STR_LIT>" } <EOL> optional_fields = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:produces>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> for rule in app . url_map . iter_rules ( ) : <EOL> endpoint = app . view_functions [ rule . endpoint ] <EOL> methods = dict ( ) <EOL> for verb in rule . methods . difference ( ignore_verbs ) : <EOL> if hasattr ( endpoint , '<STR_LIT>' ) and verb in endpoint . methods : <EOL> verb = verb . lower ( ) <EOL> methods [ verb ] = getattr ( endpoint . view_class , verb ) <EOL> else : <EOL> methods [ verb . lower ( ) ] = endpoint <EOL> operations = dict ( ) <EOL> for verb , method in methods . items ( ) : <EOL> summary , description , swag = _parse_docstring ( method , process_doc ) <EOL> if swag is not None : <EOL> defs = swag . get ( '<STR_LIT>' , [ ] ) <EOL> defs = _extract_definitions ( defs ) <EOL> params = swag . get ( '<STR_LIT>' , [ ] ) <EOL> defs += _extract_definitions ( params ) <EOL> responses = swag . get ( '<STR_LIT>' , { } ) <EOL> responses = { <EOL> str ( key ) : value <EOL> for key , value in responses . items ( ) <EOL> } <EOL> if responses is not None : <EOL> defs = defs + _extract_definitions ( responses . values ( ) ) <EOL> for definition in defs : <EOL> def_id = definition . pop ( '<STR_LIT:id>' ) <EOL> if def_id is not None : <EOL> definitions [ def_id ] . update ( definition ) <EOL> operation = dict ( <EOL> summary = summary , <EOL> description = description , <EOL> responses = responses <EOL> ) <EOL> if len ( params ) > <NUM_LIT:0> : <EOL> operation [ '<STR_LIT>' ] = params <EOL> for key in optional_fields : <EOL> if key in swag : <EOL> operation [ key ] = swag . get ( key ) <EOL> operations [ verb ] = operation <EOL> if len ( operations ) : <EOL> rule = str ( rule ) <EOL> for arg in re . findall ( '<STR_LIT>' , rule ) : <EOL> rule = rule . replace ( arg [ <NUM_LIT:0> ] , '<STR_LIT>' % arg [ <NUM_LIT:2> ] ) <EOL> paths [ rule ] . update ( operations ) <EOL> return output </s>
