~ubuntu-branches/debian/sid/gtg/sid

« back to all changes in this revision

Viewing changes to GTG/tools/taskxml.py

  • Committer: Package Import Robot
  • Author(s): Luca Falavigna
  • Date: 2013-05-05 14:23:30 UTC
  • mfrom: (17.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20130505142330-a1lmq91yf5ld4zoq
Tags: 0.3-2
* Upload to unstable.
* debian/control:
  - Use canonical URIs for VCS fields
* debian/install:
  - Install bash-completion script.
* debian/links:
  - gtcli link must point to gtcli, not gtg.

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
    if len(node.childNodes) > 0:
 
31
        return node.firstChild.nodeValue.strip()
 
32
    else:
 
33
        return ""
 
34
 
 
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])
 
39
    else:
 
40
        return ""
 
41
 
27
42
 
28
43
#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
 
44
def task_from_xml(task, xmlnode) :
 
45
    #print "********************************"
 
46
    #print xmlnode.toprettyxml()
 
47
 
 
48
    task.set_uuid(xmlnode.getAttribute("uuid"))
 
49
    task.set_title(read_node(xmlnode, "title"))
 
50
 
 
51
    status = xmlnode.getAttribute("status")
 
52
    donedate = Date.parse(read_node(xmlnode, "donedate"))
 
53
    task.set_status(status, donedate=donedate)
 
54
 
 
55
    duedate = Date(read_node(xmlnode, "duedate"))
 
56
    task.set_due_date(duedate)
 
57
 
 
58
    startdate = Date(read_node(xmlnode,"startdate"))
 
59
    task.set_start_date(startdate)
 
60
 
 
61
    modified = read_node(xmlnode, "modified")
 
62
    if modified != "":
 
63
        modified = datetime.strptime(modified, "%Y-%m-%dT%H:%M:%S")
 
64
        task.set_modified(modified)
 
65
 
 
66
    tags = xmlnode.getAttribute("tags").replace(' ','')
 
67
    tags = (tag for tag in tags.split(',') if tag.strip() != "")
 
68
    for tag in tags:
 
69
        #FIXME why unescape????
 
70
        task.tag_added(saxutils.unescape(tag))
 
71
 
 
72
    #FIXME why we need to convert that through an XML?
 
73
    content = read_node(xmlnode, "content")
 
74
    if content != "":
 
75
        content = "<content>%s</content>" % content
 
76
        content = minidom.parseString(content).firstChild.toxml()
 
77
        task.set_text(content)
 
78
 
 
79
    for subtask in xmlnode.getElementsByTagName("subtask"):
 
80
        task.add_child(get_text(subtask))
 
81
 
 
82
    for attr in xmlnode.getElementsByTagName("attribute"):
 
83
        if len(attr.childNodes) > 0:
 
84
            value = get_text(attr)
47
85
        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))
 
86
            value = ""
 
87
        key = attr.getAttribute("key")
 
88
        namespace = attr.getAttribute("namespace")
 
89
        task.set_attribute(key, value, namespace=namespace)
63
90
 
 
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)
64
93
    #REMOTE TASK IDS
 
94
    '''
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:
69
99
            backend_id = node.firstChild.nodeValue
70
100
            remote_task_id = node.childNodes[1].firstChild.nodeValue
71
101
            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
 
 
 
102
            '''
 
103
 
 
104
    return task
 
105
 
 
106
#FIXME maybe pretty XML should be enough for this...
79
107
#Task as parameter the doc where to put the XML node
80
108
def task_to_xml(doc,task) :
81
109
    t_xml = doc.createElement("task")
106
134
    if tex :
107
135
        #We take the xml text and convert it to a string
108
136
        #but without the "<content />" 
109
 
        element = xml.dom.minidom.parseString(tex)
 
137
        element = minidom.parseString(tex)
110
138
        temp = element.firstChild.toxml().partition("<content>")[2] #pylint: disable-msg=E1103
111
139
        desc = temp.partition("</content>")[0]
112
140
        #t_xml.appendChild(element.firstChild)
125
153
        backend_element.appendChild(task_element)
126
154
        task_element.appendChild(doc.createTextNode(task_id))
127
155
 
128
 
 
129
156
    return t_xml