massive refactoring to switch from camelCase to snake_case:
historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a pre-PEP8 code, to use the same coding style as in Twisted. However, snake_case is more readable and it's better to follow PEP8 best practices, so it has been decided to move on full snake_case. Because Libervia has a huge codebase, this ended with a ugly mix of camelCase and snake_case. To fix that, this patch does a big refactoring by renaming every function and method (including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case. This is a massive change, and may result in some bugs.
This commit is contained in:
parent
d50412c7f7
commit
6a6f15707a
7 changed files with 9945 additions and 9945 deletions
|
|
@ -435,7 +435,7 @@ class Const(object):
|
|||
return value.lower() in (cls.BOOL_TRUE, "1", "yes", "on")
|
||||
|
||||
@classmethod
|
||||
def boolConst(cls, value: bool) -> str:
|
||||
def bool_const(cls, value: bool) -> str:
|
||||
"""@return (str): constant associated to bool value"""
|
||||
assert isinstance(value, bool)
|
||||
return cls.BOOL_TRUE if value else cls.BOOL_FALSE
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ try:
|
|||
_ = gettext.translation("sat", "i18n", fallback=True).gettext
|
||||
_translators = {None: gettext.NullTranslations()}
|
||||
|
||||
def languageSwitch(lang=None):
|
||||
def language_switch(lang=None):
|
||||
if not lang in _translators:
|
||||
_translators[lang] = gettext.translation(
|
||||
"sat", languages=[lang], fallback=True
|
||||
|
|
@ -43,7 +43,7 @@ except ImportError:
|
|||
log.warning("gettext support disabled")
|
||||
_ = cast(Callable[[str], str], lambda msg: msg) # Libervia doesn't support gettext
|
||||
|
||||
def languageSwitch(lang=None):
|
||||
def language_switch(lang=None):
|
||||
pass
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -121,7 +121,7 @@ class Logger:
|
|||
'levelname': level,
|
||||
}
|
||||
try:
|
||||
if not self.filter_name.dictFilter(record):
|
||||
if not self.filter_name.dict_filter(record):
|
||||
raise Filtered
|
||||
except (AttributeError, TypeError): # XXX: TypeError is here because of a pyjamas bug which need to be fixed (TypeError is raised instead of AttributeError)
|
||||
if self.filter_name is not None:
|
||||
|
|
@ -133,7 +133,7 @@ class Logger:
|
|||
except KeyError as e:
|
||||
if e.args[0] == 'profile':
|
||||
# XXX: %(profile)s use some magic with introspection, for debugging purpose only *DO NOT* use in production
|
||||
record['profile'] = configure_cls[backend].getProfile()
|
||||
record['profile'] = configure_cls[backend].get_profile()
|
||||
return self.fmt % record
|
||||
else:
|
||||
raise e
|
||||
|
|
@ -174,7 +174,7 @@ class FilterName(object):
|
|||
return 1
|
||||
return 0
|
||||
|
||||
def dictFilter(self, dict_record):
|
||||
def dict_filter(self, dict_record):
|
||||
"""Filter using a dictionary record
|
||||
|
||||
@param dict_record: dictionary with at list a key "name" with logger name
|
||||
|
|
@ -208,26 +208,26 @@ class ConfigureBase:
|
|||
@param force_colors: if True ANSI colors are used even if stdout is not a tty
|
||||
"""
|
||||
self.backend_data = backend_data
|
||||
self.preTreatment()
|
||||
self.configureLevel(level)
|
||||
self.configureFormat(fmt)
|
||||
self.configureOutput(output)
|
||||
self.configureLogger(logger)
|
||||
self.configureColors(colors, force_colors, levels_taints_dict)
|
||||
self.postTreatment()
|
||||
self.updateCurrentLogger()
|
||||
self.pre_treatment()
|
||||
self.configure_level(level)
|
||||
self.configure_format(fmt)
|
||||
self.configure_output(output)
|
||||
self.configure_logger(logger)
|
||||
self.configure_colors(colors, force_colors, levels_taints_dict)
|
||||
self.post_treatment()
|
||||
self.update_current_logger()
|
||||
|
||||
def updateCurrentLogger(self):
|
||||
def update_current_logger(self):
|
||||
"""update existing logger to the class needed for this backend"""
|
||||
if self.LOGGER_CLASS is None:
|
||||
return
|
||||
for name, logger in list(_loggers.items()):
|
||||
_loggers[name] = self.LOGGER_CLASS(logger)
|
||||
|
||||
def preTreatment(self):
|
||||
def pre_treatment(self):
|
||||
pass
|
||||
|
||||
def configureLevel(self, level):
|
||||
def configure_level(self, level):
|
||||
if level is not None:
|
||||
# we deactivate methods below level
|
||||
level_idx = C.LOG_LEVELS.index(level)
|
||||
|
|
@ -236,7 +236,7 @@ class ConfigureBase:
|
|||
for _level in C.LOG_LEVELS[:level_idx]:
|
||||
setattr(Logger, _level.lower(), dev_null)
|
||||
|
||||
def configureFormat(self, fmt):
|
||||
def configure_format(self, fmt):
|
||||
if fmt is not None:
|
||||
if fmt != '%(message)s': # %(message)s is the same as None
|
||||
Logger.fmt = fmt
|
||||
|
|
@ -246,17 +246,17 @@ class ConfigureBase:
|
|||
# color_start not followed by an end, we add it
|
||||
Logger.fmt += COLOR_END
|
||||
|
||||
def configureOutput(self, output):
|
||||
def configure_output(self, output):
|
||||
if output is not None:
|
||||
if output != C.LOG_OPT_OUTPUT_SEP + C.LOG_OPT_OUTPUT_DEFAULT:
|
||||
# TODO: manage other outputs
|
||||
raise NotImplementedError("Basic backend only manage default output yet")
|
||||
|
||||
def configureLogger(self, logger):
|
||||
def configure_logger(self, logger):
|
||||
if logger:
|
||||
Logger.filter_name = FilterName(logger)
|
||||
|
||||
def configureColors(self, colors, force_colors, levels_taints_dict):
|
||||
def configure_colors(self, colors, force_colors, levels_taints_dict):
|
||||
if colors:
|
||||
# if color are used, we need to handle levels_taints_dict
|
||||
for level in list(levels_taints_dict.keys()):
|
||||
|
|
@ -280,10 +280,10 @@ class ConfigureBase:
|
|||
ansi_list.append(ansi)
|
||||
taints[level] = ''.join(ansi_list)
|
||||
|
||||
def postTreatment(self):
|
||||
def post_treatment(self):
|
||||
pass
|
||||
|
||||
def manageOutputs(self, outputs_raw):
|
||||
def manage_outputs(self, outputs_raw):
|
||||
""" Parse output option in a backend agnostic way, and fill handlers consequently
|
||||
|
||||
@param outputs_raw: output option as enterred in environment variable or in configuration
|
||||
|
|
@ -330,7 +330,7 @@ class ConfigureBase:
|
|||
raise ValueError("options [{options}] are not supported for {handler} output".format(options=options, handler=output))
|
||||
|
||||
@staticmethod
|
||||
def memoryGet(size=None):
|
||||
def memory_get(size=None):
|
||||
"""Return buffered logs
|
||||
|
||||
@param size: number of logs to return
|
||||
|
|
@ -338,7 +338,7 @@ class ConfigureBase:
|
|||
raise NotImplementedError
|
||||
|
||||
@classmethod
|
||||
def ansiColors(cls, level, message):
|
||||
def ansi_colors(cls, level, message):
|
||||
"""Colorise message depending on level for terminals
|
||||
|
||||
@param level: one of C.LOG_LEVELS
|
||||
|
|
@ -358,7 +358,7 @@ class ConfigureBase:
|
|||
return '%s%s%s' % (start, message, A.RESET)
|
||||
|
||||
@staticmethod
|
||||
def getProfile():
|
||||
def get_profile():
|
||||
"""Try to find profile value using introspection"""
|
||||
raise NotImplementedError
|
||||
|
||||
|
|
@ -396,10 +396,10 @@ def configure(backend_, **options):
|
|||
else:
|
||||
configure_class(**options)
|
||||
|
||||
def memoryGet(size=None):
|
||||
def memory_get(size=None):
|
||||
if not C.LOG_OPT_OUTPUT_MEMORY in handlers:
|
||||
raise ValueError('memory output is not used')
|
||||
return configure_cls[backend].memoryGet(size)
|
||||
return configure_cls[backend].memory_get(size)
|
||||
|
||||
def getLogger(name=C.LOG_BASE_LOGGER) -> Logger:
|
||||
try:
|
||||
|
|
|
|||
|
|
@ -47,8 +47,8 @@ class TwistedLogger(log.Logger):
|
|||
|
||||
|
||||
class ConfigureBasic(log.ConfigureBase):
|
||||
def configureColors(self, colors, force_colors, levels_taints_dict):
|
||||
super(ConfigureBasic, self).configureColors(
|
||||
def configure_colors(self, colors, force_colors, levels_taints_dict):
|
||||
super(ConfigureBasic, self).configure_colors(
|
||||
colors, force_colors, levels_taints_dict
|
||||
)
|
||||
if colors:
|
||||
|
|
@ -61,14 +61,14 @@ class ConfigureBasic(log.ConfigureBase):
|
|||
# FIXME: isatty should be tested on each handler, not globaly
|
||||
if (force_colors or isatty):
|
||||
# we need colors
|
||||
log.Logger.post_treat = lambda logger, level, message: self.ansiColors(
|
||||
log.Logger.post_treat = lambda logger, level, message: self.ansi_colors(
|
||||
level, message
|
||||
)
|
||||
elif force_colors:
|
||||
raise ValueError("force_colors can't be used if colors is False")
|
||||
|
||||
@staticmethod
|
||||
def getProfile():
|
||||
def get_profile():
|
||||
"""Try to find profile value using introspection"""
|
||||
import inspect
|
||||
|
||||
|
|
@ -107,7 +107,7 @@ class ConfigureBasic(log.ConfigureBase):
|
|||
class ConfigureTwisted(ConfigureBasic):
|
||||
LOGGER_CLASS = TwistedLogger
|
||||
|
||||
def preTreatment(self):
|
||||
def pre_treatment(self):
|
||||
from twisted import logger
|
||||
global logger
|
||||
self.level_map = {
|
||||
|
|
@ -119,17 +119,17 @@ class ConfigureTwisted(ConfigureBasic):
|
|||
}
|
||||
self.LOGGER_CLASS.level_map = self.level_map
|
||||
|
||||
def configureLevel(self, level):
|
||||
def configure_level(self, level):
|
||||
self.level = self.level_map[level]
|
||||
|
||||
def configureOutput(self, output):
|
||||
def configure_output(self, output):
|
||||
import sys
|
||||
from twisted.python import logfile
|
||||
self.log_publisher = logger.LogPublisher()
|
||||
|
||||
if output is None:
|
||||
output = C.LOG_OPT_OUTPUT_SEP + C.LOG_OPT_OUTPUT_DEFAULT
|
||||
self.manageOutputs(output)
|
||||
self.manage_outputs(output)
|
||||
|
||||
if C.LOG_OPT_OUTPUT_DEFAULT in log.handlers:
|
||||
if self.backend_data is None:
|
||||
|
|
@ -139,11 +139,11 @@ class ConfigureTwisted(ConfigureBasic):
|
|||
options = self.backend_data
|
||||
log_file = logfile.LogFile.fromFullPath(options['logfile'])
|
||||
self.log_publisher.addObserver(
|
||||
logger.FileLogObserver(log_file, self.textFormatter))
|
||||
logger.FileLogObserver(log_file, self.text_formatter))
|
||||
# we also want output to stdout if we are in debug or nodaemon mode
|
||||
if options.get("nodaemon", False) or options.get("debug", False):
|
||||
self.log_publisher.addObserver(
|
||||
logger.FileLogObserver(sys.stdout, self.textFormatter))
|
||||
logger.FileLogObserver(sys.stdout, self.text_formatter))
|
||||
|
||||
if C.LOG_OPT_OUTPUT_FILE in log.handlers:
|
||||
|
||||
|
|
@ -152,15 +152,15 @@ class ConfigureTwisted(ConfigureBasic):
|
|||
sys.stdout if path == "-" else logfile.LogFile.fromFullPath(path)
|
||||
)
|
||||
self.log_publisher.addObserver(
|
||||
logger.FileLogObserver(log_file, self.textFormatter))
|
||||
logger.FileLogObserver(log_file, self.text_formatter))
|
||||
|
||||
if C.LOG_OPT_OUTPUT_MEMORY in log.handlers:
|
||||
raise NotImplementedError(
|
||||
"Memory observer is not implemented in Twisted backend"
|
||||
)
|
||||
|
||||
def configureColors(self, colors, force_colors, levels_taints_dict):
|
||||
super(ConfigureTwisted, self).configureColors(
|
||||
def configure_colors(self, colors, force_colors, levels_taints_dict):
|
||||
super(ConfigureTwisted, self).configure_colors(
|
||||
colors, force_colors, levels_taints_dict
|
||||
)
|
||||
self.LOGGER_CLASS.colors = colors
|
||||
|
|
@ -168,7 +168,7 @@ class ConfigureTwisted(ConfigureBasic):
|
|||
if force_colors and not colors:
|
||||
raise ValueError("colors must be True if force_colors is True")
|
||||
|
||||
def postTreatment(self):
|
||||
def post_treatment(self):
|
||||
"""Install twistedObserver which manage non SàT logs"""
|
||||
# from twisted import logger
|
||||
import sys
|
||||
|
|
@ -180,7 +180,7 @@ class ConfigureTwisted(ConfigureBasic):
|
|||
)
|
||||
logger.globalLogBeginner.beginLoggingTo([filtering_obs])
|
||||
|
||||
def textFormatter(self, event):
|
||||
def text_formatter(self, event):
|
||||
if event.get('sat_logged', False):
|
||||
timestamp = ''.join([logger.formatTime(event.get("log_time", None)), " "])
|
||||
return f"{timestamp}{event.get('log_format', '')}\n"
|
||||
|
|
@ -219,7 +219,7 @@ class ConfigureStandard(ConfigureBasic):
|
|||
backend_data,
|
||||
)
|
||||
|
||||
def preTreatment(self):
|
||||
def pre_treatment(self):
|
||||
"""We use logging methods directly, instead of using Logger"""
|
||||
import logging
|
||||
|
||||
|
|
@ -230,13 +230,13 @@ class ConfigureStandard(ConfigureBasic):
|
|||
log.error = logging.error
|
||||
log.critical = logging.critical
|
||||
|
||||
def configureLevel(self, level):
|
||||
def configure_level(self, level):
|
||||
if level is None:
|
||||
level = C.LOG_LVL_DEBUG
|
||||
self.level = level
|
||||
|
||||
def configureFormat(self, fmt):
|
||||
super(ConfigureStandard, self).configureFormat(fmt)
|
||||
def configure_format(self, fmt):
|
||||
super(ConfigureStandard, self).configure_format(fmt)
|
||||
import logging
|
||||
|
||||
class SatFormatter(logging.Formatter):
|
||||
|
|
@ -250,11 +250,11 @@ class ConfigureStandard(ConfigureBasic):
|
|||
|
||||
def format(self, record):
|
||||
if self._with_profile:
|
||||
record.profile = ConfigureStandard.getProfile()
|
||||
record.profile = ConfigureStandard.get_profile()
|
||||
do_color = self.with_colors and (self.can_colors or self.force_colors)
|
||||
if ConfigureStandard._color_location:
|
||||
# we copy raw formatting strings for color_*
|
||||
# as formatting is handled in ansiColors in this case
|
||||
# as formatting is handled in ansi_colors in this case
|
||||
if do_color:
|
||||
record.color_start = log.COLOR_START
|
||||
record.color_end = log.COLOR_END
|
||||
|
|
@ -262,19 +262,19 @@ class ConfigureStandard(ConfigureBasic):
|
|||
record.color_start = record.color_end = ""
|
||||
s = super(SatFormatter, self).format(record)
|
||||
if do_color:
|
||||
s = ConfigureStandard.ansiColors(record.levelname, s)
|
||||
s = ConfigureStandard.ansi_colors(record.levelname, s)
|
||||
return s
|
||||
|
||||
self.formatterClass = SatFormatter
|
||||
|
||||
def configureOutput(self, output):
|
||||
self.manageOutputs(output)
|
||||
def configure_output(self, output):
|
||||
self.manage_outputs(output)
|
||||
|
||||
def configureLogger(self, logger):
|
||||
def configure_logger(self, logger):
|
||||
self.name_filter = log.FilterName(logger) if logger else None
|
||||
|
||||
def configureColors(self, colors, force_colors, levels_taints_dict):
|
||||
super(ConfigureStandard, self).configureColors(
|
||||
def configure_colors(self, colors, force_colors, levels_taints_dict):
|
||||
super(ConfigureStandard, self).configure_colors(
|
||||
colors, force_colors, levels_taints_dict
|
||||
)
|
||||
self.formatterClass.with_colors = colors
|
||||
|
|
@ -282,14 +282,14 @@ class ConfigureStandard(ConfigureBasic):
|
|||
if not colors and force_colors:
|
||||
raise ValueError("force_colors can't be used if colors is False")
|
||||
|
||||
def _addHandler(self, root_logger, hdlr, can_colors=False):
|
||||
def _add_handler(self, root_logger, hdlr, can_colors=False):
|
||||
hdlr.setFormatter(self.formatterClass(can_colors))
|
||||
root_logger.addHandler(hdlr)
|
||||
root_logger.setLevel(self.level)
|
||||
if self.name_filter is not None:
|
||||
hdlr.addFilter(self.name_filter)
|
||||
|
||||
def postTreatment(self):
|
||||
def post_treatment(self):
|
||||
import logging
|
||||
|
||||
root_logger = logging.getLogger()
|
||||
|
|
@ -301,7 +301,7 @@ class ConfigureStandard(ConfigureBasic):
|
|||
can_colors = hdlr.stream.isatty()
|
||||
except AttributeError:
|
||||
can_colors = False
|
||||
self._addHandler(root_logger, hdlr, can_colors=can_colors)
|
||||
self._add_handler(root_logger, hdlr, can_colors=can_colors)
|
||||
elif handler == C.LOG_OPT_OUTPUT_MEMORY:
|
||||
from logging.handlers import BufferingHandler
|
||||
|
||||
|
|
@ -315,20 +315,20 @@ class ConfigureStandard(ConfigureBasic):
|
|||
] = (
|
||||
hdlr
|
||||
) # we keep a reference to the handler to read the buffer later
|
||||
self._addHandler(root_logger, hdlr, can_colors=False)
|
||||
self._add_handler(root_logger, hdlr, can_colors=False)
|
||||
elif handler == C.LOG_OPT_OUTPUT_FILE:
|
||||
import os.path
|
||||
|
||||
for path in options:
|
||||
hdlr = logging.FileHandler(os.path.expanduser(path))
|
||||
self._addHandler(root_logger, hdlr, can_colors=False)
|
||||
self._add_handler(root_logger, hdlr, can_colors=False)
|
||||
else:
|
||||
raise ValueError("Unknown handler type")
|
||||
else:
|
||||
root_logger.warning("Handlers already set on root logger")
|
||||
|
||||
@staticmethod
|
||||
def memoryGet(size=None):
|
||||
def memory_get(size=None):
|
||||
"""Return buffered logs
|
||||
|
||||
@param size: number of logs to return
|
||||
|
|
@ -355,7 +355,7 @@ def configure(backend, **options):
|
|||
return log.configure(backend, **options)
|
||||
|
||||
|
||||
def _parseOptions(options):
|
||||
def _parse_options(options):
|
||||
"""Parse string options as given in conf or environment variable, and return expected python value
|
||||
|
||||
@param options (dict): options with (key: name, value: string value)
|
||||
|
|
@ -378,7 +378,7 @@ def _parseOptions(options):
|
|||
options[LEVEL] = level
|
||||
|
||||
|
||||
def satConfigure(backend=C.LOG_BACKEND_STANDARD, const=None, backend_data=None):
|
||||
def sat_configure(backend=C.LOG_BACKEND_STANDARD, const=None, backend_data=None):
|
||||
"""Configure logging system for SàT, can be used by frontends
|
||||
|
||||
logs conf is read in SàT conf, then in environment variables. It must be done before Memory init
|
||||
|
|
@ -396,16 +396,16 @@ def satConfigure(backend=C.LOG_BACKEND_STANDARD, const=None, backend_data=None):
|
|||
import os
|
||||
|
||||
log_conf = {}
|
||||
sat_conf = config.parseMainConf()
|
||||
sat_conf = config.parse_main_conf()
|
||||
for opt_name, opt_default in C.LOG_OPTIONS():
|
||||
try:
|
||||
log_conf[opt_name] = os.environ[
|
||||
"".join((C.ENV_PREFIX, C.LOG_OPT_PREFIX.upper(), opt_name.upper()))
|
||||
]
|
||||
except KeyError:
|
||||
log_conf[opt_name] = config.getConfig(
|
||||
log_conf[opt_name] = config.config_get(
|
||||
sat_conf, C.LOG_OPT_SECTION, C.LOG_OPT_PREFIX + opt_name, opt_default
|
||||
)
|
||||
|
||||
_parseOptions(log_conf)
|
||||
_parse_options(log_conf)
|
||||
configure(backend, backend_data=backend_data, **log_conf)
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@ class XmlStream(xmlstream.XmlStream):
|
|||
self._onElementHooks = []
|
||||
self._sendHooks = []
|
||||
|
||||
def addHook(self, hook_type, callback):
|
||||
def add_hook(self, hook_type, callback):
|
||||
"""Add a send or receive hook"""
|
||||
conflict_msg = f"Hook conflict: can't add {hook_type} hook {callback}"
|
||||
if hook_type == C.STREAM_HOOK_RECEIVE:
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
348
sat/core/xmpp.py
348
sat/core/xmpp.py
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue