comparison sat/core/log_config.py @ 3028:ab2696e34d29

Python 3 port: /!\ this is a huge commit /!\ starting from this commit, SàT is needs Python 3.6+ /!\ SàT maybe be instable or some feature may not work anymore, this will improve with time This patch port backend, bridge and frontends to Python 3. Roughly this has been done this way: - 2to3 tools has been applied (with python 3.7) - all references to python2 have been replaced with python3 (notably shebangs) - fixed files not handled by 2to3 (notably the shell script) - several manual fixes - fixed issues reported by Python 3 that where not handled in Python 2 - replaced "async" with "async_" when needed (it's a reserved word from Python 3.7) - replaced zope's "implements" with @implementer decorator - temporary hack to handle data pickled in database, as str or bytes may be returned, to be checked later - fixed hash comparison for password - removed some code which is not needed anymore with Python 3 - deactivated some code which needs to be checked (notably certificate validation) - tested with jp, fixed reported issues until some basic commands worked - ported Primitivus (after porting dependencies like urwid satext) - more manual fixes
author Goffi <goffi@goffi.org>
date Tue, 13 Aug 2019 19:08:41 +0200
parents ad00f61fd9f5
children 49c7bc3afb2b
comparison
equal deleted inserted replaced
3027:ff5bcb12ae60 3028:ab2696e34d29
1 #!/usr/bin/env python2 1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*- 2 # -*- coding: utf-8 -*-
3 3
4 # SàT: a XMPP client 4 # SàT: a XMPP client
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) 5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org)
6 6
123 if ( 123 if (
124 can_colors and self.LOGGER_CLASS.colors 124 can_colors and self.LOGGER_CLASS.colors
125 ) or self.LOGGER_CLASS.force_colors: 125 ) or self.LOGGER_CLASS.force_colors:
126 message = event.get("message", tuple()) 126 message = event.get("message", tuple())
127 if message: 127 if message:
128 event["message"] = ("".join(message),) # must be a tuple 128 event["message"] = (b"".join(message),) # must be a tuple
129 observer(event) # we can now call the original observer 129 observer(event) # we can now call the original observer
130 130
131 return observer_hook 131 return observer_hook
132 132
133 def changeFileLogObserver(self, observer): 133 def changeFileLogObserver(self, observer):
156 observer = self.changeFileLogObserver(observer) 156 observer = self.changeFileLogObserver(observer)
157 elif isinstance(observer.__self__, self.twisted_log.DefaultObserver): 157 elif isinstance(observer.__self__, self.twisted_log.DefaultObserver):
158 observer = self.changeObserver(observer, can_colors=True) 158 observer = self.changeObserver(observer, can_colors=True)
159 else: 159 else:
160 # we use print because log system is not fully initialized 160 # we use print because log system is not fully initialized
161 print("Unmanaged observer [%s]" % observer) 161 print(("Unmanaged observer [%s]" % observer))
162 return observer 162 return observer
163 self.observers[ori] = observer 163 self.observers[ori] = observer
164 return observer 164 return observer
165 165
166 def preTreatment(self): 166 def preTreatment(self):
200 twisted_log.LogPublisher.removeObserver 200 twisted_log.LogPublisher.removeObserver
201 ) 201 )
202 import types # see https://stackoverflow.com/a/4267590 (thx Chris Morgan/aaronasterling) 202 import types # see https://stackoverflow.com/a/4267590 (thx Chris Morgan/aaronasterling)
203 203
204 twisted_log.addObserver = types.MethodType( 204 twisted_log.addObserver = types.MethodType(
205 addObserverObserver, self.log_publisher, twisted_log.LogPublisher 205 addObserverObserver, self.log_publisher
206 ) 206 )
207 twisted_log.removeObserver = types.MethodType( 207 twisted_log.removeObserver = types.MethodType(
208 removeObserverObserver, self.log_publisher, twisted_log.LogPublisher 208 removeObserverObserver, self.log_publisher
209 ) 209 )
210 210
211 # we now change existing observers 211 # we now change existing observers
212 for idx, observer in enumerate(self.log_publisher.observers): 212 for idx, observer in enumerate(self.log_publisher.observers):
213 self.log_publisher.observers[idx] = self.installObserverHook(observer) 213 self.log_publisher.observers[idx] = self.installObserverHook(observer)
280 log_method = ( 280 log_method = (
281 twisted_logger.error 281 twisted_logger.error
282 if event.get("isError", False) 282 if event.get("isError", False)
283 else twisted_logger.info 283 else twisted_logger.info
284 ) 284 )
285 log_method(text.decode("utf-8")) 285 log_method(text)
286 286
287 self.log_publisher._originalAddObserver(twistedObserver) 287 self.log_publisher._originalAddObserver(twistedObserver)
288 288
289 289
290 class ConfigureStandard(ConfigureBasic): 290 class ConfigureStandard(ConfigureBasic):
334 super(ConfigureStandard, self).configureFormat(fmt) 334 super(ConfigureStandard, self).configureFormat(fmt)
335 import logging 335 import logging
336 import sys 336 import sys
337 337
338 class SatFormatter(logging.Formatter): 338 class SatFormatter(logging.Formatter):
339 u"""Formatter which manage SàT specificities""" 339 """Formatter which manage SàT specificities"""
340 _format = fmt 340 _format = fmt
341 _with_profile = "%(profile)s" in fmt 341 _with_profile = "%(profile)s" in fmt
342 342
343 def __init__(self, can_colors=False): 343 def __init__(self, can_colors=False):
344 super(SatFormatter, self).__init__(self._format) 344 super(SatFormatter, self).__init__(self._format)
393 def postTreatment(self): 393 def postTreatment(self):
394 import logging 394 import logging
395 395
396 root_logger = logging.getLogger() 396 root_logger = logging.getLogger()
397 if len(root_logger.handlers) == 0: 397 if len(root_logger.handlers) == 0:
398 for handler, options in log.handlers.items(): 398 for handler, options in list(log.handlers.items()):
399 if handler == C.LOG_OPT_OUTPUT_DEFAULT: 399 if handler == C.LOG_OPT_OUTPUT_DEFAULT:
400 hdlr = logging.StreamHandler() 400 hdlr = logging.StreamHandler()
401 try: 401 try:
402 can_colors = hdlr.stream.isatty() 402 can_colors = hdlr.stream.isatty()
403 except AttributeError: 403 except AttributeError:
424 hdlr = logging.FileHandler(os.path.expanduser(path)) 424 hdlr = logging.FileHandler(os.path.expanduser(path))
425 self._addHandler(root_logger, hdlr, can_colors=False) 425 self._addHandler(root_logger, hdlr, can_colors=False)
426 else: 426 else:
427 raise ValueError("Unknown handler type") 427 raise ValueError("Unknown handler type")
428 else: 428 else:
429 root_logger.warning(u"Handlers already set on root logger") 429 root_logger.warning("Handlers already set on root logger")
430 430
431 @staticmethod 431 @staticmethod
432 def memoryGet(size=None): 432 def memoryGet(size=None):
433 """Return buffered logs 433 """Return buffered logs
434 434