Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pony.orm import *
- db = Database()
- db.bind(provider='postgres', user='alex', password='', host='localhost', port='5432', database='menu_db')
- set_sql_debug(False)
- class Menu(db.Entity):
- id = PrimaryKey(int, auto=True)
- name = Required(str)
- menu = Optional('Menu', reverse='menus')
- menus = Set('Menu', reverse='menu')
- commands = Set('Command')
- class Command(db.Entity):
- id = PrimaryKey(int, auto=True)
- name = Required(str)
- text = Required(str)
- menu = Optional(Menu)
- db.generate_mapping(create_tables=True)
- with db_session:
- # menu_1 = Menu(name='menu_1')
- # command_1 = Command(name='command_1', text='command_1 called', menu=menu_1)
- # command_2 = Command(name='command_2', text='command_2 called', menu=menu_1)
- #
- # menu_2 = Menu(name='menu_2', menu=menu_1)
- # command_3 = Command(name='command_3', text='command_3 called', menu=menu_2)
- current_menu = None
- action_name = None
- while True:
- for menu in Menu.select(lambda m: m.menu == current_menu):
- print('> ' + menu.name)
- for command in Command.select(lambda c: c.menu == current_menu):
- print('* ' + command.name)
- if current_menu:
- print('* back')
- action_name = input()
- if not action_name.strip():
- print('please type action')
- continue
- if current_menu and action_name == 'back':
- current_menu = current_menu.menu
- continue
- menu = Menu.get(menu=current_menu, name=action_name)
- if menu:
- current_menu = menu
- else:
- command = Command.get(menu=current_menu, name=action_name)
- print(command.text if command else 'action not found')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement