summaryrefslogtreecommitdiff
path: root/suds/plugin.py
blob: ca0826fb57c92e0a9ef8684cd2cab24d57c6e274 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# This program is free software; you can redistribute it and/or modify
# it under the terms of the (LGPL) 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.
#
# This program 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 Library Lesser General Public License for more details at
# ( http://www.gnu.org/licenses/lgpl.html ).
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# written by: Jeff Ortel ( jortel@redhat.com )

"""
The plugin module provides classes for implementation
of suds plugins.
"""

from suds import *
from logging import getLogger

log = getLogger(__name__)


class Context(object):
    """
    Plugin context.
    """
    pass


class InitContext(Context):
    """
    Init Context.
    @ivar wsdl: The wsdl.
    @type wsdl: L{wsdl.Definitions}
    """
    pass


class LoadContext(Context):
    """
    The XSD load context.
    @ivar root: The loaded xsd document root.
    @type root: L{sax.Element}
    """
    pass
        
        
class SendContext(Context):
    """
    The context for sending the soap envelope.
    @ivar envelope: The soap envelope I{root} element to be sent.
    @type envelope: L{sax.Element}
    """
    pass
        
        
class ReplyContext(Context):
    """
    The context for the text received as a reply
    to method invocation.
    @ivar reply: The received text.
    @type reply: unicode
    """
    pass
    


class Plugin:
    """
    The base class for suds plugins.
    All plugins should implement this interface.
    """
    
    def initialized(self, context):
        """
        Suds initialization.
        Called after wsdl the has been loaded.  Provides the plugin
        with the opportunity to inspect/modify the WSDL.
        @param context: The init context.
        @type context: L{InitContext}
        """
        pass
    
    def loaded(self, context):
        """
        Suds has loaded an XSD document.  Provides the plugin
        with an opportunity to inspect/modify the loaded XSD.
        Called after each XSD document is loaded.
        @param context: The XSD load context.
        @type context: L{LoadContext}
        """
        pass
    
    def sending(self, context):
        """
        Suds will send the specified soap envelope.
        Provides the plugin with the opportunity to inspect/modify
        the message before it is sent.
        @param context: The send context.
        @type context: L{SendContext}
        """
        pass
    
    def received(self, context):
        """
        Suds has received the specified reply.
        Provides the plugin with the opportunity to inspect/modify
        the received XML.
        @param context: The reply context.
        @type context: L{ReplyContext}
        """
        pass
   
    
class PluginContainer:
    """
    Plugin container provides easy method invocation.
    @ivar plugins: A list of plugin objects.
    @type plugins: [L{Plugin},]
    @cvar ctxclass: A dict of plugin method / context classes.
    @type ctxclass: dict
    """
    
    ctxclass = {\
        'initialized':InitContext,
        'loaded':LoadContext,
        'sending':SendContext,
        'received':ReplyContext,
    }
    
    def __init__(self, plugins):
        """
        @param plugins: A list of plugin objects.
        @type plugins: [L{Plugin},]
        """
        self.plugins = plugins
    
    def __getattr__(self, name):
        ctx = self.ctxclass.get(name)
        if ctx:
            return Method(name, ctx, self.plugins)
        else:
            raise AttributeError(name)


class Method:
    """
    Plugin method.
    @ivar name: The method name.
    @type name: str
    @ivar ctx: A context.
    @type ctx: L{Context}
    @ivar plugins: A list of plugins (targets).
    @type plugins: list
    """

    def __init__(self, name, ctx, plugins):
        """
        @param name: The method name.
        @type name: str
        @param ctx: A context.
        @type ctx: L{Context}
        @param plugins: A list of plugins (targets).
        @type plugins: list
        """
        self.name = name
        self.ctx = ctx()
        self.plugins = plugins
            
    def __call__(self, **kwargs):
        self.ctx.__dict__.update(kwargs)
        for plugin in self.plugins:
            try:
                method = getattr(plugin, self.name, None)
                if method:
                    method(self.ctx)
            except Exception, pe:
                log.exception(pe)
        return self.ctx