Merge pull request #12 in TM/task from ~UNODE/task:2.4.0 to 2.4.0
* commit '4a305d5e51c4cccbc99b9ed41476a82966891461': Unittest - Fix path to user tx.data Unittest - Helper code to easily parse tx.data Unittest - Make client credentials available as a dictionary Unittest - Make taskd the first argument of Task()
This commit is contained in:
@@ -20,12 +20,12 @@ class Task(object):
|
|||||||
|
|
||||||
A taskw client should not be used after being destroyed.
|
A taskw client should not be used after being destroyed.
|
||||||
"""
|
"""
|
||||||
def __init__(self, taskw="task", taskd=None):
|
def __init__(self, taskd=None, taskw="task"):
|
||||||
"""Initialize a Task warrior (client) that can interact with a taskd
|
"""Initialize a Task warrior (client) that can interact with a taskd
|
||||||
server. The task client runs in a temporary folder.
|
server. The task client runs in a temporary folder.
|
||||||
|
|
||||||
:arg taskw: Task binary to use as client (defaults: task in PATH)
|
|
||||||
:arg taskd: Taskd instance for client-server configuration
|
:arg taskd: Taskd instance for client-server configuration
|
||||||
|
:arg taskw: Task binary to use as client (defaults: task in PATH)
|
||||||
"""
|
"""
|
||||||
self.taskw = taskw
|
self.taskw = taskw
|
||||||
self.taskd = taskd
|
self.taskd = taskd
|
||||||
@@ -105,8 +105,15 @@ class Task(object):
|
|||||||
else:
|
else:
|
||||||
user, group, org, userkey = taskd_user
|
user, group, org, userkey = taskd_user
|
||||||
|
|
||||||
self.credentials = "/".join((org, user, userkey))
|
credentials = "/".join((org, user, userkey))
|
||||||
self.config("taskd.credentials", self.credentials)
|
self.config("taskd.credentials", credentials)
|
||||||
|
|
||||||
|
self.credentials = {
|
||||||
|
"user": user,
|
||||||
|
"group": group,
|
||||||
|
"org": org,
|
||||||
|
"userkey": userkey,
|
||||||
|
}
|
||||||
|
|
||||||
def config(self, var, value):
|
def config(self, var, value):
|
||||||
"""Run setup `var` as `value` in taskd config
|
"""Run setup `var` as `value` in taskd config
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import atexit
|
|||||||
from time import sleep
|
from time import sleep
|
||||||
from subprocess import Popen
|
from subprocess import Popen
|
||||||
from .utils import (find_unused_port, release_port, port_used, run_cmd_wait,
|
from .utils import (find_unused_port, release_port, port_used, run_cmd_wait,
|
||||||
which)
|
which, parse_datafile)
|
||||||
from .exceptions import CommandError
|
from .exceptions import CommandError
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -278,4 +278,17 @@ class Taskd(object):
|
|||||||
else:
|
else:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def client_data(self, client):
|
||||||
|
"""Return a python list with the content of tx.data matching the given
|
||||||
|
task client. tx.data will be parsed to string and JSON.
|
||||||
|
"""
|
||||||
|
file = os.path.join(self.datadir,
|
||||||
|
"orgs",
|
||||||
|
client.credentials["org"],
|
||||||
|
"users",
|
||||||
|
client.credentials["userkey"],
|
||||||
|
"tx.data")
|
||||||
|
|
||||||
|
return parse_datafile(file)
|
||||||
|
|
||||||
# vim: ai sts=4 et sw=4
|
# vim: ai sts=4 et sw=4
|
||||||
|
|||||||
@@ -9,6 +9,10 @@ from subprocess import Popen, PIPE, STDOUT
|
|||||||
from threading import Thread
|
from threading import Thread
|
||||||
from Queue import Queue, Empty
|
from Queue import Queue, Empty
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
try:
|
||||||
|
import simplejson as json
|
||||||
|
except ImportError:
|
||||||
|
import json
|
||||||
from .exceptions import CommandError
|
from .exceptions import CommandError
|
||||||
|
|
||||||
USED_PORTS = set()
|
USED_PORTS = set()
|
||||||
@@ -247,4 +251,23 @@ except ImportError:
|
|||||||
return name
|
return name
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def parse_datafile(file):
|
||||||
|
"""Parse .data files on the client and server treating files as JSON
|
||||||
|
"""
|
||||||
|
data = []
|
||||||
|
with open(file) as fh:
|
||||||
|
for line in fh:
|
||||||
|
line = line.rstrip("\n")
|
||||||
|
|
||||||
|
# Turn [] strings into {} to be treated properly as JSON hashes
|
||||||
|
if line.startswith('[') and line.endswith(']'):
|
||||||
|
line = '{' + line[1:-1] + '}'
|
||||||
|
|
||||||
|
if line.startswith("{"):
|
||||||
|
data.append(json.loads(line))
|
||||||
|
else:
|
||||||
|
data.append(line)
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
# vim: ai sts=4 et sw=4
|
# vim: ai sts=4 et sw=4
|
||||||
|
|||||||
Reference in New Issue
Block a user