-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expose
*Record.noarch
in Python bindings (#635)
Closes #630 I barely know what I'm doing so please feel free to suggest a different API or even commit directly to my branch :) Thanks!
- Loading branch information
Showing
7 changed files
with
261 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
from __future__ import annotations | ||
from typing import Optional | ||
|
||
from rattler.rattler import PyNoArchType | ||
|
||
|
||
class NoArchType: | ||
_noarch: PyNoArchType | ||
|
||
def __init__(self, noarch: Optional[str] = None) -> None: | ||
if noarch is None: | ||
self._noarch = PyNoArchType.none() | ||
self._source = None | ||
elif noarch == "python": | ||
self._noarch = PyNoArchType.python() | ||
self._source = "python" | ||
elif noarch == "generic": | ||
self._noarch = PyNoArchType.generic() | ||
self._source = "generic" | ||
else: | ||
raise ValueError( | ||
"NoArchType constructor received unsupported value " f"{noarch} for the `noarch` parameter" | ||
) | ||
|
||
@classmethod | ||
def _from_py_no_arch_type(cls, py_no_arch_type: PyNoArchType) -> NoArchType: | ||
"""Construct Rattler NoArchType from FFI PyNoArchType object.""" | ||
no_arch_type = cls.__new__(cls) | ||
no_arch_type._noarch = py_no_arch_type | ||
no_arch_type._source = py_no_arch_type | ||
return no_arch_type | ||
|
||
@property | ||
def generic(self) -> bool: | ||
""" | ||
Return whether this NoArchType is 'generic' | ||
>>> NoArchType('generic').generic | ||
True | ||
>>> NoArchType('generic').python | ||
False | ||
>>> | ||
""" | ||
return self._noarch.is_generic | ||
|
||
@property | ||
def none(self) -> bool: | ||
""" | ||
Return whether this NoArchType is set | ||
>>> NoArchType(None).none | ||
True | ||
>>> NoArchType(None).python | ||
False | ||
>>> | ||
""" | ||
return self._noarch.is_none | ||
|
||
@property | ||
def python(self) -> bool: | ||
""" | ||
Return whether this NoArchType is 'python' | ||
>>> NoArchType('python').python | ||
True | ||
>>> NoArchType('python').generic | ||
False | ||
>>> | ||
""" | ||
return self._noarch.is_python | ||
|
||
def __hash__(self) -> int: | ||
""" | ||
Computes the hash of this instance. | ||
Examples | ||
-------- | ||
```python | ||
>>> hash(NoArchType("python")) == hash(NoArchType("python")) | ||
True | ||
>>> hash(NoArchType("python")) == hash(NoArchType("generic")) | ||
False | ||
>>> | ||
``` | ||
""" | ||
return self._noarch.__hash__() | ||
|
||
def __eq__(self, other: object) -> bool: | ||
""" | ||
Returns True if this instance represents the same NoArchType as `other`. | ||
Examples | ||
-------- | ||
```python | ||
>>> NoArchType("python") == NoArchType("generic") | ||
False | ||
>>> NoArchType("python") == NoArchType("python") | ||
True | ||
>>> NoArchType("generic") == NoArchType("generic") | ||
True | ||
>>> NoArchType("python") == "python" | ||
False | ||
>>> | ||
``` | ||
""" | ||
if not isinstance(other, NoArchType): | ||
return False | ||
|
||
return self._noarch == other._noarch | ||
|
||
def __ne__(self, other: object) -> bool: | ||
""" | ||
Returns True if this instance does not represents the same NoArchType as `other`. | ||
Examples | ||
-------- | ||
```python | ||
>>> NoArchType("python") != NoArchType("python") | ||
False | ||
>>> NoArchType("python") != "python" | ||
True | ||
>>> | ||
``` | ||
""" | ||
if not isinstance(other, NoArchType): | ||
return True | ||
|
||
return self._noarch != other._noarch | ||
|
||
def __repr__(self) -> str: | ||
""" | ||
Returns a representation of the NoArchType. | ||
Examples | ||
-------- | ||
```python | ||
>>> p = NoArchType("python") | ||
>>> p | ||
NoArchType("python") | ||
>>> | ||
``` | ||
""" | ||
return f'NoArchType("{self._source}")' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use std::{ | ||
collections::hash_map::DefaultHasher, | ||
hash::{Hash, Hasher}, | ||
}; | ||
|
||
use pyo3::{basic::CompareOp, pyclass, pymethods}; | ||
use rattler_conda_types::NoArchType; | ||
|
||
#[pyclass] | ||
#[derive(Clone)] | ||
pub struct PyNoArchType { | ||
pub inner: NoArchType, | ||
} | ||
|
||
impl From<NoArchType> for PyNoArchType { | ||
fn from(value: NoArchType) -> Self { | ||
Self { inner: value } | ||
} | ||
} | ||
|
||
impl From<PyNoArchType> for NoArchType { | ||
fn from(value: PyNoArchType) -> Self { | ||
value.inner | ||
} | ||
} | ||
|
||
#[pymethods] | ||
impl PyNoArchType { | ||
/// Constructs a new `NoArchType` of type `python`. | ||
#[staticmethod] | ||
pub fn python() -> Self { | ||
NoArchType::python().into() | ||
} | ||
|
||
#[getter] | ||
pub fn is_python(&self) -> bool { | ||
self.inner.is_python() | ||
} | ||
|
||
/// Constructs a new `NoArchType` of type `generic`. | ||
#[staticmethod] | ||
pub fn generic() -> Self { | ||
NoArchType::generic().into() | ||
} | ||
|
||
#[getter] | ||
pub fn is_generic(&self) -> bool { | ||
self.inner.is_generic() | ||
} | ||
|
||
/// Constructs a new `NoArchType` of type `none`. | ||
#[staticmethod] | ||
pub fn none() -> Self { | ||
NoArchType::none().into() | ||
} | ||
|
||
#[getter] | ||
pub fn is_none(&self) -> bool { | ||
self.inner.is_none() | ||
} | ||
|
||
/// Compute the hash of the noarch type. | ||
fn __hash__(&self) -> u64 { | ||
let mut hasher = DefaultHasher::new(); | ||
self.inner.hash(&mut hasher); | ||
hasher.finish() | ||
} | ||
|
||
/// Performs comparison between this noarch type and another. | ||
pub fn __richcmp__(&self, other: &Self, op: CompareOp) -> bool { | ||
op.matches(self.inner.cmp(&other.inner)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters