You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

inventory.py 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import os
  2. import json
  3. import aiofiles
  4. DATE_FORMAT = "%Y-%m-%d"
  5. def get_datadir():
  6. return os.getenv('SB_DATADIR')
  7. def get_date_report_path(date):
  8. dstr = date.strftime(DATE_FORMAT)
  9. return os.path.join(get_datadir(), 'days', dstr + '.json')
  10. def load_date_report(date):
  11. path = get_date_report_path(date)
  12. if not os.path.exists(path):
  13. return []
  14. with open(path, 'r') as f:
  15. return json.load(f)
  16. async def load_date_report_async(date):
  17. path = get_date_report_path(date)
  18. if not os.path.exists(path):
  19. return []
  20. async with aiofiles.open(path, 'r') as f:
  21. return json.loads(await f.read())
  22. def save_date_report(date, data):
  23. path = get_date_report_path(date)
  24. os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)
  25. with open(path, 'w') as f:
  26. json.dump(data, f)
  27. async def save_date_report_async(date, data):
  28. path = get_date_report_path(date)
  29. os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)
  30. async with aiofiles.open(path, 'w') as f:
  31. await f.write(json.dumps(data))
  32. def get_date_flags_path(date):
  33. dstr = date.strftime(DATE_FORMAT)
  34. return os.path.join(get_datadir(), 'flags', dstr + '.json')
  35. async def load_date_flags_async(date):
  36. path = get_date_flags_path(date)
  37. if not os.path.exists(path):
  38. return []
  39. async with aiofiles.open(path, 'r') as f:
  40. return json.loads(await f.read())
  41. async def save_date_flags_async(date, data):
  42. path = get_date_flags_path(date)
  43. os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)
  44. async with aiofiles.open(path, 'w') as f:
  45. await f.write(json.dumps(data))