@@ -109,47 +109,45 @@ def Parse(self, user_agent_string):
109109 match = self .user_agent_re .search (user_agent_string )
110110 if match :
111111 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 )
116113 elif match .lastindex :
117114 os = match .group (1 )
118115
119116 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 )
124118 elif match .lastindex and match .lastindex >= 2 :
125119 os_v1 = match .group (2 )
126120
127121 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 )
132123 elif match .lastindex and match .lastindex >= 3 :
133124 os_v2 = match .group (3 )
134125
135126 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 )
140128 elif match .lastindex and match .lastindex >= 4 :
141129 os_v3 = match .group (4 )
142130
143131 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 )
148133 elif match .lastindex and match .lastindex >= 5 :
149134 os_v4 = match .group (5 )
150135
151136 return os , os_v1 , os_v2 , os_v3 , os_v4
152137
138+ def MultiReplace (string , match ):
139+ def _repl (m ):
140+ index = int (m .group (1 )) - 1
141+ group = match .groups ()
142+ if index < len (group ):
143+ return group [index ]
144+ return ''
145+
146+ _string = re .sub (r'\$(\d)' , _repl , string )
147+ _string = re .sub (r'^\s+|\s+$' , '' , _string )
148+ if _string == '' :
149+ return None
150+ return _string
153151
154152class DeviceParser (object ):
155153 def __init__ (self , pattern , regex_flag = None , device_replacement = None , brand_replacement = None ,
@@ -177,34 +175,20 @@ def MatchSpans(self, user_agent_string):
177175 for group_index in range (1 , match .lastindex + 1 )]
178176 return match_spans
179177
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-
194178 def Parse (self , user_agent_string ):
195179 device , brand , model = None , None , None
196180 match = self .user_agent_re .search (user_agent_string )
197181 if match :
198182 if self .device_replacement :
199- device = self . MultiReplace (self .device_replacement , match )
183+ device = MultiReplace (self .device_replacement , match )
200184 else :
201185 device = match .group (1 )
202186
203187 if self .brand_replacement :
204- brand = self . MultiReplace (self .brand_replacement , match )
188+ brand = MultiReplace (self .brand_replacement , match )
205189
206190 if self .model_replacement :
207- model = self . MultiReplace (self .model_replacement , match )
191+ model = MultiReplace (self .model_replacement , match )
208192 elif len (match .groups ()) > 0 :
209193 model = match .group (1 )
210194
@@ -279,9 +263,9 @@ def ParseUserAgent(user_agent_string, **jsParseBits):
279263 family = family or 'Other'
280264 return {
281265 'family' : family ,
282- 'major' : v1 ,
283- 'minor' : v2 ,
284- 'patch' : v3
266+ 'major' : v1 or None ,
267+ 'minor' : v2 or None ,
268+ 'patch' : v3 or None ,
285269 }
286270
287271
0 commit comments