@@ -109,48 +109,48 @@ def Parse(self, user_agent_string):
109
109
match = self .user_agent_re .search (user_agent_string )
110
110
if match :
111
111
if self .os_replacement :
112
- if re .search (r'\$1' , self .os_replacement ):
113
- os = re .sub (r'\$1' , match .group (1 ), self .os_replacement )
114
- else :
115
- os = self .os_replacement
112
+ os = MultiReplace (self .os_replacement , match )
116
113
elif match .lastindex :
117
114
os = match .group (1 )
118
115
119
116
if self .os_v1_replacement :
120
- if re .search (r'\$1' , self .os_v1_replacement ):
121
- os_v1 = re .sub (r'\$1' , match .group (1 ), self .os_v1_replacement )
122
- else :
123
- os_v1 = self .os_v1_replacement
117
+ os_v1 = MultiReplace (self .os_v1_replacement , match )
124
118
elif match .lastindex and match .lastindex >= 2 :
125
119
os_v1 = match .group (2 )
126
120
127
121
if self .os_v2_replacement :
128
- if re .search (r'\$2' , self .os_v2_replacement ):
129
- os_v2 = re .sub (r'\$2' , match .group (2 ), self .os_v2_replacement )
130
- else :
131
- os_v2 = self .os_v2_replacement
122
+ os_v2 = MultiReplace (self .os_v2_replacement , match )
132
123
elif match .lastindex and match .lastindex >= 3 :
133
124
os_v2 = match .group (3 )
134
125
135
126
if self .os_v3_replacement :
136
- if re .search (r'\$3' , self .os_v3_replacement ):
137
- os_v3 = re .sub (r'\$3' , match .group (3 ), self .os_v3_replacement )
138
- else :
139
- os_v3 = self .os_v3_replacement
127
+ os_v3 = MultiReplace (self .os_v3_replacement , match )
140
128
elif match .lastindex and match .lastindex >= 4 :
141
129
os_v3 = match .group (4 )
142
130
143
131
if self .os_v4_replacement :
144
- if re .search (r'\$4' , self .os_v4_replacement ):
145
- os_v4 = re .sub (r'\$4' , match .group (4 ), self .os_v4_replacement )
146
- else :
147
- os_v4 = self .os_v4_replacement
132
+ os_v4 = MultiReplace (self .os_v4_replacement , match )
148
133
elif match .lastindex and match .lastindex >= 5 :
149
134
os_v4 = match .group (5 )
150
135
151
136
return os , os_v1 , os_v2 , os_v3 , os_v4
152
137
153
138
139
+ def MultiReplace (string , match ):
140
+ def _repl (m ):
141
+ index = int (m .group (1 )) - 1
142
+ group = match .groups ()
143
+ if index < len (group ):
144
+ return group [index ]
145
+ return ''
146
+
147
+ _string = re .sub (r'\$(\d)' , _repl , string )
148
+ _string = re .sub (r'^\s+|\s+$' , '' , _string )
149
+ if _string == '' :
150
+ return None
151
+ return _string
152
+
153
+
154
154
class DeviceParser (object ):
155
155
def __init__ (self , pattern , regex_flag = None , device_replacement = None , brand_replacement = None ,
156
156
model_replacement = None ):
@@ -177,34 +177,20 @@ def MatchSpans(self, user_agent_string):
177
177
for group_index in range (1 , match .lastindex + 1 )]
178
178
return match_spans
179
179
180
- def MultiReplace (self , string , match ):
181
- def _repl (m ):
182
- index = int (m .group (1 )) - 1
183
- group = match .groups ()
184
- if index < len (group ):
185
- return group [index ]
186
- return ''
187
-
188
- _string = re .sub (r'\$(\d)' , _repl , string )
189
- _string = re .sub (r'^\s+|\s+$' , '' , _string )
190
- if _string == '' :
191
- return None
192
- return _string
193
-
194
180
def Parse (self , user_agent_string ):
195
181
device , brand , model = None , None , None
196
182
match = self .user_agent_re .search (user_agent_string )
197
183
if match :
198
184
if self .device_replacement :
199
- device = self . MultiReplace (self .device_replacement , match )
185
+ device = MultiReplace (self .device_replacement , match )
200
186
else :
201
187
device = match .group (1 )
202
188
203
189
if self .brand_replacement :
204
- brand = self . MultiReplace (self .brand_replacement , match )
190
+ brand = MultiReplace (self .brand_replacement , match )
205
191
206
192
if self .model_replacement :
207
- model = self . MultiReplace (self .model_replacement , match )
193
+ model = MultiReplace (self .model_replacement , match )
208
194
elif len (match .groups ()) > 0 :
209
195
model = match .group (1 )
210
196
@@ -279,9 +265,9 @@ def ParseUserAgent(user_agent_string, **jsParseBits):
279
265
family = family or 'Other'
280
266
return {
281
267
'family' : family ,
282
- 'major' : v1 ,
283
- 'minor' : v2 ,
284
- 'patch' : v3
268
+ 'major' : v1 or None ,
269
+ 'minor' : v2 or None ,
270
+ 'patch' : v3 or None ,
285
271
}
286
272
287
273
0 commit comments