Skip to content

Commit

Permalink
pkg: split ebuild keyword support into a separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
radhermit committed Jul 4, 2024
1 parent 3f2ee13 commit 74c2bf1
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 109 deletions.
2 changes: 2 additions & 0 deletions src/pkgcraft/pkg/ebuild/__init__.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .base cimport *
from .keyword cimport *
2 changes: 2 additions & 0 deletions src/pkgcraft/pkg/ebuild/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .base import *
from .keyword import *
21 changes: 5 additions & 16 deletions src/pkgcraft/pkg/ebuild.pxd → src/pkgcraft/pkg/ebuild/base.pxd
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from .. cimport C
from ..dep cimport DependencySet
from ..error cimport Indirect
from ..types cimport OrderedFrozenSet
from . cimport Pkg
from ... cimport C
from ...dep cimport DependencySet
from ...error cimport Indirect
from ...types cimport OrderedFrozenSet
from .. cimport Pkg


cdef class EbuildPkg(Pkg):
Expand Down Expand Up @@ -30,17 +30,6 @@ cdef class EbuildPkg(Pkg):
cdef object _upstream


cdef class Keyword:
cdef C.Keyword *ptr
cdef readonly str arch
cdef readonly object status
# cached fields
cdef int _hash

@staticmethod
cdef Keyword from_ptr(C.Keyword *, Keyword inst=*)


cdef class Maintainer(Indirect):
cdef readonly str email
cdef readonly str name
Expand Down
101 changes: 8 additions & 93 deletions src/pkgcraft/pkg/ebuild.pyx → src/pkgcraft/pkg/ebuild/base.pyx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from enum import IntEnum
from pathlib import Path

cimport cython

from .. cimport C
from .._misc cimport SENTINEL, CStringArray, cstring_iter, cstring_to_str
from ..dep cimport DependencySet, MutableDependencySet
from ..error cimport Indirect
from ..types cimport OrderedFrozenSet
from . cimport Pkg
from ... cimport C
from ..._misc cimport SENTINEL, CStringArray, cstring_iter, cstring_to_str
from ...dep cimport DependencySet, MutableDependencySet
from ...error cimport Indirect
from ...types cimport OrderedFrozenSet
from .. cimport Pkg
from . cimport Keyword

from ..error import PkgcraftError
from ...error import PkgcraftError


cdef class EbuildPkg(Pkg):
Expand Down Expand Up @@ -240,91 +240,6 @@ cdef class EbuildPkg(Pkg):
return self._upstream


class KeywordStatus(IntEnum):
Disabled = C.KEYWORD_STATUS_DISABLED
Unstable = C.KEYWORD_STATUS_UNSTABLE
Stable = C.KEYWORD_STATUS_STABLE


@cython.final
cdef class Keyword:
"""Ebuild package keyword."""

def __init__(self, s: str):
"""Create a new package keyword.
Args:
s: the string to parse
Returns:
Keyword: the created package keyword instance
Raises:
PkgcraftError: on parsing failure
"""
ptr = C.pkgcraft_keyword_new(s.encode())
if ptr is NULL:
raise PkgcraftError

Keyword.from_ptr(ptr, self)

@staticmethod
cdef Keyword from_ptr(C.Keyword *ptr, Keyword inst = None):
"""Create a Keyword from a pointer."""
if inst is None:
inst = <Keyword>Keyword.__new__(Keyword)
inst.status = KeywordStatus(ptr.status)
inst.arch = ptr.arch.decode()
inst.ptr = ptr
return inst

def __lt__(self, other):
if isinstance(other, Keyword):
return C.pkgcraft_keyword_cmp(self.ptr, (<Keyword>other).ptr) == -1
return NotImplemented

def __le__(self, other):
if isinstance(other, Keyword):
return C.pkgcraft_keyword_cmp(self.ptr, (<Keyword>other).ptr) <= 0
return NotImplemented

def __eq__(self, other):
if isinstance(other, Keyword):
return C.pkgcraft_keyword_cmp(self.ptr, (<Keyword>other).ptr) == 0
return NotImplemented

def __ne__(self, other):
if isinstance(other, Keyword):
return C.pkgcraft_keyword_cmp(self.ptr, (<Keyword>other).ptr) != 0
return NotImplemented

