Created
March 1, 2018 15:53
-
-
Save crogersdev/768ca5c0aa6bd172f65139f96eb20f42 to your computer and use it in GitHub Desktop.
Parse Flask API file and make documentation (WIP)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
from codecs import open | |
import re | |
parser = argparse.ArgumentParser(description='Read API file, parse out web routes and input parameters, methods, etc... and generate a doc file') | |
parser.add_argument('--file', dest='source_api_file', help='API Source file', required=True) | |
input_args = parser.parse_args() | |
route_pattern_single_line = r"""^\@api.*?\(+["'](.*?)["'],?""" | |
route_pattern_multi_line_mid = r"""^\s+['"](.*?)['"][\),]\s?""" | |
methods_pattern = r'''methods=\[(.*?)\]''' | |
params_pattern0 = r"""request_json.pop\('?(.*?)'?, '?(.*?)'?\)""" | |
params_pattern1 = r"""request.json\[['"](.*?)['"]\]""" | |
params_pattern2 = r"""request.args.*?\('?(.*?)'?, '?(.*?)'?\)""" | |
params_pattern3 = r"""request\.args\.get.*?\(["'](.*?)["']\)""" | |
with open(input_args.source_api_file, 'r', encoding='utf-8') as src_file: | |
for line in src_file: | |
line = line.rstrip('\r\n') | |
m = re.search(route_pattern_single_line, line) | |
if m: | |
print m.group(1), | |
m = re.search(route_pattern_multi_line_mid, line) | |
if m: | |
print m.group(1), | |
m = re.search(methods_pattern, line) | |
if m: | |
print "--", m.group(1).replace("'", "") | |
m = re.search(params_pattern0, line) | |
if m: | |
if '=' in m.group(2): | |
default = m.group(2).split('=')[1] | |
else: | |
default = m.group(2) | |
print "\tparam:", m.group(1) + ",", "default:", default | |
m = re.search(params_pattern1, line) | |
if m: | |
print "\tparam:", m.group(1) + ",", "default: None" | |
m = re.search(params_pattern2, line) | |
if m: | |
if '=' in m.group(2): | |
default = m.group(2).split('=')[1] | |
else: | |
default = m.group(2) | |
print "\tparam:", m.group(1) + ",", "default:", default | |
m = re.search(params_pattern3, line) | |
if m: | |
if '=' in m.group(1): | |
arg = m.group(1).split('=')[1] | |
else: | |
arg = m.group(1) | |
print "\tparam:", arg + ",", "default: None" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment