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 args_dict
= { 'return': '' }
39 # ignore the self argument
41 # for now, we don't know yet the type of the argument, so don't set it
43 self
.tags
[tagname
] = args_dict
45 #----------------------------------------------------------------------
46 def _parse_doc_string(self
, method_name
, input):
48 Parse the given doc string for arguments and argument types
50 @param method_name (str)
54 lines
= input.splitlines()
56 m
= self
.re_doc_string
.match(line
)
58 f1
, f2
, f3
= m
.groups()
59 if f2
and f2
[0] == '(':
66 arg_type
= self
.re_clean_args
.sub('', arg_type
)
70 if arg_type
and (arg_name
in self
.tags
[method_name
]):
71 self
.tags
[method_name
][arg_name
] = arg_type
73 #----------------------------------------------------------------------
74 def process_file(self
, filename
):
76 Read the file specified by filename and look for class and function definitions
80 module
= imp
.load_source('pywsdlgen', filename
)
81 symbols
= inspect
.getmembers(module
, callable)
82 for name
, obj
in symbols
:
83 if inspect
.isclass(obj
):
84 self
.class_name
= obj
.__name
__
85 methods
= inspect
.getmembers(obj
, inspect
.ismethod
)
86 for m_name
, m_obj
in methods
:
87 # skip non-public tags and non-methods
88 if m_name
.startswith('_') or not inspect
.ismethod(m_obj
):
92 args
= inspect
.getargspec(m_obj
).args
95 self
._add
_tag
(m_name
, args
)
96 self
._parse
_doc
_string
(m_name
, m_obj
.__doc
__)