Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Deprecate js override bits #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 2 .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
- name: install package in environment
run: python setup.py develop
- name: run tests
run: pytest -v
run: pytest -v -Werror
- name: run doctests
# pprint formatting was changed a lot in 3.5
if: ${{ matrix.python-version != '2.7' }}
Expand Down
4 changes: 2 additions & 2 deletions 4 tox.ini
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[tox]
envlist = py27, py36, py37, py38, py39, py310, docs, flake8, black
envlist = py27, py36, py37, py38, py39, py310, pypy3.8, docs, flake8, black
skipsdist = True

[testenv]
usedevelop = True
deps = -rrequirements_dev.txt
commands =
pytest {posargs}
pytest -Werror {posargs}
python -mdoctest README.rst

[testenv:py27]
Expand Down
22 changes: 18 additions & 4 deletions 22 ua_parser/user_agent_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ def Parse(user_agent_string, **jsParseBits):
"""Parse all the things
Args:
user_agent_string: the full user agent string
jsParseBits: javascript override bits
Returns:
A dictionary containing all parsed bits
"""
Expand All @@ -268,14 +267,19 @@ def ParseUserAgent(user_agent_string, **jsParseBits):
"""Parses the user-agent string for user agent (browser) info.
Args:
user_agent_string: The full user-agent string.
jsParseBits: javascript override bits.
Returns:
A dictionary containing parsed bits.
"""
return _cached(user_agent_string, jsParseBits, "user_agent", _ParseUserAgent)


def _ParseUserAgent(user_agent_string, jsParseBits):
if jsParseBits:
warnings.warn(
"javascript overrides are deprecated and will be removed next release",
category=DeprecationWarning,
stacklevel=2,
)
if (
"js_user_agent_family" in jsParseBits
and jsParseBits["js_user_agent_family"] != ""
Expand Down Expand Up @@ -318,14 +322,19 @@ def ParseOS(user_agent_string, **jsParseBits):
"""Parses the user-agent string for operating system info
Args:
user_agent_string: The full user-agent string.
jsParseBits: javascript override bits.
Returns:
A dictionary containing parsed bits.
"""
return _cached(user_agent_string, jsParseBits, "os", _ParseOS)


def _ParseOS(user_agent_string, jsParseBits):
if jsParseBits:
warnings.warn(
"javascript overrides are deprecated and will be removed next release",
category=DeprecationWarning,
stacklevel=2,
)
for osParser in OS_PARSERS:
os, os_v1, os_v2, os_v3, os_v4 = osParser.Parse(user_agent_string)
if os:
Expand All @@ -344,14 +353,19 @@ def ParseDevice(user_agent_string, **jsParseBits):
"""Parses the user-agent string for device info.
Args:
user_agent_string: The full user-agent string.
ua_family: The parsed user agent family name.
Returns:
A dictionary containing parsed bits.
"""
return _cached(user_agent_string, jsParseBits, "device", _ParseDevice)


def _ParseDevice(user_agent_string, jsParseBits):
if jsParseBits:
warnings.warn(
"javascript overrides are deprecated and will be removed next release",
category=DeprecationWarning,
stacklevel=2,
)
for deviceParser in DEVICE_PARSERS:
device, brand, model = deviceParser.Parse(user_agent_string)
if device:
Expand Down
65 changes: 18 additions & 47 deletions 65 ua_parser/user_agent_parser_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/python2.5
#
# Copyright 2008 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the 'License')
Expand Down Expand Up @@ -121,38 +119,6 @@ def testParseAll(self):
),
)

# Make a YAML file for manual comparsion with pgts_browser_list-orig.yaml
def makePGTSComparisonYAML(self):
import codecs

outfile = codecs.open("outfile.yaml", "w", "utf-8")
print >> outfile, "test_cases:"

yamlFile = open(os.path.join(TEST_RESOURCES_DIR, "pgts_browser_list.yaml"))
yamlContents = yaml.load(yamlFile, Loader=SafeLoader)
yamlFile.close()

for test_case in yamlContents["test_cases"]:
user_agent_string = test_case["user_agent_string"]
kwds = {}
if "js_ua" in test_case:
kwds = eval(test_case["js_ua"])

