commit dd6f5b64ad3fb9e1b3a2990a2be7d3d5d700f228
parent 4e6123d781e6443f2531da57facf3c3bc2edf06e
Author: Daniel Moch <daniel@danielmoch.com>
Date: Sun, 12 Aug 2018 20:22:12 -0400
Use appdirs library for better portability
Using appdirs allows for data and configuration files to be placed in
the standard location for each platform.
Closes #1
Diffstat:
2 files changed, 16 insertions(+), 39 deletions(-)
diff --git a/nnotes_cli/config.py b/nnotes_cli/config.py
@@ -27,30 +27,14 @@
import os, sys, urwid, collections, configparser, subprocess
+from appdirs import user_cache_dir, user_config_dir
+
class Config:
def __init__(self, custom_file=None):
- self.home = os.path.abspath(os.path.expanduser('~'))
- if 'XDG_CONFIG_HOME' in os.environ.keys():
- self.config_home = \
- os.path.join(os.environ['XDG_CONFIG_HOME'], 'nncli')
- else:
- self.config_home = \
- os.path.join(
- os.path.expanduser('~'),
- '.config',
- 'nncli'
- )
- if 'XDG_CACHE_HOME' in os.environ.keys():
- self.cache_home = \
- os.path.join(os.environ['XDG_CACHE_HOME'], 'nncli')
- else:
- self.cache_home = \
- os.path.join(
- os.path.expanduser('~'),
- '.cache',
- 'nncli'
- )
+ self.config_home = user_config_dir('nncli', 'djmoch')
+ self.cache_home = user_cache_dir('nncli', 'djmoch')
+
defaults = \
{
'cfg_nn_username' : '',
diff --git a/tests/test_config.py b/tests/test_config.py
@@ -22,6 +22,7 @@
# SOFTWARE.
#
import os
+import sys
from nnotes_cli.config import Config
from pytest import raises
@@ -29,25 +30,17 @@
def test_init():
config = Config()
- assert config.home == os.path.abspath(os.path.expanduser('~'))
+ if sys.platform == 'linux':
+ assert config.config_home == os.path.join(os.path.expanduser('~'), \
+ '.config', 'nncli')
+ assert config.cache_home == os.path.join(os.path.expanduser('~'), \
+ '.cache', 'nncli')
+ if sys.platform == 'darwin':
+ assert config.config_home == os.path.join(os.path.expanduser('~'), \
+ 'Library', 'Preferences', 'nncli')
+ assert config.cache_home == os.path.join(os.path.expanduser('~'), \
+ 'Library', 'Caches', 'nncli')
- if 'XDG_CONFIG_HOME' in os.environ:
- del(os.environ['XDG_CONFIG_HOME'])
- else:
- os.environ['XDG_CONFIG_HOME'] = 'foo'
-
- config = Config()
-
- assert config.home == os.path.abspath(os.path.expanduser('~'))
-
- if 'XDG_CACHE_HOME' in os.environ:
- del(os.environ['XDG_CACHE_HOME'])
- else:
- os.environ['XDG_CACHE_HOME'] = 'bar'
-
- config = Config()
-
- assert config.home == os.path.abspath(os.path.expanduser('~'))
def test_custom_file():
with open('test_cfg', 'w') as config_file: