Only create the config dir after parsing the CLI args.

This also switches to pathlib.Path instead of os.path for path
manipulation.
This commit is contained in:
Emmanuel Gil Peyrot 2018-07-03 12:32:31 +02:00
parent 5483cd40ee
commit b80bb2fcfc
3 changed files with 30 additions and 25 deletions

View file

@ -3,11 +3,11 @@ Module related to the argument parsing
There is a fallback to the deprecated optparse if argparse is not found 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 from argparse import ArgumentParser, SUPPRESS
def parse_args(CONFIG_PATH=''): def parse_args(CONFIG_PATH: Path):
""" """
Parse the arguments from the command line Parse the arguments from the command line
""" """
@ -28,7 +28,8 @@ def parse_args(CONFIG_PATH=''):
"-f", "-f",
"--file", "--file",
dest="filename", dest="filename",
default=path.join(CONFIG_PATH, 'poezio.cfg'), default=CONFIG_PATH / 'poezio.cfg',
type=Path,
help="The config file you want to use", help="The config file you want to use",
metavar="CONFIG_FILE") metavar="CONFIG_FILE")
parser.add_argument( parser.add_argument(

View file

@ -506,20 +506,20 @@ def file_ok(filepath):
return bool(val) 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: try:
makedirs(CONFIG_PATH) config_home = Path(environ.get('XDG_CONFIG_HOME'))
except OSError: except TypeError:
# XDG_CONFIG_HOME isnt defined, fallback to ~/.config
pass 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(): def check_create_cache_dir():
@ -575,27 +575,32 @@ def check_config():
print(' \033[31m%s\033[0m' % option) print(' \033[31m%s\033[0m' % option)
def run_cmdline_args(CONFIG_PATH): def run_cmdline_args():
"Parse the command line arguments" "Parse the command line arguments"
global options global options
CONFIG_PATH = get_default_config_dir()
options = parse_args(CONFIG_PATH) options = parse_args(CONFIG_PATH)
# Copy a default file if none exists # Copy a default file if none exists
if not path.isfile(options.filename): if not options.filename.is_file():
default = path.join( try:
path.dirname(__file__), '../data/default_config.cfg') options.filename.parent.mkdir(parents=True, exist_ok=True)
other = pkg_resources.resource_filename('poezio', 'default_config.cfg') except OSError as e:
if path.isfile(default): 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) copy2(default, options.filename)
elif path.isfile(other): elif other.is_file():
copy2(other, options.filename) copy2(other, options.filename)
# Inside the nixstore and possibly other distributions, the reference # Inside the nixstore and possibly other distributions, the reference
# file is readonly, so is the copy. # file is readonly, so is the copy.
# Make it writable by the user who just created it. # Make it writable by the user who just created it.
if os.path.exists(options.filename): if options.filename.exists():
os.chmod(options.filename, options.filename.chmod(
os.stat(options.filename).st_mode | stat.S_IWUSR) options.filename.stat().st_mode | stat.S_IWUSR)
global firstrun global firstrun
firstrun = True firstrun = True

View file

@ -77,8 +77,7 @@ def main():
sys.stdout.write("\x1b]0;poezio\x07") sys.stdout.write("\x1b]0;poezio\x07")
sys.stdout.flush() sys.stdout.flush()
from poezio import config from poezio import config
config_path = config.check_create_config_dir() config.run_cmdline_args()
config.run_cmdline_args(config_path)
config.create_global_config() config.create_global_config()
config.check_create_data_dir() config.check_create_data_dir()
config.check_create_log_dir() config.check_create_log_dir()