Skip to content

Commit

Permalink
database: add BaseDatabase & DictDatabase
Browse files Browse the repository at this point in the history
  • Loading branch information
hearot committed Jun 4, 2020
1 parent b187e00 commit ce59115
Show file tree
Hide file tree
Showing 16 changed files with 303 additions and 87 deletions.
3 changes: 3 additions & 0 deletions pyroboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyroboard. If not, see <http://www.gnu.org/licenses/>.

from .button import Button # noqa
from .database.dict_database import DictDatabase # noqa
from .element import Element # noqa
from .keyboard import Keyboard # noqa
from .page_item_menu import PageItemMenu # noqa
from .page_menu import PageMenu # noqa
from .parameterized_tree_handler import ParameterizedTreeHandler # noqa
Expand Down
14 changes: 12 additions & 2 deletions pyroboard/base_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyroboard. If not, see <http://www.gnu.org/licenses/>.

from .button import Button
from dataclasses import dataclass
from pyrogram import CallbackQuery, CallbackQueryHandler, Client, Filters
from pyrogram import (CallbackQuery, CallbackQueryHandler, Client,
InlineKeyboardButton, Filters)
from typing import Any, Callable, List


Expand All @@ -26,11 +28,19 @@ class BaseHandler:
def get_menus(self) -> List['BaseMenu']:
raise NotImplementedError

def process_keyboard(self, keyboard: List[List[Button]],
callback_query_id: str) -> List[
List[InlineKeyboardButton]]:
return [[InlineKeyboardButton(
button.name,
callback_data=button.button_id)
for button in row] for row in keyboard]

def setup(self, client: Client):
for menu in self.get_menus():
client.add_handler(CallbackQueryHandler(
pass_handler(menu.on_callback, self),
Filters.callback_data(menu.unique_id)))
Filters.callback_data(menu.menu_id)))


