Skip to content

Commit

Permalink
scrape, format functions signatures from Rmd
Browse files Browse the repository at this point in the history
  • Loading branch information
mitzimorris committed Apr 27, 2020
1 parent 6b20c36 commit 7ec8511
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions extract_function_sigs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env python
"""
Extract function signatures from html comments in markdown.
"""

import glob
import os
import os.path
import sys
import contextlib


@contextlib.contextmanager
def pushd(new_dir):
previous_dir = os.getcwd()
os.chdir(new_dir)
yield
os.chdir(previous_dir)


def main():
if len(sys.argv) > 2:
stan_major = int(sys.argv[1])
stan_minor = int(sys.argv[2])
else:
print('Expecting 2 arguments <MAJOR> <MINOR> version numbers')
sys.exit(1)

sigs = set()
ref_dir = os.path.join('src', 'functions-reference')
with pushd(ref_dir):
for file in glob.glob('*.Rmd'):
print(file)
with open(file) as rmd_file:
lines = rmd_file.readlines()
for line in lines:
if line.startswith('<!-- '):
line = line.lstrip('<!- ')
parts = [x.strip(' ~') for x in line.split(';')]
if len(parts) == 3:
parts[1] = parts[1]
sigs.add('{}; ~; {}'.format(parts[1], parts[0]))
elif len(parts) == 4:
sigs.add('{}; {}; {}'.format(parts[1], parts[2], parts[0]))
else:
print('not a function sig: {}'.format(line))

outfile_name = 'stan-fuctions-{}_{}.txt'.format(str(stan_major), str(stan_minor))
with open(outfile_name, 'w') as outfile:
outfile.write('# This file is semicolon delimited\n')
outfile.write('StanFunction; Arguments; ReturnType\n')
for sig in sorted(sigs):
outfile.write(sig)
outfile.write('\n')


if __name__ == '__main__':
main()

2 comments on commit 7ec8511

@kaz-yos
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mitzimorris,
Thank you so much for this! I just realized the output filename is misspelled. It should be "stan-functions-" not "stan-fuctions". This is perhaps too trivial for a pull request, so I'm posting here.

@mitzimorris
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi Kaz, good catch - just fixed this - put the "fun" back in "functions" - many thanks!

Please sign in to comment.