Action

Source code in pzp/actions.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Action:
    def __init__(self, action: str, keys: Optional[Sequence[str]] = None) -> None:
        """
        Action decorator

        Args:
            action: Action
        """
        self.action: str = action
        self.keys: Sequence[str] = keys or []

    def __call__(self, func: ActionHandler) -> ActionHandler:
        setattr(func, "pzp_action", self.action)
        setattr(func, "pzp_keys", self.keys)
        return func

__init__(action, keys=None)

Action decorator

Parameters:

Name Type Description Default
action str

Action

required
Source code in pzp/actions.py
17
18
19
20
21
22
23
24
25
def __init__(self, action: str, keys: Optional[Sequence[str]] = None) -> None:
    """
    Action decorator

    Args:
        action: Action
    """
    self.action: str = action
    self.keys: Sequence[str] = keys or []

ActionsHandler

Source code in pzp/actions.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
class ActionsHandler:
    def __init__(
        self,
        keys_handler: Optional[KeysHandler] = None,
        keys_binding: Optional[KeysBinding] = None,
    ) -> None:
        """
        Action handler

        Args:
            keys_handler: Keys handler
            keys_binding: Optional[Dict[str, Sequence[str]]] = None,

        Attributes:
            actions: map action names to action handlers
            keys_handler: Keys handler
        """
        self.keys_handler = keys_handler or KeysHandler()
        # Collect the methods with the Action decorator
        self.actions: Dict[str, ActionHandler] = {}
        for name, member in inspect.getmembers(self):
            action: Optional[str] = getattr(member, "pzp_action", None)
            if action is not None:
                self.actions[action] = member
                # Default action
                if name == "default":
                    self.actions["default"] = member
                # Keys binding
                keys: Optional[Sequence[str]] = getattr(member, "pzp_keys", None)
                if keys:
                    self.keys_handler.set_keys_binding(keys, action)
        # Override keys binding
        if keys_binding:
            self.keys_handler.update(keys_binding)

    def process_key_event(self, key_event: KeyEvent) -> None:
        """
        Execute the action hander for a give key event

        Args:
            key_event: Key event to be processed

        Raises:
            MissingHander: If there is no handler for the given key event
        """
        action: str = key_event.action or "default"
        fn = self.actions.get(action)
        if not fn:
            raise MissingHander(action=action, ch=key_event.ch)
        # Check if the function has the key_event argument
        if "key_event" in inspect.getargs(fn.__code__).args:
            fn(key_event=key_event)  # type: ignore
        else:
            fn()  # type: ignore

__init__(keys_handler=None, keys_binding=None)

Action handler

Parameters:

Name Type Description Default
keys_handler Optional[KeysHandler]

Keys handler

None
keys_binding Optional[KeysBinding]

Optional[Dict[str, Sequence[str]]] = None,

None

Attributes:

Name Type Description
actions

map action names to action handlers

keys_handler

Keys handler

Source code in pzp/actions.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def __init__(
    self,
    keys_handler: Optional[KeysHandler] = None,
    keys_binding: Optional[KeysBinding] = None,
) -> None:
    """
    Action handler

    Args:
        keys_handler: Keys handler
        keys_binding: Optional[Dict[str, Sequence[str]]] = None,

    Attributes:
        actions: map action names to action handlers
        keys_handler: Keys handler
    """
    self.keys_handler = keys_handler or KeysHandler()
    # Collect the methods with the Action decorator
    self.actions: Dict[str, ActionHandler] = {}
    for name, member in inspect.getmembers(self):
        action: Optional[str] = getattr(member, "pzp_action", None)
        if action is not None:
            self.actions[action] = member
            # Default action
            if name == "default":
                self.actions["default"] = member
            # Keys binding
            keys: Optional[Sequence[str]] = getattr(member, "pzp_keys", None)
            if keys:
                self.keys_handler.set_keys_binding(keys, action)
    # Override keys binding
    if keys_binding:
        self.keys_handler.update(keys_binding)

process_key_event(key_event)

Execute the action hander for a give key event

Parameters:

Name Type Description Default
key_event KeyEvent

Key event to be processed

required

Raises:

Type Description
MissingHander

If there is no handler for the given key event

Source code in pzp/actions.py
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def process_key_event(self, key_event: KeyEvent) -> None:
    """
    Execute the action hander for a give key event

    Args:
        key_event: Key event to be processed

    Raises:
        MissingHander: If there is no handler for the given key event
    """
    action: str = key_event.action or "default"
    fn = self.actions.get(action)
    if not fn:
        raise MissingHander(action=action, ch=key_event.ch)
    # Check if the function has the key_event argument
    if "key_event" in inspect.getargs(fn.__code__).args:
        fn(key_event=key_event)  # type: ignore
    else:
        fn()  # type: ignore