Statistics
| Branch: | Tag: | Revision:

mininet / util / doxify.py @ 083322a2

History | View | Annotate | Download (1.97 KB)

1
#!/usr/bin/python
2

    
3
"""
4
Convert simple documentation to epydoc/pydoctor-compatible markup
5
"""
6

    
7
from sys import stdin, stdout, argv
8
import os
9
from tempfile import mkstemp
10
from subprocess import call
11

    
12
import re
13

    
14
spaces = re.compile( r'\s+' )
15
singleLineExp = re.compile( r'\s+"([^"]+)"' )
16
commentStartExp = re.compile( r'\s+"""' )
17
commentEndExp = re.compile( r'"""$' )
18
returnExp = re.compile( r'\s+(returns:.*)' )
19
lastindent = ''
20

    
21

    
22
comment = False
23

    
24
def fixParam( line ):
25
    "Change foo: bar to @foo bar"
26
    result = re.sub( r'(\w+):', r'@param \1', line )
27
    result = re.sub( r'   @', r'@', result)
28
    return result
29

    
30
def fixReturns( line ):
31
    "Change returns: foo to @return foo"
32
    return re.sub( 'returns:', r'@returns', line )
33
    
34
def fixLine( line ):
35
    global comment
36
    match = spaces.match( line )
37
    if not match:
38
        return line
39
    else:
40
        indent = match.group(0)
41
    if singleLineExp.match( line ):
42
        return re.sub( '"', '"""', line )
43
    if commentStartExp.match( line ):
44
        comment = True
45
    if comment:
46
        line = fixReturns( line )
47
        line = fixParam( line )
48
    if commentEndExp.search( line ):
49
        comment = False
50
    return line
51

    
52

    
53
def test():
54
    "Test transformations"
55
    assert fixLine(' "foo"') == ' """foo"""'
56
    assert fixParam( 'foo: bar' ) == '@param foo bar'
57
    assert commentStartExp.match( '   """foo"""')
58

    
59
def funTest():
60
    testFun = (
61
    'def foo():\n'
62
    '   "Single line comment"\n'
63
    '   """This is a test"""\n'
64
    '      bar: int\n'
65
    '      baz: string\n'
66
    '      returns: junk"""\n'
67
    '   if True:\n'
68
    '       print "OK"\n'
69
    ).splitlines( True )
70

    
71
    fixLines( testFun )
72
    
73
def fixLines( lines, fid ):
74
    for line in lines:
75
        os.write( fid, fixLine( line ) )
76

    
77
if __name__ == '__main__':
78
    if False:
79
        funTest()
80
    infile = open( argv[1] )
81
    outfid, outname = mkstemp()
82
    fixLines( infile.readlines(), outfid )
83
    infile.close()
84
    os.close( outfid )
85
    call( [ 'doxypy', outname ] )
86

    
87

    
88

    
89