Utilities and JSON Database support #51

Closed
ghost wants to merge 6 commits from 2.0 into 2.0
4 changed files with 97 additions and 3 deletions
Showing only changes of commit 7ca4e650fb - Show all commits

View File

@@ -68,7 +68,6 @@ class VersionInfo(NamedTuple):
releaselevel: Literal["alpha", "beta", "candidate", "final"]
serial: int
version_info: VersionInfo = VersionInfo(major=2, minor=0, micro=0, releaselevel='alpha', serial=0)
logging.getLogger(__name__).addHandler(logging.NullHandler())

View File

@@ -208,7 +208,7 @@ class Cog(metaclass=CogMeta):
for command in self.__cog_commands__:
setattr(self, command.callback.__name__, command)
parent = command.parent
if parent is not None:
if parent:
# Get the latest parent reference
parent = lookup[parent.qualified_name] # type: ignore
@@ -230,7 +230,7 @@ class Cog(metaclass=CogMeta):
This does not include subcommands.
"""
return [c for c in self.__cog_commands__ if c.parent is None]
return [c for c in self.__cog_commands__ if not c.parent]
@property
def qualified_name(self) -> str:

View File

95
discord/ext/json/types.py Normal file
View File

@@ -0,0 +1,95 @@
import json
from typing import Union, Any
class Serializer:
def bind_file(self, file:str):
"""Bind file to be serialized(loaded or dumped)"""
self.file = file
async def load(self, content:str):
raise NotImplementedError("Serializer has to be implemented!")
async def dump(self):
raise NotImplementedError("Serializer hasn't been implemented")
class File:
def __init__(self, path:str):
"""
Initialize a file.
=======
path `str` :
path to the file
"""
self.path : str = path
@staticmethod
def construct(path:str):
"""
Construct a File object statically
"""
if not isinstance(path, str):
raise TypeError(f"Expected path to be type str, got {type(path)} instead.")
return File(path)
async def access(self, mode:str):
"""
Open object to access file. Don't use this in your code.
"""
if not isinstance(mode, str):
raise TypeError(f"Expected mode to be type str, got {type(mode)} instead.")
return open(self.path, mode)
async def open_writer(self):
"""Open plain writer for file"""
return await self.access("w+")
async def open_reader(self):
"""Open plain reader for file"""
return await self.access("r+")
async def open_binary_writer(self):
"""Open binary writer for file"""
return await self.access("wb+")
async def open_binary_reader(self):
"""Open binary reader for file"""
return await self.access("rb+")
async def read_contents(self, binary=False) -> Union[str, bytes]:
"""
Dump out file contents
======
binary `bool` (optional):
Set to False normally. Controls whether to read file as binary or read as normal text.
"""
reader = None # initialize
if binary:
reader = await self.open_binary_reader()
else:
reader = await self.open_reader()
contents = reader.read()
reader.close() # safely close file
return contents
async def serialize(self, serializer:Serializer) -> Union[Any]: