Handles non-english charset encoding for filesystem paths

This commit is contained in:
Manuel Cortez 2018-12-14 15:27:20 -06:00
parent 7f2956e47a
commit 4b5d271ab4
7 changed files with 32 additions and 40 deletions

View File

@ -1,4 +1,5 @@
# -*- coding: cp1252 -*- # -*- coding: cp1252 -*-
import os
import config_utils import config_utils
import paths import paths
import logging import logging
@ -12,5 +13,5 @@ app = None
def setup (): def setup ():
global app global app
log.debug("Loading global app settings...") 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))

View File

@ -7,5 +7,5 @@ log = logging.getLogger("fixes.fix_requests")
def fix(): def fix():
log.debug("Applying fix for requests...") log.debug("Applying fix for requests...")
os.environ["REQUESTS_CA_BUNDLE"] = paths.app_path("cacert.pem") 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" % (paths.app_path("cacert.pem"),)) log.debug("Changed CA path to %s" % (os.environ["REQUESTS_CA_BUNDLE"].decode(paths.fsencoding)))

View File

@ -1,4 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os
import logging import logging
from logging.handlers import RotatingFileHandler from logging.handlers import RotatingFileHandler
import paths import paths
@ -19,12 +21,12 @@ logger.setLevel(logging.DEBUG)
#handlers #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.setFormatter(formatter)
app_handler.setLevel(logging.DEBUG) app_handler.setLevel(logging.DEBUG)
logger.addHandler(app_handler) 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.setFormatter(formatter)
error_handler.setLevel(logging.ERROR) error_handler.setLevel(logging.ERROR)
logger.addHandler(error_handler) logger.addHandler(error_handler)

View File

@ -21,7 +21,7 @@ def setup():
log.debug("Starting Socializer %s" % (application.version,)) log.debug("Starting Socializer %s" % (application.version,))
config.setup() config.setup()
log.debug("Using %s %s" % (platform.system(), platform.architecture()[0])) 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(),)) log.debug("config path is %s" % (paths.config_path(),))
output.setup() output.setup()
languageHandler.setLanguage(config.app["app-settings"]["language"]) languageHandler.setLanguage(config.app["app-settings"]["language"])

View File

@ -1,4 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals
import sys
import platform import platform
import os import os
import sys import sys
@ -9,47 +11,38 @@ from functools import wraps
mode = "portable" mode = "portable"
directory = None directory = None
fsencoding = sys.getfilesystemencoding()
#log = logging.getLogger("paths") #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(): def app_path():
return paths_.app_path() return paths_.app_path()
@merge_paths
def config_path(): def config_path():
global mode, directory global mode, directory
if mode == "portable": if mode == "portable":
if directory != None: path = os.path.join(directory, "config") if directory != None: path = os.path.join(directory.decode(fsencoding), "config")
elif directory == None: path = app_path(u"config") elif directory == None: path = os.path.join(app_path().decode(fsencoding), "config")
elif mode == "installed": elif mode == "installed":
path = data_path("config") path = os.path.join(data_path().decode(fsencoding), "config")
if not os.path.exists(path): if not os.path.exists(path):
# log.debug("%s path does not exist, creating..." % (path,)) # log.debug("%s path does not exist, creating..." % (path,))
os.mkdir(path) os.mkdir(path)
return path return path
@merge_paths
def logs_path(): def logs_path():
global mode, directory global mode, directory
if mode == "portable": if mode == "portable":
if directory != None: path = os.path.join(directory, "logs") if directory != None: path = os.path.join(directory.decode(fsencoding), "logs")
elif directory == None: path = app_path(u"logs") elif directory == None: path = os.path.join(app_path().decode(fsencoding), "logs")
elif mode == "installed": elif mode == "installed":
path = data_path("logs") path = os.path.join(data_path().decode(fsencoding), "logs")
if not os.path.exists(path): if not os.path.exists(path):
# log.debug("%s path does not exist, creating..." % (path,)) # log.debug("%s path does not exist, creating..." % (path,))
os.mkdir(path) os.mkdir(path)
return path return path
@merge_paths def data_path(app_name='socializer'):
def data_path(app_name='TW blue'):
if platform.system() == "Windows": if platform.system() == "Windows":
data_path = os.path.join(os.getenv("AppData"), app_name) data_path = os.path.join(os.getenv("AppData"), app_name)
else: else:
@ -58,22 +51,19 @@ def data_path(app_name='TW blue'):
os.mkdir(data_path) os.mkdir(data_path)
return data_path return data_path
@merge_paths
def locale_path(): def locale_path():
return app_path(u"locales") return os.path.join(app_path().decode(fsencoding), "locales")
@merge_paths
def sound_path(): def sound_path():
return app_path(u"sounds") return os.path.join(app_path().decode(fsencoding), "sounds")
@merge_paths
def com_path(): def com_path():
global mode, directory global mode, directory
if mode == "portable": if mode == "portable":
if directory != None: path = os.path.join(directory, "com_cache") if directory != None: path = os.path.join(directory.decode(fsencoding), "com_cache")
elif directory == None: path = app_path(u"com_cache") elif directory == None: path = os.path.join(app_path().decode(fsencoding), "com_cache")
elif mode == "installed": 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): if not os.path.exists(path):
# log.debug("%s path does not exist, creating..." % (path,)) # log.debug("%s path does not exist, creating..." % (path,))
os.mkdir(path) os.mkdir(path)

