From 0b13496ae8f839e278b1a04b153b4a3aa28f53f7 Mon Sep 17 00:00:00 2001 From: masklinn Date: Mon, 15 Jul 2024 21:20:24 +0200 Subject: [PATCH] Fix perf scripts - enable graal on tox (24.1 with master's virtualenv plugin seems to work) - make tracemalloc optional in CLI script (doesn't work in pypy) - add regex to CLI script - comment graalpy trove classifier (doesn't exist yet) fixes #206 --- pyproject.toml | 9 ++++++++- src/ua_parser/__main__.py | 24 +++++++++++++++++++++--- tox.ini | 6 +++--- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index b7b0280..1979ebb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,7 +40,8 @@ classifiers = [ "Programming Language :: Python :: 3.12", "Programming Language :: Python :: Implementation :: CPython", "Programming Language :: Python :: Implementation :: PyPy", - "Programming Language :: Python :: Implementation :: GraalPy", + # no graalpy classifier yet (pypa/trove-classifiers#188) + # "Programming Language :: Python :: Implementation :: GraalPy", ] [project.optional-dependencies] @@ -54,6 +55,12 @@ where = ["src"] [tool.setuptools.package-data] "ua_parser" = ["py.typed"] +[tool.ruff] +exclude = [ + "src/ua_parser/_lazy.py", + "src/ua_parser/_matchers.py", +] + [tool.ruff.lint] select = ["F", "E", "W", "I", "RET", "RUF", "PT"] ignore = [ diff --git a/src/ua_parser/__main__.py b/src/ua_parser/__main__.py index c461a28..0ed140f 100644 --- a/src/ua_parser/__main__.py +++ b/src/ua_parser/__main__.py @@ -11,7 +11,7 @@ import sys import threading import time -import tracemalloc +import types from typing import ( Any, Callable, @@ -38,8 +38,15 @@ ) from .caching import Cache, Local from .loaders import load_builtins, load_yaml -from .re2 import Resolver as Re2Resolver -from .regex import Resolver as RegexResolver + +try: + from .re2 import Resolver as Re2Resolver +except ImportError: + pass +try: + from .regex import Resolver as RegexResolver +except ImportError: + pass from .user_agent_parser import Parse CACHEABLE = { @@ -60,6 +67,17 @@ ] ) +try: + import tracemalloc +except ImportError: + snapshot = types.SimpleNamespace( + compare_to=lambda _1, _2: [], + ) + tracemalloc = types.SimpleNamespace( # type: ignore + start=lambda: None, + take_snapshot=lambda: snapshot, + ) + def get_rules(parsers: List[str], regexes: Optional[io.IOBase]) -> Matchers: if regexes: diff --git a/tox.ini b/tox.ini index 778055e..d8c7cf0 100644 --- a/tox.ini +++ b/tox.ini @@ -2,13 +2,13 @@ min_version = 4.0 env_list = py3{9,10,11,12} pypy3.10 - #graalpy-24 + graalpy-24 flake8, black, typecheck labels = - test = py3{9,10,11,12},pypy3.10#,graalpy-24 + test = py3{9,10,11,12},pypy3.10,graalpy-24 cpy = py3{9,10,11,12} pypy = pypy3.10 - #graal = graalpy-24 + graal = graalpy-24 check = flake8, black, typecheck [testenv]