annotate docs/test2rst.py @ 5660:9d6c3e227455 stable

test2rst: use python3 Seems to just work, no porting necessary. To sanity-check the script, I've used some non-ascii symbols in the .t files (utf-8) and things worked fine.
author Anton Shestakov <av6@dwimlabs.net>
date Mon, 09 Nov 2020 23:10:06 +0800
parents 5ea837c515d9
children 1d80cda7fe93
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5660
9d6c3e227455 test2rst: use python3
Anton Shestakov <av6@dwimlabs.net>
parents: 5659
diff changeset
1 #!/usr/bin/env python3
235
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
2
5656
60ce376919c5 test2rst: make one_file() useful
Anton Shestakov <av6@dwimlabs.net>
parents: 4801
diff changeset
3 import os
2960
1a4f26eec0af test2rst: ignore the #if/#endif line in the output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2959
diff changeset
4 import re
2035
94fe2cc9cd41 flake8: fix error in 'test2rst.py'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 525
diff changeset
5 import sys
235
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
6
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
7
2960
1a4f26eec0af test2rst: ignore the #if/#endif line in the output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2959
diff changeset
8 ignored_patterns = [
4801
16c1398b0063 python3: prefix all regex to work with python 2 and 3
Raphaël Gomès <rgomes@octobus.net>
parents: 2960
diff changeset
9 re.compile(r'^#if'),
16c1398b0063 python3: prefix all regex to work with python 2 and 3
Raphaël Gomès <rgomes@octobus.net>
parents: 2960
diff changeset
10 re.compile(r'^#else'),
16c1398b0063 python3: prefix all regex to work with python 2 and 3
Raphaël Gomès <rgomes@octobus.net>
parents: 2960
diff changeset
11 re.compile(r'^#endif'),
16c1398b0063 python3: prefix all regex to work with python 2 and 3
Raphaël Gomès <rgomes@octobus.net>
parents: 2960
diff changeset
12 re.compile(r'#rest-ignore$'),
2960
1a4f26eec0af test2rst: ignore the #if/#endif line in the output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2959
diff changeset
13 ]
1a4f26eec0af test2rst: ignore the #if/#endif line in the output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2959
diff changeset
14
235
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
15
5656
60ce376919c5 test2rst: make one_file() useful
Anton Shestakov <av6@dwimlabs.net>
parents: 4801
diff changeset
16 def rstify(orig):
2825
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
17 newlines = []
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
18
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
19 code_block_mode = False
2959
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
20 sphinx_directive_mode = False
2825
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
21
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
22 for line in orig.splitlines():
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
23
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
24 # Emtpy lines doesn't change output
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
25 if not line:
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
26 newlines.append(line)
2959
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
27 code_block_mode = False
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
28 sphinx_directive_mode = False
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
29 continue
2958
1cb715257130 doc: add a special flag for content to ignore in the rst
Boris Feld <boris.feld@octobus.net>
parents: 2951
diff changeset
30
2960
1a4f26eec0af test2rst: ignore the #if/#endif line in the output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2959
diff changeset
31 ignored = False
1a4f26eec0af test2rst: ignore the #if/#endif line in the output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2959
diff changeset
32 for pattern in ignored_patterns:
1a4f26eec0af test2rst: ignore the #if/#endif line in the output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2959
diff changeset
33 if pattern.search(line):
1a4f26eec0af test2rst: ignore the #if/#endif line in the output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2959
diff changeset
34 ignored = True
1a4f26eec0af test2rst: ignore the #if/#endif line in the output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2959
diff changeset
35 break
1a4f26eec0af test2rst: ignore the #if/#endif line in the output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2959
diff changeset
36 if ignored:
2825
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
37 continue
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
38
2959
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
39 # Sphinx directives mode
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
40 if line.startswith(' .. '):
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
41
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
42 # Insert a empty line to makes sphinx happy
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
43 newlines.append("")
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
44
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
45 # And unindent the directive
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
46 line = line[2:]
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
47 sphinx_directive_mode = True
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
48
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
49 # Code mode
2825
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
50 codeline = line.startswith(' ')
2959
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
51 if codeline and not sphinx_directive_mode:
2825
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
52 if code_block_mode is False:
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
53 newlines.extend(['::', ''])
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
54
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
55 code_block_mode = True
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
56
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
57 newlines.append(line)
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
58
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
59 return "\n".join(newlines)
235
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
60
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
61
5659
5ea837c515d9 test2rst: now that we don't use directory mode, remove it too
Anton Shestakov <av6@dwimlabs.net>
parents: 5657
diff changeset
62 def main(path):
5656
60ce376919c5 test2rst: make one_file() useful
Anton Shestakov <av6@dwimlabs.net>
parents: 4801
diff changeset
63 with open(path) as f:
60ce376919c5 test2rst: make one_file() useful
Anton Shestakov <av6@dwimlabs.net>
parents: 4801
diff changeset
64 content = f.read()
60ce376919c5 test2rst: make one_file() useful
Anton Shestakov <av6@dwimlabs.net>
parents: 4801
diff changeset
65 rst = rstify(content)
60ce376919c5 test2rst: make one_file() useful
Anton Shestakov <av6@dwimlabs.net>
parents: 4801
diff changeset
66 target = os.path.splitext(path)[0] + '.rst'
60ce376919c5 test2rst: make one_file() useful
Anton Shestakov <av6@dwimlabs.net>
parents: 4801
diff changeset
67 with open(target, 'w') as f:
60ce376919c5 test2rst: make one_file() useful
Anton Shestakov <av6@dwimlabs.net>
parents: 4801
diff changeset
68 f.write(rst)
235
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
69
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
70
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
71 if __name__ == '__main__':
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
72 if len(sys.argv) != 2:
2035
94fe2cc9cd41 flake8: fix error in 'test2rst.py'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 525
diff changeset
73 print('Please supply a path to tests dir as parameter')
235
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
74 sys.exit()
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
75 main(sys.argv[1])