2828from telegram .error import BadRequest
2929from telegram .ext import Handler
3030
31- try :
32- str_type = str
33- except NameError :
34- str_type = basestring # noqa pylint: disable=undefined-variable
31+
32+ def id_from_update (update ):
33+ if update .callback_query .message :
34+ return update .callback_query .message .chat_id , update .callback_query .message .message_id
35+ return update .callback_query .inline_message_id
3536
3637
3738class Menu (object ):
38- _instance = None
3939 _buttons = None
4040 text = ''
4141 buttons = None
4242 data = {}
4343 root_menu = None # populated in menuhandler
4444 stack = None # Only used in root menu assigned in menuhandler
4545
46- def __new__ (cls ):
47- if not cls ._instance :
48- cls ._instance = super (Menu , cls ).__new__ (cls )
49- return cls ._instance
50-
5146 def callback (self , bot , update , user_data , chat_data ):
52- _id = (update .callback_query .message .chat_id , update .callback_query .message .message_id
53- ) if update .callback_query .message else update .callback_query .inline_message_id
54- self .root_menu ().stack [_id ].append (self )
47+ self .root_menu .stack [id_from_update (update )].append (self )
5548 try :
5649 return update .callback_query .edit_message_text (self .get_text (update ),
5750 reply_markup = self .keyboard (user_data ,
@@ -62,11 +55,10 @@ def callback(self, bot, update, user_data, chat_data):
6255 else :
6356 raise
6457
65- @classmethod
66- def start (cls , bot , update , user_data = None , chat_data = None ):
58+ def start (self , bot , update , user_data = None , chat_data = None ):
6759 # user_ and chat_data is only needed if we wanna do stuff that need state (ie.
6860 # ToggleButtons)
69- return update .message .reply_text (cls () .get_text (update ), reply_markup = cls () .keyboard (
61+ return update .message .reply_text (self .get_text (update ), reply_markup = self .keyboard (
7062 user_data , chat_data ))
7163
7264 def keyboard (self , user_data , chat_data ):
@@ -109,7 +101,7 @@ def __init__(self,
109101 raise RuntimeError
110102 self .callback = callback
111103 if menu is not None :
112- self .callback = menu () .callback
104+ self .callback = menu .callback
113105 pass_user_data = True
114106 pass_chat_data = True
115107 self .menu = menu
@@ -158,15 +150,12 @@ def __init__(self, text, name=None):
158150 pass_user_data = True , pass_chat_data = True , name = name )
159151
160152 def _callback (self , bot , update , user_data , chat_data ):
161- _id = (update .callback_query .message .chat_id , update .callback_query .message .message_id
162- ) if update .callback_query .message else update .callback_query .inline_message_id
163-
164- stack = self .parent_menu ().root_menu ().stack [_id ]
153+ stack = self .parent_menu .root_menu .stack [id_from_update (update )]
165154 try :
166155 stack .pop ()
167156 last_menu = stack .pop ()
168157 except IndexError :
169- last_menu = self .parent_menu () .root_menu ()
158+ last_menu = self .parent_menu .root_menu
170159 last_menu .callback (bot , update , user_data , chat_data )
171160
172161
@@ -186,7 +175,7 @@ def __init__(self, menu):
186175
187176 def collect_buttons (self , menu ):
188177 menu .root_menu = self .menu
189- for button in chain .from_iterable (menu () .get_buttons ()):
178+ for button in chain .from_iterable (menu .get_buttons ()):
190179 button .parent_menu = menu
191180 if button .name not in self .buttons and (button .callback is not None or
192181 button .menu is not None ):
0 commit comments