1From 958b63ceec02b179482141cfb846ddbcae711a1b Mon Sep 17 00:00:00 2001
2From: Scott Mcdermott <scott@smemsh.net>
3Date: Sat, 28 Aug 2021 21:12:38 -0700
4Subject: [PATCH] RcFile: _read: try taskrc directory when trying to load
5 includes
6
7Taskwarrior itself tries includes as absolute path, then cwd, then
8relative to rcfile, then in various search paths (see
9GothenburgBitFactory/libshared -> src/Configuration.cpp ->
10Configuration::parse()).
11
12We won't try to duplicate that whole arrangement here, but at least look
13relative to the rcfile in addition to cwd/absolute, like taskwarrior
14does. This will allow specification as relative path in most cases.
15Otherwise, we'd have to chdir anyways because includes are always tried
16as-is by taskw. They would only work if specified as absolute paths or
17if in cwd
18
19We use a TaskRc() class variable to store the rcdir because there could
20be an include chain and all the instances will need to know the same
21one, but will be processing different paths, so we have to capture the
22directory of the first one processed, ie the base rcfile.
23
24Fixes #150
25
26Co-authored-by: Raito Bezarius <masterancpp@gmail.com>
27---
28 taskw/taskrc.py | 15 +++++++++++++++
29 1 file changed, 15 insertions(+)
30
31diff --git a/taskw/taskrc.py b/taskw/taskrc.py
32index 1b6f8e5..b72dee6 100644
33--- a/taskw/taskrc.py
34+++ b/taskw/taskrc.py
35@@ -1,5 +1,6 @@
36 import logging
37 import os
38+import os.path
39
40 from taskw.fields import (
41 ChoiceField,
42@@ -39,6 +40,7 @@ class TaskRc(dict):
43
44 """
45
46+ rcdir = None
47 UDA_TYPE_MAP = {
48 'date': DateField,
49 'duration': DurationField,
50@@ -54,6 +56,8 @@ class TaskRc(dict):
51 path
52 )
53 )
54+ if not self.rcdir:
55+ TaskRc.rcdir = os.path.dirname(os.path.realpath(self.path))
56 config = self._read(self.path)
57 else:
58 self.path = None
59@@ -92,6 +96,17 @@ class TaskRc(dict):
60
61 def _read(self, path):
62 config = {}
63+ if not os.path.exists(path) and TaskRc.rcdir is not None:
64+ # include path may be given relative to dir of rcfile
65+ oldpath = path
66+ path = os.path.join(TaskRc.rcdir, oldpath)
67+ if not os.path.exists(path):
68+ logger.error(
69+ "rcfile does not exist, tried %s and %s",
70+ oldpath, path
71+ )
72+ raise FileNotFoundError
73+
74 with open(path, 'r') as config_file:
75 for raw_line in config_file.readlines():
76 line = sanitize(raw_line)
77--
782.42.0
79