~saurabhanandiit/gtg/exportFixed

« back to all changes in this revision

Viewing changes to GTG/tools/taskxml.py

  • Committer: Izidor Matušov
  • Date: 2012-02-29 10:06:41 UTC
  • mfrom: (1098 gtg)
  • mto: This revision was merged to the branch mainline in revision 1103.
  • Revision ID: izidor.matusov@gmail.com-20120229100641-q1uns4yqz1fem2z4
Merged with the current trunk & solved problems with offset

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
# -----------------------------------------------------------------------------
19
19
 
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
23
 
import datetime
24
 
 
25
 
from GTG.tools import cleanxml
26
 
from GTG.tools import dates
 
23
from  datetime import datetime
 
24
 
 
25
from GTG.tools       import cleanxml
 
26
from GTG.tools.dates import Date
 
27
 
 
28
 
 
29
def get_text(node):
 
30
    return node.firstChild.nodeValue.strip()
 
31
 
 
32
def read_node(xmlnode, name):
 
33
    node_list =xmlnode.getElementsByTagName(name)
 
34
    if len(node_list) > 0:
 
35
        return get_text(node_list[0])
 
36
    else:
 
37
        return ""
 
38
 
27
39
 
28
40
#Take an empty task, an XML node and return a Task.
29
 
def task_from_xml(task,xmlnode) :
30
 
    cur_task = task
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"))
38
 
    #the subtasks
39
 
    sub_list = xmlnode.getElementsByTagName("subtask")
40
 
    for s in sub_list :
41
 
        sub_tid = s.childNodes[0].nodeValue
42
 
        cur_task.add_child(sub_tid)
43
 
    attr_list = xmlnode.getElementsByTagName("attribute")
44
 
    for a in attr_list:
45
 
        if len(a.childNodes):
46
 
            content = a.childNodes[0].nodeValue
 
41
def task_from_xml(task, xmlnode) :
 
42
    #print "********************************"
 
43
    #print xmlnode.toprettyxml()
 
44
 
 
45
    task.set_uuid(xmlnode.getAttribute("uuid"))
 
46
    task.set_title(read_node(xmlnode, "title"))
 
47
 
 
48
    status = xmlnode.getAttribute("status")
 
49
    donedate = Date.parse(read_node(xmlnode, "donedate"))
 
50
    task.set_status(status, donedate=donedate)
 
51
 
 
52
    duedate = Date(read_node(xmlnode, "duedate"))
 
53
    task.set_due_date(duedate)
 
54
 
 
55
    startdate = Date(read_node(xmlnode,"startdate"))
 
56
    task.set_start_date(startdate)
 
57
 
 
58
    modified = read_node(xmlnode, "modified")
 
59
    if modified != "":
 
60
        modified = datetime.strptime(modified, "%Y-%m-%dT%H:%M:%S")
 
61
        task.set_modified(modified)
 
62
 
 
63
    tags = xmlnode.getAttribute("tags").replace(' ','')
 
64
    tags = (tag for tag in tags.split(',') if tag.strip() != "")
 
65
    for tag in tags:
 
66
        #FIXME why unescape????
 
67
        task.tag_added(saxutils.unescape(tag))
 
68
 
 
69
    #FIXME why we need to convert that through an XML?
 
70
    content = read_node(xmlnode, "content")
 
71
    if content != "":
 
72
        content = "<content>%s</content>" % content
 
73
        content = minidom.parseString(content).firstChild.toxml()
 
74
        task.set_text(content)
 
75
 
 
76
    for subtask in xmlnode.getElementsByTagName("subtask"):
 
77
        task.add_child(get_text(subtask))
 
78
 
 
79
    for attr in xmlnode.getElementsByTagName("attribute"):
 
80
        if len(attr.childNodes) > 0:
 
81
            value = get_text(attr)
47
82
        else:
48
 
            content = ""
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))
 
83
            value = ""
 
84
        key = attr.getAttribute("key")
 
85
        namespace = attr.getAttribute("namespace")
 
86
        task.set_attribute(key, value, namespace=namespace)
63
87
 
 
88
    # FIXME do we need remote task ids? I don't think so
 
89
    # FIXME if so => rework them into a more usable structure!!! (like attributes)
64
90
    #REMOTE TASK IDS
 
91
    '''
65
92
    remote_ids_list = xmlnode.getElementsByTagName("task-remote-ids")
66
93
    for remote_id in remote_ids_list:
67
94
        if remote_id.childNodes:
69
96
            backend_id = node.firstChild.nodeValue
70
97
            remote_task_id = node.childNodes[1].firstChild.nodeValue
71
98
            task.add_remote_id(backend_id, remote_task_id)
72
 
    modified_string = cleanxml.readTextNode(xmlnode,"modified")
73
 
    if modified_string:
74
 
        modified_datetime = datetime.datetime.strptime(modified_string,\
75
 
                                                    "%Y-%m-%dT%H:%M:%S")
76
 
        cur_task.set_modified(modified_datetime)
77
 
    return cur_task
78
 
 
 
99
            '''
 
100
 
 
101
    return task
 
102
 
 
103
#FIXME maybe pretty XML should be enough for this...
79
104
#Task as parameter the doc where to put the XML node
80
105
def task_to_xml(doc,task) :
81
106
    t_xml = doc.createElement("task")
106
131
    if tex :
107
132
        #We take the xml text and convert it to a string
108
133
        #but without the "<content />" 
109
 
        element = xml.dom.minidom.parseString(tex)
 
134
        element = minidom.parseString(tex)
110
135
        temp = element.firstChild.toxml().partition("<content>")[2] #pylint: disable-msg=E1103
111
136
        desc = temp.partition("</content>")[0]
112
137
        #t_xml.appendChild(element.firstChild)
125
150
        backend_element.appendChild(task_element)
126
151
        task_element.appendChild(doc.createTextNode(task_id))
127
152
 
128
 
 
129
153
    return t_xml