Use appdirs in first place, it's also a dependency for pyopencl

This commit is contained in:
Dmitri Bogomolov 2020-04-23 13:56:44 +03:00
parent 8798f2fbf5
commit 9bed0c4b53
Signed by untrusted user: g1itch
GPG Key ID: 720A756F18DEED13
2 changed files with 41 additions and 54 deletions

View File

@ -6,11 +6,11 @@ Namecoin queries
import base64 import base64
import httplib import httplib
import json import json
import os
import socket import socket
import sys import sys
import defaults import defaults
import paths
import tr # translate import tr # translate
from addresses import decodeAddress from addresses import decodeAddress
from bmconfigparser import BMConfigParser from bmconfigparser import BMConfigParser
@ -267,34 +267,6 @@ class namecoinConnection(object):
raise Exception("Socket error in RPC connection: %s" % exc) 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(): def ensureNamecoinOptions():
""" """
Ensure all namecoin options are set, by setting those to default values 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. # Try to read user/password from .namecoin configuration file.
defaultUser = "" defaultUser = ""
defaultPass = "" defaultPass = ""
nmcFolder = lookupNamecoinFolder() nmcFolder = paths.lookupUserconfigDir('namecoin')
nmcConfig = nmcFolder + "namecoin.conf" nmcConfig = nmcFolder + "namecoin.conf"
try: try:
nmc = open(nmcConfig, "r") nmc = open(nmcConfig, "r")

View File

@ -32,48 +32,63 @@ def lookupExeFolder():
return exeFolder 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(): def lookupAppdataFolder():
"""Returns path of the folder where application data is stored""" """Returns path of the folder where application data is stored"""
APPNAME = "PyBitmessage" APPNAME = "PyBitmessage"
dataFolder = os.environ.get('BITMESSAGE_HOME') dataFolder = os.environ.get('BITMESSAGE_HOME')
if dataFolder: if dataFolder:
if dataFolder[-1] not in (os.path.sep, os.path.altsep): if dataFolder[-1] not in (os.path.sep, os.path.altsep):
dataFolder += os.path.sep dataFolder += os.path.sep
elif sys.platform == 'darwin': return dataFolder
try:
dataFolder = os.path.join(
os.environ['HOME'],
'Library/Application Support/', APPNAME
) + '/'
except KeyError: dataFolder = lookupUserconfigDir(APPNAME)
sys.exit( # Try to follow XDG spec on Linux, Unix or BSD
'Could not find home folder, please report this message' # TODO: use pyxdg
' and your OS X version to the BitMessage Github.') if os.name == 'posix' and sys.platform != 'darwin':
elif sys.platform.startswith('win'):
dataFolder = os.path.join(
os.environ['APPDATA'], APPNAME
).decode(sys.getfilesystemencoding(), 'ignore') + os.path.sep
try: try:
os.path.isdir(dataFolder) datadir = os.path.join(os.environ['XDG_CONFIG_HOME'], APPNAME)
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)
except KeyError: 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 # Migrate existing data to the proper location
# if this is an existing install # if this is an existing install
try: 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) logger.info('Moving data folder to %s', dataFolder)
except IOError: except IOError:
# Old directory may not exist. # Old directory may not exist.
pass pass
dataFolder = dataFolder + os.path.sep
return dataFolder return dataFolder