View File

@ -23,10 +23,9 @@ class sessionManagerController(object):
def fill_list(self): def fill_list(self):
log.debug("Filling the session list...") log.debug("Filling the session list...")
for i in os.listdir(paths.config_path()): 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,)) log.debug("Adding session %s" % (i,))
strconfig = "%s/session.conf" % (paths.config_path(i)) config_test = Configuration(os.path.join(paths.config_path(), i, "session.conf"))
config_test = Configuration(strconfig)
name = config_test["vk"]["user"] name = config_test["vk"]["user"]
if name != "" and config_test["vk"]["password"] != "": if name != "" and config_test["vk"]["password"] != "":
self.session = i self.session = i
@ -39,9 +38,8 @@ class sessionManagerController(object):
location = (str(time.time())[-6:]) location = (str(time.time())[-6:])
log.debug("Creating session in the %s path" % (location,)) log.debug("Creating session in the %s path" % (location,))
s = session.vkSession(location) s = session.vkSession(location)
path = paths.config_path(location) path = os.path.join(paths.config_path(), location)
if not os.path.exists(path): if not os.path.exists(path):
log.debug("Creating %s path" % (paths.config_path(path),))
os.mkdir(path) os.mkdir(path)
s.get_configuration() s.get_configuration()
self.get_authorisation(s) self.get_authorisation(s)

View File

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
""" Sound utilities for socialized.""" """ Sound utilities for socialized."""
from __future__ import unicode_literals
import sys import sys
import os import os
import logging as original_logger import logging as original_logger
@ -17,12 +18,12 @@ class soundSystem(object):
def check_soundpack(self): def check_soundpack(self):
""" Checks if the folder where live the current soundpack exists.""" """ Checks if the folder where live the current soundpack exists."""
self.soundpack_OK = False self.soundpack_OK = False
if os.path.exists(paths.sound_path(self.config["current_soundpack"])): if os.path.exists(os.path.join(paths.sound_path(), self.config["current_soundpack"])):
self.path = paths.sound_path(self.config["current_soundpack"]) self.path = os.path.join(paths.sound_path(), self.config["current_soundpack"])
self.soundpack_OK = True 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...") 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 self.soundpack_OK = True
else: else:
log.error("The current soundpack could not be found and the default soundpack has been deleted, Socializer will not play sounds.") log.error("The current soundpack could not be found and the default soundpack has been deleted, Socializer will not play sounds.")