BREAKING CHANGE: cache.

This commit is contained in:
grey-cat-1908 2022-05-29 13:39:56 +03:00
parent f17402856c
commit 17df39d574
8 changed files with 55 additions and 53 deletions

View file

@ -5,12 +5,12 @@ from .client import Client, Bot
from .models import *
from .exceptions import *
from .rest import RESTApp
from .cache import CacheManager, AutoCacheModels
from .cache import CacheManager, ChannelsCachingPolicy
__package__ = "melisa"
__title__ = "Melisa"
__description__ = "Cache-optimized Discord microframework for Python 3"
__author__ = "MelisaDev"
__license__ = "MIT"
__version__ = '0.0.1.dev1'
__version__ = '0.0.1.dev2'

View file

@ -12,18 +12,12 @@ from .models.guild.channel import ChannelType, Channel
from .utils.snowflake import Snowflake
class AutoCacheModels(Enum):
# ToDo: Add FULL_GUILD auto cache model
""" """
FULL_GUILDS = "FULL_GUILDS"
GUILD_ROLES = "GUILD_ROLES"
GUILD_THREADS = "GUILD_THREADS"
GUILD_EMOJIS = "GUILD_EMOJIS"
GUILD_WEBHOOKS = "GUILD_WEBHOOKS"
GUILD_MEMBERS = "GUILD_MEMBERS"
TEXT_CHANNELS = "TEXT_CHANNELS"
class ChannelsCachingPolicy(Enum):
""""Channels caching policy"""
ALL = "all"
NONE = "none"
GUILD_TEXT = 0
GUILD_VOICE = 2
class CacheManager:
@ -33,12 +27,9 @@ class CacheManager:
self,
*,
disabled: bool = False,
disabled_auto_models: Optional[List[AutoCacheModels]] = None,
policies: Dict[str, List[ChannelsCachingPolicy]] = None,
auto_unused_attributes: Optional[Dict[Any, List[str]]] = None,
):
self._disabled_auto_models: List[AutoCacheModels] = (
[] if disabled_auto_models is None else disabled_auto_models
)
self.auto_unused_attributes: Dict[Any, List[str]] = (
{} if auto_unused_attributes is not None else auto_unused_attributes
)
@ -49,6 +40,16 @@ class CacheManager:
self._disabled = disabled
# Some default values
if policies is None:
policies = {
"channels": [
ChannelsCachingPolicy.GUILD_TEXT
]
}
self._policies = policies
# We use symlinks to cache guild channels
# like we save channel in Guild and save it here
# and if you need channel, and you don't know its guild
@ -103,12 +104,16 @@ class CacheManager:
guild = self.__remove_unused_attributes(guild, Guild)
if hasattr(guild, "channels"):
policy = self._policies["channels"]
if hasattr(guild, "channels") and ChannelsCachingPolicy.NONE not in policy:
channels = guild.channels.values()
if AutoCacheModels.TEXT_CHANNELS not in self._disabled_auto_models:
if ChannelsCachingPolicy.ALL not in policy:
policy = [int(x.value) for x in policy]
channels = filter(
lambda channel: channel.type == ChannelType.GUILD_TEXT, channels
lambda channel: channel.type is not None and int(channel.type) in policy, channels
)
for sym in channels:
@ -117,6 +122,9 @@ class CacheManager:
self._channel_symlinks.pop(sym_id)
self._channel_symlinks[sym_id] = guild.id
else:
if hasattr(guild, "channels"):
guild.channels = {}
self._raw_guilds.update({guild.id: guild})

View file

@ -538,7 +538,7 @@ class MessageableChannel(Channel):
Some of specified parameters is invalid.
"""
return self._client.rest.create_message(
return await self._client.rest.create_message(
self.id,
content,
tts=tts,

View file

@ -101,7 +101,7 @@ class GuildMember(APIModelBase):
)
self.nick = data.get("nick")
self.guild_avatar = data.get("avatar")
self.role_ids = [Snowflake(x) for x in data["roles"]]
self.role_ids = [Snowflake(x) for x in data.get("roles", [])]
self.joined_at = (
Timestamp.parse(data["joined_at"])
if data.get("joined_at") is not None

3
pyproject.toml Normal file
View file

@ -0,0 +1,3 @@
[build-system]
requires = ["wheel", "setuptools"]
build-backend = "setuptools.build_meta"

View file

@ -1,49 +1,40 @@
import pathlib
import re
import setuptools
from setuptools import setup
HERE = pathlib.Path(__file__).parent
README = (HERE / "README.md").read_text(encoding="utf8")
def long_description():
with open("README.md") as fp:
return fp.read()
def parse_requirements_file(path):
with open(path, encoding='utf-8') as file:
return file.read().splitlines()
with open('melisa/__init__.py', encoding='utf-8') as file:
with open(HERE / "melisa/__init__.py") as file:
version = re.search(r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]', file.read(), re.MULTILINE).group(1)
packages = [
'melisa',
'melisa.listeners',
'melisa.models',
'melisa.models.app',
'melisa.models.guild',
'melisa.models.message',
'melisa.models.user',
'melisa.utils',
'melisa.core'
]
setup(
setuptools.setup(
name='melisa',
author='MelisaDev',
url='https://github.com/MelisaDev/melisa',
version=version,
packages=packages,
packages=setuptools.find_packages(),
license='MIT',
description='Cache-optimized Discord microframework for Python 3',
long_description=long_description(),
long_description=README,
long_description_content_type="text/markdown",
include_package_data=True,
python_requires='>=3.8,<3.11',
install_requires=parse_requirements_file("requirements.txt"),
zip_safe=False,
install_requires=[
"aiohttp", "typing_extensions"
],
extras_require={
"speedup": parse_requirements_file("packages/speed.txt")
"speedup": [
"orjson==3.6.8"
]
},
test_suite="tests",
project_urls={
"Documentation": "https://docs.melisapy.site/",
"Source (GitHub)": "https://github.com/MelisaDev/melisa",
"Discord": "https://discord.gg/QX4EG8f7aD",
},
classifiers=[
"License :: OSI Approved :: MIT License",