Import PyWsdlGen, a simple Python WSDL generator
[pywsdlgen.git] / pywsdlgen / parser_intro.py
blob5889b901180699a953a7bf642363160ecc837eec
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 # Author: Enrico Tröger
5 # License: LGPL
9 import imp
10 import inspect
11 import re
12 import sys
13 from parser import Parser
16 class ParserIntrospection(Parser):
17 """
18 Parser based on introspection code
19 """
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):
30 """
31 Verify the found tag name and if it is valid, add it to the list
33 @param tagname (str)
34 @param args (list)
35 """
36 args_dict = { 'return': '' }
37 if args:
38 for arg in args:
39 # ignore the self argument
40 if arg != 'self':
41 # for now, we don't know yet the type of the argument, so don't set it
42 args_dict[arg] = ''
43 self.tags[tagname] = args_dict
45 #----------------------------------------------------------------------
46 def _parse_doc_string(self, method_name, input):
47 """
48 Parse the given doc string for arguments and argument types
50 @param method_name (str)
51 @param input (str)
52 """
53 if input:
54 lines = input.splitlines()
55 for line in lines:
56 m = self.re_doc_string.match(line)
57 if m:
58 f1, f2, f3 = m.groups()
59 if f2 and f2[0] == '(':
60 arg_name = f3
61 arg_type = f2
62 else:
63 arg_name = f2
64 arg_type = f3
66 arg_type = self.re_clean_args.sub('', arg_type)
67 if f1 == 'return':
68 arg_name = 'return'
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):
75 """
76 Read the file specified by filename and look for class and function definitions
78 @param filename (str)
79 """
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):
89 continue
91 try:
92 args = inspect.getargspec(m_obj).args
93 except:
94 args = ''
95 self._add_tag(m_name, args)
96 self._parse_doc_string(m_name, m_obj.__doc__)