Translation update done using Pootle.
[wammu.git] / Wammu / Reader.py
blob7f252c77cdf1944db99432999105ebda9f9d0d2c
1 # -*- coding: UTF-8 -*-
2 # vim: expandtab sw=4 ts=4 sts=4:
3 '''
4 Wammu - Phone manager
5 Generic reader class
6 '''
7 __author__ = 'Michal Čihař'
8 __email__ = 'michal@cihar.com'
9 __license__ = '''
10 Copyright © 2003 - 2010 Michal Čihař
12 This program is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License version 2 as published by
14 the Free Software Foundation.
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 more details.
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 '''
26 import Wammu.Thread
27 import Wammu
28 if Wammu.gammu_error == None:
29 import gammu
31 class Reader(Wammu.Thread.Thread):
32 '''
33 Generic thread for reading information from phone.
34 '''
35 def FallBackStatus(self):
36 '''
37 Returns fall back status if real can not be obtained.
38 '''
39 return 200
41 def GetNextStart(self):
42 '''
43 Initiates get next sequence.
45 Should be implemented in subclases.
46 '''
47 raise Exception('Not implemented!')
49 def GetNext(self, location):
50 '''
51 Gets next entry.
53 Should be implemented in subclases.
54 '''
55 raise Exception('Not implemented!')
57 def Get(self, location):
58 '''
59 Gets entry.
61 Should be implemented in subclases.
62 '''
63 raise Exception('Not implemented!')
65 def GetStatus(self):
66 '''
67 Gets status of entries.
69 Should be implemented in subclases.
70 '''
71 raise Exception('Not implemented!')
73 def Parse(self):
74 '''
75 Parses entry.
77 Should be implemented in subclases.
78 '''
79 raise Exception('Not implemented!')
81 def Send(self):
82 '''
83 Sends entries to parent.
85 Should be implemented in subclases.
86 '''
87 raise Exception('Not implemented!')
89 def Run(self):
90 '''
91 Main reader function, executed in thread.
92 '''
93 self.ShowProgress(0)
95 guess = False
96 try:
97 total = self.GetStatus()
98 except gammu.GSMError, val:
99 guess = True
100 total = self.FallBackStatus()
102 remain = total
104 data = []
106 try:
107 start = True
108 while remain > 0:
109 self.ShowProgress(100 * (total - remain) / total)
110 if self.canceled:
111 self.Canceled()
112 return
113 try:
114 if start:
115 loc = 0
116 value = self.GetNextStart()
117 start = False
118 else:
119 value = self.GetNext(loc)
120 try:
121 loc = value['Location']
122 except TypeError:
123 loc = value[0]['Location']
124 self.Parse(value)
125 if type(value) == list:
126 for i in range(len(value)):
127 value[i]['Synced'] = True
128 else:
129 value['Synced'] = True
130 data.append(value)
131 except gammu.ERR_UNKNOWN:
132 self.ShowMessage(
133 _('Ignoring unknown'),
134 _('While reading, entry on location %d reported unknown error, ignoring it!') % loc)
135 loc = loc + 1
136 except gammu.ERR_CORRUPTED:
137 self.ShowMessage(
138 _('Ignoring corrupted'),
139 _('While reading, entry on location %d seems to be corrupted, ignoring it!') % loc)
140 loc = loc + 1
141 except gammu.ERR_EMPTY:
142 break
144 remain = remain - 1
145 except (gammu.ERR_NOTSUPPORTED, gammu.ERR_NOTIMPLEMENTED):
146 location = 1
147 empty = 0
148 while remain > 0:
149 self.ShowProgress(100 * (total - remain) / total)
150 if self.canceled:
151 self.Canceled()
152 return
153 try:
154 value = self.Get(location)
155 self.Parse(value)
156 if type(value) == list:
157 for i in range(len(value)):
158 value[i]['Synced'] = True
159 else:
160 value['Synced'] = True
161 data.append(value)
162 remain = remain - 1
163 # If we didn't know count and reached end, try some more entries
164 if remain == 0 and guess:
165 remain = 20
166 total = total + 20
167 empty = 0
168 except gammu.ERR_EMPTY, val:
169 empty = empty + 1
170 # If we didn't know count and saw many empty entries, stop right now
171 if empty >= Wammu.configuration.ReadInt('/Hacks/MaxEmptyGuess') and guess:
172 break
173 # If we didn't read anything for long time, we bail out (workaround bad count reported by phone)
174 if empty >= Wammu.configuration.ReadInt('/Hacks/MaxEmptyKnown') and remain < 10:
175 self.ShowError(val[0])
176 remain = 0
177 except gammu.ERR_CORRUPTED:
178 self.ShowMessage(
179 _('Ignoring corrupted'),
180 _('While reading, entry on location %d seems to be corrupted, ignoring it!') % location)
181 remain = remain - 1
182 except gammu.ERR_UNKNOWN:
183 self.ShowMessage(
184 _('Ignoring unknown'),
185 _('While reading, entry on location %d reported unknown error, ignoring it!') % location)
186 remain = remain - 1
187 except gammu.GSMError, val:
188 self.ShowError(val[0], True)
189 return
190 location = location + 1
191 except gammu.ERR_INVALIDLOCATION, val:
192 # if we reached end with guess, it is okay
193 if not guess:
194 self.ShowError(val[0], True)
195 return
196 except gammu.GSMError, val:
197 self.ShowError(val[0], True)
198 return
200 self.Send(data)