root/trunk/RBTelepathy/RBTelepathy/Packet/Builder.py

Revision 464, 3.5 kB (checked in by sholloway, 6 years ago)

*** empty log message ***

Line 
1 #!/usr/bin/env python
2 ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 ##~ License
4 ##~
5 ##- The RuneBlade Foundation library is intended to ease some
6 ##- aspects of writing intricate Jabber, XML, and User Interface (wxPython, etc.)
7 ##- applications, while providing the flexibility to modularly change the
8 ##- architecture. Enjoy.
9 ##~
10 ##~ Copyright (C) 2002  TechGame Networks, LLC.
11 ##~
12 ##~ This library is free software; you can redistribute it and/or
13 ##~ modify it under the terms of the BSD style License as found in the
14 ##~ LICENSE file included with this distribution.
15 ##~
16 ##~ TechGame Networks, LLC can be reached at:
17 ##~ 3578 E. Hartsel Drive #211
18 ##~ Colorado Springs, Colorado, USA, 80920
19 ##~
20 ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21
22 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 #~ Imports
24 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25
26 from __future__ import generators
27
28 import weakref
29 from xml.parsers.expat import ExpatError
30
31 from RBFoundation import XMLClassBuilder
32 from RBTelepathy import ErrorTypes
33
34 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
35 #~ Constants / Variables / Etc.
36 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
37
38 EF = XMLClassBuilder.ElementFactory
39
40 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
41 #~ Definitions
42 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
43
44 class ChildELementErrorFactory(object):
45     def __call__(self, owner, parent, node, attributes, namespacemap):
46         raise KeyError, '"%s" is not a valid subchild of "%s"' % (node[1], parent.__node__)
47
48 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
49
50 class RootMessageErrorFactory(object):
51     def __call__(self, owner, parent, node, attributes, namespacemap):
52         raise KeyError, '"%s" is not a valid topl level element' % (node[1], )
53
54 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55
56 class PacketFactorySet(XMLClassBuilder.ElementFactorySet):
57     def _GetElementFactoryIndices(self, (namespace, node)):
58         yield (namespace, node)
59         yield (namespace, )
60         yield node
61         yield None
62
63 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
64
65 class StreamPacketBuilder(XMLClassBuilder.XMLClassBuilder):
66     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
67     #~ Constants / Variables / Etc.
68     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
69
70     ElementFactories = PacketFactorySet()
71
72     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
73     #~ Public Methods
74     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
75
76     def BuildPacket(self, *args, **kw):
77         try:
78             return self.Parse(*args, **kw)
79         except ErrorTypes.RBTelepathyError, e:
80             raise # Pass these along...
81         except ExpatError, e:
82             raise ErrorTypes.PacketInvalidXMLError(str(e))
83         except StandardError, e:
84             # XXX: Change these to PacketErrors
85             #raise ErrorTypes.PacketError(str(e))
86             raise
87
88     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
89     #~ Protected Methods
90     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
91
92     def _xmlChildFactory(self, owner, parent, node, attributes, namespacemap):
93         try:
94             XMLClassBuilder.XMLClassBuilder._xmlChildFactory(self, owner, parent, node, attributes, namespacemap)
95         except KeyError, e:
96             # Well, we don't seem to have a packet for that... Error!  =)
97             raise ErrorTypes.PacketHandlerError(str(e))
98
99     def RootElement(self):
100         if self._elements:
101             return self._elements[0]
102         else:
103             return None
104
Note: See TracBrowser for help on using the browser.