Translated using Weblate.
[wammu.git] / Wammu / Reader.py
blobc9d7b605886fa1094fba3ebfd27fe229a50c6ee3
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 if guess and remain == 1:
118 # Read more things if there are some
119 remain = 2
120 start = False
121 else:
122 value = self.GetNext(loc)
123 try:
124 loc = value['Location']
125 except TypeError:
126 loc = value[0]['Location']
127 self.Parse(value)
128 if type(value) == list:
129 for i in range(len(value)):
130 value[i]['Synced'] = True
131 else:
132 value['Synced'] = True
133 data.append(value)
134 except gammu.ERR_UNKNOWN:
135 self.ShowMessage(
136 _('Ignoring unknown'),
137 _('While reading, entry on location %d reported unknown error, ignoring it!') % loc)
138 loc = loc + 1
139 except gammu.ERR_CORRUPTED:
140 self.ShowMessage(
141 _('Ignoring corrupted'),
142 _('While reading, entry on location %d seems to be corrupted, ignoring it!') % loc)
143 loc = loc + 1
144 except gammu.ERR_EMPTY:
145 break
147 remain = remain - 1
148 except (gammu.ERR_NOTSUPPORTED, gammu.ERR_NOTIMPLEMENTED):
149 location = 1
150 empty = 0
151 while remain > 0:
152 self.ShowProgress(100 * (total - remain) / total)
153 if self.canceled:
154 self.Canceled()
155 return
156 try:
157 value = self.Get(location)
158 self.Parse(value)
159 if type(value) == list:
160 for i in range(len(value)):
161 value[i]['Synced'] = True
162 else:
163 value['Synced'] = True
164 data.append(value)
165 remain = remain - 1
166 # If we didn't know count and reached end, try some more entries
167 if remain == 0 and guess:
168 remain = 20
169 total = total + 20
170 empty = 0
171 except gammu.ERR_EMPTY, val:
172 empty = empty + 1
173 # If we didn't know count and saw many empty entries, stop right now
174 if empty >= Wammu.configuration.ReadInt('/Hacks/MaxEmptyGuess') and guess:
175 break
176 # If we didn't read anything for long time, we bail out (workaround bad count reported by phone)
177 if empty >= Wammu.configuration.ReadInt('/Hacks/MaxEmptyKnown') and remain < 10:
178 self.ShowError(val[0])
179 remain = 0
180 except gammu.ERR_CORRUPTED:
181 self.ShowMessage(
182 _('Ignoring corrupted'),
183 _('While reading, entry on location %d seems to be corrupted, ignoring it!') % location)
184 remain = remain - 1
185 except gammu.ERR_UNKNOWN:
186 self.ShowMessage(
187 _('Ignoring unknown'),
188 _('While reading, entry on location %d reported unknown error, ignoring it!') % location)
189 remain = remain - 1
190 except gammu.GSMError, val:
191 self.ShowError(val[0], True)
192 return
193 location = location + 1
194 except gammu.ERR_INVALIDLOCATION, val:
195 # if we reached end with guess, it is okay
196 if not guess:
197 self.ShowError(val[0], True)
198 return
199 except gammu.GSMError, val:
200 self.ShowError(val[0], True)
201 return
203 self.Send(data)