Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 28d8538

Browse files
author
Andrew Selder
committed
Make python tests pass.
Have python client support v2_replacement and os_v1 and os_v2 replacement. Python client uses the None value instead of 'Other' when the device isn't found. Make the test reflect that. The probable correct fix is to make the parse return 'Other' in that case, but this would be a change to the behavior that others are probably depending on.
1 parent 782a88f commit 28d8538

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

ua_parser/user_agent_parser.py

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,20 @@
2626

2727

2828
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):
3030
"""Initialize UserAgentParser.
3131
3232
Args:
3333
pattern: a regular expression string
3434
family_replacement: a string to override the matched family (optional)
3535
v1_replacement: a string to override the matched v1 (optional)
36+
v2_replacement: a string to override the matched v2 (optional)
3637
"""
3738
self.pattern = pattern
3839
self.user_agent_re = re.compile(self.pattern)
3940
self.family_replacement = family_replacement
4041
self.v1_replacement = v1_replacement
42+
self.v2_replacement = v2_replacement
4143

4244
def MatchSpans(self, user_agent_string):
4345
match_spans = []
@@ -64,24 +66,32 @@ def Parse(self, user_agent_string):
6466
elif match.lastindex and match.lastindex >= 2:
6567
v1 = match.group(2)
6668

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:
6872
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+
7177
return family, v1, v2, v3
7278

7379

7480
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):
7682
"""Initialize UserAgentParser.
7783
7884
Args:
7985
pattern: a regular expression string
8086
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)
8189
"""
8290
self.pattern = pattern
8391
self.user_agent_re = re.compile(self.pattern)
8492
self.os_replacement = os_replacement
93+
self.os_v1_replacement = os_v1_replacement
94+
self.os_v2_replacement = os_v2_replacement
8595

8696
def MatchSpans(self, user_agent_string):
8797
match_spans = []
@@ -100,14 +110,20 @@ def Parse(self, user_agent_string):
100110
else:
101111
os = match.group(1)
102112

103-
if match.lastindex >= 2:
113+
if self.os_v1_replacement:
114+
os_v1 = self.os_v1_replacement
115+
elif match.lastindex >= 2:
104116
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)
111127

112128
return os, os_v1, os_v2, os_v3, os_v4
113129

@@ -410,9 +426,14 @@ def GetFilters(user_agent_string, js_user_agent_string=None,
410426
if 'v1_replacement' in _ua_parser:
411427
_v1_replacement = _ua_parser['v1_replacement']
412428

429+
_v2_replacement = None
430+
if 'v2_replacement' in _ua_parser:
431+
_v2_replacement = _ua_parser['v2_replacement']
432+
413433
USER_AGENT_PARSERS.append(UserAgentParser(_regex,
414434
_family_replacement,
415-
_v1_replacement))
435+
_v1_replacement,
436+
_v2_replacement))
416437

417438
OS_PARSERS = []
418439
for _os_parser in regexes['os_parsers']:

ua_parser/user_agent_parser_test.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,15 @@ def runDeviceTestsFromYAML(self, file_name):
196196
if 'js_ua' in test_case:
197197
kwds = eval(test_case['js_ua'])
198198

199+
# The python client has been returning None is the family doesn't match. This probably should be changed in the parser,
200+
# but would break many existing clients. If we want to change the parser behavior, it's line 152 of the parser.
201+
good_family = test_case['family']
202+
if good_family == 'Other':
203+
good_family = None
204+
199205
# The expected results
200206
expected = {
201-
'family': test_case['family']
207+
'family': good_family
202208
}
203209

204210
result = user_agent_parser.ParseDevice(user_agent_string, **kwds)

0 commit comments

Comments
 (0)