From 4b5d271ab4f82598651931f5779333a1ae102927 Mon Sep 17 00:00:00 2001 From: Manuel Cortez Date: Fri, 14 Dec 2018 15:27:20 -0600 Subject: [PATCH] Handles non-english charset encoding for filesystem paths --- src/config.py | 3 ++- src/fixes/fix_requests.py | 4 +-- src/logger.py | 6 +++-- src/main.py | 2 +- src/paths.py | 40 +++++++++++----------------- src/sessionmanager/sessionManager.py | 8 +++--- src/sound.py | 9 ++++--- 7 files changed, 32 insertions(+), 40 deletions(-) diff --git a/src/config.py b/src/config.py index f23342d..e6b6d78 100644 --- a/src/config.py +++ b/src/config.py @@ -1,4 +1,5 @@ # -*- coding: cp1252 -*- +import os import config_utils import paths import logging @@ -12,5 +13,5 @@ app = None def setup (): global app log.debug("Loading global app settings...") - app = config_utils.load_config(paths.config_path(MAINFILE), paths.app_path(MAINSPEC)) + app = config_utils.load_config(os.path.join(paths.config_path(), MAINFILE), os.path.join(paths.app_path(), MAINSPEC)) \ No newline at end of file diff --git a/src/fixes/fix_requests.py b/src/fixes/fix_requests.py index cecc95f..424c1c8 100644 --- a/src/fixes/fix_requests.py +++ b/src/fixes/fix_requests.py @@ -7,5 +7,5 @@ log = logging.getLogger("fixes.fix_requests") def fix(): log.debug("Applying fix for requests...") - os.environ["REQUESTS_CA_BUNDLE"] = paths.app_path("cacert.pem") - log.debug("Changed CA path to %s" % (paths.app_path("cacert.pem"),)) \ No newline at end of file + os.environ["REQUESTS_CA_BUNDLE"] = os.path.join(paths.app_path().decode(paths.fsencoding), "cacert.pem").encode(paths.fsencoding) + log.debug("Changed CA path to %s" % (os.environ["REQUESTS_CA_BUNDLE"].decode(paths.fsencoding))) \ No newline at end of file diff --git a/src/logger.py b/src/logger.py index b37a01b..7a6042d 100644 --- a/src/logger.py +++ b/src/logger.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +from __future__ import unicode_literals +import os import logging from logging.handlers import RotatingFileHandler import paths @@ -19,12 +21,12 @@ logger.setLevel(logging.DEBUG) #handlers -app_handler = RotatingFileHandler(paths.logs_path(APP_LOG_FILE), mode="w") +app_handler = RotatingFileHandler(os.path.join(paths.logs_path(), APP_LOG_FILE), mode="w") app_handler.setFormatter(formatter) app_handler.setLevel(logging.DEBUG) logger.addHandler(app_handler) -error_handler = logging.FileHandler(paths.logs_path(ERROR_LOG_FILE), mode="w") +error_handler = logging.FileHandler(os.path.join(paths.logs_path(), ERROR_LOG_FILE), mode="w") error_handler.setFormatter(formatter) error_handler.setLevel(logging.ERROR) logger.addHandler(error_handler) diff --git a/src/main.py b/src/main.py index 1c53025..bf89c04 100644 --- a/src/main.py +++ b/src/main.py @@ -21,7 +21,7 @@ def setup(): log.debug("Starting Socializer %s" % (application.version,)) config.setup() log.debug("Using %s %s" % (platform.system(), platform.architecture()[0])) - log.debug("Application path is %s" % (paths.app_path(),)) + log.debug("Application path is %s" % (paths.app_path().decode(paths.fsencoding),)) log.debug("config path is %s" % (paths.config_path(),)) output.setup() languageHandler.setLanguage(config.app["app-settings"]["language"]) diff --git a/src/paths.py b/src/paths.py index c99bbe0..377ec81 100644 --- a/src/paths.py +++ b/src/paths.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +from __future__ import unicode_literals +import sys import platform import os import sys @@ -9,47 +11,38 @@ from functools import wraps mode = "portable" directory = None +fsencoding = sys.getfilesystemencoding() #log = logging.getLogger("paths") -def merge_paths(func): - @wraps(func) - def merge_paths_wrapper(*a): - return unicode(os.path.join(func(), *a)) - return merge_paths_wrapper - -@merge_paths def app_path(): return paths_.app_path() -@merge_paths def config_path(): global mode, directory if mode == "portable": - if directory != None: path = os.path.join(directory, "config") - elif directory == None: path = app_path(u"config") + if directory != None: path = os.path.join(directory.decode(fsencoding), "config") + elif directory == None: path = os.path.join(app_path().decode(fsencoding), "config") elif mode == "installed": - path = data_path("config") + path = os.path.join(data_path().decode(fsencoding), "config") if not os.path.exists(path): # log.debug("%s path does not exist, creating..." % (path,)) os.mkdir(path) return path -@merge_paths def logs_path(): global mode, directory if mode == "portable": - if directory != None: path = os.path.join(directory, "logs") - elif directory == None: path = app_path(u"logs") + if directory != None: path = os.path.join(directory.decode(fsencoding), "logs") + elif directory == None: path = os.path.join(app_path().decode(fsencoding), "logs") elif mode == "installed": - path = data_path("logs") + path = os.path.join(data_path().decode(fsencoding), "logs") if not os.path.exists(path): # log.debug("%s path does not exist, creating..." % (path,)) os.mkdir(path) return path -@merge_paths -def data_path(app_name='TW blue'): +def data_path(app_name='socializer'): if platform.system() == "Windows": data_path = os.path.join(os.getenv("AppData"), app_name) else: @@ -58,22 +51,19 @@ def data_path(app_name='TW blue'): os.mkdir(data_path) return data_path -@merge_paths def locale_path(): - return app_path(u"locales") + return os.path.join(app_path().decode(fsencoding), "locales") -@merge_paths def sound_path(): - return app_path(u"sounds") + return os.path.join(app_path().decode(fsencoding), "sounds") -@merge_paths def com_path(): global mode, directory if mode == "portable": - if directory != None: path = os.path.join(directory, "com_cache") - elif directory == None: path = app_path(u"com_cache") + if directory != None: path = os.path.join(directory.decode(fsencoding), "com_cache") + elif directory == None: path = os.path.join(app_path().decode(fsencoding), "com_cache") elif mode == "installed": - path = data_path(u"com_cache") + path = os.path.join(data_path().decode(fsencoding), "com_cache") if not os.path.exists(path): # log.debug("%s path does not exist, creating..." % (path,)) os.mkdir(path) diff --git a/src/sessionmanager/sessionManager.py b/src/sessionmanager/sessionManager.py index 8f5ec70..f2765f7 100644 --- a/src/sessionmanager/sessionManager.py +++ b/src/sessionmanager/sessionManager.py @@ -23,10 +23,9 @@ class sessionManagerController(object): def fill_list(self): log.debug("Filling the session list...") for i in os.listdir(paths.config_path()): - if os.path.isdir(paths.config_path(i)): + if os.path.isdir(os.path.join(paths.config_path(), i)): log.debug("Adding session %s" % (i,)) - strconfig = "%s/session.conf" % (paths.config_path(i)) - config_test = Configuration(strconfig) + config_test = Configuration(os.path.join(paths.config_path(), i, "session.conf")) name = config_test["vk"]["user"] if name != "" and config_test["vk"]["password"] != "": self.session = i @@ -39,9 +38,8 @@ class sessionManagerController(object): location = (str(time.time())[-6:]) log.debug("Creating session in the %s path" % (location,)) s = session.vkSession(location) - path = paths.config_path(location) + path = os.path.join(paths.config_path(), location) if not os.path.exists(path): - log.debug("Creating %s path" % (paths.config_path(path),)) os.mkdir(path) s.get_configuration() self.get_authorisation(s) diff --git a/src/sound.py b/src/sound.py index 2e68777..25fb651 100644 --- a/src/sound.py +++ b/src/sound.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- """ Sound utilities for socialized.""" +from __future__ import unicode_literals import sys import os import logging as original_logger @@ -17,12 +18,12 @@ class soundSystem(object): def check_soundpack(self): """ Checks if the folder where live the current soundpack exists.""" self.soundpack_OK = False - if os.path.exists(paths.sound_path(self.config["current_soundpack"])): - self.path = paths.sound_path(self.config["current_soundpack"]) + if os.path.exists(os.path.join(paths.sound_path(), self.config["current_soundpack"])): + self.path = os.path.join(paths.sound_path(), self.config["current_soundpack"]) self.soundpack_OK = True - elif os.path.exists(paths.sound_path("default")): + elif os.path.exists(os.path.join(paths.sound_path(), "default")): log.error("The soundpack does not exist, using default...") - self.path = paths.sound_path("default") + self.path = os.path.join(paths.sound_path(), "default") self.soundpack_OK = True else: log.error("The current soundpack could not be found and the default soundpack has been deleted, Socializer will not play sounds.")