# Path: selene/core/wait.py
# class Wait(Generic[E]):
# 
#     # todo: provide the smallest possible timeout default, something like 1ms
#     def __init__(
#         self,
#         entity: E,
#         at_most: int,
#         or_fail_with: Optional[Callable[[TimeoutException], Exception]] = None,
#     ):
#         self._entity = entity
#         self._timeout = at_most
#         self._hook_failure = or_fail_with or identity
# 
#     def at_most(self, timeout: int) -> Wait[E]:
#         return Wait(self._entity, timeout, self._hook_failure)
# 
#     def or_fail_with(
#         self, hook_failure: Optional[Callable[[TimeoutException], Exception]]
#     ) -> Wait[E]:
# 
#         return Wait(self._entity, self._timeout, hook_failure)
# 
#     @property
#     def hook_failure(
#         self,
#     ) -> Optional[Callable[[TimeoutException], Exception]]:
#         # todo: hook_failure or failure_hook?
#         return self._hook_failure
# 
#     # todo: consider renaming to `def to(...)`, though will sound awkward when wait.to(condition)
#     def for_(self, fn: Callable[[E], R]) -> R:
#         finish_time = time.time() + self._timeout
# 
#         while True:
#             try:
#                 return fn(self._entity)
#             except Exception as reason:
#                 if time.time() > finish_time:
# 
#                     reason_message = str(reason)
# 
#                     reason_string = '{name}: {message}'.format(
#                         name=reason.__class__.__name__, message=reason_message
#                     )
#                     # todo: think on how can we improve logging failures in selene, e.g. reverse msg and stacktrace
#                     # stacktrace = getattr(reason, 'stacktrace', None)
#                     timeout = self._timeout
#                     entity = self._entity
# 
#                     failure = TimeoutException(
#                         f'''
# 
# Timed out after {timeout}s, while waiting for:
# {entity}.{fn}
# 
# Reason: {reason_string}'''
#                     )
# 
#                     raise self._hook_failure(failure)
# 
#     def until(self, fn: Callable[[E], R]) -> bool:
#         try:
#             self.for_(fn)
#             return True
#         except TimeoutException:
#             return False
# 
#     # todo: do we really need these aliases?
#     def command(self, description: str, fn: Callable[[E], None]) -> None:
#         self.for_(Command(description, fn))
# 
#     def query(self, description: str, fn: Callable[[E], R]) -> R:
#         return self.for_(Query(description, fn))


import warnings
from selene.core.wait import Wait
# MIT License
#
# Copyright (c) 2015-2021 Iakiv Kramarenko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.



def wait_for(entity, condition, timeout=4, polling=0.1):
    warnings.warn(
        'deprecated, use Wait(entity, at_most=timeout).for_(condition)',
        DeprecationWarning,

# Path: selene/core/wait.py
# class Wait(Generic[E]):
# 
#     # todo: provide the smallest possible timeout default, something like 1ms
#     def __init__(
#         self,
#         entity: E,
#         at_most: int,
#         or_fail_with: Optional[Callable[[TimeoutException], Exception]] = None,
#     ):
#         self._entity = entity
#         self._timeout = at_most
#         self._hook_failure = or_fail_with or identity
# 
#     def at_most(self, timeout: int) -> Wait[E]:
#         return Wait(self._entity, timeout, self._hook_failure)
# 
#     def or_fail_with(
#         self, hook_failure: Optional[Callable[[TimeoutException], Exception]]
#     ) -> Wait[E]:
# 
#         return Wait(self._entity, self._timeout, hook_failure)
# 
#     @property
#     def hook_failure(
#         self,
#     ) -> Optional[Callable[[TimeoutException], Exception]]:
#         # todo: hook_failure or failure_hook?
#         return self._hook_failure
# 
#     # todo: consider renaming to `def to(...)`, though will sound awkward when wait.to(condition)
#     def for_(self, fn: Callable[[E], R]) -> R:
#         finish_time = time.time() + self._timeout
# 
#         while True:
#             try:
#                 return fn(self._entity)
#             except Exception as reason:
#                 if time.time() > finish_time:
# 
#                     reason_message = str(reason)
# 
#                     reason_string = '{name}: {message}'.format(
#                         name=reason.__class__.__name__, message=reason_message
#                     )
#                     # todo: think on how can we improve logging failures in selene, e.g. reverse msg and stacktrace
#                     # stacktrace = getattr(reason, 'stacktrace', None)
#                     timeout = self._timeout
#                     entity = self._entity
# 
#                     failure = TimeoutException(
#                         f'''
# 
# Timed out after {timeout}s, while waiting for:
# {entity}.{fn}
# 
# Reason: {reason_string}'''
#                     )
# 
#                     raise self._hook_failure(failure)
# 
#     def until(self, fn: Callable[[E], R]) -> bool:
#         try:
#             self.for_(fn)
#             return True
#         except TimeoutException:
#             return False
# 
#     # todo: do we really need these aliases?
#     def command(self, description: str, fn: Callable[[E], None]) -> None:
#         self.for_(Command(description, fn))
# 
#     def query(self, description: str, fn: Callable[[E], R]) -> R:
#         return self.for_(Query(description, fn))


# Path: selene/wait.py
import warnings
from selene.core.wait import Wait

# MIT License
#
# Copyright (c) 2015-2021 Iakiv Kramarenko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.



def wait_for(entity, condition, timeout=4, polling=0.1):
    warnings.warn(
        'deprecated, use Wait(entity, at_most=timeout).for_(condition)',
        DeprecationWarning,

    )