Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/xmlio.py
blob: fe1b4d8e61afc7a6ab69911a82f9e3ea76eae3ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#
#    Copyright (C) 2006, 2007, One Laptop Per Child
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import libxml2
import os
import logging
import base64


_logger = logging.getLogger('xmlio')


class Xmlio(object):
    ''' The xmlio of the activity. Contains methods to read and write
    the configuration for a browser session to and from xml. 
    '''
    
    def __init__(self, dtdpath, linkbar):        
        self.linkbar = linkbar        
        self.data = {}
        self.dtdpath = dtdpath
        self.data['name'] = 'first'
        self.session_data = ''
        
        try:
            self.dtd = libxml2.parseDTD(None, os.path.join(self.dtdpath, 'browser.dtd'))
        except libxml2.parserError, e:
            _logger.error('Init: no browser.dtd found ' +str(e))
            self.dtd = None
        self.ctxt = libxml2.newValidCtxt()               

    def read(self, filepath):
        ''' reads the configuration from an xml file '''
        
        try:
            doc = libxml2.parseFile(filepath)
            if doc.validateDtd(self.ctxt, self.dtd):
        
                # get the requested nodes
                xpa = doc.xpathNewContext()
                res = xpa.xpathEval("//*")

                # write their content to the data structure
                for elem in res:
                    attributes = elem.get_properties()
                    thumb = ''
                    link_name = ''
                    id = None
                    if( elem.name == 'link' ):
                        for attribute in attributes:
                            if(attribute.name == 'id'):
                                id = int(attribute.content)
                            elif(attribute.name == 'name'):
                                link_name = attribute.content
                            elif(attribute.name == 'thumb'):
                                thumb = attribute.content                                
                        self.linkbar._add_link(link_name, base64.b64decode(thumb), id)
                
                    elif( elem.name == 'session' ):
                        for attribute in attributes:
                            if(attribute.name == 'data'):                            
                                self.session_data = attribute.content
                        
                    elif( elem.name == 'browser' ):
                        for attribute in attributes:
                            if(attribute.name == 'name'):                            
                                self.data['name'] = attribute.content
                                
                xpa.xpathFreeContext()
            else:
                _logger.error('Read: Error in validation of the file')
                doc.freeDoc()
                return 1
            doc.freeDoc()
            return 0
        except libxml2.parserError, e:
            _logger.error('Read: Error parsing file ' +str(e))
            return 2
        
                
    def write(self, filepath):
        ''' writes the configuration to an xml file '''
        doc = libxml2.newDoc("1.0")
        root = doc.newChild(None, "browser", None)
        
        if(self.data.get('name', None) != None):                            
            root.setProp("name", self.data['name'])
        else:
            _logger.error('Write: No name is specified. Can not write session.')
            return 1

        elem = root.newChild(None, "session", None)
        elem.setProp("data", self.session_data)

        for child in self.linkbar.get_children():
            elem = root.newChild(None, "link", None)
            elem.setProp("id", str(self.linkbar.get_item_index(child)))
            elem.setProp("name", child.link_name)
            elem.setProp("thumb", base64.b64encode(child.buf))

                        
        if doc.validateDtd(self.ctxt, self.dtd):
            doc.saveFormatFile(filepath, 1)
        else:
            _logger.error('Write: Error in validation of the file')
            doc.freeDoc()
            return 2
        doc.freeDoc()
        return 0