Skip to content

Commit 0027f21

Browse files
committed
atom: export version operator for Atom and Version objects
1 parent 99c437a commit 0027f21

5 files changed

Lines changed: 115 additions & 20 deletions

File tree

src/pkgcraft/atom/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
from .base import Atom, Blocker, Cpv, SlotOperator
2-
from .version import Version, VersionWithOp
2+
from .version import Operator, Version, VersionWithOp

src/pkgcraft/atom/base.pyx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,25 @@ cdef class Atom(Cpv):
325325
return Blocker(blocker)
326326
return None
327327

328+
@property
329+
def op(self):
330+
"""Get an atom's operator.
331+
332+
>>> from pkgcraft.atom import Atom, Operator
333+
>>> a = Atom('cat/pkg')
334+
>>> a.op is None
335+
True
336+
>>> a = Atom('>=cat/pkg-1')
337+
>>> a.op is Operator.GreaterOrEqual
338+
True
339+
>>> a.op == '>='
340+
True
341+
"""
342+
version = self.version
343+
if version is not None:
344+
return version.op
345+
return version
346+
328347
@property
329348
def slot(self):
330349
"""Get an atom's slot.

src/pkgcraft/atom/version.pxd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ from pkgcraft cimport pkgcraft_c as C
22

33

44
cdef class Version:
5-
cdef C.AtomVersion *ptr
5+
cdef C.Version *ptr
66
# cached fields
77
cdef int _hash
88

99
@staticmethod
10-
cdef Version from_ptr(C.AtomVersion *)
10+
cdef Version from_ptr(C.Version *)
1111

1212

1313
cdef class VersionWithOp(Version):

src/pkgcraft/atom/version.pyx

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,37 @@
1+
from enum import IntEnum
2+
13
from .. cimport pkgcraft_c as C
24
from ..error import InvalidVersion
35

46

7+
class Operator(IntEnum):
8+
Less = C.OPERATOR_LESS
9+
LessOrEqual = C.OPERATOR_LESS_OR_EQUAL
10+
Equal = C.OPERATOR_EQUAL
11+
EqualGlob = C.OPERATOR_EQUAL_GLOB
12+
Approximate = C.OPERATOR_APPROXIMATE
13+
GreaterOrEqual = C.OPERATOR_GREATER_OR_EQUAL
14+
Greater = C.OPERATOR_GREATER
15+
16+
@staticmethod
17+
def from_str(str s not None):
18+
op = C.pkgcraft_version_op_from_str(s.encode())
19+
if op > 0:
20+
return Operator(op)
21+
raise ValueError(f'invalid operator: {s}')
22+
23+
def __str__(self):
24+
c_str = C.pkgcraft_version_op_str(self)
25+
s = c_str.decode()
26+
C.pkgcraft_str_free(c_str)
27+
return s
28+
29+
def __eq__(self, object other):
30+
if isinstance(other, str):
31+
return str(self) == other
32+
return self is other
33+
34+
535
cdef class Version:
636
"""Atom version.
737
@@ -30,7 +60,7 @@ cdef class Version:
3060
raise InvalidVersion
3161

3262
@staticmethod
33-
cdef Version from_ptr(C.AtomVersion *ptr):
63+
cdef Version from_ptr(C.Version *ptr):
3464
"""Convert a Version pointer to a Version object."""
3565
if ptr is not NULL:
3666
obj = <Version>Version.__new__(Version)
@@ -55,6 +85,25 @@ cdef class Version:
5585
C.pkgcraft_str_free(c_str)
5686
return s
5787

