Permalink
Browse files

Fallback to Sux if Expat is not available

  • Loading branch information...
1 parent 9d1de93 commit de24cff32c8e35a9da14ed32c0ac1193045e4cd3 Oleksandr Ruzhytskyi committed Dec 22, 2016
Showing with 22 additions and 6 deletions.
  1. +22 −6 punjab/jabber.py
View
@@ -4,8 +4,7 @@
from twisted.python import log
from copy import deepcopy
-from twisted.words.xish import domish
-from twisted.words.xish.domish import ExpatElementStream
+from twisted.words.xish.domish import ExpatElementStream, SuxElementStream
from twisted.words.protocols.jabber import xmlstream
from punjab.xmpp.ns import XMPP_PREFIXES
@@ -120,6 +119,26 @@ def _onEndElement(self, name):
ExpatElementStream._onEndElement(self, name)
+def elementStream(shallow=False):
+ """ Preferred method to construct an ElementStream
+
+ Uses regular or 'shallow' Expat-based stream if available,
+ and falls back to Sux if necessary.
+
+ """
+ try:
+ if shallow:
+ es = ShallowExpatElementStream()
+ else:
+ es = ExpatElementStream()
+ return es
+ except ImportError:
+ if SuxElementStream is None:
+ raise Exception("No parsers available :(")
+ es = SuxElementStream()
+ return es
+
+
class PunjabAuthenticator(xmlstream.ConnectAuthenticator):
namespace = "jabber:client"
version = '1.0'
@@ -159,10 +178,7 @@ def associateWithStream(self, xs):
def _reset(self, shallow=False):
# need this to be in xmlstream
- if shallow:
- stream = ShallowExpatElementStream()
- else:
- stream = domish.elementStream()
+ stream = elementStream(shallow)
stream.DocumentStartEvent = self.xmlstream.onDocumentStart
stream.ElementEvent = self.xmlstream.onElement
stream.DocumentEndEvent = self.xmlstream.onDocumentEnd

0 comments on commit de24cff

Please sign in to comment.