Skip to content

Commit 476b572

Browse files
committed
Add command line parsing to rst_to_pxd.py
1 parent 29fce01 commit 476b572

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

bin/rst_to_pxd.py

100644100755
Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
#!/usr/bin/env python3
2+
13
from collections import defaultdict
24
import re
35
import sys
46
import os
57
import fnmatch
8+
import argparse
69

710
"""
811
This is relatively rudimentary, but works for the purpose intended
@@ -34,10 +37,6 @@
3437
3538
"""
3639

37-
pyflintlibdir = "/Users/davideinstein/projects/python-flint/src/flint/flintlib"
38-
arbdocdir = "/Users/davideinstein/projects/arb/doc/source"
39-
flintdocdir ="/Users/davideinstein/projects/flint2/doc/source"
40-
4140
# recognize a function definition in rst
4241
is_func = re.compile(r"\.\.( )+(c:)?function( )*::")
4342
# rename types to avoid python -- c name collisions
@@ -64,7 +63,7 @@ def get_cython_struct_types(file):
6463
ret.append(l.split()[-1])
6564
return ret
6665

67-
def fill_import_dict():
66+
def fill_import_dict(pyflintlibdir):
6867
"""
6968
Get a map from cython structs to the pxd that defines them
7069
"""
@@ -138,13 +137,13 @@ def gen_imports(function_list):
138137
print("from flint.flintlib." + k + " cimport " + types)
139138
return ret
140139

141-
def generate_pxd_file(h_name):
142-
fill_import_dict()
140+
def generate_pxd_file(h_name, opts):
141+
fill_import_dict(opts.flint_lib_dir)
143142
l=[]
144-
docdir = arbdocdir
143+
docdir = opts.arb_doc_dir
145144
name = h_name
146145
if name[:6] == "flint/":
147-
docdir = flintdocdir
146+
docdir = opts.flint_doc_dir
148147
name = name[6:]
149148
with open(os.path.join(docdir, name + ".rst")) as f:
150149
l = get_functions(f)
@@ -159,7 +158,27 @@ def generate_pxd_file(h_name):
159158
else:
160159
print(" " + f)
161160

161+
162+
def main(*args):
163+
usage = """
164+
$ cd /path/to/python-flint
165+
$ bin/rst_to_pxd.py flint/fmpz --flint-doc-dir=/path/to/flint/doc/source
166+
"""
167+
parser = argparse.ArgumentParser(description='Generate a pxd file from an rst file',
168+
usage=usage)
169+
parser.add_argument('--flint-lib-dir', help='location of the flintlib submodule',
170+
default="./src/flint/flintlib")
171+
parser.add_argument('--arb-doc-dir', help='location of the arb doc source directory',
172+
default="/Users/davideinstein/projects/arb/doc/source")
173+
parser.add_argument('--flint-doc-dir', help='location of the flint doc source directory',
174+
default="/Users/davideinstein/projects/flint2/doc/source")
175+
parser.add_argument('name', help='name of the rst file to parse (e.g. flint/fmpz)')
176+
177+
args = parser.parse_args()
178+
179+
generate_pxd_file(args.name, args)
180+
181+
162182
if __name__ == "__main__":
183+
main(*sys.argv[1:])
163184

164-
name = sys.argv[1]
165-
generate_pxd_file(name)

0 commit comments

Comments
 (0)