@@ -109,48 +109,48 @@ 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
153138
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+
154154class DeviceParser (object ):
155155 def __init__ (self , pattern , regex_flag = None , device_replacement = None , brand_replacement = None ,
156156 model_replacement = None ):
@@ -177,34 +177,20 @@ def MatchSpans(self, user_agent_string):
177177 for group_index in range (1 , match .lastindex + 1 )]
178178 return match_spans
179179
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-
194180 def Parse (self , user_agent_string ):
195181 device , brand , model = None , None , None
196182 match = self .user_agent_re .search (user_agent_string )
197183 if match :
198184 if self .device_replacement :
199- device = self . MultiReplace (self .device_replacement , match )
185+ device = MultiReplace (self .device_replacement , match )
200186 else :
201187 device = match .group (1 )
202188
203189 if self .brand_replacement :
204- brand = self . MultiReplace (self .brand_replacement , match )
190+ brand = MultiReplace (self .brand_replacement , match )
205191
206192 if self .model_replacement :
207- model = self . MultiReplace (self .model_replacement , match )
193+ model = MultiReplace (self .model_replacement , match )
208194 elif len (match .groups ()) > 0 :
209195 model = match .group (1 )
210196
@@ -279,9 +265,9 @@ def ParseUserAgent(user_agent_string, **jsParseBits):
279265 family = family or 'Other'
280266 return {
281267 '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 ,
285271 }
286272
287273
0 commit comments