(family, major, minor, patch) = user_agent_parser.ParseUserAgent(
user_agent_string, **kwds
)

# Escape any double-quotes in the UA string
user_agent_string = re.sub(r'"', '\\"', user_agent_string)
print >> outfile, ' - user_agent_string: "' + user_agent_string + '"' + "\n" + ' family: "' + family + '"\n' + " major: " + (
"" if (major is None) else "'" + major + "'"
) + "\n" + " minor: " + (
"" if (minor is None) else "'" + minor + "'"
) + "\n" + " patch: " + (
"" if (patch is None) else "'" + patch + "'"
)
outfile.close()

# Run a set of test cases from a YAML file
def runUserAgentTestsFromYAML(self, file_name):
yamlFile = open(os.path.join(TEST_RESOURCES_DIR, file_name))
Expand All @@ -162,9 +128,6 @@ def runUserAgentTestsFromYAML(self, file_name):
for test_case in yamlContents["test_cases"]:
# Inputs to Parse()
user_agent_string = test_case["user_agent_string"]
kwds = {}
if "js_ua" in test_case:
kwds = eval(test_case["js_ua"])

# The expected results
expected = {
Expand All @@ -175,7 +138,7 @@ def runUserAgentTestsFromYAML(self, file_name):
}

result = {}
result = user_agent_parser.ParseUserAgent(user_agent_string, **kwds)
result = user_agent_parser.ParseUserAgent(user_agent_string)
self.assertEqual(
result,
expected,
Expand Down Expand Up @@ -205,9 +168,6 @@ def runOSTestsFromYAML(self, file_name):
for test_case in yamlContents["test_cases"]:
# Inputs to Parse()
user_agent_string = test_case["user_agent_string"]
kwds = {}
if "js_ua" in test_case:
kwds = eval(test_case["js_ua"])

# The expected results
expected = {
Expand All @@ -218,7 +178,7 @@ def runOSTestsFromYAML(self, file_name):
"patch_minor": test_case["patch_minor"],
}

result = user_agent_parser.ParseOS(user_agent_string, **kwds)
result = user_agent_parser.ParseOS(user_agent_string)
self.assertEqual(
result,
expected,
Expand All @@ -245,9 +205,6 @@ def runDeviceTestsFromYAML(self, file_name):
for test_case in yamlContents["test_cases"]:
# Inputs to Parse()
user_agent_string = test_case["user_agent_string"]
kwds = {}
if "js_ua" in test_case:
kwds = eval(test_case["js_ua"])

# The expected results
expected = {
Expand All @@ -256,7 +213,7 @@ def runDeviceTestsFromYAML(self, file_name):
"model": test_case["model"],
}

result = user_agent_parser.ParseDevice(user_agent_string, **kwds)
result = user_agent_parser.ParseDevice(user_agent_string)
self.assertEqual(
result,
expected,
Expand Down Expand Up @@ -308,7 +265,7 @@ def setUp(self):
seems to work out of the box.
"""
super(TestDeprecationWarnings, self).setUp()
warnings.simplefilter("default", DeprecationWarning)
warnings.simplefilter("always", DeprecationWarning)

def tearDown(self):
# not ideal as it discards all other warnings updates from the
Expand All @@ -329,6 +286,20 @@ def test_printer_deprecation(self):
self.assertEqual(len(ws), 1)
self.assertEqual(ws[0].category, DeprecationWarning)

def test_js_bits_deprecation(self):
for parser, count in [
(user_agent_parser.Parse, 3),
(user_agent_parser.ParseUserAgent, 1),
(user_agent_parser.ParseOS, 1),
(user_agent_parser.ParseDevice, 1),
]:
user_agent_parser._PARSE_CACHE.clear()
with warnings.catch_warnings(record=True) as ws:
parser("some random thing", js_attribute=True)
self.assertEqual(len(ws), count)
for w in ws:
self.assertEqual(w.category, DeprecationWarning)


if __name__ == "__main__":
unittest.main()
Morty Proxy This is a proxified and sanitized view of the page, visit original site.