~ubuntu-branches/ubuntu/oneiric/emesene/oneiric-proposed

« back to all changes in this revision

Viewing changes to emesenelib/XmlParser.py

  • Committer: Bazaar Package Importer
  • Author(s): Devid Antonio Filoni
  • Date: 2011-03-03 14:49:13 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20110303144913-0adl9cmw2s35lvzo
Tags: 2.0~git20110303-0ubuntu1
* New upstream git revision (LP: #728469).
* Remove debian/watch, debian/emesene.xpm, debian/install and
  debian/README.source files.
* Remove 21_svn2451_fix_avatar and 20_dont_build_own_libmimic patches.
* debian/control: modify python to python (>= 2.5) in Build-Depends field.
* debian/control: remove python-libmimic from Recommends field.
* debian/control: modify python-gtk2 (>= 2.10) to python-gtk2 (>= 2.12) in
  Depends field.
* debian/control: add python-appindicator and python-xmpp to Recommends
  field.
* debian/control: add python-papyon (>= 0.5.4) and python-webkit to Depends
  field.
* debian/control: update Description field.
* debian/control: add python-setuptools to Build-Depends field.
* debian/control: move python-dbus and python-notify to Depends field.
* Update debian/copyright file.
* Update debian/links file.
* debian/menu: update description field.
* Bump Standards-Version to 3.9.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
 
3
 
#   This file is part of emesene.
4
 
#
5
 
#    Emesene is free software; you can redistribute it and/or modify
6
 
#    it under the terms of the GNU General Public License as published by
7
 
#    the Free Software Foundation; either version 2 of the License, or
8
 
#    (at your option) any later version.
9
 
#
10
 
#    emesene is distributed in the hope that it will be useful,
11
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
#    GNU General Public License for more details.
14
 
#
15
 
#    You should have received a copy of the GNU General Public License
16
 
#    along with emesene; if not, write to the Free Software
17
 
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
 
19
 
import xml.parsers.expat
20
 
 
21
 
class DynamicParser:
22
 
    '''Parse dynamic xml'''
23
 
    def __init__(self, xml_raw):
24
 
        '''init parser and setup handlers'''
25
 
        self.parser = xml.parsers.expat.ParserCreate()
26
 
        self.parser.buffer_text = True
27
 
        self.parser.returns_unicode = False
28
 
        
29
 
        self.groups = []
30
 
        self.contacts = []
31
 
        self.annotations = []
32
 
        self.group_ids = []
33
 
        
34
 
        self.in_group = False
35
 
        self.in_contact = False
36
 
        self.in_annotation = False
37
 
        self.in_group_ids = False
38
 
        
39
 
        self.group_data = {}
40
 
        self.contact_data = {}
41
 
        self.annotation_data = {}
42
 
        self.group_ids_data = {}
43
 
        
44
 
        self.current_tag = ''
45
 
        
46
 
        #connect handlers
47
 
        self.parser.StartElementHandler = self.start_element
48
 
        self.parser.EndElementHandler = self.end_element
49
 
        self.parser.CharacterDataHandler = self.char_data
50
 
        self.parser.Parse(xml_raw)
51
 
                
52
 
        del(xml_raw)
53
 
        
54
 
    def start_element(self, name, attrs):
55
 
        '''Start xml element handler'''
56
 
        if name == 'Group':
57
 
            self.in_group = True
58
 
        elif name == 'Contact':
59
 
            self.in_contact = True
60
 
        elif name == 'Annotation':
61
 
            self.in_annotation = True
62
 
        elif name == 'groupIds':
63
 
            self.in_group_ids = True
64
 
        self.current_tag = name
65
 
        
66
 
    def end_element(self, name):
67
 
        '''End xml element handler'''
68
 
        if name == 'Group':
69
 
            self.in_group = False
70
 
            if len(self.group_data) > 0:
71
 
                if len(self.annotations) > 0:
72
 
                    self.group_data.update({'Annotations':self.annotations})
73
 
                    self.annotations = []
74
 
                self.groups.append(self.group_data)
75
 
                self.group_data = {}
76
 
        elif name == 'Contact':
77
 
            self.in_contact = False
78
 
            if len(self.contact_data) > 0:
79
 
                annotations = self.annotations
80
 
                self.contact_data.update({'Annotations':annotations})
81
 
                self.contact_data.update({'groupIds':self.group_ids})
82
 
                self.contacts.append(self.contact_data)
83
 
                self.contact_data = {}
84
 
                self.annotations = []
85
 
                self.group_ids = []
86
 
        elif name == 'Annotation':
87
 
            self.in_annotation = False
88
 
            if len(self.annotation_data) > 0:
89
 
                self.annotations.append(self.annotation_data)
90
 
                self.annotation_data = {}
91
 
        elif name == 'groupIds':
92
 
            self.in_group_ids = False
93
 
            if len(self.group_ids_data) > 0:
94
 
                self.group_ids.append(self.group_ids_data)
95
 
                self.group_ids_data = {}
96
 
 
97
 
    def char_data(self, data):
98
 
        '''Char xml element handler'''
99
 
        if self.in_group_ids:
100
 
            self.group_ids.append(data)
101
 
        elif self.in_annotation:
102
 
            self.annotation_data.update({self.current_tag:data})
103
 
        elif self.in_group:
104
 
            self.group_data.update({self.current_tag:data})
105
 
        elif self.in_contact:
106
 
            self.contact_data.update({self.current_tag:data})
107
 
 
108
 
class MembershipParser:
109
 
    '''Parse membership xml'''
110
 
    def __init__(self, xml_raw):
111
 
        '''init parser and setup handlers'''
112
 
        self.parser = xml.parsers.expat.ParserCreate()
113
 
        self.parser.buffer_text = True
114
 
        self.parser.returns_unicode = False
115
 
        
116
 
        self.memberships = []
117
 
        self.members = []
118
 
        
119
 
        self.in_membership = False
120
 
        self.in_member = False
121
 
        
122
 
        self.membership_data = {}
123
 
        self.member_data = {}
124
 
        
125
 
        self.current_tag = ''
126
 
        
127
 
        #connect handlers
128
 
        self.parser.StartElementHandler = self.start_element
129
 
        self.parser.EndElementHandler = self.end_element
130
 
        self.parser.CharacterDataHandler = self.char_data
131
 
        self.parser.Parse(xml_raw)
132
 
        del(xml_raw)
133
 
        
134
 
    def start_element(self, name, attrs):
135
 
        '''Start xml element handler'''
136
 
        if name == 'Membership':
137
 
            self.in_membership = True
138
 
        elif name == 'Member':
139
 
            self.in_member = True
140
 
        self.current_tag = name
141
 
        
142
 
    def end_element(self, name):
143
 
        '''End xml element handler'''
144
 
        if name == 'Membership':
145
 
            self.in_membership = False
146
 
            if len(self.membership_data) > 0:
147
 
                self.membership_data.update({'Members':self.members})
148
 
                self.memberships.append(self.membership_data)
149
 
                self.membership_data = {}
150
 
                self.members = []
151
 
        if name == 'Member':
152
 
            self.in_member = False
153
 
            if len(self.member_data) > 0:
154
 
                self.members.append(self.member_data)
155
 
                self.member_data = {}
156
 
 
157
 
    def char_data(self, data):
158
 
        '''Char xml element handler'''
159
 
        if self.in_member:
160
 
            self.member_data.update({self.current_tag:data})
161
 
        elif self.in_membership:
162
 
            self.membership_data.update({self.current_tag:data})
163
 
 
164
 
class SSoParser:
165
 
    '''Parse sso xml'''
166
 
    def __init__(self, xml_raw):
167
 
        '''init parser and setup handlers'''
168
 
        self.parser = xml.parsers.expat.ParserCreate()
169
 
        self.parser.buffer_text = True
170
 
        self.parser.returns_unicode = False
171
 
        
172
 
        self.tokens = {}
173
 
        
174
 
        self.in_token_response = False
175
 
        self.in_address = False
176
 
        self.in_binary_secret = False
177
 
        self.in_binary_security = False
178
 
        
179
 
        self.current_tag = ''
180
 
        self.current_token = ''
181
 
        
182
 
        #connect handlers
183
 
        self.parser.StartElementHandler = self.start_element
184
 
        self.parser.EndElementHandler = self.end_element
185
 
        self.parser.CharacterDataHandler = self.char_data
186
 
        self.parser.Parse(xml_raw)
187
 
        del(xml_raw)
188
 
        
189
 
    def start_element(self, name, attrs):
190
 
        '''Start xml element handler'''
191
 
        if name == 'RequestSecurityTokenResponse':
192
 
            self.in_token_response = True
193
 
        elif name == 'wsa:Address':
194
 
            self.in_address = True
195
 
        elif name == 'wst:BinarySecret':
196
 
            self.in_binary_secret = True
197
 
        elif name == 'wsse:BinarySecurityToken':
198
 
            self.in_binary_security = True
199
 
        self.current_tag = name
200
 
        
201
 
    def end_element(self, name):
202
 
        '''End xml element handler'''
203
 
        if name == 'RequestSecurityTokenResponse':
204
 
            self.in_token_response = False
205
 
        elif name == 'wsa:Address':
206
 
            self.in_address = False
207
 
        elif name == 'wst:BinarySecret':
208
 
            self.in_binary_secret = False
209
 
        elif name == 'wsse:BinarySecurityToken':
210
 
            self.in_binary_security = False
211
 
 
212
 
    def char_data(self, data):
213
 
        '''Char xml element handler'''
214
 
        if self.in_address:
215
 
            self.tokens.update({data:{}})
216
 
            self.current_token = data
217
 
        elif self.in_binary_secret:
218
 
            self.tokens[self.current_token].update({'secret':data})
219
 
        elif self.in_binary_security:
220
 
            self.tokens[self.current_token].update({'security':data})