Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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