summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmmanuel Gil Peyrot <linkmauve@linkmauve.fr>2018-07-03 12:32:31 +0200
committerEmmanuel Gil Peyrot <linkmauve@linkmauve.fr>2018-07-03 12:32:37 +0200
commitb80bb2fcfcceb9b00a3fec02280e93da33ba8bd0 (patch)
treea16b35eed09e8b5465585626cd6dfa9e262d66cc
parent5483cd40ee1e84e86bfe775ba06ca9d7d5d5a0ea (diff)
downloadpoezio-b80bb2fcfcceb9b00a3fec02280e93da33ba8bd0.tar.gz
poezio-b80bb2fcfcceb9b00a3fec02280e93da33ba8bd0.tar.bz2
poezio-b80bb2fcfcceb9b00a3fec02280e93da33ba8bd0.tar.xz
poezio-b80bb2fcfcceb9b00a3fec02280e93da33ba8bd0.zip
Only create the config dir after parsing the CLI args.
This also switches to pathlib.Path instead of os.path for path manipulation.
-rw-r--r--poezio/args.py7
-rw-r--r--poezio/config.py45
-rw-r--r--poezio/poezio.py3
3 files changed, 30 insertions, 25 deletions
diff --git a/poezio/args.py b/poezio/args.py
index 71ae7488..31525bb2 100644
--- a/poezio/args.py
+++ b/poezio/args.py
@@ -3,11 +3,11 @@ Module related to the argument parsing
There is a fallback to the deprecated optparse if argparse is not found
"""
-from os import path
+from pathlib import Path
from argparse import ArgumentParser, SUPPRESS
-def parse_args(CONFIG_PATH=''):
+def parse_args(CONFIG_PATH: Path):
"""
Parse the arguments from the command line
"""
@@ -28,7 +28,8 @@ def parse_args(CONFIG_PATH=''):
"-f",
"--file",
dest="filename",
- default=path.join(CONFIG_PATH, 'poezio.cfg'),
+ default=CONFIG_PATH / 'poezio.cfg',
+ type=Path,
help="The config file you want to use",
metavar="CONFIG_FILE")
parser.add_argument(
diff --git a/poezio/config.py b/poezio/config.py
index ed295c92..fe60be63 100644
--- a/poezio/config.py
+++ b/poezio/config.py
@@ -506,20 +506,20 @@ def file_ok(filepath):
return bool(val)
-def check_create_config_dir():
+def get_default_config_dir():
"""
- create the configuration directory if it doesn't exist
+ returns the default configuration directory path
"""
- config_home = environ.get("XDG_CONFIG_HOME")
- if config_home is None or not Path(config_home).is_absolute():
- config_home = path.join(environ.get('HOME'), '.config')
- CONFIG_PATH = path.join(config_home, 'poezio')
-
try:
- makedirs(CONFIG_PATH)
- except OSError:
+ config_home = Path(environ.get('XDG_CONFIG_HOME'))
+ except TypeError:
+ # XDG_CONFIG_HOME isn’t defined, fallback to ~/.config
pass
- return CONFIG_PATH
+ else:
+ if config_home.is_absolute():
+ return config_home / 'poezio'
+ # HOME has already been checked to be non-None in test_env().
+ return Path.home() / '.config' / 'poezio'
def check_create_cache_dir():
@@ -575,27 +575,32 @@ def check_config():
print(' \033[31m%s\033[0m' % option)
-def run_cmdline_args(CONFIG_PATH):
+def run_cmdline_args():
"Parse the command line arguments"
global options
+ CONFIG_PATH = get_default_config_dir()
options = parse_args(CONFIG_PATH)
# Copy a default file if none exists
- if not path.isfile(options.filename):
- default = path.join(
- path.dirname(__file__), '../data/default_config.cfg')
- other = pkg_resources.resource_filename('poezio', 'default_config.cfg')
- if path.isfile(default):
+ if not options.filename.is_file():
+ try:
+ options.filename.parent.mkdir(parents=True, exist_ok=True)
+ except OSError as e:
+ sys.stderr.write('Poezio was unable to create the config directory: %s\n' % e)
+ sys.exit(1)
+ default = Path(__file__).parent / '..' / 'data' / 'default_config.cfg'
+ other = Path(pkg_resources.resource_filename('poezio', 'default_config.cfg'))
+ if default.is_file():
copy2(default, options.filename)
- elif path.isfile(other):
+ elif other.is_file():
copy2(other, options.filename)
# Inside the nixstore and possibly other distributions, the reference
# file is readonly, so is the copy.
# Make it writable by the user who just created it.
- if os.path.exists(options.filename):
- os.chmod(options.filename,
- os.stat(options.filename).st_mode | stat.S_IWUSR)
+ if options.filename.exists():
+ options.filename.chmod(
+ options.filename.stat().st_mode | stat.S_IWUSR)
global firstrun
firstrun = True
diff --git a/poezio/poezio.py b/poezio/poezio.py
index d74eca74..ee6cd6db 100644
--- a/poezio/poezio.py
+++ b/poezio/poezio.py
@@ -77,8 +77,7 @@ def main():
sys.stdout.write("\x1b]0;poezio\x07")
sys.stdout.flush()
from poezio import config
- config_path = config.check_create_config_dir()
- config.run_cmdline_args(config_path)
+ config.run_cmdline_args()
config.create_global_config()
config.check_create_data_dir()
config.check_create_log_dir()