def __ge__(self, other):
if isinstance(other, Keyword):
return C.pkgcraft_keyword_cmp(self.ptr, (<Keyword>other).ptr) >= 0
return NotImplemented

def __gt__(self, other):
if isinstance(other, Keyword):
return C.pkgcraft_keyword_cmp(self.ptr, (<Keyword>other).ptr) == 1
return NotImplemented

def __hash__(self):
if not self._hash:
self._hash = C.pkgcraft_keyword_hash(self.ptr)
return self._hash

def __str__(self):
return cstring_to_str(C.pkgcraft_keyword_str(self.ptr))

def __repr__(self):
addr = <size_t>&self.ptr
name = self.__class__.__name__
return f"<{name} '{self}' at 0x{addr:0x}>"

def __dealloc__(self):
C.pkgcraft_keyword_free(self.ptr)


@cython.final
cdef class Maintainer(Indirect):
"""Ebuild package maintainer."""
Expand Down
12 changes: 12 additions & 0 deletions src/pkgcraft/pkg/ebuild/keyword.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from ... cimport C


cdef class Keyword:
cdef C.Keyword *ptr
cdef readonly str arch
cdef readonly object status
# cached fields
cdef int _hash

@staticmethod
cdef Keyword from_ptr(C.Keyword *, Keyword inst=*)
93 changes: 93 additions & 0 deletions src/pkgcraft/pkg/ebuild/keyword.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from enum import IntEnum

cimport cython

from ... cimport C
from ..._misc cimport cstring_to_str

from ...error import PkgcraftError


class KeywordStatus(IntEnum):
Disabled = C.KEYWORD_STATUS_DISABLED
Unstable = C.KEYWORD_STATUS_UNSTABLE
Stable = C.KEYWORD_STATUS_STABLE


@cython.final
cdef class Keyword:
"""Ebuild package keyword."""

def __init__(self, s: str):
"""Create a new package keyword.
Args:
s: the string to parse
Returns:
Keyword: the created package keyword instance
Raises:
PkgcraftError: on parsing failure
"""
ptr = C.pkgcraft_keyword_new(s.encode())
if ptr is NULL:
raise PkgcraftError

Keyword.from_ptr(ptr, self)

@staticmethod
cdef Keyword from_ptr(C.Keyword *ptr, Keyword inst = None):
"""Create a Keyword from a pointer."""
if inst is None:
inst = <Keyword>Keyword.__new__(Keyword)
inst.status = KeywordStatus(ptr.status)
inst.arch = ptr.arch.decode()
inst.ptr = ptr
return inst

def __lt__(self, other):
if isinstance(other, Keyword):
return C.pkgcraft_keyword_cmp(self.ptr, (<Keyword>other).ptr) == -1
return NotImplemented

def __le__(self, other):
if isinstance(other, Keyword):
return C.pkgcraft_keyword_cmp(self.ptr, (<Keyword>other).ptr) <= 0
return NotImplemented

def __eq__(self, other):
if isinstance(other, Keyword):
return C.pkgcraft_keyword_cmp(self.ptr, (<Keyword>other).ptr) == 0
return NotImplemented

def __ne__(self, other):
if isinstance(other, Keyword):
return C.pkgcraft_keyword_cmp(self.ptr, (<Keyword>other).ptr) != 0
return NotImplemented

def __ge__(self, other):
if isinstance(other, Keyword):
return C.pkgcraft_keyword_cmp(self.ptr, (<Keyword>other).ptr) >= 0
return NotImplemented

def __gt__(self, other):
if isinstance(other, Keyword):
return C.pkgcraft_keyword_cmp(self.ptr, (<Keyword>other).ptr) == 1
return NotImplemented

def __hash__(self):
if not self._hash:
self._hash = C.pkgcraft_keyword_hash(self.ptr)
return self._hash

def __str__(self):
return cstring_to_str(C.pkgcraft_keyword_str(self.ptr))

def __repr__(self):
addr = <size_t>&self.ptr
name = self.__class__.__name__
return f"<{name} '{self}' at 0x{addr:0x}>"

def __dealloc__(self):
C.pkgcraft_keyword_free(self.ptr)

0 comments on commit 74c2bf1

Please sign in to comment.