Skip to content

Commit

Permalink
dep: add Cpv.with_op() support
Browse files Browse the repository at this point in the history
  • Loading branch information
radhermit committed Dec 17, 2023
1 parent 3e58a42 commit 9235fe4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/pkgcraft/C.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,14 @@ cdef extern from "pkgcraft.h":
# The argument must be a non-null Cpv pointer.
Version *pkgcraft_cpv_version(Cpv *c)

# Create a Dep from a Cpv by applying a version operator.
#
# Returns NULL on error.
#
# # Safety
# The argument must be a non-null Cpv pointer.
Dep *pkgcraft_cpv_with_op(Cpv *c, Operator op)

# Get a package dependency's raw blocker value.
# For example, the package dependency "!cat/pkg" has a weak blocker.
#
Expand Down
45 changes: 44 additions & 1 deletion src/pkgcraft/dep/cpv.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ from ..restrict cimport Restrict
from . cimport Dep
from .version cimport Version

from ..error import InvalidCpv
from ..error import InvalidCpv, PkgcraftError
from .version import Operator


@cython.final
Expand Down Expand Up @@ -71,6 +72,48 @@ cdef class Cpv:
inst.ptr = <C.Cpv *>ptr
return inst

def with_op(self, op not None):
"""Create a Dep from a Cpv by applying a version operator.
Args:
op (str | Operator): the version operator to apply
Returns:
Dep: the newly created Dep
Raises:
PkgcraftError: for invalid operator arguments
>>> from pkgcraft.dep import Cpv, Operator
>>> cpv = Cpv('cat/pkg-1-r2')
String-based operator
>>> str(cpv.with_op('>='))
'>=cat/pkg-1-r2'
Enum-based operator
>>> str(cpv.with_op(Operator.Less))
'<cat/pkg-1-r2'
Invalid operator
>>> cpv.with_op(Operator.Approximate)
Traceback (most recent call last):
...
pkgcraft.error.PkgcraftError: ~ version operator can't be used with a revision
"""
if isinstance(op, str):
op = Operator.from_str(op)
else:
op = Operator(op)

ptr = C.pkgcraft_cpv_with_op(self.ptr, op)
if ptr is NULL:
raise PkgcraftError
return Dep.from_ptr(ptr)

@property
def category(self):
"""Get the category of a Cpv.
Expand Down

0 comments on commit 9235fe4

Please sign in to comment.