mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 03:08:55 -05:00
Ya'll are gonna hate me. * Initial modifications * Add initial setup.py * working setup py help * Modify setup file to package stuff * Move a bunch of shit and fix imports * Fix or skip tests * Must add init files for find_packages to work * Move main to scripts folder and rename * Add shebangs * Copy over translation files * WORKING PIP INSTALL * add dependency information * Hardcoded version for now, will need to figure out a better way to do this * OKAY ITS FINALLY FUCKING WORKING * Add this guy * Fix stuff * Change readme to rst * Remove double sentry opt in * Oopsie * Fix this thing * Aaaand fix test * Aaaand fix test * Fix core cog importing and default cog install path * Adjust readme * change instance name from optional to required * Ayyy let's do more dependency injection
50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
from pathlib import Path
|
|
from typing import Tuple
|
|
|
|
from ..json_io import JsonIO
|
|
|
|
from .red_base import BaseDriver
|
|
|
|
|
|
class JSON(BaseDriver):
|
|
def __init__(self, cog_name, *, data_path_override: Path=None,
|
|
file_name_override: str="settings.json"):
|
|
super().__init__()
|
|
self.cog_name = cog_name
|
|
self.file_name = file_name_override
|
|
if data_path_override:
|
|
self.data_path = data_path_override
|
|
else:
|
|
self.data_path = Path.cwd() / 'cogs' / '.data' / self.cog_name
|
|
|
|
self.data_path.mkdir(parents=True, exist_ok=True)
|
|
|
|
self.data_path = self.data_path / self.file_name
|
|
|
|
self.jsonIO = JsonIO(self.data_path)
|
|
|
|
try:
|
|
self.data = self.jsonIO._load_json()
|
|
except FileNotFoundError:
|
|
self.data = {}
|
|
self.jsonIO._save_json(self.data)
|
|
|
|
def get_driver(self):
|
|
return self
|
|
|
|
async def get(self, identifiers: Tuple[str]):
|
|
partial = self.data
|
|
for i in identifiers:
|
|
partial = partial[i]
|
|
return partial
|
|
|
|
async def set(self, identifiers, value):
|
|
partial = self.data
|
|
for i in identifiers[:-1]:
|
|
if i not in partial:
|
|
partial[i] = {}
|
|
partial = partial[i]
|
|
|
|
partial[identifiers[-1]] = value
|
|
await self.jsonIO._threadsafe_save_json(self.data)
|