Exemple #1
0
    def __init__(self):
        self.view_rep = None

        self.on_destroyed = Event()
        self.is_destroyed = False

        self.destroy = Mock(side_effect=self.sim_destroy)
Exemple #2
0
    def __init__(
        self,
        check_equals: Callable[[_T, _T],
                               bool] = _general_purpose_equality_check
    ) -> None:
        self.on_changed = Event()

        self._check_equals = check_equals
class MockBindable:
    def __init__(self):
        self.on_changed = Event()
        self.get = Mock()
        self.set = Mock()

    def poke(self) -> None:
        self.on_changed.fire()

    def reset(self):
        self.get.reset_mock()
        self.set.reset_mock()
Exemple #4
0
class MockComponent:
    def __init__(self):
        self.view_rep = None

        self.on_destroyed = Event()
        self.is_destroyed = False

        self.destroy = Mock(side_effect=self.sim_destroy)

    def sim_destroy(self):
        self.is_destroyed = True
        self.on_destroyed.fire(self)
Exemple #5
0
    def __init__(self, view: View[Any, ComponentViewRepType], presenter: Presenter) -> None:
        self._view = view
        self._presenter = presenter

        self._children = []  # type: MutableSequence[Component.ChildContainer]

        self.on_destroyed = Event()
        self.is_destroyed = False

        self._presenter._component_hooks.component_destroy = self.destroy

        self._view._component_hooks.new_component = self._new_component_for_view
        self._view._component_hooks.remove_component = self._remove_component_for_view

        self._presenter._init(self._view)
        self.view_rep = self._view._init(self._presenter)  # type: ComponentViewRepType
Exemple #6
0
class Bindable(Generic[_T]):
    def __init__(
        self,
        check_equals: Callable[[_T, _T],
                               bool] = _general_purpose_equality_check
    ) -> None:
        self.on_changed = Event()

        self._check_equals = check_equals

    def get(self) -> _T:
        return self._get_value()

    def set(self, new_value: _T) -> None:
        try:
            current_value = self.get()
        except NotImplementedError:
            pass
        else:
            if self._check_equals(current_value, new_value):
                return

        self._set_value(new_value)
        self.on_changed.fire()

    # Convenience methods for creating Bindings
    def bind(self, dst: 'Bindable[_T]') -> 'Binding':
        from .binding import Binding
        return Binding(src=self, dst=dst)

    def bind_to(self, dst: 'Bindable[_T]') -> 'Binding':
        from .binding import Binding
        return Binding(src=self, dst=dst, one_way=True)

    def bind_from(self, src: 'Bindable[_T]') -> 'Binding':
        from .binding import Binding
        return Binding(src=src, dst=self, one_way=True)

    @abstractmethod
    def _get_value(self) -> _T:
        """Actual implementation used to get the value"""
        raise NotImplementedError

    @abstractmethod
    def _set_value(self, new_value: _T) -> None:
        """Actual implementation used to set a new value"""
        raise NotImplementedError
Exemple #7
0
class Bindable(typing.Bindable[_T]):
    def __init__(
        self,
        check_equals: Callable[[_T, _T],
                               bool] = _general_purpose_equality_check
    ) -> None:
        self.on_changed = Event()

        self._check_equals = check_equals

    def get(self) -> _T:
        return self._get_value()

    def set(self, value: _T) -> None:
        try:
            current_value = self.get()
        except NotImplementedError:
            pass
        else:
            if self._check_equals(current_value, value):
                return

        self._set_value(value)

        self.on_changed.fire()

    def bind(self, dst: typing.Bindable[_T]) -> typing.Binding:
        return Binding(src=self, dst=dst)

    def bind_to(self, dst: typing.WriteBindable[_T]) -> typing.Binding:
        return Binding(src=self, dst=dst, one_way=True)

    def bind_from(self, src: typing.ReadBindable[_T]) -> typing.Binding:
        return Binding(src=src, dst=self, one_way=True)

    def _get_value(self) -> _T:
        """Actual implementation used to get the value"""
        raise NotImplementedError

    def _set_value(self, new_value: _T) -> None:
        """Actual implementation used to set a new value"""
        raise NotImplementedError
Exemple #8
0
    def __init__(self,
                 value: ValueType,
                 *,
                 errors_out: Optional[Bindable[Set[ErrorType]]] = None,
                 on_user_finished_editing: Optional[Event] = None) -> None:
        if errors_out is None:
            errors_out = BoxBindable(set())  # type: Bindable[Set[ErrorType]]

        if on_user_finished_editing is None:
            on_user_finished_editing = Event()

        self.value = value
        self.errors_out = errors_out
        self.on_user_finished_editing = on_user_finished_editing
 def __init__(self):
     self.on_changed = Event()
     self.get = Mock()
     self.set = Mock()
Exemple #10
0
class Component(Generic[ComponentViewRepType]):
    ChildContainer = namedtuple('ChildContainer',
                                ['component', 'on_destroyed_conn'])

    def __init__(self, view: View[Any, ComponentViewRepType],
                 presenter: Presenter) -> None:
        self._view = view
        self._presenter = presenter

        self._children = []  # type: MutableSequence[Component.ChildContainer]

        self.on_destroyed = Event()
        self.is_destroyed = False

        self._presenter._component_hooks.component_destroy = self.destroy

        self._view._component_hooks.new_component = self._new_component_for_view
        self._view._component_hooks.remove_component = self._remove_component_for_view

        self._presenter._init(self._view)
        self.view_rep = self._view._init(
            self._presenter)  # type: ComponentViewRepType

    def new_component(self, cfactory: ComponentFactory[T]) -> 'Component[T]':
        new_child_view_env = self._view.extend_env()
        new_child_presenter_env = self._presenter.extend_env()

        new_child = cfactory.create(view_env=new_child_view_env,
                                    presenter_env=new_child_presenter_env)

        self._children.append(
            self.ChildContainer(
                component=new_child,
                on_destroyed_conn=new_child.on_destroyed.connect(
                    self.remove_component)))

        return new_child

    def remove_component(self, component: 'Component') -> None:
        for x in self._children:
            if x.component == component:
                child_container = x
                break
        else:
            raise ValueError('Component is not a child of this')

        self._children.remove(child_container)

        child_container.on_destroyed_conn.disconnect()
        child_component = child_container.component

        if not child_component.is_destroyed:
            child_component.destroy()

    def _new_component_for_view(
            self, cfactory: ComponentFactory[T]) -> Tuple[Any, T]:
        new_child = self.new_component(cfactory)

        return new_child, new_child.view_rep

    def _remove_component_for_view(self, component_id: Any) -> None:
        return self.remove_component(component_id)

    def destroy(self) -> None:
        for child_container in list(self._children):
            self.remove_component(child_container.component)

        self._presenter._destroy()
        self._view._destroy()

        self.is_destroyed = True
        self.on_destroyed.fire(self)