1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- import os
- import json
-
- import aiofiles
-
- DATE_FORMAT = "%Y-%m-%d"
-
- def get_datadir():
- return os.getenv('SB_DATADIR')
-
- def get_date_report_path(date):
- dstr = date.strftime(DATE_FORMAT)
- return os.path.join(get_datadir(), 'days', dstr + '.json')
-
- def load_date_report(date):
- path = get_date_report_path(date)
- if not os.path.exists(path):
- return []
-
- with open(path, 'r') as f:
- return json.load(f)
-
- async def load_date_report_async(date):
- path = get_date_report_path(date)
- if not os.path.exists(path):
- return []
-
- async with aiofiles.open(path, 'r') as f:
- return json.loads(await f.read())
-
- def save_date_report(date, data):
- path = get_date_report_path(date)
- os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)
- with open(path, 'w') as f:
- json.dump(data, f)
-
- async def save_date_report_async(date, data):
- path = get_date_report_path(date)
- os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)
- async with aiofiles.open(path, 'w') as f:
- await f.write(json.dumps(data))
-
- def get_date_flags_path(date):
- dstr = date.strftime(DATE_FORMAT)
- return os.path.join(get_datadir(), 'flags', dstr + '.json')
-
- async def load_date_flags_async(date):
- path = get_date_flags_path(date)
- if not os.path.exists(path):
- return []
-
- async with aiofiles.open(path, 'r') as f:
- return json.loads(await f.read())
-
- async def save_date_flags_async(date, data):
- path = get_date_flags_path(date)
- os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)
- async with aiofiles.open(path, 'w') as f:
- await f.write(json.dumps(data))
|