11import json
22import click
33
4- HTTP_VERBS = (' GET' , ' POST' , ' HEAD' , ' OPTIONS' , ' PUT' , ' PATCH' , ' DELETE' )
4+ HTTP_VERBS = (" GET" , " POST" , " HEAD" , " OPTIONS" , " PUT" , " PATCH" , " DELETE" )
55
66
77def get_single_endpoint_detail (lines ):
@@ -10,38 +10,42 @@ def get_single_endpoint_detail(lines):
1010 "method" : str (),
1111 "description" : str (),
1212 "request_body" : str (),
13- "response_body" : str ()
13+ "response_body" : str (),
1414 }
1515 lines_iterator = iter (lines )
1616 for line in lines_iterator :
17- if not line or line == ' ```' :
17+ if not line or line == " ```" :
1818 continue
19- if line .startswith ('##' ):
20- endpoint_details ["description" ] = line .split (' ## ' )[1 ]
19+ if line .startswith ("##" ):
20+ endpoint_details ["description" ] = line .split (" ## " )[1 ]
2121 continue
2222 if line .startswith (HTTP_VERBS ):
23- method , endpoint = line .split (' ' )[:2 ]
23+ method , endpoint = line .split (" " )[:2 ]
2424 endpoint_details ["endpoint" ] = endpoint
2525 endpoint_details ["method" ] = method
2626 continue
27- if line .startswith (' __Example__' ):
27+ if line .startswith (" __Example__" ):
2828 json_data = parse_and_get_json_from_subsequent_lines (lines_iterator )
2929 try :
3030 endpoint_details ["request_body" ] = json .loads (json_data )
3131 except ValueError as e :
32- print ("Error in parsing request_body of {}: {}" .format (
33- endpoint_details ['endpoint' ], e )
32+ print (
33+ "Error in parsing request_body of {}: {}" .format (
34+ endpoint_details ["endpoint" ], e
35+ )
3436 )
3537 print ("Invalid JSON: {}" .format (json_data ))
3638 return None
3739 continue
38- if line .startswith (' __Response__' ):
40+ if line .startswith (" __Response__" ):
3941 json_data = parse_and_get_json_from_subsequent_lines (lines_iterator )
4042 try :
4143 endpoint_details ["response_body" ] = json .loads (json_data )
4244 except ValueError as e :
43- print ("Error in parsing response_body of {}: {}" .format (
44- endpoint_details ['endpoint' ], e )
45+ print (
46+ "Error in parsing response_body of {}: {}" .format (
47+ endpoint_details ["endpoint" ], e
48+ )
4549 )
4650 print ("Invalid JSON: {}" .format (json_data ))
4751 return None
@@ -53,31 +57,31 @@ def parse_and_get_json_from_subsequent_lines(lines_iterator):
5357 try :
5458 next_line = next (lines_iterator )
5559 except StopIteration :
56- return ''
57- while next_line != ' ```json' :
60+ return ""
61+ while next_line != " ```json" :
5862 try :
5963 next_line = next (lines_iterator )
6064 except StopIteration :
61- return ''
65+ return ""
6266 # Skip the row having starting json tag
6367 next_line = next (lines_iterator )
6468 array_of_json_statements = list ()
65- while next_line != ' ```' :
69+ while next_line != " ```" :
6670 array_of_json_statements .append (next_line )
6771 try :
6872 next_line = next (lines_iterator )
6973 except StopIteration :
7074 pass
7175
72- json_statements = '' .join (array_of_json_statements )
76+ json_statements = "" .join (array_of_json_statements )
7377 json_statements = json_statements .replace ("..." , "" )
7478 return json_statements
7579
7680
7781def get_json_from_endpoints (lines ):
78- all_lines = lines .split (' \n ' )
82+ all_lines = lines .split (" \n " )
7983 next_endpoint_starting_location = [
80- i for i , line in enumerate (all_lines ) if line .startswith ('##' )
84+ i for i , line in enumerate (all_lines ) if line .startswith ("##" )
8185 ]
8286 endpoint_json_list = list ()
8387 for x in range (len (next_endpoint_starting_location )):
@@ -95,16 +99,20 @@ def get_json_from_endpoints(lines):
9599
96100
97101@click .command ()
98- @click .option ('--file-path' , default = 'proposed_endpoints.md' , help = 'Path for the proposed endpoints docs' )
102+ @click .option (
103+ "--file-path" ,
104+ default = "proposed_endpoints.md" ,
105+ help = "Path for the proposed endpoints docs" ,
106+ )
99107def generate_json_from_docs_file (file_path ):
100108 lines = None
101- with open (file_path , 'r' ) as endpoint_file :
109+ with open (file_path , "r" ) as endpoint_file :
102110 lines = endpoint_file .read ()
103111 json_data = get_json_from_endpoints (lines )
104112
105- with open (' endpoints_data.json' , 'w' ) as json_file :
113+ with open (" endpoints_data.json" , "w" ) as json_file :
106114 json .dump (json_data , json_file , indent = 4 )
107115
108116
109- if __name__ == ' __main__' :
117+ if __name__ == " __main__" :
110118 generate_json_from_docs_file ()
0 commit comments