-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #180 from stan-dev/feature/179-stan-functions-listing
Feature/179 stan functions listing
- Loading branch information
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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() |