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
This commit is contained in:
Goffi 2019-08-13 19:08:41 +02:00
commit fbfe96c505
8 changed files with 6336 additions and 6398 deletions

View file

@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# SàT: a XMPP client
@ -28,21 +28,21 @@ import sat
class Const(object):
## Application ##
APP_NAME = u"Salut à Toi"
APP_NAME_SHORT = u"SàT"
APP_NAME_FILE = u"sat"
APP_NAME_FULL = u"{name_short} ({name})".format(
APP_NAME = "Salut à Toi"
APP_NAME_SHORT = "SàT"
APP_NAME_FILE = "sat"
APP_NAME_FULL = "{name_short} ({name})".format(
name_short=APP_NAME_SHORT, name=APP_NAME
)
APP_VERSION = (
sat.__version__
) # Please add 'D' at the end of version in sat/VERSION for dev versions
APP_RELEASE_NAME = u"La Cecília"
APP_URL = u"https://salut-a-toi.org"
APP_RELEASE_NAME = "La Cecília"
APP_URL = "https://salut-a-toi.org"
## Runtime ##
PLUGIN_EXT = "py"
HISTORY_SKIP = u"skip"
HISTORY_SKIP = "skip"
## Main config ##
DEFAULT_BRIDGE = "dbus"
@ -122,15 +122,15 @@ class Const(object):
)
MESS_TYPE_ALL = MESS_TYPE_STANDARD + (MESS_TYPE_INFO, MESS_TYPE_AUTO)
MESS_EXTRA_INFO = u"info_type"
EXTRA_INFO_DECR_ERR = u"DECRYPTION_ERROR"
EXTRA_INFO_ENCR_ERR = u"ENCRYPTION_ERROR"
MESS_EXTRA_INFO = "info_type"
EXTRA_INFO_DECR_ERR = "DECRYPTION_ERROR"
EXTRA_INFO_ENCR_ERR = "ENCRYPTION_ERROR"
# encryption is a key for plugins
MESS_KEY_ENCRYPTION = u"ENCRYPTION"
MESS_KEY_ENCRYPTION = "ENCRYPTION"
# encrypted is a key for frontends
MESS_KEY_ENCRYPTED = u"encrypted"
MESS_KEY_TRUSTED = u"trusted"
MESS_KEY_ENCRYPTED = "encrypted"
MESS_KEY_TRUSTED = "trusted"
## Chat ##
CHAT_ONE2ONE = "one2one"
@ -162,110 +162,44 @@ class Const(object):
## Directories ##
# directory for components specific data
COMPONENTS_DIR = u"components"
CACHE_DIR = u"cache"
COMPONENTS_DIR = "components"
CACHE_DIR = "cache"
# files in file dir are stored for long term
# files dir is global, i.e. for all profiles
FILES_DIR = u"files"
FILES_DIR = "files"
# FILES_LINKS_DIR is a directory where files owned by a specific profile
# are linked to the global files directory. This way the directory can be
#  shared per profiles while keeping global directory where identical files
# shared between different profiles are not duplicated.
FILES_LINKS_DIR = u"files_links"
FILES_LINKS_DIR = "files_links"
# FILES_TMP_DIR is where profile's partially transfered files are put.
# Once transfer is completed, they are moved to FILES_DIR
FILES_TMP_DIR = u"files_tmp"
## Configuration ##
if (
BaseDirectory
): # skipped when xdg module is not available (should not happen in backend)
if "org.salutatoi.cagou" in BaseDirectory.__file__:
# FIXME: hack to make config read from the right location on Android
# TODO: fix it in a more proper way
# we need to use Android API to get downloads directory
import os.path
from jnius import autoclass
Environment = autoclass("android.os.Environment")
BaseDirectory = None
DEFAULT_CONFIG = {
"local_dir": "/data/data/org.salutatoi.cagou/app",
"media_dir": "/data/data/org.salutatoi.cagou/files/app/media",
# FIXME: temporary location for downloads, need to call API properly
"downloads_dir": os.path.join(
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS
).getAbsolutePath(),
APP_NAME_FILE,
),
"pid_dir": "%(local_dir)s",
"log_dir": "%(local_dir)s",
}
CONFIG_FILES = [
"/data/data/org.salutatoi.cagou/files/app/android/"
+ APP_NAME_FILE
+ ".conf"
]
else:
import os
CONFIG_PATHS = (
["/etc/", "~/", "~/.", "", "."]
+ [
"%s/" % path
for path in list(BaseDirectory.load_config_paths(APP_NAME_FILE))
]
)
# on recent versions of Flatpak, FLATPAK_ID is set at run time
# it seems that this is not the case on older versions,
# but FLATPAK_SANDBOX_DIR seems set then
if os.getenv('FLATPAK_ID') or os.getenv('FLATPAK_SANDBOX_DIR'):
# for Flatpak, the conf can't be set in /etc or $HOME, so we have
# to add /app
CONFIG_PATHS.append('/app/')
## Configuration ##
DEFAULT_CONFIG = {
"media_dir": "/usr/share/" + APP_NAME_FILE + "/media",
"local_dir": BaseDirectory.save_data_path(APP_NAME_FILE),
"downloads_dir": "~/Downloads/" + APP_NAME_FILE,
"pid_dir": "%(local_dir)s",
"log_dir": "%(local_dir)s",
}
# List of the configuration filenames sorted by ascending priority
CONFIG_FILES = [
realpath(expanduser(path) + APP_NAME_FILE + ".conf")
for path in CONFIG_PATHS
]
FILES_TMP_DIR = "files_tmp"
## Templates ##
TEMPLATE_TPL_DIR = u"templates"
TEMPLATE_THEME_DEFAULT = u"default"
TEMPLATE_STATIC_DIR = u"static"
KEY_LANG = u"lang" # templates i18n
TEMPLATE_TPL_DIR = "templates"
TEMPLATE_THEME_DEFAULT = "default"
TEMPLATE_STATIC_DIR = "static"
KEY_LANG = "lang" # templates i18n
## Plugins ##
# PLUGIN_INFO keys
# XXX: we use PI instead of PLUG_INFO which would normally be used
# to make the header more readable
PI_NAME = u"name"
PI_IMPORT_NAME = u"import_name"
PI_MAIN = u"main"
PI_HANDLER = u"handler"
PI_NAME = "name"
PI_IMPORT_NAME = "import_name"
PI_MAIN = "main"
PI_HANDLER = "handler"
PI_TYPE = (
u"type"
"type"
) #  FIXME: should be types, and should handle single unicode type or tuple of types (e.g. "blog" and "import")
PI_MODES = u"modes"
PI_PROTOCOLS = u"protocols"
PI_DEPENDENCIES = u"dependencies"
PI_RECOMMENDATIONS = u"recommendations"
PI_DESCRIPTION = u"description"
PI_USAGE = u"usage"
PI_MODES = "modes"
PI_PROTOCOLS = "protocols"
PI_DEPENDENCIES = "dependencies"
PI_RECOMMENDATIONS = "recommendations"
PI_DESCRIPTION = "description"
PI_USAGE = "usage"
# Types
PLUG_TYPE_XEP = "XEP"
@ -387,8 +321,8 @@ class Const(object):
META_TYPE_OVERWRITE = "overwrite"
## HARD-CODED ACTIONS IDS (generated with uuid.uuid4) ##
AUTHENTICATE_PROFILE_ID = u"b03bbfa8-a4ae-4734-a248-06ce6c7cf562"
CHANGE_XMPP_PASSWD_ID = u"878b9387-de2b-413b-950f-e424a147bcd0"
AUTHENTICATE_PROFILE_ID = "b03bbfa8-a4ae-4734-a248-06ce6c7cf562"
CHANGE_XMPP_PASSWD_ID = "878b9387-de2b-413b-950f-e424a147bcd0"
## Text values ##
BOOL_TRUE = "true"
@ -399,32 +333,32 @@ class Const(object):
HISTORY_LIMIT_NONE = -2
## Progress error special values ##
PROGRESS_ERROR_DECLINED = u"declined" #  session has been declined by peer user
PROGRESS_ERROR_DECLINED = "declined" #  session has been declined by peer user
## Files ##
FILE_TYPE_DIRECTORY = "directory"
FILE_TYPE_FILE = "file"
## Permissions management ##
ACCESS_PERM_READ = u"read"
ACCESS_PERM_WRITE = u"write"
ACCESS_PERM_READ = "read"
ACCESS_PERM_WRITE = "write"
ACCESS_PERMS = {ACCESS_PERM_READ, ACCESS_PERM_WRITE}
ACCESS_TYPE_PUBLIC = u"public"
ACCESS_TYPE_WHITELIST = u"whitelist"
ACCESS_TYPE_PUBLIC = "public"
ACCESS_TYPE_WHITELIST = "whitelist"
ACCESS_TYPES = (ACCESS_TYPE_PUBLIC, ACCESS_TYPE_WHITELIST)
## Common data keys ##
KEY_THUMBNAILS = u"thumbnails"
KEY_PROGRESS_ID = u"progress_id"
KEY_THUMBNAILS = "thumbnails"
KEY_PROGRESS_ID = "progress_id"
## Common extra keys/values ##
KEY_ORDER_BY = u"order_by"
KEY_ORDER_BY = "order_by"
ORDER_BY_CREATION = u'creation'
ORDER_BY_MODIFICATION = u'modification'
ORDER_BY_CREATION = 'creation'
ORDER_BY_MODIFICATION = 'modification'
# internationalisation
DEFAULT_LOCALE = u"en_GB"
DEFAULT_LOCALE = "en_GB"
## Misc ##
SAVEFILE_DATABASE = APP_NAME_FILE + ".db"
@ -434,11 +368,11 @@ class Const(object):
NO_LIMIT = -1 # used in bridge when a integer value is expected
DEFAULT_MAX_AGE = 1209600 # default max age of cached files, in seconds
HASH_SHA1_EMPTY = "da39a3ee5e6b4b0d3255bfef95601890afd80709"
STANZA_NAMES = (u"iq", u"message", u"presence")
STANZA_NAMES = ("iq", "message", "presence")
# Stream Hooks
STREAM_HOOK_SEND = u"send"
STREAM_HOOK_RECEIVE = u"receive"
STREAM_HOOK_SEND = "send"
STREAM_HOOK_RECEIVE = "receive"
@classmethod
def LOG_OPTIONS(cls):
@ -456,7 +390,7 @@ class Const(object):
@classmethod
def bool(cls, value):
"""@return (bool): bool value for associated constant"""
assert isinstance(value, basestring)
assert isinstance(value, str)
return value.lower() in (cls.BOOL_TRUE, "1", "yes", "on")
@classmethod
@ -464,3 +398,72 @@ class Const(object):
"""@return (str): constant associated to bool value"""
assert isinstance(value, bool)
return cls.BOOL_TRUE if value else cls.BOOL_FALSE
## Configuration ##
if (
BaseDirectory
): # skipped when xdg module is not available (should not happen in backend)
if "org.salutatoi.cagou" in BaseDirectory.__file__:
# FIXME: hack to make config read from the right location on Android
# TODO: fix it in a more proper way
# we need to use Android API to get downloads directory
import os.path
from jnius import autoclass
Environment = autoclass("android.os.Environment")
BaseDirectory = None
Const.DEFAULT_CONFIG = {
"local_dir": "/data/data/org.salutatoi.cagou/app",
"media_dir": "/data/data/org.salutatoi.cagou/files/app/media",
# FIXME: temporary location for downloads, need to call API properly
"downloads_dir": os.path.join(
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS
).getAbsolutePath(),
Const.APP_NAME_FILE,
),
"pid_dir": "%(local_dir)s",
"log_dir": "%(local_dir)s",
}
Const.CONFIG_FILES = [
"/data/data/org.salutatoi.cagou/files/app/android/"
+ Const.APP_NAME_FILE
+ ".conf"
]
else:
import os
Const.CONFIG_PATHS = (
["/etc/", "~/", "~/.", "", "."]
+ [
"%s/" % path
for path in list(BaseDirectory.load_config_paths(Const.APP_NAME_FILE))
]
)
# on recent versions of Flatpak, FLATPAK_ID is set at run time
# it seems that this is not the case on older versions,
# but FLATPAK_SANDBOX_DIR seems set then
if os.getenv('FLATPAK_ID') or os.getenv('FLATPAK_SANDBOX_DIR'):
# for Flatpak, the conf can't be set in /etc or $HOME, so we have
# to add /app
Const.CONFIG_PATHS.append('/app/')
## Configuration ##
Const.DEFAULT_CONFIG = {
"media_dir": "/usr/share/" + Const.APP_NAME_FILE + "/media",
"local_dir": BaseDirectory.save_data_path(Const.APP_NAME_FILE),
"downloads_dir": "~/Downloads/" + Const.APP_NAME_FILE,
"pid_dir": "%(local_dir)s",
"log_dir": "%(local_dir)s",
}
# List of the configuration filenames sorted by ascending priority
Const.CONFIG_FILES = [
realpath(expanduser(path) + Const.APP_NAME_FILE + ".conf")
for path in Const.CONFIG_PATHS
]

