From e9ea28a8ad0dcfc452c07097a3d874d002484667 Mon Sep 17 00:00:00 2001 From: hiiwave Date: Sat, 21 Sep 2019 07:21:17 +0800 Subject: [PATCH] Create default config if it does not exist --- watson/cli.py | 7 ++----- watson/watson.py | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/watson/cli.py b/watson/cli.py index 47b3c9c7..d3a86175 100644 --- a/watson/cli.py +++ b/watson/cli.py @@ -1341,11 +1341,8 @@ def config(context, key, value, edit): wconfig = watson.config if edit: - try: - with open(watson.config_file) as fp: - rawconfig = fp.read() - except (IOError, OSError): - rawconfig = '' + with open(watson.config_file) as fp: + rawconfig = fp.read() newconfig = click.edit(text=rawconfig, extension='.ini') diff --git a/watson/watson.py b/watson/watson.py index 62dc2c1f..ceccf3cc 100644 --- a/watson/watson.py +++ b/watson/watson.py @@ -6,6 +6,8 @@ import operator import os import uuid +from pathlib import Path +import textwrap try: import configparser @@ -115,6 +117,7 @@ def config(self): Return Watson's config as a ConfigParser object. """ if not self._config: + self._check_default_config() try: config = ConfigParser() config.read(self.config_file) @@ -134,6 +137,30 @@ def config(self, value): self._config = value self._config_changed = True + def _check_default_config(self): + if Path(self.config_file).exists(): + return + Path(self.config_file).write_text(textwrap.dedent(""" + [backend] + # url = + # token = yourapitoken + + [options] + confirm_new_project = false + confirm_new_tag = false + date_format = %Y.%m.%d + log_current = false + pager = true + report_current = false + stop_on_start = false + stop_on_restart = false + time_format = %H:%M:%S%z + week_start = monday + + [default_tags] + # some_project = some_tag1 some_tag2 + """).strip()) + def save(self): """ Save the state in the appropriate files. Create them if necessary.