author | Boris Feld <boris.feld@octobus.net> |
Tue, 29 Aug 2017 16:42:40 +0200 | |
changeset 2950 | 1b4c92621e23 |
parent 2825 | 7608f1e04205 |
child 2951 | 2ddc63d13af8 |
permissions | -rw-r--r-- |
235
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
1 |
#!/usr/bin/env python |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
2 |
|
2035
94fe2cc9cd41
flake8: fix error in 'test2rst.py'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
525
diff
changeset
|
3 |
import os |
94fe2cc9cd41
flake8: fix error in 'test2rst.py'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
525
diff
changeset
|
4 |
import os.path as op |
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 |
INDEX = ''' |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
8 |
Mercurial tests |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
9 |
=============== |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
10 |
|
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
11 |
.. toctree:: |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
12 |
:maxdepth: 1 |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
13 |
''' |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
14 |
|
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
15 |
|
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
16 |
def rstify(orig, name): |
2825 | 17 |
newlines = [] |
18 |
||
19 |
code_block_mode = False |
|
2950
1b4c92621e23
doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents:
2825
diff
changeset
|
20 |
sphinx_directive_mode = False |
2825 | 21 |
|
22 |
for line in orig.splitlines(): |
|
23 |
||
24 |
# Emtpy lines doesn't change output |
|
25 |
if not line: |
|
26 |
newlines.append(line) |
|
2950
1b4c92621e23
doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents:
2825
diff
changeset
|
27 |
code_block_mode = False |
1b4c92621e23
doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents:
2825
diff
changeset
|
28 |
sphinx_directive_mode = False |
1b4c92621e23
doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents:
2825
diff
changeset
|
29 |
continue |
1b4c92621e23
doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents:
2825
diff
changeset
|
30 |
|
1b4c92621e23
doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents:
2825
diff
changeset
|
31 |
# Ignore line |
1b4c92621e23
doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents:
2825
diff
changeset
|
32 |
if line.endswith('#ignore'): |
2825 | 33 |
continue |
34 |
||
2950
1b4c92621e23
doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents:
2825
diff
changeset
|
35 |
# Sphinx directives mode |
1b4c92621e23
doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents:
2825
diff
changeset
|
36 |
if line.startswith(' .. '): |
1b4c92621e23
doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents:
2825
diff
changeset
|
37 |
|
1b4c92621e23
doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents:
2825
diff
changeset
|
38 |
# Insert a empty line to makes sphinx happy |
1b4c92621e23
doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents:
2825
diff
changeset
|
39 |
newlines.append("") |
1b4c92621e23
doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents:
2825
diff
changeset
|
40 |
|
1b4c92621e23
doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents:
2825
diff
changeset
|
41 |
# And unindent the directive |
1b4c92621e23
doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents:
2825
diff
changeset
|
42 |
line = line[2:] |
1b4c92621e23
doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents:
2825
diff
changeset
|
43 |
sphinx_directive_mode = True |
1b4c92621e23
doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents:
2825
diff
changeset
|
44 |
|
1b4c92621e23
doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents:
2825
diff
changeset
|
45 |
# Code mode |
2825 | 46 |
codeline = line.startswith(' ') |
2950
1b4c92621e23
doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents:
2825
diff
changeset
|
47 |
if codeline and not sphinx_directive_mode: |
2825 | 48 |
if code_block_mode is False: |
49 |
newlines.extend(['::', '']) |
|
50 |
||
51 |
code_block_mode = True |
|
52 |
||
53 |
newlines.append(line) |
|
54 |
||
55 |
return "\n".join(newlines) |
|
235
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
56 |
|
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
57 |
|
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
58 |
def main(base): |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
59 |
if os.path.isdir(base): |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
60 |
one_dir(base) |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
61 |
else: |
525
a0327c78a5d3
doc: remove spurious print in test2rest.py
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
235
diff
changeset
|
62 |
one_file(base) |
235
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
63 |
|
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
64 |
|
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
65 |
def one_dir(base): |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
66 |
index = INDEX |
2035
94fe2cc9cd41
flake8: fix error in 'test2rst.py'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
525
diff
changeset
|
67 |
# doc = lambda x: op.join(op.dirname(__file__), 'docs', x) |
235
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
68 |
|
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
69 |
for fn in sorted(os.listdir(base)): |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
70 |
if not fn.endswith('.t'): |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
71 |
continue |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
72 |
name = os.path.splitext(fn)[0] |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
73 |
content = one_file(op.join(base, fn)) |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
74 |
target = op.join(base, name + '.rst') |
2035
94fe2cc9cd41
flake8: fix error in 'test2rst.py'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
525
diff
changeset
|
75 |
# with file(doc(name + '.rst'), 'w') as f: |
94fe2cc9cd41
flake8: fix error in 'test2rst.py'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
525
diff
changeset
|
76 |
with open(target, 'w') as f: |
235
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
77 |
f.write(content) |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
78 |
|
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
79 |
index += '\n ' + name |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
80 |
|
2035
94fe2cc9cd41
flake8: fix error in 'test2rst.py'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
525
diff
changeset
|
81 |
# with file(doc('index.rst'), 'w') as f: |
94fe2cc9cd41
flake8: fix error in 'test2rst.py'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
525
diff
changeset
|
82 |
# f.write(index) |
235
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
83 |
|
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
84 |
|
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
85 |
def one_file(path): |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
86 |
name = os.path.basename(path)[:-2] |
2035
94fe2cc9cd41
flake8: fix error in 'test2rst.py'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
525
diff
changeset
|
87 |
return rstify(open(path).read(), name) |
235
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
88 |
|
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
89 |
|
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
90 |
if __name__ == '__main__': |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
91 |
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
|
92 |
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
|
93 |
sys.exit() |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
94 |
main(sys.argv[1]) |