From afb178ad4ffa27463e590f23471ef21dd7874eec Mon Sep 17 00:00:00 2001 From: Andrei Kliuchnikov Date: Tue, 17 Dec 2024 14:21:28 +0400 Subject: [PATCH 1/2] Upgrade ua-parser version to 1.0.0 --- requirements.txt | 5 ++--- setup.py | 14 ++++++-------- user_agents/parsers.py | 10 +++++----- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/requirements.txt b/requirements.txt index 013c6b4..1a3f999 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,2 @@ -ua-parser==0.10.0 -PyYAML==5.4; python_version != '3.4' -PyYAML==5.4; python_version == '3.4' # the last version support py34 +ua-parser==1.0.0 +PyYAML==5.4 diff --git a/setup.py b/setup.py index 95ee319..fec6a81 100644 --- a/setup.py +++ b/setup.py @@ -15,7 +15,8 @@ zip_safe=False, include_package_data=True, package_data={'': ['README.rst']}, - install_requires=['ua-parser>=0.10.0'], + install_requires=['ua-parser>=1.0.0'], + python_requires='>=3.9', classifiers=[ 'Development Status :: 5 - Production/Stable', 'Environment :: Web Environment', @@ -23,14 +24,11 @@ 'License :: OSI Approved :: MIT License', 'Operating System :: OS Independent', 'Programming Language :: Python', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Programming Language :: Python :: 3.8', + 'Programming Language :: Python :: 3.9', + 'Programming Language :: Python :: 3.10', + 'Programming Language :: Python :: 3.11', + 'Programming Language :: Python :: 3.12', 'Topic :: Internet :: WWW/HTTP', 'Topic :: Software Development :: Libraries :: Python Modules', ] diff --git a/user_agents/parsers.py b/user_agents/parsers.py index 65f1ea6..6cabbf8 100644 --- a/user_agents/parsers.py +++ b/user_agents/parsers.py @@ -1,6 +1,6 @@ from collections import namedtuple -from ua_parser import user_agent_parser +from ua_parser import parser from .compat import string_types @@ -140,11 +140,11 @@ def parse_device(family, brand, model): class UserAgent(object): def __init__(self, user_agent_string): - ua_dict = user_agent_parser.Parse(user_agent_string) + ua = parser.parse(user_agent_string).with_defaults() self.ua_string = user_agent_string - self.os = parse_operating_system(**ua_dict['os']) - self.browser = parse_browser(**ua_dict['user_agent']) - self.device = parse_device(**ua_dict['device']) + self.os = parse_operating_system(ua.os.family, ua.os.major, ua.os.minor, ua.os.patch) + self.browser = parse_browser(ua.user_agent.family, ua.user_agent.major, ua.user_agent.minor, ua.user_agent.patch) + self.device = parse_device(ua.device.family, ua.device.brand, ua.device.model) def __str__(self): return "{device} / {os} / {browser}".format( From 746c33dec93ecbc12866a83c002e3aae0f7f7951 Mon Sep 17 00:00:00 2001 From: Andrei Kliuchnikov Date: Thu, 9 Jan 2025 13:32:23 +0400 Subject: [PATCH 2/2] Remove all python 2 compatibility code --- user_agents/compat.py | 14 -------------- user_agents/parsers.py | 6 +----- user_agents/tests.py | 16 +++------------- 3 files changed, 4 insertions(+), 32 deletions(-) delete mode 100644 user_agents/compat.py diff --git a/user_agents/compat.py b/user_agents/compat.py deleted file mode 100644 index c4d1a24..0000000 --- a/user_agents/compat.py +++ /dev/null @@ -1,14 +0,0 @@ -import sys - -PY3 = sys.version_info[0] == 3 - -if PY3: - string_types = str - - def iteritems(d, **kw): - return iter(d.items(**kw)) -else: - string_types = basestring - - def iteritems(d, **kw): - return iter(d.iteritems(**kw)) diff --git a/user_agents/parsers.py b/user_agents/parsers.py index 6cabbf8..3d19cc2 100644 --- a/user_agents/parsers.py +++ b/user_agents/parsers.py @@ -1,7 +1,6 @@ from collections import namedtuple from ua_parser import parser -from .compat import string_types MOBILE_DEVICE_FAMILIES = ( @@ -93,7 +92,7 @@ )) def verify_attribute(attribute): - if isinstance(attribute, string_types) and attribute.isdigit(): + if isinstance(attribute, str) and attribute.isdigit(): return int(attribute) return attribute @@ -153,9 +152,6 @@ def __str__(self): browser=self.get_browser() ) - def __unicode__(self): - return unicode(str(self)) - def _is_android_tablet(self): # Newer Android tablets don't have "Mobile" in their user agent string, # older ones like Galaxy Tab still have "Mobile" though they're not diff --git a/user_agents/tests.py b/user_agents/tests.py index d2633c0..dbf79d7 100644 --- a/user_agents/tests.py +++ b/user_agents/tests.py @@ -3,7 +3,6 @@ import unittest from ua_parser import user_agent_parser -from . import compat from .parsers import parse @@ -242,17 +241,8 @@ def test_strings(self): self.assertEqual(str(android_firefox_aurora_ua), "Generic Smartphone / Android / Firefox Mobile 27.0") def test_unicode_strings(self): - try: - # Python 2 - unicode_ua_str = unicode(devices['iphone']['user_agent']) - self.assertEqual(unicode_ua_str, - u"iPhone / iOS 5.1 / Mobile Safari 5.1") - self.assertTrue(isinstance(unicode_ua_str, unicode)) - except NameError: - # Python 3 - unicode_ua_str = str(devices['iphone']['user_agent']) - self.assertEqual(unicode_ua_str, - "iPhone / iOS 5.1 / Mobile Safari 5.1") + unicode_ua_str = str(devices['iphone']['user_agent']) + self.assertEqual(unicode_ua_str, "iPhone / iOS 5.1 / Mobile Safari 5.1") with open(os.path.join(os.path.dirname(__file__), 'devices.json')) as f: @@ -271,6 +261,6 @@ def test_func(self): # self.assertEqual(str(items['user_agent']), items['str']) return test_func -for device, items in compat.iteritems(devices): +for device, items in devices.items(): items['user_agent'] = parse(items['ua_string']) setattr(UserAgentsTest, 'test_' + device, test_wrapper(items))