Skip to content

Commit

Permalink
feat!: preliminary functions are now passed as arguments (resolve #6)
Browse files Browse the repository at this point in the history
  • Loading branch information
hearot committed Jun 13, 2020
1 parent eb6bb8b commit 45687a4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 21 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
### New features

- Import all the public functions ([c0a7deb30fbe75d6b8c37f71dcaaa49ba8b3ab8f](https://github.com/hearot/pyrubrum/commit/c0a7deb30fbe75d6b8c37f71dcaaa49ba8b3ab8f))
- Import database errors by default
- Import database errors by default ([eb6bb8b5320676e1f474097e3738d54ddddb31e7](https://github.com/hearot/pyrubrum/commit/eb6bb8b5320676e1f474097e3738d54ddddb31e7))

### Other changes

Expand All @@ -26,6 +26,7 @@
### ‼️ Breaking changes

- Create specific directories for module entities ([25593e6d40fa34dbc47528ff3fa6fdc30c0a41b9](https://github.com/hearot/pyrubrum/commit/25593e6d40fa34dbc47528ff3fa6fdc30c0a41b9))
- Preliminary functions are now passed as arguments (resolve #6)
- `BaseDatabase.get` always returns a string (resolve #2) ([b1d0dac010d2d0cd46a4bcc41d2f07c2099d6ac9](https://github.com/hearot/pyrubrum/commit/b1d0dac010d2d0cd46a4bcc41d2f07c2099d6ac9))

## v0.1a1.dev5 - 2020-06-12
Expand Down
3 changes: 2 additions & 1 deletion FEATURES.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
### New features

- Import all the public functions ([c0a7deb30fbe75d6b8c37f71dcaaa49ba8b3ab8f](https://github.com/hearot/pyrubrum/commit/c0a7deb30fbe75d6b8c37f71dcaaa49ba8b3ab8f))
- Import database errors by default
- Import database errors by default ([eb6bb8b5320676e1f474097e3738d54ddddb31e7](https://github.com/hearot/pyrubrum/commit/eb6bb8b5320676e1f474097e3738d54ddddb31e7))

### Other changes

Expand All @@ -11,4 +11,5 @@
### ‼️ Breaking changes

- Create specific directories for module entities ([25593e6d40fa34dbc47528ff3fa6fdc30c0a41b9](https://github.com/hearot/pyrubrum/commit/25593e6d40fa34dbc47528ff3fa6fdc30c0a41b9))
- Preliminary functions are now passed as arguments (resolve #6)
- `BaseDatabase.get` always returns a string (resolve #2) ([b1d0dac010d2d0cd46a4bcc41d2f07c2099d6ac9](https://github.com/hearot/pyrubrum/commit/b1d0dac010d2d0cd46a4bcc41d2f07c2099d6ac9))
60 changes: 41 additions & 19 deletions pyrubrum/menus/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@
],
]

Preliminary = Optional[
Callable[
["Handler", Client, Union[CallbackQuery, Message], Dict[str, Any]],
None,
]
]


@dataclass(eq=False, init=False, repr=True)
class Menu(BaseMenu):
Expand All @@ -58,6 +65,10 @@ class Menu(BaseMenu):
provided as well and must follow the following arguments pattern:
``func(handler, client, context, parameters)``
limit (Optional[int]): The limit of buttons per row. Defaults to 2.
preliminary (Preliminary): A function which is executed each time
before doing anything else in `on_callback` and `on_message`.
Defaults to ``None``, which means that no function is going to
be executed.
Note:
This implementation is compatible with a non parameterized handler.
Expand Down Expand Up @@ -85,6 +96,7 @@ def __init__(
],
back_button_text: Optional[str] = "🔙",
limit: Optional[int] = 2,
preliminary: Preliminary = None,
):
"""Initialize the object.
Expand All @@ -105,12 +117,17 @@ def __init__(
inside the button that lets the user go back to the parent
menu. Defaults to "🔙".
limit (Optional[int]): The limit of buttons per row. Defaults to 2.
preliminary (Preliminary): A function which is executed each time
before doing anything else in `on_callback` and `on_message`.
Defaults to ``None``, which means that no function is going to
be executed.
"""

BaseMenu.__init__(self, name, menu_id)
self.back_button_text = back_button_text
self.content = content
self.limit = limit
self.preliminary = preliminary

def get_content(
self,
Expand Down Expand Up @@ -149,16 +166,17 @@ def on_callback(
parameters: Optional[Dict[str, Any]] = None,
):
"""Each time a callback query is handled, this function calls the preliminary
function (i.e. `Menu.preliminary`), then gets the content that is going
to be provided to the user (both text and media are compatible), sets
up an inline keyboard filled with all the references to the menus that
are linked to the children of this menu node, including a special
button for going back to the parent menu, whose text is defined using
`Menu.back_button_text`, which overwrites the text of the parent menu
which usually should be displayed (see `Menu.keyboard`), and finally
edits the message with `CallbackQuery.edit_message_text` (if the
content is a string, i.e. a text) or `CallbackQuery.edit_message_media`
(if the content is an instance of `InputMedia`, i.e. a media).
function (i.e. `Menu.preliminary`), if callable, then gets the content
that is going to be provided to the user (both text and media are
compatible), sets up an inline keyboard filled with all the references
to the menus that are linked to the children of this menu node,
including a special button for going back to the parent menu, whose
text is defined using `Menu.back_button_text`, which overwrites the
text of the parent menu which usually should be displayed (see
`Menu.keyboard`), and finally edits the message with
`CallbackQuery.edit_message_text` (if the content is a string, i.e. a
text) or `CallbackQuery.edit_message_media` (if the content is an
instance of `InputMedia`, i.e. a media).
Args:
handler (BaseHandler): The handler which coordinates the management
Expand All @@ -172,7 +190,9 @@ def on_callback(
if not parameters:
parameters = {}

self.preliminary(handler, client, callback, parameters)
if callable(self.preliminary):
self.preliminary(handler, client, callback, parameters)

content = self.get_content(handler, client, callback, parameters)

if isinstance(content, InputMedia):
Expand Down Expand Up @@ -200,13 +220,13 @@ def on_message(
parameters: Optional[Dict[str, Any]] = None,
):
"""Each time a message is handled, this function calls the preliminary
function (i.e. `Menu.preliminary`), then gets the content that is going
to be provided to the user (both text and media are compatible), sets
up an inline keyboard filled with all the references to the menus that
are linked to the children of this menu node), and finally sends the
message with `Message.reply_text` (if the content is a string, i.e. a
text) or `Message.reply_cached_media` (if the content is an instance of
`InputMedia`, i.e. a media).
function (i.e. `Menu.preliminary`), if callable, then gets the content
that is going to be provided to the user (both text and media are
compatible), sets up an inline keyboard filled with all the references
to the menus that are linked to the children of this menu node), and
finally sends the message with `Message.reply_text` (if the content is
a string, i.e. a text) or `Message.reply_cached_media` (if the content
is an instance of `InputMedia`, i.e. a media).
Args:
handler (BaseHandler): The handler which coordinates the management
Expand All @@ -219,7 +239,9 @@ def on_message(
if not parameters:
parameters = {}

self.preliminary(handler, client, message, parameters)
if callable(self.preliminary):
self.preliminary(handler, client, message, parameters)

content = self.get_content(handler, client, message, parameters)

if isinstance(content, InputMedia):
Expand Down
7 changes: 7 additions & 0 deletions pyrubrum/menus/page_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from pyrubrum.keyboard import Element
from pyrubrum.keyboard import Keyboard
from .menu import Menu
from .menu import Preliminary

Items = Union[
List[Element],
Expand Down Expand Up @@ -90,6 +91,7 @@ def __init__(
limit: Optional[int] = 2,
limit_page: Optional[int] = 4,
next_page_button_text: Optional[str] = "▶️",
preliminary: Preliminary = None,
previous_page_button_text: Optional[str] = "◀️",
):
"""Initialize the object.
Expand Down Expand Up @@ -120,6 +122,10 @@ def __init__(
next_page_button_text (Optional[str]): The text which is displayed
inside the button that lets the user move on to the next page,
if any. Defaults to "▶️".
preliminary (Preliminary): A function which is executed each time
before doing anything else in `on_callback` and `on_message`.
Defaults to ``None``, which means that no function is going to
be executed.
previous_page_button_text (Optional[str]): The text which is
displayed inside the button that lets the user go back to the
previous page, if any. Defaults to "◀️".
Expand All @@ -131,6 +137,7 @@ def __init__(
content,
back_button_text=back_button_text,
limit=limit,
preliminary=preliminary,
)

self.items = items
Expand Down

0 comments on commit 45687a4

Please sign in to comment.