price_check.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/python3
  2. #
  3. # Copyright (c) Daniel Sheffield 2021 - 2023
  4. #
  5. # All rights reserved
  6. #
  7. # THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
  8. from psycopg import Cursor
  9. from urwid import raw_display, WidgetPlaceholder, SolidFill, MainLoop
  10. from app.activities.ActivityManager import ActivityManager, show_or_exit
  11. from app.activities.PriceCheck import PriceCheck
  12. from app.data.QueryManager import QueryManager, display_mapper
  13. from app.palette import high_contrast
  14. try:
  15. from db_credentials import HOST, PASSWORD
  16. host = f'host={HOST}'
  17. password = f'password={PASSWORD}'
  18. except:
  19. host = ''
  20. password = ''
  21. try:
  22. import os
  23. import psycopg
  24. user = os.getenv('USER')
  25. conn = psycopg.connect(f"{host} dbname=grocery user={user} {password}")
  26. cur: Cursor = conn.cursor()
  27. except:
  28. print('Failed to set up db connection. Entering Mock mode')
  29. exit(1)
  30. #from mock import *
  31. class GroceryPriceCheck(WidgetPlaceholder):
  32. def __init__(self, activity_manager):
  33. super().__init__(SolidFill(u'/'))
  34. self.activity_manager = activity_manager
  35. price_check = self.activity_manager.get('price_check')
  36. self.activity_manager.show(self)
  37. self.activity_manager.show(price_check)
  38. cur.execute("BEGIN")
  39. activity_manager = ActivityManager()
  40. query_manager = QueryManager(cur, display_mapper)
  41. activity_manager.create(PriceCheck, 'price_check',
  42. activity_manager, query_manager
  43. )
  44. app = GroceryPriceCheck(activity_manager)
  45. def iter_palettes():
  46. palettes = [ v for k,v in high_contrast.theme.items() ]
  47. while True:
  48. p = palettes.pop(0)
  49. palettes.append(p)
  50. yield p
  51. palettes = iter_palettes()
  52. screen = raw_display.Screen()
  53. loop = MainLoop(app, next(palettes), screen=screen,
  54. unhandled_input=lambda k: show_or_exit(k, screen=screen, palettes=palettes),
  55. pop_ups=True)
  56. loop.run()
  57. cur.close()
  58. conn.close()