Skip to content

Commit

Permalink
[#505] REFACTOR: small prep for merging with selene.Element one day:)
Browse files Browse the repository at this point in the history
  • Loading branch information
yashaka committed Jul 22, 2024
1 parent cdf4efc commit 7787202
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions selene/support/_pom.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def __init__(self, selector: str | Tuple[str, str], _context=None):
# TODO: should we set _name on init too?
# at least to None, if value was not provided...
self._selector = selector
self._instantiated_as_descriptor = False

# todo: should we wrap lambda below into lru_cache?
self._context = lambda instance: (
Expand All @@ -77,6 +78,9 @@ def __init__(self, selector: str | Tuple[str, str], _context=None):

def _as_context(self, instance) -> selene.Element:
return (
# # todo: one day we gonna pass a name to element ;)
# # once selene entities support naming
# context_of_self.element(self._selector, _name=self._name)
context_of_self.element(self._selector)
if isinstance(
context_of_self := self._context(instance),
Expand Down Expand Up @@ -122,7 +126,8 @@ def element(self, selector: str | Tuple[str, str]) -> element:
# might be not accessed yet, thus we have to simulate such assess
# on our own by forcing getattr
)[1]
if hasattr(self, '_name')
# if hasattr(self, '_name')
if self._instantiated_as_descriptor
# => self if a "pass-through"-descriptor)
else self._as_context(instance)
),
Expand All @@ -136,7 +141,7 @@ def all(self, selector: str | Tuple[str, str]) -> all_:
getattr(instance, self._name),
self,
)[1]
if hasattr(self, '_name')
if self._instantiated_as_descriptor
else self._as_context(instance)
),
)
Expand All @@ -145,6 +150,7 @@ def all(self, selector: str | Tuple[str, str]) -> all_:

def __set_name__(self, owner, name):
self._name = name # TODO: use it
self._instantiated_as_descriptor = True

def __get__(self, instance, owner):
# # this if is not needed, because unless we define __set__ on the descriptor
Expand All @@ -166,6 +172,7 @@ def __get__(self, instance, owner):
class all_:
def __init__(self, selector: str | Tuple[str, str], _context=None):
self._selector = selector
self._instantiated_as_descriptor = False

self._context = lambda instance: (
(_context(instance) if callable(_context) else _context)
Expand Down Expand Up @@ -228,6 +235,7 @@ def within_browser(self):

def __set_name__(self, owner, name):
self._name = name # TODO: use it
self._instantiated_as_descriptor = True

def __get__(self, instance, owner):
as_context = self._as_context(instance)
Expand Down

0 comments on commit 7787202

Please sign in to comment.