Skip to content
Open
Changes from 5 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
51 changes: 43 additions & 8 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,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():
Copy link
Contributor

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 in operator to dictionary keys so would be idiomatic to write this as if "TASKRC" in os.environ:

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():
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

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


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 All @@ -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(
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down