Advertisement
Guest User

Untitled

a guest
May 12th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. from pony.orm import *
  2.  
  3. db = Database()
  4. db.bind(provider='postgres', user='alex', password='', host='localhost', port='5432', database='menu_db')
  5.  
  6. set_sql_debug(False)
  7.  
  8.  
  9. class Menu(db.Entity):
  10. id = PrimaryKey(int, auto=True)
  11. name = Required(str)
  12. menu = Optional('Menu', reverse='menus')
  13. menus = Set('Menu', reverse='menu')
  14. commands = Set('Command')
  15.  
  16.  
  17. class Command(db.Entity):
  18. id = PrimaryKey(int, auto=True)
  19. name = Required(str)
  20. text = Required(str)
  21. menu = Optional(Menu)
  22.  
  23.  
  24. db.generate_mapping(create_tables=True)
  25.  
  26. with db_session:
  27.  
  28. # menu_1 = Menu(name='menu_1')
  29. # command_1 = Command(name='command_1', text='command_1 called', menu=menu_1)
  30. # command_2 = Command(name='command_2', text='command_2 called', menu=menu_1)
  31. #
  32. # menu_2 = Menu(name='menu_2', menu=menu_1)
  33. # command_3 = Command(name='command_3', text='command_3 called', menu=menu_2)
  34.  
  35. current_menu = None
  36. action_name = None
  37.  
  38. while True:
  39.  
  40. for menu in Menu.select(lambda m: m.menu == current_menu):
  41. print('> ' + menu.name)
  42.  
  43. for command in Command.select(lambda c: c.menu == current_menu):
  44. print('* ' + command.name)
  45.  
  46. if current_menu:
  47. print('* back')
  48.  
  49. action_name = input()
  50.  
  51. if not action_name.strip():
  52. print('please type action')
  53. continue
  54.  
  55. if current_menu and action_name == 'back':
  56. current_menu = current_menu.menu
  57. continue
  58.  
  59. menu = Menu.get(menu=current_menu, name=action_name)
  60.  
  61. if menu:
  62. current_menu = menu
  63. else:
  64. command = Command.get(menu=current_menu, name=action_name)
  65. print(command.text if command else 'action not found')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement