summaryrefslogtreecommitdiff
path: root/taurus/doc/source/sphinxext/taurusextension.py
blob: cd7b993fcdbdc3b503961b020c454b69c8002ffe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python

##############################################################################
##
## This file is part of Taurus, a Tango User Interface Library
## 
## http://www.tango-controls.org/static/taurus/latest/doc/html/index.html
##
## Copyright 2011 CELLS / ALBA Synchrotron, Bellaterra, Spain
## 
## Taurus is free software: you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
## 
## Taurus is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU Lesser General Public License for more details.
## 
## You should have received a copy of the GNU Lesser General Public License
## along with Taurus.  If not, see <http://www.gnu.org/licenses/>.
##
##############################################################################

"""helper methods for taurus sphinx documentation"""

__expr = ('or',)

def process_type(t, obj_type='class'):
    t = t.strip()
    if not t: return ''
    if t in __expr: return t
    if t.count(' or '):
       i = t.index(' or ')
       return ' '.join(map(process_type, (t[:i],'or', t[i+4:])))
    if not t.count('<') or not t.count('>'):
        ret = ':%s:~`%s`' % (obj_type, t)
        return ret
    
    #process a container template
    start, stop = t.index('<'), t.index('>')
    main_type = t[:start]
    main_type = process_type(main_type)
    types = t[start+1:stop].split(',')
    types = ', '.join(map(process_type, types))
    return "%s <%s>" % (main_type, types)

def process_param(line):
    new_lines = []
    try:
        prefix, param, desc = line.split(':', 2)
        p, param_name = param.split()
        desc = desc.strip()
        if desc[0] == '(' :
            pos = desc.find(')')
            if pos != -1:
                elem_type = desc[1:pos]
                klass = process_type(elem_type)
                desc = desc[pos+1:]
                new_lines.append('%s:type %s: %s' % (prefix, param_name, klass))
        new_lines.append('%s:param %s: %s' % (prefix, param_name, desc))
    except Exception, e:
        print "Taurus sphinx extension: Not able to process param: '%s'" % line
        print "      Reason:",str(e)
        new_lines.append(line)
    return new_lines

def process_return(line):
    new_lines = []
    try:
        prefix, param, desc = line.split(':', 2)
        desc = desc.strip()
        if desc[0] == '(' :
            pos = desc.find(')')
            if pos != -1:
                elem_type = desc[1:pos]
                klass = process_type(elem_type)
                desc = desc[pos+1:]
                new_lines.append('%s:rtype: %s' % (prefix, klass))
        new_lines.append('%s:return: %s' % (prefix, desc))
    except Exception, e:
        print "TaurusExtension: Not able to process 'return': '%s'" % line
        print "      Reason:",str(e)
        new_lines.append(line)
    return new_lines

def process_raise(line):
    new_lines = []
    try:
        prefix, param, desc = line.split(':', 2)
        desc = desc.strip()
        klass = ''
        if desc[0] == '(' :
            pos = desc.find(')')
            if pos != -1:
                elem_type = desc[1:pos]
                klass = "(" + process_type(elem_type, obj_type='exc') + ")"
                desc = desc[pos+1:]
        new_lines.append('%s:raise: %s %s' % (prefix, klass, desc))
    except Exception, e:
        print "TaurusExtension: Not able to process 'raise': '%s'" % line
        print "      Reason:", str(e)
        new_lines.append(line)
    return new_lines

def _is_return(line):
    ret = line.startswith(':return')
    ret |= line.startswith(':returns')
    return ret

def _is_param(line):
    ret = line.startswith(':param')
    ret |= line.startswith(':parameter')
    ret |= line.startswith(':arg')
    ret |= line.startswith(':argument')
    ret |= line.startswith(':key')
    ret |= line.startswith(':keyword')
    return ret

def _is_raise(line):
    ret = line.startswith(':raise')
    ret |= line.startswith(':except')
    return ret

def process_signature(app, what, name, obj, options, lines):
    ret = []
    for nb, line in enumerate(lines):
        line_strip = line.strip()
        if _is_param(line_strip):
            ret.extend(process_param(line))
        elif _is_return(line_strip):
            ret.extend(process_return(line))
        elif _is_raise(line_strip):
            ret.extend(process_raise(line))
        else:
            ret.append(line)
    
    del lines[:]
    lines.extend(ret)
    
    
def setup(app):
    app.connect('autodoc-process-docstring', process_signature)