docs/test2rst.py
author Pierre-Yves David <pierre-yves.david@ens-lyon.org>
Thu, 02 Mar 2017 20:23:18 +0100
changeset 2035 94fe2cc9cd41
parent 525 a0327c78a5d3
child 2825 7608f1e04205
permissions -rw-r--r--
flake8: fix error in 'test2rst.py' There is variosu dead code in there :-/ but we just fix flake8 complains for now.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 re
94fe2cc9cd41 flake8: fix error in 'test2rst.py'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 525
diff changeset
     6
import sys
235
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
     7
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
     8
# line starts with two chars one of which is not a space (and both are not
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
     9
# newlines obviously) and ends with one or more newlines followed by two spaces
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    10
# on a next line (indented text)
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    11
CODEBLOCK = re.compile(r'()\n(([^ \n][^\n]|[^\n][^ \n])[^\n]*)\n+  ')
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    12
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    13
INDEX = '''
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    14
Mercurial tests
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
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    17
.. toctree::
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    18
   :maxdepth: 1
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    19
'''
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    20
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    21
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    22
def rstify(orig, name):
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    23
    header = '%s\n%s\n\n' % (name, '=' * len(name))
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    24
    content = header + orig
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    25
    content = CODEBLOCK.sub(r'\n\1\n\n::\n\n  ', content)
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    26
    return content
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    27
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    28
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    29
def main(base):
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    30
    if os.path.isdir(base):
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    31
        one_dir(base)
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    32
    else:
525
a0327c78a5d3 doc: remove spurious print in test2rest.py
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 235
diff changeset
    33
        one_file(base)
235
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    34
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    35
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    36
def one_dir(base):
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    37
    index = INDEX
2035
94fe2cc9cd41 flake8: fix error in 'test2rst.py'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 525
diff changeset
    38
    # 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
    39
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    40
    for fn in sorted(os.listdir(base)):
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    41
        if not fn.endswith('.t'):
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    42
            continue
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    43
        name = os.path.splitext(fn)[0]
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    44
        content = one_file(op.join(base, fn))
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    45
        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
    46
        # 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
    47
        with open(target, 'w') as f:
235
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    48
            f.write(content)
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    49
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    50
        index += '\n   ' + name
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    51
2035
94fe2cc9cd41 flake8: fix error in 'test2rst.py'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 525
diff changeset
    52
    # 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
    53
    #     f.write(index)
235
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    54
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    55
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    56
def one_file(path):
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    57
    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
    58
    return rstify(open(path).read(), name)
235
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    59
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
if __name__ == '__main__':
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    62
    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
    63
        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
    64
        sys.exit()
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
    65
    main(sys.argv[1])