2 # -*- coding: utf-8 -*-
4 # Author: Enrico Tröger
13 from parser
import Parser
16 class ParserIntrospection(Parser
):
18 Parser based on introspection code
21 #----------------------------------------------------------------------
22 def __init__(self
, **kw
):
23 super(ParserIntrospection
, self
).__init
__(**kw
)
25 self
.re_doc_string
= re
.compile('^[ \t]*@(param|return)[ \t]+(.*)[ \t]+(.*).*')
26 self
.re_clean_args
= re
.compile('(\(|\)|[ \t])')
28 #----------------------------------------------------------------------
29 def _add_tag(self
, tagname
, args
):
31 Verify the found tag name and if it is valid, add it to the list
36 # add two lists, one for the arguments and the other one for the argument types
37 # a dcitionary would be much better but then we will loose the argument order
38 # TODO use a lift of tuples
41 # TODO find a more elegant way to create an empty list of X elements
43 for x
in xrange(0, args_len
+ 1):
46 self
.tags
[tagname
] = [ args
, arg_types
]
48 #----------------------------------------------------------------------
49 def _parse_doc_string(self
, method_name
, input):
51 Parse the given doc string for arguments and argument types
53 @param method_name (str)
57 lines
= input.splitlines()
59 m
= self
.re_doc_string
.match(line
)
61 f1
, f2
, f3
= m
.groups()
62 if f2
and f2
[0] == '(':
69 arg_type
= self
.re_clean_args
.sub('', arg_type
)
75 idx
= self
.tags
[method_name
][0].index(arg_name
)
76 self
.tags
[method_name
][1][idx
] = arg_type
80 #----------------------------------------------------------------------
81 def process_file(self
, filename
):
83 Read the file specified by filename and look for class and function definitions
87 module
= imp
.load_source('pywsdlgen', filename
)
88 symbols
= inspect
.getmembers(module
, callable)
89 for name
, obj
in symbols
:
90 if inspect
.isclass(obj
):
91 self
.class_name
= obj
.__name
__
92 methods
= inspect
.getmembers(obj
, inspect
.ismethod
)
93 for m_name
, m_obj
in methods
:
94 # skip non-public tags and non-methods
95 if m_name
.startswith('_') or not inspect
.ismethod(m_obj
):
99 args
= inspect
.getargspec(m_obj
).args
102 self
._add
_tag
(m_name
, args
)
103 self
._parse
_doc
_string
(m_name
, m_obj
.__doc
__)