Skip to content

Commit

Permalink
Use property() & implement Abstract Base Class PangoObject (#47)
Browse files Browse the repository at this point in the history
* refactor(Layout): use `property()`

* refactor(Context): use `property()`

* feat: create PangoObject class

* fix(PangoObject): defaulting arguments, properties, etc.

* refactor(Rectangle): use `property()` and `PangoObject`

* refactor(FontDescription): use `property()` and `PangoObject`

* fix(init): include PangoObject

* refactor(*): implement PangoObject

* fix(PangoObject): fix repr

* fix(Color): fix constructor

* refactor(PangoObject): use `@property`

* fix(*): fix references to `_init_pointer()` and `pointer()`

* test(*): update tests

* docs(PangoObject): add property Docstrings

* test(Attribute): update

* test(*): fix tests

* fix(PangoObject): fix `__eq__` and `__init__`

* fix(AttrList): remove _EQ_METHOD not available in Pango 1.0.0

* fix(AttrList): overwrite `__eq__()`

* style(*): fix linting issues

* test(Color): fix comparison test

* fix(Color): overwrite `__copy__()`

* docs(PangoObject): link to ABC, add PO to Low Level Functionality

* fix(Color): fix `copy()` method name

* test(Attribute): fix string

* test(Attr*): fix two equality tests

* fix(Color, GII): remove GC to avoid Segfault

* docs(Color): add leifgehrmann's link comment

* test(Color): test `from_pointer()`

* test(Layout): fix attribute tests

* fix(Layout): terrible typo!

* style(PangoObject): lint

* refactor(AttrList): in `__eq__()`, don't check pointers

* refactor(PangoObject, AttrList): check for pointer equality first

* test(PangoObject): increase coverage

* style(PangoObject): lint
  • Loading branch information
cAttte authored Oct 7, 2022
1 parent c4e4910 commit 6ca1c5b
Show file tree
Hide file tree
Showing 29 changed files with 699 additions and 1,186 deletions.
5 changes: 5 additions & 0 deletions docs/modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,8 @@ ________________
.. autofunction:: pangocffi.pango_version

.. autofunction:: pangocffi.pango_version_string

Pango Object
____________

.. autoclass:: PangoObject
1 change: 1 addition & 0 deletions pangocffi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def _dlopen(dl_name: str, generated_ffi, names: List[str]):
from .version import * # noqa
from .enums import * # noqa
from .convert import * # noqa
from .pango_object import PangoObject # noqa
from .font_description import FontDescription # noqa
from .rectangle import Rectangle # noqa
from .item import Item # noqa
Expand Down
73 changes: 19 additions & 54 deletions pangocffi/attr_list.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from . import ffi, pango, Attribute
from . import ffi, pango, Attribute, PangoObject


class AttrList:
class AttrList(PangoObject):
"""
The :class:`AttrList` represents a list of attributes(:class:`Attribute`)
that apply to a section of text. The attributes are, in general, allowed
Expand All @@ -13,34 +13,9 @@ class AttrList:
one paragraph of text due to internal structures.
"""

def __init__(self) -> None:
self._init_pointer(pango.pango_attr_list_new())

def _init_pointer(self, pointer: ffi.CData):
self._pointer = ffi.gc(pointer, pango.pango_attr_list_unref)

def get_pointer(self) -> ffi.CData:
"""
Returns the pointer to the AttrList
:return:
the pointer to the AttrList.
"""
return self._pointer

@classmethod
def from_pointer(cls, pointer: ffi.CData) -> "AttrList":
"""
Instantiates a :class:`AttrList` from a pointer.
:return:
the AttrList.
"""
if pointer == ffi.NULL:
raise ValueError("Null pointer")
self = object.__new__(cls)
self._pointer = pointer
return self
_INIT_METHOD = pango.pango_attr_list_new
_GC_METHOD = pango.pango_attr_list_unref
_COPY_METHOD = pango.pango_attr_list_copy

def _ref(self) -> None:
"""
Expand All @@ -56,18 +31,6 @@ def _unref(self) -> None:
"""
pango.pango_attr_list_unref(self._pointer)

def copy(self) -> "AttrList":
"""
Copy :class:`AttrList` and return an identical new.
"""
return AttrList.from_pointer(pango.pango_attr_list_copy(self._pointer))

def __copy__(self) -> "AttrList":
return self.copy()

def __deepcopy__(self, memo) -> "AttrList":
return self.copy()

def insert(self, attr: Attribute) -> None:
"""
Insert the given attribute into the PangoAttrList. It will be inserted
Expand All @@ -80,7 +43,7 @@ def insert(self, attr: Attribute) -> None:
"""
assert isinstance(attr, Attribute), "attr isn't a Attribute"
self._ref()
pango.pango_attr_list_insert(self._pointer, attr.get_pointer())
pango.pango_attr_list_insert(self._pointer, attr.pointer)

def insert_before(self, attr: Attribute) -> None:
"""
Expand All @@ -94,7 +57,7 @@ def insert_before(self, attr: Attribute) -> None:
"""
assert isinstance(attr, Attribute), "attr isn't a Attribute"
self._ref()
pango.pango_attr_list_insert_before(self._pointer, attr.get_pointer())
pango.pango_attr_list_insert_before(self._pointer, attr.pointer)

def change(self, attr: Attribute) -> None:
"""
Expand All @@ -115,7 +78,7 @@ def change(self, attr: Attribute) -> None:
"""
assert isinstance(attr, Attribute), "attr isn't a Attribute"
self._ref()
pango.pango_attr_list_change(self._pointer, attr.get_pointer())
pango.pango_attr_list_change(self._pointer, attr.pointer)

def splice(self, attr_list: "AttrList", pos: int, length: int):
"""
Expand Down Expand Up @@ -156,14 +119,16 @@ def splice(self, attr_list: "AttrList", pos: int, length: int):
length,
)

def __eq__(self, other: "AttrList") -> bool:
if isinstance(other, AttrList):
return bool(
pango.pango_attr_list_equal(
self.get_pointer(),
other.get_pointer(),
)
)
raise NotImplementedError
# avoid _EQ_METHOD since pango_attr_list_equal is a newer method
def __eq__(self, other) -> bool:
if isinstance(other, PangoObject):
if self.pointer == other.pointer:
return True
else:
return bool(pango.pango_attr_list_equal(
self.pointer, other.pointer
))

return False

# TODO: pango_attr_list_filter ()
Loading

0 comments on commit 6ca1c5b

Please sign in to comment.