def pass_handler(func: Callable[[Client, Any], None],
Expand Down
15 changes: 7 additions & 8 deletions pyroboard/base_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
# along with Pyroboard. If not, see <http://www.gnu.org/licenses/>.

from .base_handler import BaseHandler
from .button import Button
from dataclasses import dataclass
from pyrogram import (CallbackQuery, Client,
InlineKeyboardButton,
InlineKeyboardMarkup,
InputMedia, Message)
from typing import Union
Expand All @@ -28,14 +28,14 @@
@dataclass(eq=False, init=False, repr=True)
class BaseMenu:
name: str
unique_id: str
menu_id: str

def __hash__(self):
return hash(self.unique_id)
return hash(self.menu_id)

def __init__(self, name: str, unique_id: str):
def __init__(self, name: str, menu_id: str):
self.name = name
self.unique_id = unique_id
self.menu_id = menu_id

def get_content(self) -> Union[InputMedia, str]:
raise NotImplementedError
Expand All @@ -47,9 +47,8 @@ def on_callback(self, handler: BaseHandler,
def button(self, handler: BaseHandler, client: Client,
context: Union[CallbackQuery,
Message],
**_) -> InlineKeyboardButton:
return InlineKeyboardButton(self.name,
callback_data=self.unique_id)
**_) -> Button:
return Button(self.name, self.menu_id)

def keyboard(self, handler: BaseHandler, client: Client,
context: Union[CallbackQuery,
Expand Down
36 changes: 36 additions & 0 deletions pyroboard/button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Pyroboard - Keyboard manager for Pyrogram
# Copyright (C) 2020 Hearot <https://github.com/hearot>
#
# This file is part of Pyroboard.
#
# Pyroboard is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyroboard is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyroboard. If not, see <http://www.gnu.org/licenses/>.

from dataclasses import dataclass


@dataclass(init=False)
class Button:
button_id: str
element_id: str = ""
name: str
parameters: dict

def __init__(self, name: str, button_id: str, **kwargs):
self.button_id = button_id
self.name = name
self.parameters = kwargs
self.parameters['name'] = name

if "element_id" in kwargs:
self.element_id = kwargs['element_id']
19 changes: 19 additions & 0 deletions pyroboard/database/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Pyroboard - Keyboard manager for Pyrogram
# Copyright (C) 2020 Hearot <https://github.com/hearot>
#
# This file is part of Pyroboard.
#
# Pyroboard is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyroboard is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyroboard. If not, see <http://www.gnu.org/licenses/>.

from .dict_database import DictDatabase # noqa
30 changes: 30 additions & 0 deletions pyroboard/database/base_database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Pyroboard - Keyboard manager for Pyrogram
# Copyright (C) 2020 Hearot <https://github.com/hearot>
#
# This file is part of Pyroboard.
#
# Pyroboard is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyroboard is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyroboard. If not, see <http://www.gnu.org/licenses/>.

from typing import Optional


class BaseDatabase:
def get(self, callback_query_id: str) -> Optional[str]:
raise NotImplementedError

def insert(self, callback_query_id: str, data: str) -> bool:
raise NotImplementedError

def delete(self, callback_query_id: str) -> bool:
raise NotImplementedError
36 changes: 36 additions & 0 deletions pyroboard/database/dict_database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Pyroboard - Keyboard manager for Pyrogram
# Copyright (C) 2020 Hearot <https://github.com/hearot>
#
# This file is part of Pyroboard.
#
# Pyroboard is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyroboard is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyroboard. If not, see <http://www.gnu.org/licenses/>.

from .base_database import BaseDatabase
from typing import Optional


class DictDatabase(dict, BaseDatabase):
def get(self, callback_query_id: str) -> Optional[str]:
return dict.get(self, callback_query_id)

def insert(self, callback_query_id: str, data: str) -> bool:
self.update({callback_query_id: data})
return True

def delete(self, callback_query_id: str) -> bool:
try:
self.pop(callback_query_id)
return True
except KeyError:
return False
30 changes: 30 additions & 0 deletions pyroboard/keyboard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Pyroboard - Keyboard manager for Pyrogram
# Copyright (C) 2020 Hearot <https://github.com/hearot>
#
# This file is part of Pyroboard.
#
# Pyroboard is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyroboard is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyroboard. If not, see <http://www.gnu.org/licenses/>.

from .base_handler import BaseHandler
from .button import Button
from pyrogram import InlineKeyboardMarkup
from typing import List


class Keyboard(InlineKeyboardMarkup):
def __init__(self, inline_keyboard: List[List[Button]],
handler: BaseHandler, callback_query_id: str):
super().__init__(
handler.process_keyboard(inline_keyboard,
str(callback_query_id)))
8 changes: 4 additions & 4 deletions pyroboard/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ def get_children_menus(self) -> List[BaseMenu]:
children = [child.menu for child in self.children]
return children if children else None

def get_family(self, unique_id: str,
def get_family(self, menu_id: str,
parent: Optional['Node']) -> Tuple[Optional[BaseMenu],
Optional[
List[BaseMenu]]]:
if self.menu.unique_id == unique_id:
if self.menu.menu_id == menu_id:
return (parent.menu if isinstance(parent, Node) else None,
self.get_children_menus())

for child in self.children:
child_menus = child.get_family(unique_id, self)
child_menus = child.get_family(menu_id, self)

if child_menus[0]:
return child_menus
Expand All @@ -62,7 +62,7 @@ def get_menus(self) -> List[BaseMenu]:
menus = [self.menu]

for child in self.children:
if child.menu.unique_id != self.menu.unique_id:
if child.menu.menu_id != self.menu.menu_id:
menus += child.get_menus()

return menus
17 changes: 8 additions & 9 deletions pyroboard/page_item_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyroboard. If not, see <http://www.gnu.org/licenses/>.

from .button import Button
from .keyboard import Keyboard
from .parameterized_tree_handler import ParameterizedTreeHandler
from .tree_menu import TreeMenu
from dataclasses import dataclass
from pyrogram import (Client, CallbackQuery,
InlineKeyboardButton,
InlineKeyboardMarkup, Message)
from typing import Union

Expand All @@ -32,7 +33,7 @@ def keyboard(self, tree: ParameterizedTreeHandler,
context: Union[CallbackQuery,
Message],
page=0, **_) -> InlineKeyboardMarkup:
parent, children = tree.get_family(self.unique_id)
parent, children = tree.get_family(self.menu_id)

if isinstance(context, Message):
raise TypeError("PageItemMenu supports only CallbackQuery")
Expand All @@ -46,14 +47,12 @@ def keyboard(self, tree: ParameterizedTreeHandler,
range(0, len(children), self.limit)]

if parent:
parent_button = InlineKeyboardButton(
parent_button = Button(
self.back_button_text,
callback_data=tree.parameterize(
parent.unique_id,
page=page
)
)
parent.menu_id,
page=page)

keyboard = keyboard + [[parent_button]]

return InlineKeyboardMarkup(keyboard) if keyboard else None
return (Keyboard(keyboard, tree,
context.id) if keyboard else None)
Loading

0 comments on commit ce59115

Please sign in to comment.