@@ -94,16 +94,22 @@ class TaskWarrior(Backend):
9494 VERSION_2_4_4 = six .u ('2.4.4' )
9595 VERSION_2_4_5 = six .u ('2.4.5' )
9696
97- def __init__ (self , data_location = None , create = True , taskrc_location = '~/.taskrc' ):
98- self .taskrc_location = os .path .expanduser (taskrc_location )
97+ def __init__ (self , data_location = None , create = True ,
98+ taskrc_location = None , task_command = 'task' ,
99+ version_override = None ):
100+ self .taskrc_location = None
101+ if taskrc_location :
102+ self .taskrc_location = os .path .expanduser (taskrc_location )
99103
100- # If taskrc does not exist, pass / to use defaults and avoid creating
101- # dummy .taskrc file by TaskWarrior
102- if not os .path .exists (self .taskrc_location ):
103- self .taskrc_location = '/'
104+ # If taskrc does not exist, pass / to use defaults and avoid creating
105+ # dummy .taskrc file by TaskWarrior
106+ if not os .path .exists (self .taskrc_location ):
107+ self .taskrc_location = '/'
108+
109+ self .task_command = task_command
104110
105111 self ._config = None
106- self .version = self ._get_version ()
112+ self .version = version_override or self ._get_version ()
107113 self .overrides = {
108114 'confirmation' : 'no' ,
109115 'dependency.confirmation' : 'no' , # See TW-1483 or taskrc man page
@@ -126,8 +132,11 @@ def __init__(self, data_location=None, create=True, taskrc_location='~/.taskrc')
126132
127133 self .tasks = TaskQuerySet (self )
128134
135+ def _get_task_command (self ):
136+ return self .task_command .split ()
137+
129138 def _get_command_args (self , args , config_override = None ):
130- command_args = [ 'task' , 'rc:{0}' . format ( self .taskrc_location )]
139+ command_args = self ._get_task_command ()
131140 overrides = self .overrides .copy ()
132141 overrides .update (config_override or dict ())
133142 for item in overrides .items ():
@@ -140,7 +149,7 @@ def _get_command_args(self, args, config_override=None):
140149
141150 def _get_version (self ):
142151 p = subprocess .Popen (
143- [ 'task' , '--version' ],
152+ self . _get_task_command () + [ '--version' ],
144153 stdout = subprocess .PIPE ,
145154 stderr = subprocess .PIPE )
146155 stdout , stderr = [x .decode ('utf-8' ) for x in p .communicate ()]
@@ -214,21 +223,25 @@ def format_description(self, task):
214223 if self .version < self .VERSION_2_4_0 :
215224 return task ._data ['description' ]
216225 else :
217- return six .u ("description:'{0}'" ).format (task ._data ['description' ] or '' )
226+ return six .u ("description:'{0}'" ).format (
227+ task ._data ['description' ] or '' ,
228+ )
218229
219230 def convert_datetime_string (self , value ):
220231
221232 if self .version >= self .VERSION_2_4_0 :
222- # For strings, use 'task calc' to evaluate the string to datetime
233+ # For strings, use 'calc' to evaluate the string to datetime
223234 # available since TW 2.4.0
224235 args = value .split ()
225236 result = self .execute_command (['calc' ] + args )
226237 naive = datetime .datetime .strptime (result [0 ], DATE_FORMAT_CALC )
227238 localized = local_zone .localize (naive )
228239 else :
229- raise ValueError ("Provided value could not be converted to "
230- "datetime, its type is not supported: {}"
231- .format (type (value )))
240+ raise ValueError (
241+ 'Provided value could not be converted to '
242+ 'datetime, its type is not supported: {}'
243+ .format (type (value )),
244+ )
232245
233246 return localized
234247
@@ -269,8 +282,11 @@ def execute_command(self, args, config_override=None, allow_failure=True,
269282 args , config_override = config_override )
270283 logger .debug (u' ' .join (command_args ))
271284
285+ env = os .environ .copy ()
286+ if self .taskrc_location :
287+ env ['TASKRC' ] = self .taskrc_location
272288 p = subprocess .Popen (command_args , stdout = subprocess .PIPE ,
273- stderr = subprocess .PIPE )
289+ stderr = subprocess .PIPE , env = env )
274290 stdout , stderr = [x .decode ('utf-8' ) for x in p .communicate ()]
275291 if p .returncode and allow_failure :
276292 if stderr .strip ():
@@ -338,8 +354,10 @@ def save_task(self, task):
338354 # Expected output: Created task 1.
339355 # Created task 1 (recurrence template).
340356 if len (id_lines ) != 1 or len (id_lines [0 ].split (' ' )) not in (3 , 5 ):
341- raise TaskWarriorException ("Unexpected output when creating "
342- "task: %s" % '\n ' .join (id_lines ))
357+ raise TaskWarriorException (
358+ 'Unexpected output when creating '
359+ 'task: %s' % '\n ' .join (id_lines ),
360+ )
343361
344362 # Circumvent the ID storage, since ID is considered read-only
345363 identifier = id_lines [0 ].split (' ' )[2 ].rstrip ('.' )
@@ -411,8 +429,8 @@ def valid(output):
411429 # If more than 1 task has been matched still, raise an exception
412430 if not valid (output ):
413431 raise TaskWarriorException (
414- " Unique identifiers {0} with description: {1} matches "
415- " multiple tasks: {2}" .format (
432+ ' Unique identifiers {0} with description: {1} matches '
433+ ' multiple tasks: {2}' .format (
416434 task ['uuid' ] or task ['id' ], task ['description' ], output )
417435 )
418436
0 commit comments