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.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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_article_path(slug):
  8. return os.path.join(get_datadir(), 'articles', slug + '.json')
  9. def load_article_by_slug(slug):
  10. with open(get_article_path(slug), 'r') as f:
  11. return json.load(f)
  12. def save_article(data):
  13. with open(get_article_path(data['slug']), 'r') as f:
  14. return json.save(data, f)
  15. def get_date_report_path(date):
  16. dstr = date.strftime(DATE_FORMAT)
  17. return os.path.join(get_datadir(), 'days', dstr + '.json')
  18. def load_date_report(date):
  19. path = get_date_report_path(date)
  20. if not os.path.exists(path):
  21. return []
  22. with open(path, 'r') as f:
  23. return json.load(f)
  24. async def load_date_report_async(date):
  25. path = get_date_report_path(date)
  26. if not os.path.exists(path):
  27. return []
  28. async with aiofiles.open(path, 'r') as f:
  29. return json.loads(await f.read())
  30. def save_date_report(date, data):
  31. path = get_date_report_path(date)
  32. os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)
  33. with open(path, 'w') as f:
  34. json.dump(data, f)
  35. async def save_date_report_async(date, data):
  36. path = get_date_report_path(date)
  37. os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)
  38. async with aiofiles.open(path, 'w') as f:
  39. await f.write(json.dumps(data))