18
18
# -----------------------------------------------------------------------------
20
20
#Functions to convert a Task object to an XML string and back
21
import xml.dom.minidom
21
import xml.dom.minidom as minidom
22
22
import xml.sax.saxutils as saxutils
25
from GTG.tools import cleanxml
26
from GTG.tools import dates
23
from datetime import datetime
25
from GTG.tools import cleanxml
26
from GTG.tools.dates import Date
30
if len(node.childNodes) > 0:
31
return node.firstChild.nodeValue.strip()
35
def read_node(xmlnode, name):
36
node_list =xmlnode.getElementsByTagName(name)
37
if len(node_list) > 0:
38
return get_text(node_list[0])
28
43
#Take an empty task, an XML node and return a Task.
29
def task_from_xml(task,xmlnode) :
31
cur_stat = "%s" %xmlnode.getAttribute("status")
32
uuid = "%s" %xmlnode.getAttribute("uuid")
33
cur_task.set_uuid(uuid)
34
donedate = cleanxml.readTextNode(xmlnode,"donedate")
35
cur_task.set_status(cur_stat,donedate=dates.strtodate(donedate))
36
#we will fill the task with its content
37
cur_task.set_title(cleanxml.readTextNode(xmlnode,"title"))
39
sub_list = xmlnode.getElementsByTagName("subtask")
41
sub_tid = s.childNodes[0].nodeValue
42
cur_task.add_child(sub_tid)
43
attr_list = xmlnode.getElementsByTagName("attribute")
46
content = a.childNodes[0].nodeValue
44
def task_from_xml(task, xmlnode) :
45
#print "********************************"
46
#print xmlnode.toprettyxml()
48
task.set_uuid(xmlnode.getAttribute("uuid"))
49
task.set_title(read_node(xmlnode, "title"))
51
status = xmlnode.getAttribute("status")
52
donedate = Date.parse(read_node(xmlnode, "donedate"))
53
task.set_status(status, donedate=donedate)
55
duedate = Date(read_node(xmlnode, "duedate"))
56
task.set_due_date(duedate)
58
startdate = Date(read_node(xmlnode,"startdate"))
59
task.set_start_date(startdate)
61
modified = read_node(xmlnode, "modified")
63
modified = datetime.strptime(modified, "%Y-%m-%dT%H:%M:%S")
64
task.set_modified(modified)
66
tags = xmlnode.getAttribute("tags").replace(' ','')
67
tags = (tag for tag in tags.split(',') if tag.strip() != "")
69
#FIXME why unescape????
70
task.tag_added(saxutils.unescape(tag))
72
#FIXME why we need to convert that through an XML?
73
content = read_node(xmlnode, "content")
75
content = "<content>%s</content>" % content
76
content = minidom.parseString(content).firstChild.toxml()
77
task.set_text(content)
79
for subtask in xmlnode.getElementsByTagName("subtask"):
80
task.add_child(get_text(subtask))
82
for attr in xmlnode.getElementsByTagName("attribute"):
83
if len(attr.childNodes) > 0:
84
value = get_text(attr)
49
key = a.getAttribute("key")
50
namespace = a.getAttribute("namespace")
51
cur_task.set_attribute(key, content, namespace=namespace)
52
tasktext = xmlnode.getElementsByTagName("content")
53
if len(tasktext) > 0 :
54
if tasktext[0].firstChild :
55
tas = "<content>%s</content>" %tasktext[0].firstChild.nodeValue
56
content = xml.dom.minidom.parseString(tas)
57
cur_task.set_text(content.firstChild.toxml()) #pylint: disable-msg=E1103
58
cur_task.set_due_date(dates.strtodate(cleanxml.readTextNode(xmlnode,"duedate")))
59
cur_task.set_start_date(dates.strtodate(cleanxml.readTextNode(xmlnode,"startdate")))
60
cur_tags = xmlnode.getAttribute("tags").replace(' ','').split(",")
61
if "" in cur_tags: cur_tags.remove("")
62
for tag in cur_tags: cur_task.tag_added(saxutils.unescape(tag))
87
key = attr.getAttribute("key")
88
namespace = attr.getAttribute("namespace")
89
task.set_attribute(key, value, namespace=namespace)
91
# FIXME do we need remote task ids? I don't think so
92
# FIXME if so => rework them into a more usable structure!!! (like attributes)
65
95
remote_ids_list = xmlnode.getElementsByTagName("task-remote-ids")
66
96
for remote_id in remote_ids_list:
67
97
if remote_id.childNodes: