Skip to content
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

Taskrc discovery: check ${XDG_CONFIG_DIR}/task/taskrc; raise execption if not found #163

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
38 changes: 35 additions & 3 deletions taskw/warrior.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import subprocess
import sys
import json
from pathlib import Path

import kitchen.text.converters

Expand All @@ -32,9 +33,40 @@
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.

Follows Taskwarrior's config discovery order
* ${HOME}/.taskrc
* ${TASKRC}
* ${XDG_CONFIG_HOME}/task/taskrc

Raises FileNotFoundError if either
* Specified taskrc is not a file
* No taskrc was found
"""
taskrc = Path.home() / ".taskrc"
if taskrc.is_file():
return taskrc.as_posix()

if "TASKRC" in os.environ:
taskrc = Path(os.environ["TASKRC"])
if taskrc.is_file():
return taskrc.as_posix()
else:
raise FileNotFoundError("Environment variable 'TASKRC' did not resolve to a taskrc file")

if "XDG_CONFIG_HOME" in os.environ.keys():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could remove the .keys() here as well.

taskrc = Path(os.environ["XDG_CONFIG_HOME"]) / "task/taskrc"
if taskrc.is_file():
return taskrc.as_posix()

raise FileNotFoundError("Unable to find taskrc. Set environment variable 'TASKRC=<file>' for a non-standard location")


TASKRC = find_taskrc()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to assign TASKRC = find_taskrc() here? Then you wouldn't have to change anything else in this module and we could avoid the extra conditionals in this module.



class TaskWarriorBase(metaclass=abc.ABCMeta):
Expand Down