Finalize 1.7.6 release.
[ginac.git] / ginsh / ginsh_op_help.py
1 #!/usr/bin/env python
2 # encoding: utf-8
3 # Convert help for ginsh operators from man page to C source
4 import sys, re, optparse
5
6 rxStart = re.compile('^.*GINSH_OP_HELP_START$')
7 rxEnd = re.compile('^.*GINSH_OP_HELP_END$')
8 fcnRx = re.compile('^[.]B\s+')
9 minusRx = re.compile('\\\\[-]')
10 codeFmt = 'insert_help("operators", "%s\\t" "%s");\n'
11
12 def extractHelp(inp, out):
13         sym, synopsis = None, None
14         seenStart = False
15         for l in inp:
16                 l = l.strip()
17                 if not seenStart:
18                         if rxStart.match(l):
19                                 seenStart = True
20                         continue
21                 if rxEnd.match(l):
22                         if sym is not None:
23                                 out.write(codeFmt % ( sym, synopsis ))
24                         break
25                 if fcnRx.match(l):
26                         l = fcnRx.sub('', l)
27                         sym = minusRx.sub('-', l)
28                 elif l.lower() == '.tp':
29                         out.write(codeFmt % ( sym, synopsis ))
30                         sym, synopsis = None, None
31                 else:
32                         synopsis = l
33
34 def main():
35         op = optparse.OptionParser()
36         op.add_option('-o', dest = 'outfile')
37         options, args = op.parse_args()
38         outfile = sys.stdout
39         infile = sys.stdin
40         if not options.outfile is None:
41                 outfile = open(options.outfile, 'wt')
42         if len(args) >= 1:
43                 infile = open(args[0])
44         extractHelp(infile, outfile)
45         if infile != sys.stdin:
46                 infile.close()
47         outfile.flush()
48         if outfile != sys.stdout:
49                 outfile.close()
50
51 if __name__ == "__main__":
52         main()
53         sys.exit(0)
54