diff options
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> | 2015-05-06 13:03:47 +0200 |
---|---|---|
committer | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> | 2015-05-06 13:03:47 +0200 |
commit | 116a33ba5162b2129dd787478f5ff2dd8f698b45 (patch) | |
tree | 0505cb02f617890a8755e4490e4b9ef5e41f0eb0 | |
parent | b8d7b9520cec828d7b4113e95bbb6e97727a4243 (diff) | |
download | slixmpp-116a33ba5162b2129dd787478f5ff2dd8f698b45.tar.gz slixmpp-116a33ba5162b2129dd787478f5ff2dd8f698b45.tar.bz2 slixmpp-116a33ba5162b2129dd787478f5ff2dd8f698b45.tar.xz slixmpp-116a33ba5162b2129dd787478f5ff2dd8f698b45.zip |
Make syntax highlighting for XML lazy, to only call pygments when debug logs are enabled.
Makes poezio about 11% faster when sending/receiving messages.
-rw-r--r-- | slixmpp/xmlstream/tostring.py | 9 | ||||
-rw-r--r-- | slixmpp/xmlstream/xmlstream.py | 6 |
2 files changed, 11 insertions, 4 deletions
diff --git a/slixmpp/xmlstream/tostring.py b/slixmpp/xmlstream/tostring.py index 521ee460..041346cb 100644 --- a/slixmpp/xmlstream/tostring.py +++ b/slixmpp/xmlstream/tostring.py @@ -169,7 +169,14 @@ def _get_highlight(): LEXER = get_lexer_by_name('xml') FORMATTER = Terminal256Formatter() - return lambda x: highlight(x, LEXER, FORMATTER) + class Highlighter: + __slots__ = ['string'] + def __init__(self, string): + self.string = string + def __str__(self): + return highlight(str(self.string).strip(), LEXER, FORMATTER) + + return Highlighter except ImportError: return lambda x: x diff --git a/slixmpp/xmlstream/xmlstream.py b/slixmpp/xmlstream/xmlstream.py index 0cd6720a..71873e48 100644 --- a/slixmpp/xmlstream/xmlstream.py +++ b/slixmpp/xmlstream/xmlstream.py @@ -354,7 +354,7 @@ class XMLStream(asyncio.BaseProtocol): log.debug('[33;1mRECV[0m: %s', highlight(tostring(self.xml_root, xmlns=self.default_ns, stream=self, top_level=True, - open_only=True)).strip()) + open_only=True))) self.start_stream_handler(self.xml_root) self.xml_depth += 1 if event == 'end': @@ -845,7 +845,7 @@ class XMLStream(asyncio.BaseProtocol): :param string data: Any bytes or utf-8 string value. """ - log.debug("[36;1mSEND[0m: %s", highlight(data).strip()) + log.debug("[36;1mSEND[0m: %s", highlight(data)) if not self.transport: raise NotConnectedError() if isinstance(data, str): @@ -898,7 +898,7 @@ class XMLStream(asyncio.BaseProtocol): if stanza is None: return - log.debug("[33;1mRECV[0m: %s", highlight(str(stanza)).strip()) + log.debug("[33;1mRECV[0m: %s", highlight(stanza)) # Match the stanza against registered handlers. Handlers marked # to run "in stream" will be executed immediately; the rest will |