1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 """Defines a class that parses the user agent string from the browser and
17 provides information about the browser."""
18
19 import re
20
21
23 """Class that parses the user agent string from the browser and provides
24 information about the browser. Used internally by L{BrowserInfo} and
25 L{WebBrowser}. Should not be used directly.
26
27 @author: Vaadin Ltd.
28 @author: Richard Lincoln
29 @version: 1.1.2
30 """
31
33 """Create an instance based on the given user agent.
34
35 @param userAgent:
36 User agent as provided by the browser.
37 """
38 self._isGecko = False
39 self._isWebKit = False
40 self._isPresto = False
41 self._isSafari = False
42 self._isChrome = False
43 self._isFirefox = False
44 self._isOpera = False
45 self._isIE = False
46 self._isWindows = False
47 self._isMacOSX = False
48 self._isLinux = False
49 self._browserEngineVersion = -1
50 self._browserMajorVersion = -1
51 self._browserMinorVersion = -1
52
53 userAgent = userAgent.lower()
54
55
56 self._isGecko = (userAgent.find('gecko') != -1
57 and userAgent.find('webkit') == -1)
58 self._isWebKit = userAgent.find('applewebkit') != -1
59 self._isPresto = userAgent.find(' presto/') != -1
60
61 self._isChrome = userAgent.find(' chrome/') != -1
62 self._isSafari = ((not self._isChrome)
63 and userAgent.find('safari') != -1)
64 self._isOpera = userAgent.find('opera') != -1
65 self._isIE = (userAgent.find('msie') != -1
66 and (not self._isOpera)
67 and userAgent.find('webtv') == -1)
68 self._isFirefox = userAgent.find(' firefox/') != -1
69
70
71 try:
72 if self._isGecko:
73 rvPos = userAgent.find('rv:')
74 if rvPos >= 0:
75 tmp = userAgent[rvPos + 3:]
76 tmp = re.sub('(\\.[0-9]+).+', '\\1', tmp, count=1)
77 self._browserEngineVersion = float(tmp)
78 elif self._isWebKit:
79 tmp = userAgent[userAgent.find('webkit/') + 7:]
80 tmp = re.sub('([0-9]+)[^0-9].+', '\\1', tmp, count=1)
81 self._browserEngineVersion = float(tmp)
82 except Exception:
83
84 print 'Browser engine version parsing failed for: ' + userAgent
85
86
87 try:
88 if self._isIE:
89 ieVersionString = userAgent[userAgent.find('msie ') + 5:]
90 ieVersionString = self.safeSubstring(ieVersionString, 0,
91 ieVersionString.find(';'))
92 self.parseVersionString(ieVersionString)
93 elif self._isFirefox:
94 i = userAgent.find(' firefox/') + 9
95 ver = self.safeSubstring(userAgent, i, i + 5)
96 self.parseVersionString(ver)
97 elif self._isChrome:
98 i = userAgent.find(' chrome/') + 8
99 ver = self.safeSubstring(userAgent, i, i + 5)
100 self.parseVersionString(ver)
101 elif self._isSafari:
102 i = userAgent.find(' version/') + 9
103 ver = self.safeSubstring(userAgent, i, i + 5)
104 self.parseVersionString(ver)
105 elif self._isOpera:
106 i = userAgent.find(' version/')
107 if i != -1:
108
109 i += 9
110 else:
111 i = userAgent.find('opera/') + 6
112 ver = self.safeSubstring(userAgent, i, i + 5)
113 self.parseVersionString(ver)
114 except Exception:
115
116 print 'Browser version parsing failed for: ' + userAgent
117
118
119 if 'windows ' in userAgent:
120 self._isWindows = True
121 elif 'linux' in userAgent:
122 self._isLinux = True
123 elif 'macintosh' in userAgent \
124 or 'mac osx' in userAgent \
125 or 'mac os x' in userAgent:
126 self._isMacOSX = True
127
128
130 idx = versionString.find('.')
131 if idx < 0:
132 idx = len(versionString)
133
134 ver = self.safeSubstring(versionString, 0, idx)
135 self._browserMajorVersion = int(ver)
136
137 idx2 = versionString.find('.', idx + 1)
138 if idx2 < 0:
139 idx2 = len(versionString)
140
141 try:
142 ver = self.safeSubstring(versionString, idx + 1, idx2)
143 self._browserMinorVersion = \
144 int( re.sub('[^0-9].*', '', ver) )
145 except ValueError:
146 pass
147
148
150 if beginIndex < 0:
151 beginIndex = 0
152
153 if endIndex < 0 or endIndex > len(string):
154 endIndex = len(string)
155
156 return string[beginIndex:endIndex]
157
158
160 """Tests if the browser is Firefox.
161
162 @return: true if it is Firefox, false otherwise
163 """
164 return self._isFirefox
165
166
168 """Tests if the browser is using the Gecko engine
169
170 @return: true if it is Gecko, false otherwise
171 """
172 return self._isGecko
173
174
176 """Tests if the browser is using the WebKit engine
177
178 @return: true if it is WebKit, false otherwise
179 """
180 return self._isWebKit
181
182
184 """Tests if the browser is using the Presto engine
185
186 @return: true if it is Presto, false otherwise
187 """
188 return self._isPresto
189
190
192 """Tests if the browser is Safari.
193
194 @return: true if it is Safari, false otherwise
195 """
196 return self._isSafari
197
198
200 """Tests if the browser is Chrome.
201
202 @return: true if it is Chrome, false otherwise
203 """
204 return self._isChrome
205
206
208 """Tests if the browser is Opera.
209
210 @return: true if it is Opera, false otherwise
211 """
212 return self._isOpera
213
214
216 """Tests if the browser is Internet Explorer.
217
218 @return: true if it is Internet Explorer, false otherwise
219 """
220 return self._isIE
221
222
224 """Returns the version of the browser engine. For WebKit this is
225 an integer e.g., 532.0. For gecko it is a float e.g., 1.8 or 1.9.
226
227 @return: The version of the browser engine
228 """
229 return self._browserEngineVersion
230
231
233 """Returns the browser major version e.g., 3 for Firefox 3.5, 4 for
234 Chrome 4, 8 for Internet Explorer 8.
235
236 Note that Internet Explorer 8 and newer will return the document
237 mode so IE8 rendering as IE7 will return 7.
238
239 @return: The major version of the browser.
240 """
241 return self._browserMajorVersion
242
243
245 """Returns the browser minor version e.g., 5 for Firefox 3.5.
246
247 @see: #getBrowserMajorVersion()
248
249 @return: The minor version of the browser, or -1 if not known/parsed.
250 """
251 return self._browserMinorVersion
252
253
255 """Sets the version for IE based on the documentMode. This is used
256 to return the correct the correct IE version when the version from
257 the user agent string and the value of the documentMode property do
258 not match.
259
260 @param documentMode:
261 The current document mode
262 """
263 self._browserMajorVersion = documentMode
264 self._browserMinorVersion = 0
265
266
268 """Tests if the browser is run on Windows.
269
270 @return: true if run on Windows, false otherwise
271 """
272 return self._isWindows
273
274
276 """Tests if the browser is run on Mac OSX.
277
278 @return: true if run on Mac OSX, false otherwise
279 """
280 return self._isMacOSX
281
282
284 """Tests if the browser is run on Linux.
285
286 @return: true if run on Linux, false otherwise
287 """
288 return self._isLinux
289