88+
@property
89+
def op(self):
90+
"""Get a version's operator.
91+
92+
>>> from pkgcraft.atom import Operator, Version, VersionWithOp
93+
>>> v = Version('1-r2')
94+
>>> v.op is None
95+
True
96+
>>> v = VersionWithOp('>=1')
97+
>>> v.op is Operator.GreaterOrEqual
98+
True
99+
>>> v.op == '>='
100+
True
101+
"""
102+
cdef int op = C.pkgcraft_version_op(self.ptr)
103+
if op > 0:
104+
return Operator(op)
105+
return None
106+
58107
def intersects(self, Version other):
59108
"""Determine if two versions intersect.
60109

src/pkgcraft/pkgcraft_c.pxd

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ cdef extern from "pkgcraft.h":
2525
LOG_LEVEL_WARN,
2626
LOG_LEVEL_ERROR,
2727

28+
cdef enum Operator:
29+
OPERATOR_LESS # = 1,
30+
OPERATOR_LESS_OR_EQUAL,
31+
OPERATOR_EQUAL,
32+
OPERATOR_EQUAL_GLOB,
33+
OPERATOR_APPROXIMATE,
34+
OPERATOR_GREATER_OR_EQUAL,
35+
OPERATOR_GREATER,
36+
2837
cdef enum PkgFormat:
2938
PKG_FORMAT_EBUILD,
3039
PKG_FORMAT_FAKE,
@@ -54,10 +63,6 @@ cdef extern from "pkgcraft.h":
5463
cdef struct Atom:
5564
pass
5665

57-
# Opaque wrapper for AtomVersion objects.
58-
cdef struct AtomVersion:
59-
pass
60-
6166
# System config
6267
cdef struct Config:
6368
pass
@@ -110,6 +115,9 @@ cdef extern from "pkgcraft.h":
110115
cdef struct Uri:
111116
pass
112117

118+
cdef struct Version:
119+
pass
120+
113121
cdef struct PkgcraftError:
114122
char *message
115123
ErrorKind kind
@@ -295,7 +303,7 @@ cdef extern from "pkgcraft.h":
295303
#
296304
# # Safety
297305
# The argument must be a non-null Atom pointer.
298-
AtomVersion *pkgcraft_atom_version(Atom *atom)
306+
Version *pkgcraft_atom_version(Atom *atom)
299307

300308
# Add an external Repo to the config.
301309
#
@@ -850,7 +858,7 @@ cdef extern from "pkgcraft.h":
850858
#
851859
# # Safety
852860
# The argument must be a non-null Pkg pointer.
853-
AtomVersion *pkgcraft_pkg_version(Pkg *p)
861+
Version *pkgcraft_pkg_version(Pkg *p)
854862

855863
# Return a repo's categories.
856864
#
@@ -1237,57 +1245,76 @@ cdef extern from "pkgcraft.h":
12371245
#
12381246
# # Safety
12391247
# The version arguments should be non-null Version pointers.
1240-
int pkgcraft_version_cmp(AtomVersion *v1,
1241-
AtomVersion *v2)
1248+
int pkgcraft_version_cmp(Version *v1,
1249+
Version *v2)
12421250

12431251
# Free a version.
12441252
#
12451253
# # Safety
12461254
# The version argument should be a non-null Version pointer.
1247-
void pkgcraft_version_free(AtomVersion *v)
1255+
void pkgcraft_version_free(Version *v)
12481256

12491257
# Return the hash value for a version.
12501258
#
12511259
# # Safety
12521260
# The version argument should be a non-null Version pointer.
1253-
uint64_t pkgcraft_version_hash(AtomVersion *v)
1261+
uint64_t pkgcraft_version_hash(Version *v)
12541262

12551263
# Determine if two versions intersect.
12561264
#
12571265
# # Safety
12581266
# The version arguments should be non-null Version pointers.
1259-
bool pkgcraft_version_intersects(AtomVersion *v1, AtomVersion *v2)
1267+
bool pkgcraft_version_intersects(Version *v1, Version *v2)
12601268

12611269
# Parse a string into a version.
12621270
#
12631271
# Returns NULL on error.
12641272
#
12651273
# # Safety
12661274
# The version argument should point to a valid string.
1267-
AtomVersion *pkgcraft_version_new(const char *s)
1275+
Version *pkgcraft_version_new(const char *s)
1276+
1277+
# Return a version's operator.
1278+
#
1279+
# Returns 0 on nonexistence.
1280+
#
1281+
# # Safety
1282+
# The argument must be a non-null Version pointer.
1283+
int pkgcraft_version_op(Version *v)
1284+
1285+
# Parse a string into an Operator.
1286+
#
1287+
# Returns 0 on error.
1288+
#
1289+
# # Safety
1290+
# The argument should be a UTF-8 string.
1291+
int pkgcraft_version_op_from_str(const char *s)
1292+
1293+
# Return the string for an Operator.
1294+
char *pkgcraft_version_op_str(Operator op)
12681295

12691296
# Return a version's revision, e.g. the version "1-r2" has a revision of "2".
12701297
#
12711298
# # Safety
12721299
# The version argument should be a non-null Version pointer.
1273-
char *pkgcraft_version_revision(AtomVersion *v)
1300+
char *pkgcraft_version_revision(Version *v)
12741301

12751302
# Return a version's string value without operator.
12761303
#
12771304
# # Safety
12781305
# The version argument should be a non-null Version pointer.
1279-
char *pkgcraft_version_str(AtomVersion *v)
1306+
char *pkgcraft_version_str(Version *v)
12801307

12811308
# Return a version's string value including the operator if it exists.
12821309
#
12831310
# # Safety
12841311
# The version argument should be a non-null Version pointer.
1285-
char *pkgcraft_version_str_with_op(AtomVersion *v)
1312+
char *pkgcraft_version_str_with_op(Version *v)
12861313

12871314
# Parse a string into a version with an operator.
12881315
#
12891316
# Returns NULL on error.
12901317
#
12911318
# # Safety
12921319
# The version argument should point to a valid string.
1293-
AtomVersion *pkgcraft_version_with_op(const char *s)
1320+
Version *pkgcraft_version_with_op(const char *s)

0 commit comments

Comments
 (0)