Skip to content

Commit

Permalink
refactor: move registered types to registries
Browse files Browse the repository at this point in the history
  • Loading branch information
tlambert03 committed Jun 29, 2022
1 parent 5609044 commit 7233dcf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
38 changes: 35 additions & 3 deletions src/app_model/_registries.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,45 @@
from __future__ import annotations

from concurrent.futures import Future, ThreadPoolExecutor
from typing import TYPE_CHECKING, Any, Callable, Optional
from functools import cached_property
from typing import TYPE_CHECKING, Any, Callable, NamedTuple, Optional

from psygnal import Signal

from ._types import MenuItem, SubmenuItem, _RegisteredCommand, _RegisteredKeyBinding
from ._types import MenuItem, SubmenuItem

if TYPE_CHECKING:
from typing import Dict, Iterator, List, Sequence, Set, Tuple, Union

from ._types import CommandCallable, CommandIdStr, KeybindingRule
from . import context
from ._types import CommandCallable, CommandIdStr, KeybindingRule, KeyCodeStr

DisposeCallable = Callable[[], None]
CommandDecorator = Callable[[CommandCallable], CommandCallable]
MenuOrSubmenu = Union[MenuItem, SubmenuItem]


class _RegisteredCommand:
"""Small object to represent a command in the CommandsRegistry.
Only used internally by the CommandsRegistry.
This helper class allows us to cache the dependency-injected variant of the
command. As usual with `cached_property`, the cache can be cleard by deleting
the attribute: `del cmd.run_injected`
"""

def __init__(self, id: CommandIdStr, run: CommandCallable, title: str) -> None:
self.id = id
self.run = run
self.title = title

@cached_property
def run_injected(self) -> Callable:
# from .._injection import inject_dependencies
# return inject_dependencies(self.run)
return self.run


class CommandsRegistry:

registered = Signal(str)
Expand Down Expand Up @@ -136,6 +159,15 @@ def __str__(self) -> str:
return "\n".join(lines)


class _RegisteredKeyBinding(NamedTuple):
"""Internal object representing a fully registered keybinding."""

keybinding: KeyCodeStr # the keycode to bind to
command_id: CommandIdStr # the command to run
weight: int # the weight of the binding, for prioritization
when: Optional[context.Expr] = None # condition to enable keybinding


class KeybindingsRegistry:

registered = Signal()
Expand Down
32 changes: 0 additions & 32 deletions src/app_model/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

import os
import sys
from functools import cached_property
from typing import (
TYPE_CHECKING,
Any,
Callable,
Generator,
Generic,
List,
NamedTuple,
NewType,
Optional,
TypedDict,
Expand Down Expand Up @@ -131,27 +129,6 @@ class CommandRule(BaseModel):
)


class _RegisteredCommand:
"""Small object to represent a command in the CommandsRegistry.
Only used internally by the CommandsRegistry.
This helper class allows us to cache the dependency-injected variant of the
command. As usual with `cached_property`, the cache can be cleard by deleting
the attribute: `del cmd.run_injected`
"""

def __init__(self, id: CommandIdStr, run: CommandCallable, title: str) -> None:
self.id = id
self.run = run
self.title = title

@cached_property
def run_injected(self) -> Callable:
# from .._injection import inject_dependencies
# return inject_dependencies(self.run)
return self.run


# ------------------ keybinding-related types --------------------


Expand Down Expand Up @@ -194,15 +171,6 @@ def _bind_to_current_platform(self) -> Optional[KeyCodeStr]:
return self.primary


class _RegisteredKeyBinding(NamedTuple):
"""Internal object representing a fully registered keybinding."""

keybinding: KeyCodeStr # the keycode to bind to
command_id: CommandIdStr # the command to run
weight: int # the weight of the binding, for prioritization
when: Optional[context.Expr] = None # condition to enable keybinding


# ------------------ menus-related types --------------------


Expand Down

0 comments on commit 7233dcf

Please sign in to comment.