View file

@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# SàT Exceptions

View file

@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# SAT: a jabber client
@ -26,7 +26,7 @@ try:
import gettext
_ = gettext.translation("sat", "i18n", fallback=True).ugettext
_ = gettext.translation("sat", "i18n", fallback=True).gettext
_translators = {None: gettext.NullTranslations()}
def languageSwitch(lang=None):
@ -34,7 +34,7 @@ try:
_translators[lang] = gettext.translation(
"sat", languages=[lang], fallback=True
)
_translators[lang].install(unicode=True)
_translators[lang].install()
except ImportError:

View file

@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# SàT: a XMPP client
@ -67,7 +67,7 @@ class Logger(object):
"""
if kwargs.get('exc_info', False):
message = self.addTraceback(message)
print message
print(message)
def log(self, level, message, **kwargs):
"""Print message
@ -197,7 +197,7 @@ class ConfigureBase(object):
"""update existing logger to the class needed for this backend"""
if self.LOGGER_CLASS is None:
return
for name, logger in _loggers.items():
for name, logger in list(_loggers.items()):
_loggers[name] = self.LOGGER_CLASS(logger)
def preTreatment(self):
@ -235,7 +235,7 @@ class ConfigureBase(object):
def configureColors(self, colors, force_colors, levels_taints_dict):
if colors:
# if color are used, we need to handle levels_taints_dict
for level in levels_taints_dict.keys():
for level in list(levels_taints_dict.keys()):
# we wants levels in uppercase to correspond to contstants
levels_taints_dict[level.upper()] = levels_taints_dict[level]
taints = self.__class__.taints = {}
@ -283,7 +283,7 @@ class ConfigureBase(object):
options = None
if output not in (C.LOG_OPT_OUTPUT_DEFAULT, C.LOG_OPT_OUTPUT_FILE, C.LOG_OPT_OUTPUT_MEMORY):
raise ValueError(u"Invalid output [%s]" % output)
raise ValueError("Invalid output [%s]" % output)
if output == C.LOG_OPT_OUTPUT_DEFAULT:
# no option for defaut handler
@ -303,7 +303,7 @@ class ConfigureBase(object):
handlers[output] = limit
if options: # we should not have unparsed options
raise ValueError(u"options [{options}] are not supported for {handler} output".format(options=options, handler=output))
raise ValueError("options [{options}] are not supported for {handler} output".format(options=options, handler=output))
@staticmethod
def memoryGet(size=None):

View file

@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# SàT: a XMPP client
@ -125,7 +125,7 @@ class ConfigureTwisted(ConfigureBasic):
) or self.LOGGER_CLASS.force_colors:
message = event.get("message", tuple())
if message:
event["message"] = ("".join(message),) # must be a tuple
event["message"] = (b"".join(message),) # must be a tuple
observer(event) # we can now call the original observer
return observer_hook
@ -158,7 +158,7 @@ class ConfigureTwisted(ConfigureBasic):
observer = self.changeObserver(observer, can_colors=True)
else:
# we use print because log system is not fully initialized
print("Unmanaged observer [%s]" % observer)
print(("Unmanaged observer [%s]" % observer))
return observer
self.observers[ori] = observer
return observer
@ -202,10 +202,10 @@ class ConfigureTwisted(ConfigureBasic):
import types # see https://stackoverflow.com/a/4267590 (thx Chris Morgan/aaronasterling)
twisted_log.addObserver = types.MethodType(
addObserverObserver, self.log_publisher, twisted_log.LogPublisher
addObserverObserver, self.log_publisher
)
twisted_log.removeObserver = types.MethodType(
removeObserverObserver, self.log_publisher, twisted_log.LogPublisher
removeObserverObserver, self.log_publisher
)
# we now change existing observers
@ -282,7 +282,7 @@ class ConfigureTwisted(ConfigureBasic):
if event.get("isError", False)
else twisted_logger.info
)
log_method(text.decode("utf-8"))
log_method(text)
self.log_publisher._originalAddObserver(twistedObserver)
@ -336,7 +336,7 @@ class ConfigureStandard(ConfigureBasic):
import sys
class SatFormatter(logging.Formatter):
u"""Formatter which manage SàT specificities"""
"""Formatter which manage SàT specificities"""
_format = fmt
_with_profile = "%(profile)s" in fmt
@ -395,7 +395,7 @@ class ConfigureStandard(ConfigureBasic):
root_logger = logging.getLogger()
if len(root_logger.handlers) == 0:
for handler, options in log.handlers.items():
for handler, options in list(log.handlers.items()):
if handler == C.LOG_OPT_OUTPUT_DEFAULT:
hdlr = logging.StreamHandler()
try:
@ -426,7 +426,7 @@ class ConfigureStandard(ConfigureBasic):
else:
raise ValueError("Unknown handler type")
else:
root_logger.warning(u"Handlers already set on root logger")
root_logger.warning("Handlers already set on root logger")
@staticmethod
def memoryGet(size=None):

View file

