Make syntax highlighting for XML lazy, to only call pygments when debug logs are enabled.
Makes poezio about 11% faster when sending/receiving messages.
This commit is contained in:
parent
b8d7b9520c
commit
116a33ba51
2 changed files with 11 additions and 4 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue