-
-
Notifications
You must be signed in to change notification settings - Fork 241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create default config if it does not exist #320
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(""" | ||
hiiwave marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 Maybe we should define default values in a dedicated module (e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we had a default config, we actually don't need default value in CLI. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have considered an approach of adding another file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I like the way you generate it, but I think it would be more pythonic (at least more explicit) to declare defaults as code and then generate a default configuration from those. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you suggesting something like this? # defaults.py
configs = {
'options': {
'confirm_new_project': False,
# ...
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that being explicit is always favorable, though I'm not really into this style. One drawback includes that this does not differentiate configs from different sections. And I actually think that a default config itself (e.g.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get you point, but still, I think using a configuration-file-driven-approach because of the CLI is not ideal. When people are using parts of Watson as a library I think it's better to avoid loading defaults from a configuration file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like your point if there's future plan to support calling watson api as a library. As currently IMHO, a better design will be creating a class What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good to me, yay! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
def save(self): | ||
""" | ||
Save the state in the appropriate files. Create them if necessary. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that's still a nice thing to be resilient in case we can't find the config file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice suggestion.