diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f82d553..abc77c8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,6 +2,7 @@ name: CI on: push: + branches: pull_request: permissions: {} @@ -128,7 +129,7 @@ jobs: - run: python -mpip install pytest pyyaml - run: python -mpip install ./ua-parser-builtins # install rs accelerator if available, ignore if not - - run: python -mpip install ua-parser-rs || true + - run: python -mpip install ./ua-parser-rs || true # re2 is basically impossible to install from source so don't # bother, and suppress installation failure so the test does # not fail (re2 tests will just be skipped for versions / diff --git a/.github/workflows/release-wheels.yml b/.github/workflows/release-wheels.yml new file mode 100644 index 0000000..2317c67 --- /dev/null +++ b/.github/workflows/release-wheels.yml @@ -0,0 +1,256 @@ +name: Wheels + +on: + pull_request: + types: [opened, reopened, labeled, synchronize] + workflow_dispatch: + inputs: + release: + description: 'Push wheels to pypi' + type: boolean + default: false + required: true + +permissions: {} + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + py-wheels-matrix: + name: "generate build matrix" + runs-on: ubuntu-latest + if: > + github.event_name == 'workflow_dispatch' || + ( + github.event_name == 'pull_request' && + github.event.type == 'labeled' && + github.event.label.name == 'check-wheels' + ) || + ( + github.event_name == 'pull_request' && + github.event.type != 'labeled' && + contains(github.event.pull_request.labels.*.name, 'check-wheels') + ) + outputs: + matrix: ${{ steps.make-matrix.outputs.matrix }} + steps: + - id: make-matrix + shell: python + name: generate matrix + run: | + import itertools + import json + import os + import pprint + + builder = { + ('linux', 'x86_64'): 'ubuntu-latest', + ('linux', 'aarch64'): 'ubuntu-24.04-arm', + ('musllinux', 'x86_64'): 'ubuntu-latest', + ('musllinux', 'aarch64'): 'ubuntu-24.04-arm', + ('macos', 'x86_64'): 'macos-15-intel', + ('macos', 'aarch64'): 'macos-latest', + ('windows', 'x86_64'): 'windows-latest', + ('windows', 'aarch64'): 'windows-11-arm', + } + + matrix = [ + d + for d in map(dict, itertools.product( + (('python-version', v) for v in ["3.x", "3.14t", "pypy-3.11", "graalpy-25"]), + (('arch', a) for a in ["x86_64", "aarch64"]), + (('platform', p) for p in ["linux", "musllinux", "windows", "macos"]) + )) + # on windows, only cpython has arm builds (?) + if d['python-version'].startswith('3.') \ + or d['platform'] != 'windows' \ + or d['arch'] != 'aarch64' + ] + for job in matrix: + match job['platform']: + case 'linux': + job['manylinux'] = 'auto' + job['args'] = ' --zig' + case 'mussllinux': + job['manylinux'] = 'musllinux_1_2' + + job['runs'] = builder[job['platform'], job['arch']] + + with open(os.environ['GITHUB_OUTPUT'], 'w') as f: + f.write("matrix=") + json.dump({'include': matrix}, f) + f.flush() + + py-release-wheels: + needs: [py-wheels-matrix] + strategy: + matrix: ${{fromJson(needs.py-wheels-matrix.outputs.matrix)}} + + runs-on: ${{ matrix.runs }} + + steps: + - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # 6.0.1 + with: + persist-credentials: false + - uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # 6.1.0 + with: + python-version: ${{ matrix.python-version }} + # windows/arm doesn't have a rust toolchain by default + - if: matrix.platform == 'windows' && matrix.arch == 'aarch64' + uses: actions-rust-lang/setup-rust-toolchain@9d7e65c320fdb52dcd45ffaa68deb6c02c8754d9 # 1.12.0 + - name: Build wheels + uses: PyO3/maturin-action@04ac600d27cdf7a9a280dadf7147097c42b757ad # 1.50.1 + with: + args: --release --out dist -m ua-parser-rs/Cargo.toml -i python ${{ matrix.args }} + sccache: 'true' + manylinux: ${{ matrix.manylinux }} + - name: Upload wheels + uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # 7.0.0 + with: + name: ua-parser-rs-${{ matrix.platform }}-${{ matrix.arch }}-${{ matrix.python-version }} + path: dist/* + retention-days: 1 + compression-level: 0 + + py-release-sdist: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # 6.0.1 + with: + persist-credentials: false + - name: Build sdist + uses: PyO3/maturin-action@04ac600d27cdf7a9a280dadf7147097c42b757ad # 1.50.1 + with: + command: sdist + args: --out dist -m ua-parser-rs/Cargo.toml + - name: Upload sdist + uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # 7.0.0 + with: + name: wheels-sdist + path: dist + + py-release-tests: + needs: py-release-wheels + + strategy: + matrix: + python-version: + - "3.10" + - "3.11" + - "3.12" + - "3.13" + - "3.14" + - "3.14t" + - "pypy-3.11" + - "graalpy-25" + platform: + - linux + # probably requires a custom image of some sort + # - musllinux + - windows + - macos + arch: + - x86_64 + - aarch64 + + exclude: + - platform: windows + python-version: 3.10 + arch: aarch64 + - platform: windows + arch: aarch64 + python-version: pypy-3.11 + - platform: windows + python-version: graalpy-25 + + include: + - wheel: "3.x" + - python-version: "3.14t" + wheel: "3.14t" + - python-version: "pypy-3.11" + wheel: "pypy-3.11" + - python-version: "graalpy-25" + wheel: "graalpy-25" + + - runner: ubuntu-latest + - arch: aarch64 + runner: ubuntu-24.04-arm + - platform: windows + runner: windows-latest + - platform: windows + arch: aarch64 + runner: windows-11-arm + - platform: macos + runner: macos-latest + - platform: macos + arch: x86_64 + runner: macos-15-intel + - opts: "" + - python-version: graalpy-25 + opts: "--experimental-options --engine.CompileOnly='~tregex re'" + + runs-on: ${{ matrix.runner }} + + steps: + - name: Checkout working copy + uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # 6.0.1 + with: + submodules: true + persist-credentials: false + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # 6.1.0 + with: + python-version: ${{ matrix.python-version }} + allow-prereleases: true + - name: Retrieve wheel + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # 8.0.1 + with: + name: ua-parser-rs-${{ matrix.platform }}-${{ matrix.arch }}-${{ matrix.wheel }} + path: dist + - name: Update pip + run: python -mpip install --upgrade pip + - name: Maybe install libyaml-dev + if: startsWith(matrix.runs, 'ubuntu-latest') + run: | + # if binary wheels are not available for the current + # package install libyaml-dev so we can install pyyaml + # from source + if ! pip download --only-binary :all: pyyaml > /dev/null 2>&1; then + sudo apt install libyaml-dev + fi + - name: Install test dependencies + run: python -mpip install pytest pyyaml pytest-error-for-skips + - name: Install wheel + run: python -mpip install --only-binary ':all:' --no-index --find-links dist ua_parser_rs + - name: Install package + run: python -mpip install . + - name: Run tests + run: python ${{ matrix.opts }} -m pytest -v -Werror -Wignore::ImportWarning --doctest-glob="*.rst" -ra --error-for-skips -k '(-regex)' + + py-release: + name: Release + runs-on: ubuntu-latest + needs: [py-release-tests, py-release-sdist] + if: github.event.name == 'workflow_dispatch' + permissions: + id-token: write + environment: release + steps: + - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # 8.0.1 + with: + path: dist/ + merge-multiple: true # dump every wheel file in the same directory + - name: Publish to PyPI + if: (inputs.release) + uses: pypa/gh-action-pypi-publish@ed0c53931b1dc9bd32cbe73a98c7f6766f8a527e # 1.13.0 + with: + verbose: true + - name: Publish to TestPyPI + if: (!inputs.release) + uses: pypa/gh-action-pypi-publish@ed0c53931b1dc9bd32cbe73a98c7f6766f8a527e # 1.13.0 + with: + repository-url: https://test.pypi.org/legacy/ + skip-existing: true + verbose: true diff --git a/.github/workflows/rs-checks.yml b/.github/workflows/rs-checks.yml new file mode 100644 index 0000000..a3d6d10 --- /dev/null +++ b/.github/workflows/rs-checks.yml @@ -0,0 +1,26 @@ +name: rs checks + +on: + pull_request: + push: + branches: + +permissions: {} + +jobs: + rs-checks: + runs-on: ubuntu-latest + steps: + - name: Checkout working copy + uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # 6.0.1 + with: + submodules: true + fetch-depth: 0 + persist-credentials: false + - name: cargo check + run: cargo check --manifest-path ua-parser-rs/Cargo.toml + - name: cargo clippy # don't run clippy if check fails + run: cargo clippy --manifest-path ua-parser-rs/Cargo.toml + - name: cargo fmt + if: always() + run: cargo fmt --check --manifest-path ua-parser-rs/Cargo.toml diff --git a/.github/workflows/zizmor.yml b/.github/workflows/zizmor.yml index f94fa7b..a4932fc 100644 --- a/.github/workflows/zizmor.yml +++ b/.github/workflows/zizmor.yml @@ -2,6 +2,7 @@ name: Zizmor on: push: + branches: pull_request: permissions: {} diff --git a/.gitignore b/.gitignore index 5b31997..7603c08 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,6 @@ tmp/ regexes.yaml _regexes.py doc/_build +uv.lock +ua-parser-rs/Cargo.lock +ua-parser-rs/target diff --git a/tests/test_core.py b/tests/test_core.py index 1a87702..b1efb9d 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -80,6 +80,8 @@ CORE_DIR / "tests" / "test_ua.yaml", CORE_DIR / "test_resources" / "firefox_user_agent_strings.yaml", CORE_DIR / "test_resources" / "pgts_browser_list.yaml", + CORE_DIR / "test_resources" / "opera_mini_user_agent_strings.yaml", + CORE_DIR / "test_resources" / "podcasting_user_agent_strings.yaml", ], ids=attrgetter("stem"), ) diff --git a/tox.ini b/tox.ini index 63cddcd..37621d9 100644 --- a/tox.ini +++ b/tox.ini @@ -13,10 +13,11 @@ package = wheel wheel_build_env = .pkg # for extra deps # extras = +allowlist_externals = cargo deps = pytest pyyaml - ua-parser-rs + ./ua-parser-rs ./ua-parser-builtins commands = pytest -Werror --doctest-glob="*.rst" {posargs} @@ -27,21 +28,25 @@ deps = pytest pyyaml google-re2 - ua-parser-rs + ./ua-parser-rs ./ua-parser-builtins [testenv:check] labels = check package = skip deps = ruff -commands = ruff check {posargs} +commands = + cargo clippy --manifest-path ua-parser-rs/Cargo.toml {posargs} + ruff check {posargs} [testenv:format] description = Runs the formatter (just showing errors by default) labels = check package = skip deps = ruff -commands = ruff format {posargs:--diff} +commands = + cargo fmt --manifest-path ua-parser-rs/Cargo.toml {posargs:--check} + ruff format {posargs:--diff} [testenv:typecheck] labels = check @@ -49,8 +54,11 @@ package = skip deps = mypy types-PyYaml + ./ua-parser-rs ./ua-parser-builtins -commands = mypy {posargs} +commands = + cargo check --manifest-path ua-parser-rs/Cargo.toml + mypy {posargs} [testenv:docs] description = Builds the documentation diff --git a/ua-parser-rs/Cargo.toml b/ua-parser-rs/Cargo.toml new file mode 100644 index 0000000..1715929 --- /dev/null +++ b/ua-parser-rs/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "ua-parser-rs" +version = "0.1.4" +edition = "2024" +license = "Apache 2.0" +description = "A native accelerator for uap-python" +repository = "https://github.com/ua-parser/uap-rust/" +homepage = "https://github.com/ua-parser/uap-rust/blob/main/ua-parser/" +authors = ["masklinn "] + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[lib] +name = "ua_parser_rs" +crate-type = ["cdylib"] + +[dependencies] +pyo3 = { version = "0.27", features = ["extension-module", "abi3", "abi3-py310"] } +ua-parser = "0.2.2" diff --git a/ua-parser-rs/LICENSE b/ua-parser-rs/LICENSE new file mode 100644 index 0000000..261eeb9 --- /dev/null +++ b/ua-parser-rs/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/ua-parser-rs/README.md b/ua-parser-rs/README.md new file mode 100644 index 0000000..daa1aea --- /dev/null +++ b/ua-parser-rs/README.md @@ -0,0 +1,14 @@ +a ua-parser accelerator +======================= + +This package is (currently) not intended to be used directly, instead +it is one of the native accelerators for [ua-parser][1]. + +The API is very simplistic and should be pretty stable (if only +because having to update [ua-parser][1] all the time is undesirable), +but there is no formal guarantee that it'll keep, as the goal is +really nothing more than a very basic export of [uap-rust][2] to +Python. + +[1]: https://pypi.org/project/ua-parser/ +[2]: https://crates.io/crates/ua-parser diff --git a/ua-parser-rs/pyproject.toml b/ua-parser-rs/pyproject.toml new file mode 100644 index 0000000..555cbec --- /dev/null +++ b/ua-parser-rs/pyproject.toml @@ -0,0 +1,24 @@ +[build-system] +requires = ["maturin>=1.5,<2.0"] +build-backend = "maturin" + +[project] +name = "ua-parser-rs" +description = "native accelerator for ua-parser" +readme = "README.md" +requires-python = ">=3.10" +classifiers = [ + "Programming Language :: Rust", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.14", + "Programming Language :: Python :: Implementation :: CPython", + "Programming Language :: Python :: Implementation :: PyPy", + "Programming Language :: Python :: Implementation :: GraalPy", + "License :: OSI Approved :: Apache Software License", +] +dynamic = ["version", "license", "urls"] +[tool.maturin] +features = ["pyo3/extension-module"] diff --git a/ua-parser-rs/src/lib.rs b/ua-parser-rs/src/lib.rs new file mode 100644 index 0000000..a3278dd --- /dev/null +++ b/ua-parser-rs/src/lib.rs @@ -0,0 +1,202 @@ +/// An uap-python Resolver is a callable which returns a PartialResult +/// (~a triplet of optional user_agent, os, and domain). A resolver +/// has lists of matchers for user agents, os, and devices taken in as +/// a Matchers (a 3-uple of lists of matchers). +/// +/// A matcher is a callable with a `pattern` and a `flags` properties. +/// But matchers also have additional properties for the replacement +/// information. +/// +/// In uap-rs, that's the ua_parser::Extractor doing that, kinda, but +/// it takes a struct from regexes.yaml and parses them in, so +/// probably better leaving that to the Python side and instead +/// exposing individual extractors with convenient interfaces. +/// +/// An Extractor is built off of a series of Parsers, which we could +/// get directly from the python side matchers *if those kept the +/// string as `regex` -> swap pattern and regex in the matcher (/ +/// rename pattern), and maybe expose regex_flag? +/// +/// May not matter as much because pyo3 natively can't piggyback +/// FromPyObject onto Deserialize, this requires the pythonize crate +/// and that seems a bit much for a measly 3 structs... Still, would +/// probably be a good idea for uap-python's matchers to retain the +/// structure of regexes.yaml parsers. Would have been nice to rename +/// them to Parsers as well but that's still very confusing given the +/// global Parser object, unless *that* gets renamed to Extractor on +/// the python side, or something. +use pyo3::prelude::*; +use pyo3::{exceptions::PyValueError, types::PyString}; +use std::borrow::Cow::Owned; + +type UAParser = ( + String, + Option, + Option, + Option, + Option, + Option, +); +#[pyclass(frozen)] +struct UserAgentExtractor(ua_parser::user_agent::Extractor<'static>); +#[pyclass(frozen)] +struct UserAgent { + #[pyo3(get)] + family: Py, + #[pyo3(get)] + major: Option>, + #[pyo3(get)] + minor: Option>, + #[pyo3(get)] + patch: Option>, + #[pyo3(get)] + patch_minor: Option>, +} +#[pymethods] +impl UserAgentExtractor { + #[new] + fn new(it: &Bound) -> PyResult { + use ua_parser::user_agent::{Builder, Parser}; + it.try_iter()? + .try_fold(Builder::new(), |s, p| { + let p: UAParser = p?.extract()?; + s.push(Parser { + regex: Owned(p.0), + family_replacement: p.1.map(Owned), + v1_replacement: p.2.map(Owned), + v2_replacement: p.3.map(Owned), + v3_replacement: p.4.map(Owned), + v4_replacement: p.5.map(Owned), + }) + .map_err(|e| PyValueError::new_err(e.to_string())) + })? + .build() + .map_err(|e| PyValueError::new_err(e.to_string())) + .map(Self) + } + fn extract(&self, py: Python<'_>, s: &str) -> PyResult> { + Ok(self.0.extract(s).map(|v| UserAgent { + family: PyString::new(py, &v.family).unbind(), + major: v.major.map(|s| PyString::new(py, s).unbind()), + minor: v.minor.map(|s| PyString::new(py, s).unbind()), + patch: v.patch.map(|s| PyString::new(py, s).unbind()), + patch_minor: v.patch_minor.map(|s| PyString::new(py, s).unbind()), + })) + } +} + +type OSParser = ( + String, + Option, + Option, + Option, + Option, + Option, +); +#[pyclass(frozen)] +struct OSExtractor(ua_parser::os::Extractor<'static>); +#[pyclass(frozen)] +struct OS { + #[pyo3(get)] + family: Py, + #[pyo3(get)] + major: Option>, + #[pyo3(get)] + minor: Option>, + #[pyo3(get)] + patch: Option>, + #[pyo3(get)] + patch_minor: Option>, +} +#[pymethods] +impl OSExtractor { + #[new] + fn new(it: &Bound) -> PyResult { + use ua_parser::os::{Builder, Parser}; + it.try_iter()? + .try_fold(Builder::new(), |s, p| { + let p: OSParser = p?.extract()?; + s.push(Parser { + regex: Owned(p.0), + os_replacement: p.1.map(Owned), + os_v1_replacement: p.2.map(Owned), + os_v2_replacement: p.3.map(Owned), + os_v3_replacement: p.4.map(Owned), + os_v4_replacement: p.5.map(Owned), + }) + .map_err(|e| PyValueError::new_err(e.to_string())) + })? + .build() + .map_err(|e| PyValueError::new_err(e.to_string())) + .map(Self) + } + fn extract(&self, py: Python<'_>, s: &str) -> PyResult> { + Ok(self.0.extract(s).map(|v| OS { + family: PyString::new(py, &v.os).unbind(), + major: v.major.map(|s| PyString::new(py, &s).unbind()), + minor: v.minor.map(|s| PyString::new(py, &s).unbind()), + patch: v.patch.map(|s| PyString::new(py, &s).unbind()), + patch_minor: v.patch_minor.map(|s| PyString::new(py, &s).unbind()), + })) + } +} + +type DeviceParser = ( + String, + Option, + Option, + Option, + Option, +); +#[pyclass(frozen)] +struct DeviceExtractor(ua_parser::device::Extractor<'static>); +#[pyclass(frozen)] +struct Device { + #[pyo3(get)] + family: Py, + #[pyo3(get)] + brand: Option>, + #[pyo3(get)] + model: Option>, +} +#[pymethods] +impl DeviceExtractor { + #[new] + fn new(it: &Bound) -> PyResult { + use ua_parser::device::{Builder, Flag, Parser}; + it.try_iter()? + .try_fold(Builder::new(), |s, p| { + let p: DeviceParser = p?.extract()?; + s.push(Parser { + regex: Owned(p.0), + regex_flag: if p.1.as_deref() == Some("i") { + Some(Flag::IgnoreCase) + } else { + None + }, + device_replacement: p.2.map(Owned), + brand_replacement: p.3.map(Owned), + model_replacement: p.4.map(Owned), + }) + .map_err(|e| PyValueError::new_err(e.to_string())) + })? + .build() + .map_err(|e| PyValueError::new_err(e.to_string())) + .map(Self) + } + fn extract(&self, py: Python<'_>, s: &str) -> PyResult> { + Ok(self.0.extract(s).map(|v| Device { + family: PyString::new(py, &v.device).unbind(), + brand: v.brand.map(|s| PyString::new(py, &s).unbind()), + model: v.model.map(|s| PyString::new(py, &s).unbind()), + })) + } +} + +#[pymodule(gil_used = false)] +fn ua_parser_rs(m: &Bound) -> PyResult<()> { + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + Ok(()) +} diff --git a/ua-parser-rs/ua_parser_rs.pyi b/ua-parser-rs/ua_parser_rs.pyi new file mode 100644 index 0000000..5b39f02 --- /dev/null +++ b/ua-parser-rs/ua_parser_rs.pyi @@ -0,0 +1,59 @@ +from collections.abc import Iterable +from typing import Literal, Protocol + +UAParser = tuple[ + str, + str | None, + str | None, + str | None, + str | None, + str | None, +] + +class UserAgent(Protocol): + family: str + major: str | None + minor: str | None + patch: str | None + patch_minor: str | None + +class UserAgentExtractor: + def __init__(self, it: Iterable[UAParser], /) -> None: ... + def extract(self, s: str, /) -> UserAgent | None: ... + +OSParser = tuple[ + str, + str | None, + str | None, + str | None, + str | None, + str | None, +] + +class OS(Protocol): + family: str + major: str | None + minor: str | None + patch: str | None + patch_minor: str | None + +class OSExtractor: + def __init__(self, it: Iterable[OSParser], /) -> None: ... + def extract(self, s: str, /) -> OS | None: ... + +DeviceParser = tuple[ + str, + Literal["i"] | None, + str | None, + str | None, + str | None, +] + +class Device(Protocol): + family: str + brand: str | None + model: str | None + +class DeviceExtractor: + def __init__(self, it: Iterable[DeviceParser], /) -> None: ... + def extract(self, s: str, /) -> Device | None: ...