-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
help_dat.dat automatic handling (#998)
* help_dat.dat automatic handling * support python2 pickling * windows pathlib fix Co-authored-by: ramcdougal <robert.mcdougal@yale.edu>
- Loading branch information
1 parent
cf3a61f
commit 3e3f4b8
Showing
8 changed files
with
117 additions
and
69 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
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 |
---|---|---|
|
@@ -37,3 +37,4 @@ src/nrnoc/syn.c | |
src/nrnoc/vclmp.c | ||
src/oc/parse.c | ||
src/oc/parse.h | ||
share/lib/python/neuron/help_data.dat |
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
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
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
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 |
---|---|---|
@@ -1,76 +1,96 @@ | ||
""" | ||
Convert rst to a compressed dictionary suitable for NEURON + Python | ||
help system. | ||
Convert rst to a compressed dictionary suitable for NEURON + Python help system. | ||
Run via: | ||
python parse_rst.py `find . -name \*.rst -print` | ||
>>> python3 parse_rst.py ./python/ help_data.dat | ||
""" | ||
|
||
import os | ||
import sys | ||
from pathlib import Path | ||
|
||
help_dictionary = {} | ||
|
||
filenames = sys.argv[1:] | ||
|
||
def handle_identifier(lines, i, identifier): | ||
identifier = '.. %s::' % identifier | ||
line = lines[i] | ||
start = line.find(identifier) | ||
#print line, identifier, start | ||
#print identifier | ||
if start >= 0: | ||
name = line[start + len(identifier):].strip() | ||
class ParseRst(object): | ||
|
||
print '%s -- %s' % (name, identifier) | ||
help_dictionary = {} | ||
|
||
def __init__(self, rst_path, out_file): | ||
self._rst_path = rst_path | ||
self._out_file = out_file | ||
self._filenames = Path(self._rst_path).glob('**/*.rst') | ||
|
||
@classmethod | ||
def handle_identifier(cls, lines, i, identifier): | ||
identifier = '.. %s::' % identifier | ||
line = lines[i] | ||
start = line.find(identifier) | ||
#print line, identifier, start | ||
#print identifier | ||
if start >= 0: | ||
name = line[start + len(identifier):].strip() | ||
|
||
#print('%s -- %s' % (name, identifier)) | ||
|
||
indent_line = lines[i + 1] | ||
while not indent_line.strip(): | ||
i += 1 | ||
indent_line = lines[i + 1] | ||
start = 0 | ||
while start < len(indent_line): | ||
if indent_line[start] != ' ': break | ||
start += 1 | ||
|
||
# TODO: store the body text | ||
body = [] | ||
while i < len(lines) - 1: | ||
i += 1 | ||
if lines[i].strip(): | ||
if lines[i][:start] == indent_line[:start]: | ||
next_line = lines[i][start:] | ||
if next_line[-1] == '\n': | ||
next_line = next_line[: -1] | ||
body.append(next_line) | ||
while not indent_line.strip(): | ||
i += 1 | ||
indent_line = lines[i + 1] | ||
start = 0 | ||
while start < len(indent_line): | ||
if indent_line[start] != ' ': break | ||
start += 1 | ||
|
||
# TODO: store the body text | ||
body = [] | ||
while i < len(lines) - 1: | ||
i += 1 | ||
if lines[i].strip(): | ||
if lines[i][:start] == indent_line[:start]: | ||
next_line = lines[i][start:] | ||
if next_line[-1] == '\n': | ||
next_line = next_line[: -1] | ||
body.append(next_line) | ||
else: | ||
break | ||
else: | ||
break | ||
else: | ||
if not body or body[-1] != '\n': | ||
body.append('\n') | ||
help_dictionary[name] = '\n'.join(body) | ||
|
||
|
||
for filename in filenames: | ||
with open(filename) as f: | ||
lines = [] | ||
for line in f: | ||
if line[-1] == '\n': | ||
line = line[:-1] | ||
lines.append(line) | ||
|
||
i = 0 | ||
while i < len(lines): | ||
for kind in ['method', 'data', 'class', 'function']: | ||
handle_identifier(lines, i, kind) | ||
i += 1 | ||
|
||
import cPickle | ||
import zlib | ||
compressed = zlib.compress(cPickle.dumps(help_dictionary)) | ||
try: | ||
import os | ||
os.mkdir('_build') | ||
except: | ||
# directory already exists, so nothing to do | ||
pass | ||
with open('_build/help_data.dat', 'wb') as f: | ||
f.write(compressed) | ||
if not body or body[-1] != '\n': | ||
body.append('\n') | ||
cls.help_dictionary[name] = '\n'.join(body) | ||
|
||
def parse(self): | ||
for filename in self._filenames: | ||
with open(str(filename)) as f: | ||
lines = [] | ||
for line in f: | ||
if line[-1] == '\n': | ||
line = line[:-1] | ||
lines.append(line) | ||
i = 0 | ||
while i < len(lines): | ||
for kind in ['method', 'data', 'class', 'function']: | ||
ParseRst.handle_identifier(lines, i, kind) | ||
i += 1 | ||
|
||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) == 3: | ||
rst_path = sys.argv[1] | ||
out_file = sys.argv[2] | ||
else: | ||
print('usage: python3 parse_rst.py <rst_folder_path> <output_file>') | ||
exit(1) | ||
|
||
try: | ||
ParseRst(rst_path, out_file).parse() | ||
with open(out_file, 'wb') as f: | ||
import pickle | ||
import zlib | ||
|
||
# TODO - protocol parameter shall be dropped along with Python2 support | ||
compressed = zlib.compress(pickle.dumps(ParseRst.help_dictionary, protocol=2)) | ||
f.write(compressed) | ||
except Exception: | ||
import traceback | ||
|
||
print(traceback.format_exc()) | ||
exit(1) |
Binary file not shown.
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