-
Notifications
You must be signed in to change notification settings - Fork 45
Taskrc discovery: check ${XDG_CONFIG_DIR}/task/taskrc; raise execption if not found #163
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
base: develop
Are you sure you want to change the base?
Changes from 5 commits
6ffddf1
9a62264
883fc15
a82f41d
b1fce65
cafc308
26610e2
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 |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
| import subprocess | ||
| import sys | ||
| import json | ||
| from pathlib import Path | ||
|
|
||
| import kitchen.text.converters | ||
|
|
||
|
|
@@ -32,9 +33,36 @@ | |
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| # Location of configuration file: either specified by TASKRC environment | ||
| # variable, or ~/.taskrc (default). | ||
| TASKRC = os.getenv("TASKRC", "~/.taskrc") | ||
| def find_taskrc(): | ||
| """ | ||
| Find the location of the taskwarrior configuration file. | ||
|
|
||
| First checks ${TASKRC} environment variable, then falls back to ~/.taskrc and finally ${XDG_CONFIG_DIR}/task/taskrc. | ||
|
|
||
| Raises FileNotFoundErrors if either | ||
| * Specified taskrc is not a file | ||
| * No taskrc was found | ||
| """ | ||
| if "TASKRC" in os.environ.keys(): | ||
| taskrc = Path(os.environ["TASKRC"]) | ||
| if taskrc.is_file(): | ||
| return taskrc | ||
| else: | ||
| raise FileNotFoundError("Environment variable 'TASKRC' did not resolve to a taskrc file") | ||
|
|
||
|
|
||
| taskrc = Path.home() / ".taskrc" | ||
| if taskrc.is_file(): | ||
| return taskrc | ||
|
|
||
| if "XDG_CONFIG_HOME" in os.environ.keys(): | ||
|
Contributor
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. You could remove the |
||
| taskrc = Path(os.environ["XDG_CONFIG_HOME"]) / "task/taskrc" | ||
| if taskrc.is_file(): | ||
| return taskrc | ||
|
|
||
| raise FileNotFoundError("Unable to find taskrc. Set environment variable 'TASKRC=<file>' for a non-standard location") | ||
|
|
||
|
|
||
|
Contributor
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. Any reason not to assign |
||
|
|
||
|
|
||
| class TaskWarriorBase(metaclass=abc.ABCMeta): | ||
|
|
@@ -46,11 +74,14 @@ class TaskWarriorBase(metaclass=abc.ABCMeta): | |
|
|
||
| def __init__( | ||
| self, | ||
| config_filename=TASKRC, | ||
| config_filename=None, | ||
| config_overrides=None, | ||
| marshal=False | ||
| ): | ||
| self.config_filename = config_filename | ||
| if config_filename is None: | ||
| self.config_filename = find_taskrc() | ||
| else: | ||
| self.config_filename = config_filename | ||
| self.config = TaskWarriorBase.load_config(config_filename) | ||
| if marshal: | ||
| raise NotImplementedError( | ||
|
|
@@ -159,7 +190,7 @@ def filter_by(self, func): | |
| return filtered | ||
|
|
||
| @classmethod | ||
| def load_config(cls, config_filename=TASKRC, overrides=None): | ||
| def load_config(cls, config_filename=None, overrides=None): | ||
| """ Load ~/.taskrc into a python dict | ||
|
|
||
| >>> config = TaskWarrior.load_config() | ||
|
|
@@ -169,6 +200,8 @@ def load_config(cls, config_filename=TASKRC, overrides=None): | |
| 'yes' | ||
|
|
||
| """ | ||
| if config_filename is None: | ||
| config_filename = find_taskrc() | ||
| return TaskRc(config_filename, overrides=overrides) | ||
|
|
||
| @abc.abstractmethod | ||
|
|
@@ -423,10 +456,12 @@ class TaskWarriorShellout(TaskWarriorBase): | |
|
|
||
| def __init__( | ||
| self, | ||
| config_filename=TASKRC, | ||
| config_filename=None, | ||
| config_overrides=None, | ||
| marshal=False, | ||
| ): | ||
| if config_filename is None: | ||
| config_filename = find_taskrc() | ||
| super(TaskWarriorShellout, self).__init__(config_filename) | ||
| self.config_overrides = config_overrides if config_overrides else {} | ||
| self._marshal = marshal | ||
|
|
@@ -463,7 +498,7 @@ def _execute(self, *args): | |
| + [str(arg) for arg in args] | ||
| ) | ||
| env = os.environ.copy() | ||
| env['TASKRC'] = self.config_filename | ||
| env['TASKRC'] = self.config_filename.as_posix() | ||
|
|
||
| # subprocess is expecting bytestrings only, so nuke unicode if present | ||
| # and remove control characters | ||
|
|
||
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.
FYI, python automatically applies the
inoperator to dictionary keys so would be idiomatic to write this asif "TASKRC" in os.environ: