26
26
27
27
28
28
class UserAgentParser (object ):
29
- def __init__ (self , pattern , family_replacement = None , v1_replacement = None ):
29
+ def __init__ (self , pattern , family_replacement = None , v1_replacement = None , v2_replacement = None ):
30
30
"""Initialize UserAgentParser.
31
31
32
32
Args:
33
33
pattern: a regular expression string
34
34
family_replacement: a string to override the matched family (optional)
35
35
v1_replacement: a string to override the matched v1 (optional)
36
+ v2_replacement: a string to override the matched v2 (optional)
36
37
"""
37
38
self .pattern = pattern
38
39
self .user_agent_re = re .compile (self .pattern )
39
40
self .family_replacement = family_replacement
40
41
self .v1_replacement = v1_replacement
42
+ self .v2_replacement = v2_replacement
41
43
42
44
def MatchSpans (self , user_agent_string ):
43
45
match_spans = []
@@ -64,24 +66,32 @@ def Parse(self, user_agent_string):
64
66
elif match .lastindex and match .lastindex >= 2 :
65
67
v1 = match .group (2 )
66
68
67
- if match .lastindex and match .lastindex >= 3 :
69
+ if self .v2_replacement :
70
+ v2 = self .v2_replacement
71
+ elif match .lastindex and match .lastindex >= 3 :
68
72
v2 = match .group (3 )
69
- if match .lastindex >= 4 :
70
- v3 = match .group (4 )
73
+
74
+ if match .lastindex and match .lastindex >= 4 :
75
+ v3 = match .group (4 )
76
+
71
77
return family , v1 , v2 , v3
72
78
73
79
74
80
class OSParser (object ):
75
- def __init__ (self , pattern , os_replacement = None ):
81
+ def __init__ (self , pattern , os_replacement = None , os_v1_replacement = None , os_v2_replacement = None ):
76
82
"""Initialize UserAgentParser.
77
83
78
84
Args:
79
85
pattern: a regular expression string
80
86
os_replacement: a string to override the matched os (optional)
87
+ os_v1_replacement: a string to override the matched v1 (optional)
88
+ os_v2_replacement: a string to override the matched v2 (optional)
81
89
"""
82
90
self .pattern = pattern
83
91
self .user_agent_re = re .compile (self .pattern )
84
92
self .os_replacement = os_replacement
93
+ self .os_v1_replacement = os_v1_replacement
94
+ self .os_v2_replacement = os_v2_replacement
85
95
86
96
def MatchSpans (self , user_agent_string ):
87
97
match_spans = []
@@ -100,14 +110,20 @@ def Parse(self, user_agent_string):
100
110
else :
101
111
os = match .group (1 )
102
112
103
- if match .lastindex >= 2 :
113
+ if self .os_v1_replacement :
114
+ os_v1 = self .os_v1_replacement
115
+ elif match .lastindex >= 2 :
104
116
os_v1 = match .group (2 )
105
- if match .lastindex >= 3 :
106
- os_v2 = match .group (3 )
107
- if match .lastindex >= 4 :
108
- os_v3 = match .group (4 )
109
- if match .lastindex >= 5 :
110
- os_v4 = match .group (5 )
117
+
118
+ if self .os_v2_replacement :
119
+ os_v2 = self .os_v2_replacement
120
+ elif match .lastindex >= 3 :
121
+ os_v2 = match .group (3 )
122
+
123
+ if match .lastindex >= 4 :
124
+ os_v3 = match .group (4 )
125
+ if match .lastindex >= 5 :
126
+ os_v4 = match .group (5 )
111
127
112
128
return os , os_v1 , os_v2 , os_v3 , os_v4
113
129
@@ -410,9 +426,14 @@ def GetFilters(user_agent_string, js_user_agent_string=None,
410
426
if 'v1_replacement' in _ua_parser :
411
427
_v1_replacement = _ua_parser ['v1_replacement' ]
412
428
429
+ _v2_replacement = None
430
+ if 'v2_replacement' in _ua_parser :
431
+ _v2_replacement = _ua_parser ['v2_replacement' ]
432
+
413
433
USER_AGENT_PARSERS .append (UserAgentParser (_regex ,
414
434
_family_replacement ,
415
- _v1_replacement ))
435
+ _v1_replacement ,
436
+ _v2_replacement ))
416
437
417
438
OS_PARSERS = []
418
439
for _os_parser in regexes ['os_parsers' ]:
0 commit comments