Skip to content

Commit

Permalink
Add some metadata to ua-parser-py
Browse files Browse the repository at this point in the history
While it's not really in need of documentation as it's an
implementation detail of uap-python, opening the link to the
dependency from the main package and seeing an empty page is rather
suspicious.

Adding info / metadata would, I hope and assume, assuage fears.

Fixes #13
  • Loading branch information
masklinn committed Nov 3, 2024
1 parent 2cd7c6a commit 3bf7f26
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
8 changes: 6 additions & 2 deletions ua-parser-py/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
[package]
name = "ua-parser-rs"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
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 <uap@masklinn.net>"]

# 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.22", features = ["extension-module", "abi3", "abi3-py38"] }
pyo3 = { version = "0.22", features = ["extension-module", "abi3", "abi3-py39"] }
ua-parser = { version = "0.2.0", path = "../ua-parser" }
14 changes: 14 additions & 0 deletions ua-parser-py/README.md
Original file line number Diff line number Diff line change
@@ -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
24 changes: 12 additions & 12 deletions ua-parser-py/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/// 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::exceptions::PyValueError;
use pyo3::{exceptions::PyValueError, types::PyString};
use pyo3::prelude::*;
use std::borrow::Cow::{self, Owned};

Expand All @@ -42,15 +42,15 @@ struct UserAgentExtractor(ua_parser::user_agent::Extractor<'static>);
#[pyclass(frozen)]
struct UserAgent {
#[pyo3(get)]
family: String,
family: Py<PyString>,
#[pyo3(get)]
major: Option<String>,
major: Option<Py<PyString>>,
#[pyo3(get)]
minor: Option<String>,
minor: Option<Py<PyString>>,
#[pyo3(get)]
patch: Option<String>,
patch: Option<Py<PyString>>,
#[pyo3(get)]
patch_minor: Option<String>,
patch_minor: Option<Py<PyString>>,
}
#[pymethods]
impl UserAgentExtractor {
Expand All @@ -74,13 +74,13 @@ impl UserAgentExtractor {
.map_err(|e| PyValueError::new_err(e.to_string()))
.map(Self)
}
fn extract(&self, s: &str) -> PyResult<Option<UserAgent>> {
fn extract(&self, py: Python<'_>, s: &str) -> PyResult<Option<UserAgent>> {
Ok(self.0.extract(s).map(|v| UserAgent {
family: v.family.into_owned(),
major: v.major.map(|s| s.to_string()),
minor: v.minor.map(|s| s.to_string()),
patch: v.patch.map(|s| s.to_string()),
patch_minor: v.patch_minor.map(|s| s.to_string()),
family: PyString::new_bound(py, &v.family).unbind(),
major: v.major.map(|s| PyString::new_bound(py, s).unbind()),
minor: v.minor.map(|s| PyString::new_bound(py, s).unbind()),
patch: v.patch.map(|s| PyString::new_bound(py, s).unbind()),
patch_minor: v.patch_minor.map(|s| PyString::new_bound(py, s).unbind()),
}))
}
}
Expand Down

0 comments on commit 3bf7f26

Please sign in to comment.