diff sat/core/constants.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 ff5bcb12ae60
children fee60f17ebac
line wrap: on
line diff
--- a/sat/core/constants.py	Wed Jul 31 11:31:22 2019 +0200
+++ b/sat/core/constants.py	Tue Aug 13 19:08:41 2019 +0200
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
 # -*- coding: utf-8 -*-
 
 # SàT: a XMPP client
@@ -28,21 +28,21 @@
 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 @@
     )
     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 @@
     ## 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 @@
     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 @@
     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 @@
     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 @@
     @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 @@
         """@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
+        ]
+