-
Notifications
You must be signed in to change notification settings - Fork 164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce the StoreItem base class #1158
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -25,17 +25,52 @@ | |||||
import logging | ||||||
|
||||||
from lxml.etree import _Element | ||||||
from typing import Dict, List, Optional, TypeVar, Generic, Protocol | ||||||
from typing import Dict, List, Optional, TypeVar, Generic | ||||||
from typing_extensions import Self | ||||||
|
||||||
|
||||||
log = logging.getLogger(__name__) | ||||||
|
||||||
S = TypeVar('S',bound='Storeable') | ||||||
S = TypeVar('S',bound='StoreItem') | ||||||
|
||||||
class StoreItem(GObject.Object): | ||||||
"""Base class for items in BaseStore.""" | ||||||
|
||||||
__gtype_name__ = 'gtg_StoreItem' | ||||||
|
||||||
|
||||||
def __init__(self,id: UUID): | ||||||
self.id: UUID = id | ||||||
self.parent: Optional[Self] = None | ||||||
self.children: List[Self] = [] | ||||||
super(StoreItem, self).__init__() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Bad habits form Python 2 still in the code base :) Let's fix them as we see them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||||||
|
||||||
|
||||||
@GObject.Property(type=int) | ||||||
def children_count(self) -> int: | ||||||
"""Read only property.""" | ||||||
return len(self.children) | ||||||
|
||||||
|
||||||
@GObject.Property(type=bool, default=False) | ||||||
def has_children(self) -> bool: | ||||||
return len(self.children) > 0 | ||||||
|
||||||
|
||||||
def get_ancestors(self) -> List[Self]: | ||||||
"""Return all ancestors of this tag""" | ||||||
ancestors: List[Self] = [] | ||||||
here = self | ||||||
while here.parent: | ||||||
here = here.parent | ||||||
ancestors.append(here) | ||||||
return ancestors | ||||||
|
||||||
|
||||||
def check_possible_parent(self, target) -> bool: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
and inverse the conditions below. I think it might be clearer to read There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that the name is not perfect. However, |
||||||
"""Check for parenting an item to its own descendant or to itself.""" | ||||||
return self != target and self not in target.get_ancestors() | ||||||
|
||||||
class Storeable(Protocol[S]): | ||||||
id: UUID | ||||||
parent: Optional[S] | ||||||
children: List[S] | ||||||
|
||||||
|
||||||
class BaseStore(GObject.Object,Generic[S]): | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that Python 3.8 is gone, we can write code that targets 3.9, which means we no longer need
typing.List
and the like.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. (We should also update the readme and every other reference to python version.)