#!/usr/bin/env python3
import sys
def emit(s):
sys.stdout.write(s)
def str2hex(s):
return ":".join("{:02x}".format(ord(c)) for c in s)
def funchdr(line):
if len(line) > 1 and (not line.startswith(" ")) and line.find("(") != -1:
return True
else:
return False
def get_line(f):
line = f.readline()
line = line.replace("<", "<")
line = line.replace(">", ">")
return line
def get_file():
try:
fn = sys.argv[1]
f = open(fn, "r")
except:
exit("aborting, can't open {}".format(fn))
return f
NONE = 0
DESCRIPTION = 1
OVERVIEW = 2
CLASSES = 3
CLASS = 4
FUNC = 5
XREF = 6
DATA = 7
f = get_file()
param_used = []
param_defd = []
param_refd = []
at = NONE
in_code = False
in_samp = False
in_table = False
left = 0
while True:
line = get_line(f)
if line == "":
for p in param_used:
if p not in param_defd:
sys.stderr.write("{} is used but not defined.\n".format(p))
for p in param_defd:
if p not in param_used and p not in param_refd:
sys.stderr.write("{} is defined but not used.\n".format(p))
break
if line.startswith("DESCRIPTION"):
left = 4
at = DESCRIPTION
continue
elif line.startswith(" OVERVIEW"):
left = 4
emit("
OVERVIEW
")
emit(
"")
at = OVERVIEW
continue
elif line.startswith("CLASSES"):
left = 4
emit("
")
at = NONE
continue
elif line.startswith(" class "):
in_func = False
if line.startswith(" class error"):
at = NONE
else:
left = 8
emit("{}
".format(line))
_class = line[10:-1]
at = CLASS
continue
elif line.startswith("FUNCTIONS"):
left = 4
emit("FUNCTIONS
")
in_func = False
at = FUNC
continue
elif line.startswith(" xref()"):
left = 8
emit("PARAMETERS
")
in_func = False
last_par = ""
at = XREF
continue
elif line.startswith("DATA"):
at = DATA
continue
line = line[left:]
at_funchdr = funchdr(line)
if at != NONE and at != OVERVIEW:
if at == CLASS or at == FUNC:
if not at_funchdr:
line = line[4:]
line = line.replace(' \n', '
')
if line.find('@') != -1:
if not in_table:
in_table = True
emit("")
emit("")
cols = line.split('@')
for col in cols:
emit("{} | ".format(col.strip()))
emit("
")
continue
else:
if in_table:
in_table = False
emit("
")
if line.find(":=") != -1:
if not in_samp:
emit("
Parameters
")
in_samp = True
if line == '...\n' or line == '. .\n':
if in_code:
in_code = False
emit("")
else:
in_code = True
if line == '...\n':
emit("Example
")
emit("")
continue
if line == '\n' or line == '':
if in_samp:
emit("
")
in_samp = False
emit('
')
if not in_code:
emit('
')
continue
if in_code or in_samp:
line = line.replace(" ", " ")
line = line.replace("\n", "
")
while line.find('[*') != -1 and line.find('*]') != -1:
(b, s, e) = line.partition('[*')
(l, s, e) = e.partition('*]')
line = '{}{}{}'.format(b, l, l, e)
if l not in param_refd:
param_refd.append(l)
while line.find('[[') != -1 and line.find(']]') != -1:
(b, s, e) = line.partition('[[')
(l, s, e) = e.partition(']]')
line = '{}{}{}'.format(b, l, l, e)
if at == DESCRIPTION:
if line[0] == '*' and line[-2] == '*':
emit("{}
".format(line[1:-2]))
elif line[0] == '[' and line[-2] == ']':
pass
else:
emit(line)
elif at == OVERVIEW:
if line == "\n":
emit(" | |
")
else:
(func, sep, desc) = line.partition(' ')
if desc != '':
emit("{} | {} |
".
format(func, func, desc))
else:
emit("{} | |
".
format(func).replace("_", " "))
elif at == CLASS or at == FUNC:
if at_funchdr:
in_func = True
if in_code:
emit("
***C***")
in_code = False
if in_samp:
emit("
***S***")
in_samp = False
(func, sep1, end1) = line.partition("(")
(parl, sep2, end2) = end1.partition(")")
pars = parl.split(",")
func = func.strip()
if at == FUNC:
func = "pigpio." + func
else:
if func == "__init__":
func = "pigpio." + _class
emit("\n')
else:
if in_func:
emit(line)
elif at == XREF:
if line.find(':') != -1:
(par, sep, end) = line.partition(':')
par = par.strip()
end = end.strip()
emit("{}: {}
".format(par, par, end))
if par.lower() < last_par.lower():
sys.stderr.write("Out of order {} after {}.\n".
format(par, last_par))
last_par = par
if par in param_defd:
sys.stderr.write("Duplicate definition of {}.\n".format(par))
else:
param_defd.append(par)
else:
emit(line)
f.close()