Skip to content

Commit

Permalink
Applied black result
Browse files Browse the repository at this point in the history
  • Loading branch information
eunwoo1104 committed Dec 31, 2021
1 parent 4a260a2 commit 7ca7175
Show file tree
Hide file tree
Showing 39 changed files with 5,081 additions and 2,061 deletions.
1 change: 1 addition & 0 deletions dico/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .http import AsyncHTTPRequest, HTTPRequest
from .model import *
from .voice import *

"""
from .model.extras import File
from .model.channel import *
Expand Down
2,283 changes: 1,536 additions & 747 deletions dico/api.py

Large diffs are not rendered by default.

1,436 changes: 1,026 additions & 410 deletions dico/base/http.py

Large diffs are not rendered by default.

33 changes: 25 additions & 8 deletions dico/base/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ def create(cls, client, resp: dict):


class DiscordObjectBase(CopyableObject):
TYPING = typing.Union[int, str, Snowflake, "DiscordObjectBase", typing.Type["DiscordObjectBase"]]
TYPING = typing.Union[
int, str, Snowflake, "DiscordObjectBase", typing.Type["DiscordObjectBase"]
]
RESPONSE = typing.Union["DiscordObjectBase", typing.Awaitable["DiscordObjectBase"]]
RESPONSE_AS_LIST = typing.Union[typing.List["DiscordObjectBase"], typing.Awaitable[typing.List["DiscordObjectBase"]]]
RESPONSE_AS_LIST = typing.Union[
typing.List["DiscordObjectBase"],
typing.Awaitable[typing.List["DiscordObjectBase"]],
]
_cache_type = None

def __init__(self, client: "APIClient", resp: dict, **kwargs: typing.Any):
Expand Down Expand Up @@ -59,7 +64,9 @@ def update(self, new_resp: dict, **kwargs: typing.Any):
def create(cls, client: "APIClient", resp: dict, **kwargs: typing.Any):
ensure_cache_type = kwargs.pop("ensure_cache_type", cls._cache_type)
prevent_caching = kwargs.pop("prevent_caching", False)
maybe_exist = client.has_cache and client.cache.get(resp["id"], ensure_cache_type)
maybe_exist = client.has_cache and client.cache.get(
resp["id"], ensure_cache_type
)
if maybe_exist:
if prevent_caching:
maybe_exist = maybe_exist.copy()
Expand All @@ -77,13 +84,17 @@ def create(cls, client: "APIClient", resp: dict, **kwargs: typing.Any):
if client.has_cache and not prevent_caching:
client.cache.add(ret.id, ret._cache_type, ret)
if hasattr(ret, "guild_id") and ret.guild_id:
client.cache.get_guild_container(ret.guild_id).add(ret.id, ret._cache_type, ret)
client.cache.get_guild_container(ret.guild_id).add(
ret.id, ret._cache_type, ret
)
return ret


class AbstractObject(dict):
RESPONSE = typing.Union["AbstractObject", typing.Awaitable["AbstractObject"]]
RESPONSE_AS_LIST = typing.Union[typing.List["AbstractObject"], typing.Awaitable[typing.List["AbstractObject"]]]
RESPONSE_AS_LIST = typing.Union[
typing.List["AbstractObject"], typing.Awaitable[typing.List["AbstractObject"]]
]

def __init__(self, resp: dict):
super().__init__(**resp)
Expand All @@ -97,7 +108,9 @@ def __setattr__(self, key, value):

class FlagBase:
def __init__(self, *args: str, **kwargs: bool):
self.values: typing.Dict[str, int] = {x: getattr(self, x) for x in dir(self) if isinstance(getattr(self, x), int)}
self.values: typing.Dict[str, int] = {
x: getattr(self, x) for x in dir(self) if isinstance(getattr(self, x), int)
}
self.value: int = 0
for x in args:
if x.upper() not in self.values:
Expand Down Expand Up @@ -155,7 +168,9 @@ def from_value(cls, value: int):

class TypeBase:
def __init__(self, value):
self.values: typing.Dict[int, str] = {getattr(self, x): x for x in dir(self) if isinstance(getattr(self, x), int)}
self.values: typing.Dict[int, str] = {
getattr(self, x): x for x in dir(self) if isinstance(getattr(self, x), int)
}
self.value: int = value

if self.value not in self.values:
Expand All @@ -180,5 +195,7 @@ def is_type(self, name: str) -> bool:

@classmethod
def to_string(cls, value: int) -> str:
values = {getattr(cls, x): x for x in dir(cls) if isinstance(getattr(cls, x), int)}
values = {
getattr(cls, x): x for x in dir(cls) if isinstance(getattr(cls, x), int)
}
return values.get(value)
48 changes: 39 additions & 9 deletions dico/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,22 @@
class CacheContainer:
def __init__(self, default_expiration_time=None, **max_sizes):
self.default_expiration_time = default_expiration_time
self.__cache_dict: typing.Dict[str, typing.Union[dict, CacheStorage]] = {"guild_cache": {}}
self.__cache_dict: typing.Dict[str, typing.Union[dict, CacheStorage]] = {
"guild_cache": {}
}
self.max_sizes = max_sizes

def get(self, snowflake_id: typing.Union[str, int, Snowflake], storage_type: str = None, *, ignore_expiration=True):
def get(
self,
snowflake_id: typing.Union[str, int, Snowflake],
storage_type: str = None,
*,
ignore_expiration=True
):
if storage_type:
return self.get_storage(storage_type).get(snowflake_id, ignore_expiration=ignore_expiration)
return self.get_storage(storage_type).get(
snowflake_id, ignore_expiration=ignore_expiration
)
for x in self.__cache_dict.values():
if isinstance(x, dict):
continue
Expand All @@ -22,23 +32,41 @@ def get_storage(self, storage_type: str):
if storage_type == "guild_cache":
return self.__cache_dict["guild_cache"]
if storage_type not in self.__cache_dict:
self.__cache_dict[storage_type] = CacheStorage(max_size=self.max_sizes.get(storage_type, 0), root_remove=self.remove, cache_type=storage_type)
self.__cache_dict[storage_type] = CacheStorage(
max_size=self.max_sizes.get(storage_type, 0),
root_remove=self.remove,
cache_type=storage_type,
)
return self.__cache_dict.get(storage_type)

def get_guild_container(self, guild_id: typing.Union[str, int, Snowflake]):
guild_caches = self.get_storage("guild_cache")
guild_id = Snowflake.ensure_snowflake(guild_id)
if guild_id not in guild_caches:
guild_caches[guild_id] = GuildCacheContainer(default_expiration_time=self.default_expiration_time)
guild_caches[guild_id] = GuildCacheContainer(
default_expiration_time=self.default_expiration_time
)
return guild_caches[guild_id]

def add(self, snowflake_id: typing.Union[str, int, Snowflake], obj_type: str, obj, expire_at=None):
def add(
self,
snowflake_id: typing.Union[str, int, Snowflake],
obj_type: str,
obj,
expire_at=None,
):
if not expire_at:
expire_at = self.default_expiration_time

if obj_type not in self.__cache_dict:
self.__cache_dict[obj_type] = CacheStorage(max_size=self.max_sizes.get(obj_type, 0), root_remove=self.remove, cache_type=obj_type)
self.__cache_dict[obj_type].add(Snowflake.ensure_snowflake(snowflake_id), obj, expire_at)
self.__cache_dict[obj_type] = CacheStorage(
max_size=self.max_sizes.get(obj_type, 0),
root_remove=self.remove,
cache_type=obj_type,
)
self.__cache_dict[obj_type].add(
Snowflake.ensure_snowflake(snowflake_id), obj, expire_at
)

def remove(self, snowflake_id: typing.Union[str, int, Snowflake], obj_type: str):
if obj_type in self.__cache_dict:
Expand Down Expand Up @@ -92,7 +120,9 @@ def __iter__(self):
for x in self.__cache_dict.values():
yield x

def get(self, snowflake_id: typing.Union[str, int, Snowflake], *, ignore_expiration=True):
def get(
self, snowflake_id: typing.Union[str, int, Snowflake], *, ignore_expiration=True
):
res = self.__cache_dict.get(Snowflake.ensure_snowflake(snowflake_id))
if res: # TODO: add expiration time check
return res["value"]
Expand Down
Loading

0 comments on commit 7ca7175

Please sign in to comment.