@ -84,7 +84,7 @@ class XmlStream(xmlstream.XmlStream):
def addHook(self, hook_type, callback):
"""Add a send or receive hook"""
conflict_msg = (u"Hook conflict: can't add {hook_type} hook {callback}"
conflict_msg = ("Hook conflict: can't add {hook_type} hook {callback}"
.format(hook_type=hook_type, callback=callback))
if hook_type == C.STREAM_HOOK_RECEIVE:
if callback not in self._onElementHooks:
@ -97,7 +97,7 @@ class XmlStream(xmlstream.XmlStream):
else:
log.warning(conflict_msg)
else:
raise ValueError(u"Invalid hook type: {hook_type}"
raise ValueError("Invalid hook type: {hook_type}"
.format(hook_type=hook_type))
def onElement(self, element):
@ -161,9 +161,10 @@ def internJID(jidstring):
def apply():
# certificate validation
xmlstream.TLSInitiatingInitializer = TLSInitiatingInitializer
client.XMPPClient = XMPPClient
# FIXME: certificate validation is now implemented in Twisted trunk, to be removed
# # certificate validation
# xmlstream.TLSInitiatingInitializer = TLSInitiatingInitializer
# client.XMPPClient = XMPPClient
# XmlStream triggers
xmlstream.XmlStreamFactory.protocol = XmlStream
# jid fix

View file

@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# SAT: a jabber client
@ -66,8 +66,8 @@ class SAT(service.Service):
self.plugins = {}
# map for short name to whole namespace,
self.ns_map = {
u"x-data": xmpp.NS_X_DATA,
u"disco#info": xmpp.NS_DISCO_INFO,
"x-data": xmpp.NS_X_DATA,
"disco#info": xmpp.NS_DISCO_INFO,
}
# extended by plugins with registerNamespace
self.memory = memory.Memory(self)
@ -79,13 +79,13 @@ class SAT(service.Service):
bridge_module = dynamic_import.bridge(bridge_name)
if bridge_module is None:
log.error(u"Can't find bridge module of name {}".format(bridge_name))
log.error("Can't find bridge module of name {}".format(bridge_name))
sys.exit(1)
log.info(u"using {} bridge".format(bridge_name))
log.info("using {} bridge".format(bridge_name))
try:
self.bridge = bridge_module.Bridge()
except exceptions.BridgeInitError:
log.error(u"Bridge can't be initialised, can't start SàT core")
log.error("Bridge can't be initialised, can't start SàT core")
sys.exit(1)
self.bridge.register_method("getReady", lambda: self.initialised)
self.bridge.register_method("getVersion", lambda: self.full_version)
@ -181,7 +181,7 @@ class SAT(service.Service):
try:
return self._version_cache
except AttributeError:
self._version_cache = u"{} « {} » ({})".format(
self._version_cache = "{} « {} » ({})".format(
version, C.APP_RELEASE_NAME, utils.getRepositoryData(sat)
)
return self._version_cache
@ -202,14 +202,14 @@ class SAT(service.Service):
ui_profile_manager.ProfileManager(self)
except Exception as e:
log.error(
_(u"Could not initialize backend: {reason}").format(
_("Could not initialize backend: {reason}").format(
reason=str(e).decode("utf-8", "ignore")
)
)
sys.exit(1)
self._addBaseMenus()
self.initialised.callback(None)
log.info(_(u"Backend is ready"))
log.info(_("Backend is ready"))
def _addBaseMenus(self):
"""Add base menus"""
@ -246,15 +246,15 @@ class SAT(service.Service):
except exceptions.MissingModule as e:
self._unimport_plugin(plugin_path)
log.warning(
u"Can't import plugin [{path}] because of an unavailale third party "
u"module:\n{msg}".format(
"Can't import plugin [{path}] because of an unavailale third party "
"module:\n{msg}".format(
path=plugin_path, msg=e
)
)
continue
except exceptions.CancelError as e:
log.info(
u"Plugin [{path}] cancelled its own import: {msg}".format(
"Plugin [{path}] cancelled its own import: {msg}".format(
path=plugin_path, msg=e
)
)
@ -264,7 +264,7 @@ class SAT(service.Service):
import traceback
log.error(
_(u"Can't import plugin [{path}]:\n{error}").format(
_("Can't import plugin [{path}]:\n{error}").format(
path=plugin_path, error=traceback.format_exc()
)
)
@ -274,17 +274,17 @@ class SAT(service.Service):
plugin_info = mod.PLUGIN_INFO
import_name = plugin_info["import_name"]
plugin_modes = plugin_info[u"modes"] = set(
plugin_info.setdefault(u"modes", C.PLUG_MODE_DEFAULT)
plugin_modes = plugin_info["modes"] = set(
plugin_info.setdefault("modes", C.PLUG_MODE_DEFAULT)
)
# if the plugin is an entry point, it must work in component mode
if plugin_info[u"type"] == C.PLUG_TYPE_ENTRY_POINT:
if plugin_info["type"] == C.PLUG_TYPE_ENTRY_POINT:
# if plugin is an entrypoint, we cache it
if C.PLUG_MODE_COMPONENT not in plugin_modes:
log.error(
_(
u"{type} type must be used with {mode} mode, ignoring plugin"
"{type} type must be used with {mode} mode, ignoring plugin"
).format(type=C.PLUG_TYPE_ENTRY_POINT, mode=C.PLUG_MODE_COMPONENT)
)
self._unimport_plugin(plugin_path)
@ -293,8 +293,8 @@ class SAT(service.Service):
if import_name in plugins_to_import:
log.error(
_(
u"Name conflict for import name [{import_name}], can't import "
u"plugin [{name}]"
"Name conflict for import name [{import_name}], can't import "
"plugin [{name}]"
).format(**plugin_info)
)
continue
@ -320,7 +320,7 @@ class SAT(service.Service):
is raised
"""
if import_name in self.plugins:
log.debug(u"Plugin {} already imported, passing".format(import_name))
log.debug("Plugin {} already imported, passing".format(import_name))
return
if not import_name:
import_name, (plugin_path, mod, plugin_info) = plugins_to_import.popitem()
@ -328,10 +328,10 @@ class SAT(service.Service):
if not import_name in plugins_to_import:
if optional:
log.warning(
_(u"Recommended plugin not found: {}").format(import_name)
_("Recommended plugin not found: {}").format(import_name)
)
return
msg = u"Dependency not found: {}".format(import_name)
msg = "Dependency not found: {}".format(import_name)
log.error(msg)
raise ImportError(msg)
plugin_path, mod, plugin_info = plugins_to_import.pop(import_name)
@ -340,7 +340,7 @@ class SAT(service.Service):
for to_import in dependencies + recommendations:
if to_import not in self.plugins:
log.debug(
u"Recursively import dependency of [%s]: [%s]"
"Recursively import dependency of [%s]: [%s]"
% (import_name, to_import)
)
try:
@ -349,7 +349,7 @@ class SAT(service.Service):
)
except ImportError as e:
log.warning(
_(u"Can't import plugin {name}: {error}").format(
_("Can't import plugin {name}: {error}").format(
name=plugin_info["name"], error=e
)
)
@ -362,13 +362,13 @@ class SAT(service.Service):
self.plugins[import_name] = getattr(mod, plugin_info["main"])(self)
except Exception as e:
log.warning(
u'Error while loading plugin "{name}", ignoring it: {error}'.format(
'Error while loading plugin "{name}", ignoring it: {error}'.format(
name=plugin_info["name"], error=e
)
)
if optional:
return
raise ImportError(u"Error during initiation")
raise ImportError("Error during initiation")
if C.bool(plugin_info.get(C.PI_HANDLER, C.BOOL_FALSE)):
self.plugins[import_name].is_handler = True
else:
@ -386,7 +386,7 @@ class SAT(service.Service):
# pluging depending on the unloaded one should be unloaded too
# for now, just a basic call on plugin.unload is done
defers_list = []
for plugin in self.plugins.itervalues():
for plugin in self.plugins.values():
try:
unload = plugin.unload
except AttributeError:
@ -419,7 +419,7 @@ class SAT(service.Service):
def connectProfile(__=None):
if self.isConnected(profile):
log.info(_(u"already connected !"))
log.info(_("already connected !"))
return True
if self.memory.isComponent(profile):
@ -439,7 +439,7 @@ class SAT(service.Service):
if not self.isConnected(profile_key):
# isConnected is checked here and not on client
# because client is deleted when session is ended
log.info(_(u"not connected !"))
log.info(_("not connected !"))
return defer.succeed(None)
client = self.getClient(profile_key)
return client.entityDisconnect()
@ -468,7 +468,7 @@ class SAT(service.Service):
pass
features = []
for import_name, plugin in self.plugins.iteritems():
for import_name, plugin in self.plugins.items():
try:
features_d = defer.maybeDeferred(plugin.getFeatures, profile_key)
except AttributeError:
@ -485,14 +485,14 @@ class SAT(service.Service):
ret[name] = data
else:
log.warning(
u"Error while getting features for {name}: {failure}".format(
"Error while getting features for {name}: {failure}".format(
name=name, failure=data
)
)
ret[name] = {}
return ret
d_list.addCallback(buildFeatures, self.plugins.keys())
d_list.addCallback(buildFeatures, list(self.plugins.keys()))
return d_list
def getContacts(self, profile_key):
@ -527,10 +527,10 @@ class SAT(service.Service):
self.memory.purgeProfileSession(profile)
def startService(self):
log.info(u"Salut à toi ô mon frère !")
log.info("Salut à toi ô mon frère !")
def stopService(self):
log.info(u"Salut aussi à Rantanplan")
log.info("Salut aussi à Rantanplan")
return self.pluginsUnload()
def run(self):
@ -576,13 +576,13 @@ class SAT(service.Service):
@return: list of clients
"""
if not profile_key:
raise exceptions.DataError(_(u"profile_key must not be empty"))
raise exceptions.DataError(_("profile_key must not be empty"))
try:
profile = self.memory.getProfileName(profile_key, True)
except exceptions.ProfileUnknownError:
return []
if profile == C.PROF_KEY_ALL:
return self.profiles.values()
return list(self.profiles.values())
elif profile[0] == "@": #  only profile keys can start with "@"
raise exceptions.ProfileKeyUnknown
return [self.profiles[profile]]
@ -594,9 +594,9 @@ class SAT(service.Service):
@param name: name of the option
@return: unicode representation of the option
"""
return unicode(self.memory.getConfig(section, name, ""))
return str(self.memory.getConfig(section, name, ""))
def logErrback(self, failure_, msg=_(u"Unexpected error: {failure_}")):
def logErrback(self, failure_, msg=_("Unexpected error: {failure_}")):
"""Generic errback logging
@param msg(unicode): error message ("failure_" key will be use for format)
@ -610,7 +610,7 @@ class SAT(service.Service):
def registerNamespace(self, short_name, namespace):
"""associate a namespace to a short name"""
if short_name in self.ns_map:
raise exceptions.ConflictError(u"this short name is already used")
raise exceptions.ConflictError("this short name is already used")
self.ns_map[short_name] = namespace
def getNamespaces(self):
@ -620,7 +620,7 @@ class SAT(service.Service):
try:
return self.ns_map[short_name]
except KeyError:
raise exceptions.NotFound(u"namespace {short_name} is not registered"
raise exceptions.NotFound("namespace {short_name} is not registered"
.format(short_name=short_name))
def getSessionInfos(self, profile_key):
@ -628,7 +628,7 @@ class SAT(service.Service):
client = self.getClient(profile_key)
data = {
"jid": client.jid.full(),
"started": unicode(int(client.started))
"started": str(int(client.started))
}
return defer.succeed(data)
@ -714,9 +714,9 @@ class SAT(service.Service):
ret = []
for p in plugins:
ret.append({
u"name": p.name,
u"namespace": p.namespace,
u"priority": unicode(p.priority),
"name": p.name,
"namespace": p.namespace,
"priority": str(p.priority),
})
return ret
@ -740,7 +740,7 @@ class SAT(service.Service):
message,
subject,
mess_type,
{unicode(key): unicode(value) for key, value in extra.items()},
{str(key): str(value) for key, value in list(extra.items())},
)
def _setPresence(self, to="", show="", statuses=None, profile_key=C.PROF_KEY_NONE):
@ -774,7 +774,7 @@ class SAT(service.Service):
assert profile
to_jid = jid.JID(raw_jid)
log.debug(
_(u"subsciption request [%(subs_type)s] for %(jid)s")
_("subsciption request [%(subs_type)s] for %(jid)s")
% {"subs_type": subs_type, "jid": to_jid.full()}
)
if subs_type == "subscribe":
@ -901,15 +901,15 @@ class SAT(service.Service):
service_jid = services_jids[idx]
if not success:
log.warning(
_(u"Can't find features for service {service_jid}, ignoring")
_("Can't find features for service {service_jid}, ignoring")
.format(service_jid=service_jid.full()))
continue
if (identities is not None
and not set(infos.identities.keys()).issuperset(identities)):
continue
found_identities = [
(cat, type_, name or u"")
for (cat, type_), name in infos.identities.iteritems()
(cat, type_, name or "")
for (cat, type_), name in infos.identities.items()
]
found_service[service_jid.full()] = found_identities
@ -960,7 +960,7 @@ class SAT(service.Service):
full_jid = full_jids[idx]
if not success:
log.warning(
_(u"Can't retrieve {full_jid} infos, ignoring")
_("Can't retrieve {full_jid} infos, ignoring")
.format(full_jid=full_jid.full()))
continue
if infos.features.issuperset(namespaces):
@ -969,8 +969,8 @@ class SAT(service.Service):
).issuperset(identities):
continue
found_identities = [
(cat, type_, name or u"")
for (cat, type_), name in infos.identities.iteritems()
(cat, type_, name or "")
for (cat, type_), name in infos.identities.items()
]
found[full_jid.full()] = found_identities
@ -979,7 +979,7 @@ class SAT(service.Service):
## Generic HMI ##
def _killAction(self, keep_id, client):
log.debug(u"Killing action {} for timeout".format(keep_id))
log.debug("Killing action {} for timeout".format(keep_id))
client.actions[keep_id]
def actionNew(
@ -998,7 +998,7 @@ class SAT(service.Service):
Action will be deleted after 30 min.
@param profile: %(doc_profile)s
"""
id_ = unicode(uuid.uuid4())
id_ = str(uuid.uuid4())
if keep_id is not None:
client = self.getClient(profile)
action_timer = reactor.callLater(60 * 30, self._killAction, keep_id, client)
@ -1012,7 +1012,7 @@ class SAT(service.Service):
@param profile: %(doc_profile)s
"""
client = self.getClient(profile)
return [action_tuple[:-1] for action_tuple in client.actions.itervalues()]
return [action_tuple[:-1] for action_tuple in client.actions.values()]
def registerProgressCb(
self, progress_id, callback, metadata=None, profile=C.PROF_KEY_NONE
@ -1022,7 +1022,7 @@ class SAT(service.Service):
metadata = {}
client = self.getClient(profile)
if progress_id in client._progress_cb:
raise exceptions.ConflictError(u"Progress ID is not unique !")
raise exceptions.ConflictError("Progress ID is not unique !")
client._progress_cb[progress_id] = (callback, metadata)
def removeProgressCb(self, progress_id, profile):
@ -1031,11 +1031,11 @@ class SAT(service.Service):
try:
del client._progress_cb[progress_id]
except KeyError:
log.error(_(u"Trying to remove an unknow progress callback"))
log.error(_("Trying to remove an unknow progress callback"))
def _progressGet(self, progress_id, profile):
data = self.progressGet(progress_id, profile)
return {k: unicode(v) for k, v in data.iteritems()}
return {k: str(v) for k, v in data.items()}
def progressGet(self, progress_id, profile):
"""Return a dict with progress information
@ -1057,10 +1057,10 @@ class SAT(service.Service):
def _progressGetAll(self, profile_key):
progress_all = self.progressGetAll(profile_key)
for profile, progress_dict in progress_all.iteritems():
for progress_id, data in progress_dict.iteritems():
for key, value in data.iteritems():
data[key] = unicode(value)
for profile, progress_dict in progress_all.items():
for progress_id, data in progress_dict.items():
for key, value in data.items():
data[key] = str(value)
return progress_all
def progressGetAllMetadata(self, profile_key):
@ -1082,7 +1082,7 @@ class SAT(service.Service):
for (
progress_id,
(__, progress_metadata),
) in client._progress_cb.iteritems():
) in client._progress_cb.items():
progress_dict[progress_id] = progress_metadata
return progress_all
@ -1101,7 +1101,7 @@ class SAT(service.Service):
profile = client.profile
progress_dict = {}
progress_all[profile] = progress_dict
for progress_id, (progress_cb, __) in client._progress_cb.iteritems():
for progress_id, (progress_cb, __) in client._progress_cb.items():
progress_dict[progress_id] = progress_cb(progress_id, profile)
return progress_all
@ -1121,7 +1121,7 @@ class SAT(service.Service):
callback_id = str(uuid.uuid4())
else:
if callback_id in self._cb_map:
raise exceptions.ConflictError(_(u"id already registered"))
raise exceptions.ConflictError(_("id already registered"))
self._cb_map[callback_id] = (callback, args, kwargs)
if "one_shot" in kwargs: # One Shot callback are removed after 30 min
@ -1163,7 +1163,7 @@ class SAT(service.Service):
profile = self.memory.getProfileName(profile_key)
if not profile:
raise exceptions.ProfileUnknownError(
_(u"trying to launch action with a non-existant profile")
_("trying to launch action with a non-existant profile")
)
else:
profile = client.profile
@ -1179,7 +1179,7 @@ class SAT(service.Service):
try:
callback, args, kwargs = self._cb_map[callback_id]
except KeyError:
raise exceptions.DataError(u"Unknown callback id {}".format(callback_id))
raise exceptions.DataError("Unknown callback id {}".format(callback_id))
if kwargs.get("with_data", False):
if data is None:
@ -1210,7 +1210,7 @@ class SAT(service.Service):
def importMenu(self, path, callback, security_limit=C.NO_SECURITY_LIMIT,
help_string="", type_=C.MENU_GLOBAL):
"""register a new menu for frontends
r"""register a new menu for frontends
@param path(iterable[unicode]): path to go to the menu
(category/subcategory/.../item) (e.g.: ("File", "Open"))
@ -1245,7 +1245,7 @@ class SAT(service.Service):
if callable(callback):
callback_id = self.registerCallback(callback, with_data=True)
elif isinstance(callback, basestring):
elif isinstance(callback, str):
# The callback is already registered
callback_id = callback
try:
@ -1256,7 +1256,7 @@ class SAT(service.Service):
else:
raise exceptions.DataError("Unknown callback type")
for menu_data in self._menus.itervalues():
for menu_data in self._menus.values():
if menu_data["path"] == path and menu_data["type"] == type_:
raise exceptions.ConflictError(
_("A menu with the same path and type already exists")
@ -1267,7 +1267,7 @@ class SAT(service.Service):
if menu_key in self._menus_paths:
raise exceptions.ConflictError(
u"this menu path is already used: {path} ({menu_key})".format(
"this menu path is already used: {path} ({menu_key})".format(
path=path_canonical, menu_key=menu_key
)
)
@ -1300,7 +1300,7 @@ class SAT(service.Service):
- help_url: link to a page with more complete documentation (TODO)
"""
ret = []
for menu_id, menu_data in self._menus.iteritems():
for menu_id, menu_data in self._menus.items():
type_ = menu_data["type"]
path = menu_data["path"]
menu_security_limit = menu_data["security_limit"]
@ -1339,7 +1339,7 @@ class SAT(service.Service):
callback_id = self._menus_paths[menu_key]
except KeyError:
raise exceptions.NotFound(
u"Can't find menu {path} ({menu_type})".format(
"Can't find menu {path} ({menu_type})".format(
path=canonical_path, menu_type=menu_type
)
)

View file

@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# SAT: a jabber client
@ -40,18 +40,18 @@ from sat.core import exceptions
from sat.memory import encryption
from sat.memory import persistent
from sat.tools import xml_tools
from zope.interface import implements
from zope.interface import implementer
log = getLogger(__name__)
NS_X_DATA = u"jabber:x:data"
NS_DISCO_INFO = u"http://jabber.org/protocol/disco#info"
NS_XML_ELEMENT = u"urn:xmpp:xml-element"
NS_ROSTER_VER = u"urn:xmpp:features:rosterver"
NS_X_DATA = "jabber:x:data"
NS_DISCO_INFO = "http://jabber.org/protocol/disco#info"
NS_XML_ELEMENT = "urn:xmpp:xml-element"
NS_ROSTER_VER = "urn:xmpp:features:rosterver"
# we use 2 "@" which is illegal in a jid, to be sure we are not mixing keys
# with roster jids
ROSTER_VER_KEY = u"@version@"
ROSTER_VER_KEY = "@version@"
class SatXMPPEntity(object):
@ -65,9 +65,9 @@ class SatXMPPEntity(object):
clientConnectionFailed_ori = factory.clientConnectionFailed
clientConnectionLost_ori = factory.clientConnectionLost
factory.clientConnectionFailed = partial(
self.connectionTerminated, term_type=u"failed", cb=clientConnectionFailed_ori)
self.connectionTerminated, term_type="failed", cb=clientConnectionFailed_ori)
factory.clientConnectionLost = partial(
self.connectionTerminated, term_type=u"lost", cb=clientConnectionLost_ori)
self.connectionTerminated, term_type="lost", cb=clientConnectionLost_ori)
factory.maxRetries = max_retries
factory.maxDelay = 30
@ -87,7 +87,7 @@ class SatXMPPEntity(object):
self.encryption = encryption.EncryptionHandler(self)
def __unicode__(self):
return u"Client instance for profile {profile}".format(profile=self.profile)
return "Client instance for profile {profile}".format(profile=self.profile)
def __str__(self):
return self.__unicode__.encode('utf-8')
@ -206,11 +206,11 @@ class SatXMPPEntity(object):
def logPluginResults(results):
all_succeed = all([success for success, result in results])
if not all_succeed:
log.error(_(u"Plugins initialisation error"))
log.error(_("Plugins initialisation error"))
for idx, (success, result) in enumerate(results):
if not success:
log.error(
u"error (plugin %(name)s): %(failure)s"
"error (plugin %(name)s): %(failure)s"
% {
"name": plugin_conn_cb[idx][0]._info["import_name"],
"failure": result,
@ -226,11 +226,11 @@ class SatXMPPEntity(object):
self._connected_d = None
def _disconnectionEb(self, failure_):
log.error(_(u"Error while disconnecting: {}".format(failure_)))
log.error(_("Error while disconnecting: {}".format(failure_)))
def _authd(self, xmlstream):
super(SatXMPPEntity, self)._authd(xmlstream)
log.debug(_(u"{profile} identified").format(profile=self.profile))
log.debug(_("{profile} identified").format(profile=self.profile))
self.streamInitialized()
def _finish_connection(self, __):
@ -238,7 +238,7 @@ class SatXMPPEntity(object):
def streamInitialized(self):
"""Called after _authd"""
log.debug(_(u"XML stream is initialized"))
log.debug(_("XML stream is initialized"))
if not self.host_app.trigger.point("xml_init", self):
return
self.postStreamInit()
@ -246,7 +246,7 @@ class SatXMPPEntity(object):
def postStreamInit(self):
"""Workflow after stream initalisation."""
log.info(
_(u"********** [{profile}] CONNECTED **********").format(profile=self.profile)
_("********** [{profile}] CONNECTED **********").format(profile=self.profile)
)
# the following Deferred is used to know when we are connected
@ -273,7 +273,7 @@ class SatXMPPEntity(object):
def initializationFailed(self, reason):
log.error(
_(
u"ERROR: XMPP connection failed for profile '%(profile)s': %(reason)s"
"ERROR: XMPP connection failed for profile '%(profile)s': %(reason)s"
% {"profile": self.profile, "reason": reason}
)
)
@ -306,17 +306,17 @@ class SatXMPPEntity(object):
if reason is not None and not isinstance(reason.value,
internet_error.ConnectionDone):
try:
reason_str = unicode(reason.value)
reason_str = str(reason.value)
except Exception:
# FIXME: workaround for Android were p4a strips docstrings
# while Twisted use docstring in __str__
# TODO: create a ticket upstream, Twisted should work when optimization
# is used
reason_str = unicode(reason.value.__class__)
log.warning(u"Connection {term_type}: {reason}".format(
reason_str = str(reason.value.__class__)
log.warning("Connection {term_type}: {reason}".format(
term_type = term_type,
reason=reason_str))
if not self.host_app.trigger.point(u"connection_" + term_type, connector, reason):
if not self.host_app.trigger.point("connection_" + term_type, connector, reason):
return
return cb(connector, reason)
@ -327,7 +327,7 @@ class SatXMPPEntity(object):
Retrying is disabled too, as it makes no sense to try without network, and it may
use resources (notably battery on mobiles).
"""
log.info(_(u"stopping connection because of network disabled"))
log.info(_("stopping connection because of network disabled"))
self.factory.continueTrying = 0
self._network_disabled = True
if self.xmlstream is not None:
@ -344,13 +344,13 @@ class SatXMPPEntity(object):
except AttributeError:
# connection has not been stopped by networkDisabled
# we don't have to restart it
log.debug(u"no connection to restart")
log.debug("no connection to restart")
return
else:
del self._network_disabled
if not network_disabled:
raise exceptions.InternalError(u"network_disabled should be True")
log.info(_(u"network is available, trying to connect"))
raise exceptions.InternalError("network_disabled should be True")
log.info(_("network is available, trying to connect"))
# we want to be sure to start fresh
self.factory.resetDelay()
# we have a saved connector, meaning the connection has been stopped previously
@ -378,23 +378,23 @@ class SatXMPPEntity(object):
self.profile
) # and we remove references to this client
log.info(
_(u"********** [{profile}] DISCONNECTED **********").format(
_("********** [{profile}] DISCONNECTED **********").format(
profile=self.profile
)
)
if not self.conn_deferred.called:
if reason is None:
err = error.StreamError(u"Server unexpectedly closed the connection")
err = error.StreamError("Server unexpectedly closed the connection")
else:
err = reason
try:
if err.value.args[0][0][2] == "certificate verify failed":
err = exceptions.InvalidCertificate(
_(u"Your server certificate is not valid "
u"(its identity can't be checked).\n\n"
u"This should never happen and may indicate that "
u"somebody is trying to spy on you.\n"
u"Please contact your server administrator."))
_("Your server certificate is not valid "
"(its identity can't be checked).\n\n"
"This should never happen and may indicate that "
"somebody is trying to spy on you.\n"
"Please contact your server administrator."))
self.factory.stopTrying()
try:
# with invalid certificate, we should not retry to connect
@ -434,7 +434,7 @@ class SatXMPPEntity(object):
def entityDisconnect(self):
if not self.host_app.trigger.point("disconnecting", self):
return
log.info(_(u"Disconnecting..."))
log.info(_("Disconnecting..."))
self.stopService()
if self._connected_d is not None:
return self._connected_d
@ -443,7 +443,7 @@ class SatXMPPEntity(object):
## sending ##
def IQ(self, type_=u"set", timeout=60):
def IQ(self, type_="set", timeout=60):
"""shortcut to create an IQ element managing deferred
@param type_(unicode): IQ type ('set' or 'get')
@ -486,11 +486,11 @@ class SatXMPPEntity(object):
if data["uid"]: # key must be present but can be set to ''
# by a plugin to avoid id on purpose
message_elt["id"] = data["uid"]
for lang, subject in data["subject"].iteritems():
for lang, subject in data["subject"].items():
subject_elt = message_elt.addElement("subject", content=subject)
if lang:
subject_elt[(C.NS_XML, "lang")] = lang
for lang, message in data["message"].iteritems():
for lang, message in data["message"].items():
body_elt = message_elt.addElement("body", content=message)
if lang:
body_elt[(C.NS_XML, "lang")] = lang
@ -499,7 +499,7 @@ class SatXMPPEntity(object):
except KeyError:
if "thread_parent" in data["extra"]:
raise exceptions.InternalError(
u"thread_parent found while there is not associated thread"
"thread_parent found while there is not associated thread"
)
else:
thread_elt = message_elt.addElement("thread", content=thread)
@ -546,7 +546,7 @@ class SatXMPPEntity(object):
data = { # dict is similar to the one used in client.onMessage
"from": self.jid,
"to": to_jid,
"uid": uid or unicode(uuid.uuid4()),
"uid": uid or str(uuid.uuid4()),
"message": message,
"subject": subject,
"type": mess_type,
@ -599,15 +599,15 @@ class SatXMPPEntity(object):
):
return defer.succeed(None)
log.debug(_(u"Sending message (type {type}, to {to})")
log.debug(_("Sending message (type {type}, to {to})")
.format(type=data["type"], to=to_jid.full()))
pre_xml_treatments.addCallback(lambda __: self.generateMessageXML(data))
pre_xml_treatments.chainDeferred(post_xml_treatments)
post_xml_treatments.addCallback(self.sendMessageData)
if send_only:
log.debug(_(u"Triggers, storage and echo have been inhibited by the "
u"'send_only' parameter"))
log.debug(_("Triggers, storage and echo have been inhibited by the "
"'send_only' parameter"))
else:
self.addPostXmlCallbacks(post_xml_treatments)
post_xml_treatments.addErrback(self._cancelErrorTrap)
@ -625,22 +625,22 @@ class SatXMPPEntity(object):
@param data: message data dictionnary
@param client: profile's client
"""
if data[u"type"] != C.MESS_TYPE_GROUPCHAT:
if data["type"] != C.MESS_TYPE_GROUPCHAT:
# we don't add groupchat message to history, as we get them back
# and they will be added then
if data[u"message"] or data[u"subject"]: # we need a message to store
if data["message"] or data["subject"]: # we need a message to store
self.host_app.memory.addToHistory(self, data)
else:
log.warning(
u"No message found"
"No message found"
) # empty body should be managed by plugins before this point
return data
def messageGetBridgeArgs(self, data):
"""Generate args to use with bridge from data dict"""
return (data[u"uid"], data[u"timestamp"], data[u"from"].full(),
data[u"to"].full(), data[u"message"], data[u"subject"],
data[u"type"], data[u"extra"])
return (data["uid"], data["timestamp"], data["from"].full(),
data["to"].full(), data["message"], data["subject"],
data["type"], data["extra"])
def messageSendToBridge(self, data):
@ -649,10 +649,10 @@ class SatXMPPEntity(object):
@param data: message data dictionnary
@param client: profile's client
"""
if data[u"type"] != C.MESS_TYPE_GROUPCHAT:
if data["type"] != C.MESS_TYPE_GROUPCHAT:
# we don't send groupchat message to bridge, as we get them back
# and they will be added the
if (data[u"message"] or data[u"subject"]): # we need a message to send
if (data["message"] or data["subject"]): # we need a message to send
# something
# We send back the message, so all frontends are aware of it
@ -661,12 +661,12 @@ class SatXMPPEntity(object):
profile=self.profile
)
else:
log.warning(_(u"No message found"))
log.warning(_("No message found"))
return data
@implementer(iwokkel.IDisco)
class SatXMPPClient(SatXMPPEntity, wokkel_client.XMPPClient):
implements(iwokkel.IDisco)
trigger_suffix = ""
is_component = False
@ -681,34 +681,34 @@ class SatXMPPClient(SatXMPPEntity, wokkel_client.XMPPClient):
# with a web frontend,
# etc., we should implement a way to dynamically update identities through the
# bridge
self.identities = [disco.DiscoIdentity(u"client", u"pc", C.APP_NAME)]
self.identities = [disco.DiscoIdentity("client", "pc", C.APP_NAME)]
if sys.platform == "android":
# FIXME: temporary hack as SRV is not working on android
# TODO: remove this hack and fix SRV
log.info(u"FIXME: Android hack, ignoring SRV")
log.info("FIXME: Android hack, ignoring SRV")
if host is None:
host = user_jid.host
# for now we consider Android devices to be always phones
self.identities = [disco.DiscoIdentity(u"client", u"phone", C.APP_NAME)]
self.identities = [disco.DiscoIdentity("client", "phone", C.APP_NAME)]
hosts_map = host_app.memory.getConfig(None, "hosts_dict", {})
if host is None and user_jid.host in hosts_map:
host_data = hosts_map[user_jid.host]
if isinstance(host_data, basestring):
if isinstance(host_data, str):
host = host_data
elif isinstance(host_data, dict):
if u"host" in host_data:
host = host_data[u"host"]
if u"port" in host_data:
port = host_data[u"port"]
if "host" in host_data:
host = host_data["host"]
if "port" in host_data:
port = host_data["port"]
else:
log.warning(
_(u"invalid data used for host: {data}").format(data=host_data)
_("invalid data used for host: {data}").format(data=host_data)
)
host_data = None
if host_data is not None:
log.info(
u"using {host}:{port} for host {host_ori} as requested in config"
"using {host}:{port} for host {host_ori} as requested in config"
.format(host_ori=user_jid.host, host=host, port=port)
)
@ -717,22 +717,22 @@ class SatXMPPClient(SatXMPPEntity, wokkel_client.XMPPClient):
wokkel_client.XMPPClient.__init__(
self, user_jid, password, host or None, port or C.XMPP_C2S_PORT,
check_certificate = self.check_certificate
# check_certificate = self.check_certificate # FIXME: currently disabled with Python 3 port
)
SatXMPPEntity.__init__(self, host_app, profile, max_retries)
if not self.check_certificate:
msg = (_(u"Certificate validation is deactivated, this is unsecure and "
u"somebody may be spying on you. If you have no good reason to disable "
u"certificate validation, please activate \"Check certificate\" in your "
u"settings in \"Connection\" tab."))
xml_tools.quickNote(host_app, self, msg, _(u"Security notice"),
msg = (_("Certificate validation is deactivated, this is unsecure and "
"somebody may be spying on you. If you have no good reason to disable "
"certificate validation, please activate \"Check certificate\" in your "
"settings in \"Connection\" tab."))
xml_tools.quickNote(host_app, self, msg, _("Security notice"),
level = C.XMLUI_DATA_LVL_WARNING)
def _getPluginsList(self):
for p in self.host_app.plugins.itervalues():
if C.PLUG_MODE_CLIENT in p._info[u"modes"]:
for p in self.host_app.plugins.values():
if C.PLUG_MODE_CLIENT in p._info["modes"]:
yield p
def _createSubProtocols(self):
@ -795,7 +795,7 @@ class SatXMPPClient(SatXMPPEntity, wokkel_client.XMPPClient):
# This trigger point can't cancel the method
yield self.host_app.trigger.asyncPoint("sendMessageData", self, mess_data,
triggers_no_cancel=True)
self.send(mess_data[u"xml"])
self.send(mess_data["xml"])
defer.returnValue(mess_data)
def feedback(self, to_jid, message, extra=None):
@ -811,11 +811,11 @@ class SatXMPPClient(SatXMPPEntity, wokkel_client.XMPPClient):
if extra is None:
extra = {}
self.host_app.bridge.messageNew(
uid=unicode(uuid.uuid4()),
uid=str(uuid.uuid4()),
timestamp=time.time(),
from_jid=self.jid.full(),
to_jid=to_jid.full(),
message={u"": message},
message={"": message},
subject={},
mess_type=C.MESS_TYPE_INFO,
extra=extra,
@ -827,6 +827,7 @@ class SatXMPPClient(SatXMPPEntity, wokkel_client.XMPPClient):
d.addCallback(lambda __: super(SatXMPPClient, self)._finish_connection(__))
@implementer(iwokkel.IDisco)
class SatXMPPComponent(SatXMPPEntity, component.Component):
"""XMPP component
@ -835,7 +836,6 @@ class SatXMPPComponent(SatXMPPEntity, component.Component):
Component need to instantiate MessageProtocol itself
"""
implements(iwokkel.IDisco)
trigger_suffix = (
"Component"
) # used for to distinguish some trigger points set in SatXMPPEntity
@ -857,19 +857,19 @@ class SatXMPPComponent(SatXMPPEntity, component.Component):
self.entry_plugin = host_app.plugins[entry_point]
except KeyError:
raise exceptions.NotFound(
_(u"The requested entry point ({entry_point}) is not available").format(
_("The requested entry point ({entry_point}) is not available").format(
entry_point=entry_point
)
)
self.identities = [disco.DiscoIdentity(u"component", u"generic", C.APP_NAME)]
self.identities = [disco.DiscoIdentity("component", "generic", C.APP_NAME)]
# jid is set automatically on bind by Twisted for Client, but not for Component
self.jid = component_jid
if host is None:
try:
host = component_jid.host.split(u".", 1)[1]
host = component_jid.host.split(".", 1)[1]
except IndexError:
raise ValueError(u"Can't guess host from jid, please specify a host")
raise ValueError("Can't guess host from jid, please specify a host")
# XXX: component.Component expect unicode jid, while Client expect jid.JID.
# this is not consistent, so we use jid.JID for SatXMPP*
component.Component.__init__(self, host, port, component_jid.full(), password)
@ -890,20 +890,20 @@ class SatXMPPComponent(SatXMPPEntity, component.Component):
@raise KeyError: one plugin should be present in self.host_app.plugins but it
is not
"""
if C.PLUG_MODE_COMPONENT not in current._info[u"modes"]:
if C.PLUG_MODE_COMPONENT not in current._info["modes"]:
if not required:
return
else:
log.error(
_(
u"Plugin {current_name} is needed for {entry_name}, "
u"but it doesn't handle component mode"
"Plugin {current_name} is needed for {entry_name}, "
"but it doesn't handle component mode"
).format(
current_name=current._info[u"import_name"],
entry_name=self.entry_plugin._info[u"import_name"],
current_name=current._info["import_name"],
entry_name=self.entry_plugin._info["import_name"],
)
)
raise exceptions.InternalError(_(u"invalid plugin mode"))
raise exceptions.InternalError(_("invalid plugin mode"))
for import_name in current._info.get(C.PI_DEPENDENCIES, []):
# plugins are already loaded as dependencies
@ -960,9 +960,9 @@ class SatMessageProtocol(xmppim.MessageProtocol):
if None, mapping will not be done
@return(dict): message data
"""
if message_elt.name != u"message":
if message_elt.name != "message":
log.warning(_(
u"parseMessage used with a non <message/> stanza, ignoring: {xml}"
"parseMessage used with a non <message/> stanza, ignoring: {xml}"
.format(xml=message_elt.toXml())))
return {}
@ -974,31 +974,31 @@ class SatMessageProtocol(xmppim.MessageProtocol):
c.uri = C.NS_CLIENT
elif message_elt.uri != C.NS_CLIENT:
log.warning(_(
u"received <message> with a wrong namespace: {xml}"
"received <message> with a wrong namespace: {xml}"
.format(xml=message_elt.toXml())))
client = self.parent
if not message_elt.hasAttribute(u'to'):
if not message_elt.hasAttribute('to'):
message_elt['to'] = client.jid.full()
message = {}
subject = {}
extra = {}
data = {
u"from": jid.JID(message_elt["from"]),
u"to": jid.JID(message_elt["to"]),
u"uid": message_elt.getAttribute(
u"uid", unicode(uuid.uuid4())
"from": jid.JID(message_elt["from"]),
"to": jid.JID(message_elt["to"]),
"uid": message_elt.getAttribute(
"uid", str(uuid.uuid4())
), # XXX: uid is not a standard attribute but may be added by plugins
u"message": message,
u"subject": subject,
u"type": message_elt.getAttribute(u"type", u"normal"),
u"extra": extra,
"message": message,
"subject": subject,
"type": message_elt.getAttribute("type", "normal"),
"extra": extra,
}
try:
message_id = data[u"extra"][u"message_id"] = message_elt[u"id"]
message_id = data["extra"]["message_id"] = message_elt["id"]
except KeyError:
pass
else:
@ -1006,11 +1006,11 @@ class SatMessageProtocol(xmppim.MessageProtocol):
# message
for e in message_elt.elements(C.NS_CLIENT, "body"):
message[e.getAttribute((C.NS_XML, "lang"), "")] = unicode(e)
message[e.getAttribute((C.NS_XML, "lang"), "")] = str(e)
# subject
for e in message_elt.elements(C.NS_CLIENT, "subject"):
subject[e.getAttribute((C.NS_XML, "lang"), "")] = unicode(e)
subject[e.getAttribute((C.NS_XML, "lang"), "")] = str(e)
# delay and timestamp
try:
@ -1018,12 +1018,12 @@ class SatMessageProtocol(xmppim.MessageProtocol):
except AttributeError:
# message_elt._received_timestamp should have been set in onMessage
# but if parseMessage is called directly, it can be missing
log.debug(u"missing received timestamp for {message_elt}".format(
log.debug("missing received timestamp for {message_elt}".format(
message_elt=message_elt))
received_timestamp = time.time()
try:
delay_elt = message_elt.elements(delay.NS_DELAY, "delay").next()
delay_elt = next(message_elt.elements(delay.NS_DELAY, "delay"))
except StopIteration:
data["timestamp"] = received_timestamp
else:
@ -1060,7 +1060,7 @@ class SatMessageProtocol(xmppim.MessageProtocol):
client = self.parent
if not "from" in message_elt.attributes:
message_elt["from"] = client.jid.host
log.debug(_(u"got message from: {from_}").format(from_=message_elt["from"]))
log.debug(_("got message from: {from_}").format(from_=message_elt["from"]))
# plugin can add their treatments to this deferred
post_treat = defer.Deferred()
@ -1077,24 +1077,24 @@ class SatMessageProtocol(xmppim.MessageProtocol):
return data
def addToHistory(self, data):
if data.pop(u"history", None) == C.HISTORY_SKIP:
log.info(u"history is skipped as requested")
data[u"extra"][u"history"] = C.HISTORY_SKIP
if data.pop("history", None) == C.HISTORY_SKIP:
log.info("history is skipped as requested")
data["extra"]["history"] = C.HISTORY_SKIP
else:
if data[u"message"] or data[u"subject"]: # we need a message to store
if data["message"] or data["subject"]: # we need a message to store
return self.host.memory.addToHistory(self.parent, data)
else:
log.debug(u"not storing empty message to history: {data}"
log.debug("not storing empty message to history: {data}"
.format(data=data))
def bridgeSignal(self, __, data):
try:
data["extra"]["received_timestamp"] = unicode(data["received_timestamp"])
data["extra"]["received_timestamp"] = str(data["received_timestamp"])
data["extra"]["delay_sender"] = data["delay_sender"]
except KeyError:
pass
if C.MESS_KEY_ENCRYPTION in data:
data[u"extra"][u"encrypted"] = C.BOOL_TRUE
data["extra"]["encrypted"] = C.BOOL_TRUE
if data is not None:
if data["message"] or data["subject"] or data["type"] == C.MESS_TYPE_INFO:
self.host.bridge.messageNew(
@ -1109,7 +1109,7 @@ class SatMessageProtocol(xmppim.MessageProtocol):
profile=self.parent.profile,
)
else:
log.debug(u"Discarding bridge signal for empty message: {data}".format(
log.debug("Discarding bridge signal for empty message: {data}".format(
data=data))
return data
@ -1131,7 +1131,7 @@ class SatRosterProtocol(xmppim.RosterClientProtocol):
@property
def versioning(self):
"""True if server support roster versioning"""
return (NS_ROSTER_VER, u'ver') in self.parent.xmlstream.features
return (NS_ROSTER_VER, 'ver') in self.parent.xmlstream.features
@property
def roster_cache(self):
@ -1148,23 +1148,23 @@ class SatRosterProtocol(xmppim.RosterClientProtocol):
item must be already registered in self._jids before this method is called
@param item (RosterIem): item added
"""
log.debug(u"registering item: {}".format(item.entity.full()))
log.debug("registering item: {}".format(item.entity.full()))
if item.entity.resource:
log.warning(
u"Received a roster item with a resource, this is not common but not "
u"restricted by RFC 6121, this case may be not well tested."
"Received a roster item with a resource, this is not common but not "
"restricted by RFC 6121, this case may be not well tested."
)
if not item.subscriptionTo:
if not item.subscriptionFrom:
log.info(
_(u"There's no subscription between you and [{}]!").format(
_("There's no subscription between you and [{}]!").format(
item.entity.full()
)
)
else:
log.info(_(u"You are not subscribed to [{}]!").format(item.entity.full()))
log.info(_("You are not subscribed to [{}]!").format(item.entity.full()))
if not item.subscriptionFrom:
log.info(_(u"[{}] is not subscribed to you!").format(item.entity.full()))
log.info(_("[{}] is not subscribed to you!").format(item.entity.full()))
for group in item.groups:
self._groups.setdefault(group, set()).add(item.entity)
@ -1178,7 +1178,7 @@ class SatRosterProtocol(xmppim.RosterClientProtocol):
roster_cache = self.roster_cache
yield roster_cache.clear()
roster_cache[ROSTER_VER_KEY] = version
for roster_jid, roster_item in self._jids.iteritems():
for roster_jid, roster_item in self._jids.items():
roster_jid_s = roster_jid.full()
roster_item_elt = roster_item.toElement().toXml()
roster_cache[roster_jid_s] = roster_item_elt
@ -1200,19 +1200,19 @@ class SatRosterProtocol(xmppim.RosterClientProtocol):
def requestRoster(self):
"""Ask the server for Roster list """
if self.versioning:
log.info(_(u"our server support roster versioning, we use it"))
log.info(_("our server support roster versioning, we use it"))
roster_cache = self.roster_cache
yield roster_cache.load()
try:
version = roster_cache[ROSTER_VER_KEY]
except KeyError:
log.info(_(u"no roster in cache, we start fresh"))
log.info(_("no roster in cache, we start fresh"))
# u"" means we use versioning without valid roster in cache
version = u""
version = ""
else:
log.info(_(u"We have roster v{version} in cache").format(version=version))
log.info(_("We have roster v{version} in cache").format(version=version))
# we deserialise cached roster to our local cache
for roster_jid_s, roster_item_elt_s in roster_cache.iteritems():
for roster_jid_s, roster_item_elt_s in roster_cache.items():
if roster_jid_s == ROSTER_VER_KEY:
continue
roster_jid = jid.JID(roster_jid_s)
@ -1221,26 +1221,26 @@ class SatRosterProtocol(xmppim.RosterClientProtocol):
self._jids[roster_jid] = roster_item
self._registerItem(roster_item)
else:
log.warning(_(u"our server doesn't support roster versioning"))
log.warning(_("our server doesn't support roster versioning"))
version = None
log.debug("requesting roster")
roster = yield self.getRoster(version=version)
if roster is None:
log.debug(u"empty roster result received, we'll get roster item with roster "
u"pushes")
log.debug("empty roster result received, we'll get roster item with roster "
"pushes")
else:
# a full roster is received
self._groups.clear()
self._jids = roster
for item in roster.itervalues():
for item in roster.values():
if not item.subscriptionTo and not item.subscriptionFrom and not item.ask:
# XXX: current behaviour: we don't want contact in our roster list
# if there is no presence subscription
# may change in the future
log.info(
u"Removing contact {} from roster because there is no presence "
u"subscription".format(
"Removing contact {} from roster because there is no presence "
"subscription".format(
item.jid
)
)
@ -1267,9 +1267,9 @@ class SatRosterProtocol(xmppim.RosterClientProtocol):
@return: dictionary of attributes
"""
item_attr = {
"to": unicode(item.subscriptionTo),
"from": unicode(item.subscriptionFrom),
"ask": unicode(item.ask),
"to": str(item.subscriptionTo),
"from": str(item.subscriptionFrom),
"ask": str(item.ask),
}
if item.name:
item_attr["name"] = item.name
@ -1278,7 +1278,7 @@ class SatRosterProtocol(xmppim.RosterClientProtocol):
def setReceived(self, request):
item = request.item
entity = item.entity
log.info(_(u"adding {entity} to roster").format(entity=entity.full()))
log.info(_("adding {entity} to roster").format(entity=entity.full()))
if request.version is not None:
# we update the cache in storage
roster_cache = self.roster_cache
@ -1302,7 +1302,7 @@ class SatRosterProtocol(xmppim.RosterClientProtocol):
def removeReceived(self, request):
entity = request.item.entity
log.info(_(u"removing {entity} from roster").format(entity=entity.full()))
log.info(_("removing {entity} from roster").format(entity=entity.full()))
if request.version is not None:
# we update the cache in storage
roster_cache = self.roster_cache
@ -1319,7 +1319,7 @@ class SatRosterProtocol(xmppim.RosterClientProtocol):
item = self._jids.pop(entity)
except KeyError:
log.error(
u"Received a roster remove event for an item not in cache ({})".format(
"Received a roster remove event for an item not in cache ({})".format(
entity
)
)
@ -1332,8 +1332,8 @@ class SatRosterProtocol(xmppim.RosterClientProtocol):
del self._groups[group]
except KeyError:
log.warning(
u"there is no cache for the group [{group}] of the removed roster "
u"item [{jid_}]".format(group=group, jid=entity)
"there is no cache for the group [{group}] of the removed roster "
"item [{jid_}]".format(group=group, jid=entity)
)
# then we send the bridge signal
@ -1341,7 +1341,7 @@ class SatRosterProtocol(xmppim.RosterClientProtocol):
def getGroups(self):
"""Return a list of groups"""
return self._groups.keys()
return list(self._groups.keys())
def getItem(self, entity_jid):
"""Return RosterItem for a given jid
@ -1354,7 +1354,7 @@ class SatRosterProtocol(xmppim.RosterClientProtocol):
def getJids(self):
"""Return all jids of the roster"""
return self._jids.keys()
return list(self._jids.keys())
def isJidInRoster(self, entity_jid):
"""Return True if jid is in roster"""
@ -1370,7 +1370,7 @@ class SatRosterProtocol(xmppim.RosterClientProtocol):
def getItems(self):
"""Return all items of the roster"""
return self._jids.values()
return list(self._jids.values())
def getJidsFromGroup(self, group):
try:
@ -1398,7 +1398,7 @@ class SatRosterProtocol(xmppim.RosterClientProtocol):
jids.update(self.getJidsFromGroup(group))
return jids
else:
raise ValueError(u"Unexpected type_ {}".format(type_))
raise ValueError("Unexpected type_ {}".format(type_))
def getNick(self, entity_jid):
"""Return a nick name for an entity
@ -1447,7 +1447,7 @@ class SatPresenceProtocol(xmppim.PresenceClientProtocol):
def unavailableReceived(self, entity, statuses=None):
log.debug(
_(u"presence update for [%(entity)s] (unavailable, statuses=%(statuses)s)")
_("presence update for [%(entity)s] (unavailable, statuses=%(statuses)s)")
% {"entity": entity, C.PRESENCE_STATUSES: statuses}
)
@ -1539,16 +1539,16 @@ class SatPresenceProtocol(xmppim.PresenceClientProtocol):
self.host.memory.delWaitingSub(entity.userhost(), self.parent.profile)
def subscribedReceived(self, entity):
log.debug(_(u"subscription approved for [%s]") % entity.userhost())
log.debug(_("subscription approved for [%s]") % entity.userhost())
self.host.bridge.subscribe("subscribed", entity.userhost(), self.parent.profile)
def unsubscribedReceived(self, entity):
log.debug(_(u"unsubscription confirmed for [%s]") % entity.userhost())
log.debug(_("unsubscription confirmed for [%s]") % entity.userhost())
self.host.bridge.subscribe("unsubscribed", entity.userhost(), self.parent.profile)
@defer.inlineCallbacks
def subscribeReceived(self, entity):
log.debug(_(u"subscription request from [%s]") % entity.userhost())
log.debug(_("subscription request from [%s]") % entity.userhost())
yield self.parent.roster.got_roster
item = self.parent.roster.getItem(entity)
if item and item.subscriptionTo:
@ -1566,7 +1566,7 @@ class SatPresenceProtocol(xmppim.PresenceClientProtocol):
@defer.inlineCallbacks
def unsubscribeReceived(self, entity):
log.debug(_(u"unsubscription asked for [%s]") % entity.userhost())
log.debug(_("unsubscription asked for [%s]") % entity.userhost())
yield self.parent.roster.got_roster
item = self.parent.roster.getItem(entity)
if item and item.subscriptionFrom: # we automatically remove contact
@ -1575,8 +1575,8 @@ class SatPresenceProtocol(xmppim.PresenceClientProtocol):
self.host.bridge.subscribe("unsubscribe", entity.userhost(), self.parent.profile)
@implementer(iwokkel.IDisco)
class SatDiscoProtocol(disco.DiscoClientProtocol):
implements(iwokkel.IDisco)
def __init__(self, host):
disco.DiscoClientProtocol.__init__(self)
@ -1599,7 +1599,7 @@ class SatFallbackHandler(generic.FallbackHandler):
def iqFallback(self, iq):
if iq.handled is True:
return
log.debug(u"iqFallback: xml = [%s]" % (iq.toXml()))
log.debug("iqFallback: xml = [%s]" % (iq.toXml()))
generic.FallbackHandler.iqFallback(self, iq)
@ -1615,9 +1615,9 @@ class SatVersionHandler(generic.VersionHandler):
return generic.VersionHandler.getDiscoInfo(self, requestor, target, None)
@implementer(iwokkel.IDisco)
class SatIdentityHandler(XMPPHandler):
"""Manage disco Identity of SàT."""
implements(iwokkel.IDisco)
# TODO: dynamic identity update (see docstring). Note that a XMPP entity can have
# several identities