From b4645c98230700c33aaf670db99005002bc1848c Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Wed, 22 Apr 2020 13:04:03 +0300 Subject: [PATCH 1/4] Fix exception in class_sqlThread on Windows when loading database from the file with unicode character in path. Closes: #165. --- src/class_sqlThread.py | 3 ++- src/paths.py | 10 +++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/class_sqlThread.py b/src/class_sqlThread.py index 84188408..2188a9cc 100644 --- a/src/class_sqlThread.py +++ b/src/class_sqlThread.py @@ -29,7 +29,8 @@ class sqlThread(threading.Thread): def run(self): # pylint: disable=too-many-locals, too-many-branches, too-many-statements """Process SQL queries from `.helper_sql.sqlSubmitQueue`""" helper_sql.sql_available = True - self.conn = sqlite3.connect(state.appdata + 'messages.dat') + self.conn = sqlite3.connect( + os.path.join(state.appdata, 'messages.dat')) self.conn.text_factory = str self.cur = self.conn.cursor() diff --git a/src/paths.py b/src/paths.py index e2f8c97e..49e312a1 100644 --- a/src/paths.py +++ b/src/paths.py @@ -23,7 +23,8 @@ def lookupExeFolder(): # targetdir/Bitmessage.app/Contents/MacOS/Bitmessage os.path.dirname(sys.executable).split(os.path.sep)[0] + os.path.sep if frozen == "macosx_app" else - os.path.dirname(sys.executable) + os.path.sep) + os.path.dirname(sys.executable).decode( + sys.getfilesystemencoding(), 'ignore') + os.path.sep) elif __file__: exeFolder = os.path.dirname(__file__) + os.path.sep else: @@ -49,11 +50,10 @@ def lookupAppdataFolder(): sys.exit( 'Could not find home folder, please report this message' ' and your OS X version to the BitMessage Github.') - elif 'win32' in sys.platform or 'win64' in sys.platform: + elif sys.platform.startswith('win'): dataFolder = os.path.join( - os.environ['APPDATA'].decode( - sys.getfilesystemencoding(), 'ignore'), APPNAME - ) + os.path.sep + os.environ['APPDATA'], APPNAME + ).decode(sys.getfilesystemencoding(), 'ignore') + os.path.sep else: try: dataFolder = os.path.join(os.environ['XDG_CONFIG_HOME'], APPNAME) -- 2.45.1 From 6570a06dc71904ce1ca7f2f7e0bd059af391d6a3 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Fri, 24 Apr 2020 16:30:35 +0300 Subject: [PATCH 2/4] Use unicode in os.path.abspath (https://bugs.python.org/issue17320) --- src/bitmessagemain.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index 4a27f967..9280c947 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -12,7 +12,7 @@ The PyBitmessage startup script import os import sys -app_dir = os.path.dirname(os.path.abspath(__file__)) +app_dir = os.path.dirname(os.path.abspath(unicode(__file__))) os.chdir(app_dir) sys.path.insert(0, app_dir) -- 2.45.1 From 8798f2fbf51d0c8161cf38a627cb0e21b2418da7 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Mon, 27 Apr 2020 18:56:35 +0300 Subject: [PATCH 3/4] Try using ActiveState appdirs on Windows --- src/paths.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/paths.py b/src/paths.py index 49e312a1..c162f0b3 100644 --- a/src/paths.py +++ b/src/paths.py @@ -54,6 +54,11 @@ def lookupAppdataFolder(): dataFolder = os.path.join( os.environ['APPDATA'], APPNAME ).decode(sys.getfilesystemencoding(), 'ignore') + os.path.sep + try: + os.path.isdir(dataFolder) + except WindowsError: + from appdirs import user_data_dir + dataFolder = user_data_dir('PyBitmessage', False) else: try: dataFolder = os.path.join(os.environ['XDG_CONFIG_HOME'], APPNAME) -- 2.45.1 From 9bed0c4b538c94abd0ef8b5fcb62005761d0665a Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Thu, 23 Apr 2020 13:56:44 +0300 Subject: [PATCH 4/4] Use appdirs in first place, it's also a dependency for pyopencl --- src/namecoin.py | 32 ++----------------------- src/paths.py | 63 ++++++++++++++++++++++++++++++------------------- 2 files changed, 41 insertions(+), 54 deletions(-) diff --git a/src/namecoin.py b/src/namecoin.py index ae2bde79..4a6cf8d5 100644 --- a/src/namecoin.py +++ b/src/namecoin.py @@ -6,11 +6,11 @@ Namecoin queries import base64 import httplib import json -import os import socket import sys import defaults +import paths import tr # translate from addresses import decodeAddress from bmconfigparser import BMConfigParser @@ -267,34 +267,6 @@ class namecoinConnection(object): raise Exception("Socket error in RPC connection: %s" % exc) -def lookupNamecoinFolder(): - """ - Look up the namecoin data folder. - - .. todo:: Check whether this works on other platforms as well! - """ - - app = "namecoin" - from os import path, environ - if sys.platform == "darwin": - if "HOME" in environ: - dataFolder = path.join(os.environ["HOME"], - "Library/Application Support/", app) + '/' - else: - print( - "Could not find home folder, please report this message" - " and your OS X version to the BitMessage Github." - ) - sys.exit() - - elif "win32" in sys.platform or "win64" in sys.platform: - dataFolder = path.join(environ["APPDATA"], app) + "\\" - else: - dataFolder = path.join(environ["HOME"], ".%s" % app) + "/" - - return dataFolder - - def ensureNamecoinOptions(): """ Ensure all namecoin options are set, by setting those to default values @@ -313,7 +285,7 @@ def ensureNamecoinOptions(): # Try to read user/password from .namecoin configuration file. defaultUser = "" defaultPass = "" - nmcFolder = lookupNamecoinFolder() + nmcFolder = paths.lookupUserconfigDir('namecoin') nmcConfig = nmcFolder + "namecoin.conf" try: nmc = open(nmcConfig, "r") diff --git a/src/paths.py b/src/paths.py index c162f0b3..cb847f37 100644 --- a/src/paths.py +++ b/src/paths.py @@ -32,48 +32,63 @@ def lookupExeFolder(): return exeFolder +def lookupUserconfigDir(appname): + """Lookup user data directory for the *appname* application""" + try: + from appdirs import user_config_dir + return user_config_dir(appname, False, roaming=True) + os.path.sep + except ImportError: + pass + + if sys.platform == 'darwin': + try: + dataFolder = os.path.join( + os.environ['HOME'], + "Library/Application Support/", appname) + '/' + except KeyError: + sys.exit( + "Could not find home folder, please report this message" + " and your OS X version to the BitMessage Github.") + elif sys.platform.startswith('win'): + dataFolder = os.path.join( + os.environ['APPDATA'], appname + ).decode(sys.getfilesystemencoding(), 'ignore') + os.path.sep + else: + dataFolder = os.path.join( + os.environ['HOME'], '.%s' % appname) + os.path.sep + + return dataFolder + + def lookupAppdataFolder(): """Returns path of the folder where application data is stored""" APPNAME = "PyBitmessage" + dataFolder = os.environ.get('BITMESSAGE_HOME') if dataFolder: if dataFolder[-1] not in (os.path.sep, os.path.altsep): dataFolder += os.path.sep - elif sys.platform == 'darwin': - try: - dataFolder = os.path.join( - os.environ['HOME'], - 'Library/Application Support/', APPNAME - ) + '/' + return dataFolder - except KeyError: - sys.exit( - 'Could not find home folder, please report this message' - ' and your OS X version to the BitMessage Github.') - elif sys.platform.startswith('win'): - dataFolder = os.path.join( - os.environ['APPDATA'], APPNAME - ).decode(sys.getfilesystemencoding(), 'ignore') + os.path.sep + dataFolder = lookupUserconfigDir(APPNAME) + # Try to follow XDG spec on Linux, Unix or BSD + # TODO: use pyxdg + if os.name == 'posix' and sys.platform != 'darwin': try: - os.path.isdir(dataFolder) - except WindowsError: - from appdirs import user_data_dir - dataFolder = user_data_dir('PyBitmessage', False) - else: - try: - dataFolder = os.path.join(os.environ['XDG_CONFIG_HOME'], APPNAME) + datadir = os.path.join(os.environ['XDG_CONFIG_HOME'], APPNAME) except KeyError: - dataFolder = os.path.join(os.environ['HOME'], '.config', APPNAME) + datadir = os.path.join(os.environ['HOME'], '.config', APPNAME) # Migrate existing data to the proper location # if this is an existing install try: - move(os.path.join(os.environ['HOME'], '.%s' % APPNAME), dataFolder) + move(dataFolder, datadir) + dataFolder = datadir + os.path.sep logger.info('Moving data folder to %s', dataFolder) except IOError: # Old directory may not exist. pass - dataFolder = dataFolder + os.path.sep + return dataFolder -- 2.45.1