From 988965ed52b3b5f980e17f002d6aa767ae047651 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 11 Sep 2022 21:57:27 +0200 Subject: [PATCH 1/5] Improved cpl-query & added first types from discord --- src/cpl_discord/container/__init__.py | 1 + src/cpl_discord/container/category_channel.py | 11 +++++ src/cpl_discord/container/container.py | 30 +++++++++++++ src/cpl_discord/container/guild.py | 42 +++++++++++++++++++ src/cpl_discord/container/member.py | 11 +++++ src/cpl_discord/container/role.py | 16 +++++++ src/cpl_discord/container/text_channel.py | 11 +++++ src/cpl_discord/container/voice_channel.py | 11 +++++ .../helper/ToContainersConverter.py | 13 ++++++ src/cpl_discord/helper/__init__.py | 1 + .../service/discord_bot_service.py | 8 ++++ .../service/discord_bot_service_abc.py | 10 ++++- src/cpl_query/_query/select.py | 19 +++++++++ src/cpl_query/extension/iterable.py | 7 ++++ .../src/modules/hello_world/on_ready_event.py | 23 +++++++++- 15 files changed, 211 insertions(+), 3 deletions(-) create mode 100644 src/cpl_discord/container/__init__.py create mode 100644 src/cpl_discord/container/category_channel.py create mode 100644 src/cpl_discord/container/container.py create mode 100644 src/cpl_discord/container/guild.py create mode 100644 src/cpl_discord/container/member.py create mode 100644 src/cpl_discord/container/role.py create mode 100644 src/cpl_discord/container/text_channel.py create mode 100644 src/cpl_discord/container/voice_channel.py create mode 100644 src/cpl_discord/helper/ToContainersConverter.py create mode 100644 src/cpl_discord/helper/__init__.py create mode 100644 src/cpl_query/_query/select.py diff --git a/src/cpl_discord/container/__init__.py b/src/cpl_discord/container/__init__.py new file mode 100644 index 00000000..425ab6c1 --- /dev/null +++ b/src/cpl_discord/container/__init__.py @@ -0,0 +1 @@ +# imports diff --git a/src/cpl_discord/container/category_channel.py b/src/cpl_discord/container/category_channel.py new file mode 100644 index 00000000..3c9ad3cf --- /dev/null +++ b/src/cpl_discord/container/category_channel.py @@ -0,0 +1,11 @@ +import discord + +from cpl_discord.container.container import Container +from cpl_discord.helper.ToContainersConverter import ToContainersConverter +from cpl_query.extension import List + + +class CategoryChannel(discord.CategoryChannel, Container): + + def __init__(self, _t: discord.CategoryChannel): + Container.__init__(self, _t, CategoryChannel) diff --git a/src/cpl_discord/container/container.py b/src/cpl_discord/container/container.py new file mode 100644 index 00000000..a32479d6 --- /dev/null +++ b/src/cpl_discord/container/container.py @@ -0,0 +1,30 @@ +from typing import Callable + + +class Container: + + def __init__(self, _o: object, _t: type): + self._object = _o + self._type = _t + + def __to_type(_f: Callable, _t: type): + def wrapper(*args, **kwargs): + result = _f(*args, **kwargs) + return _t(result) + + return wrapper + + def __getitem__(self, item): + result = self._object[item] + if isinstance(result, type(self._guild)): + result = self._type(result) + return result + + def __getattr__(self, item): + result = getattr(self._object, item) + if callable(result): + result = self.__to_type(result, self._type) + return result + + def __repr__(self): + return repr(self._object) diff --git a/src/cpl_discord/container/guild.py b/src/cpl_discord/container/guild.py new file mode 100644 index 00000000..b69af4ed --- /dev/null +++ b/src/cpl_discord/container/guild.py @@ -0,0 +1,42 @@ +import discord + +from cpl_discord.container.category_channel import CategoryChannel +from cpl_discord.container.container import Container +from cpl_discord.container.member import Member +from cpl_discord.container.role import Role +from cpl_discord.container.text_channel import TextChannel +from cpl_discord.container.voice_channel import VoiceChannel +from cpl_discord.helper.ToContainersConverter import ToContainersConverter +from cpl_query.extension import List + + +class Guild(discord.Guild, Container): + + def __init__(self, _t: discord.Guild): + self._object: discord.Guild = _t + + Container.__init__(self, _t, Guild) + + @property + def categories(self) -> List[CategoryChannel]: + return List(CategoryChannel, ToContainersConverter.convert(self._object.categories, CategoryChannel)) + + @property + def members(self) -> List[Member]: + return List(Member, ToContainersConverter.convert(self._object.members, Member)) + + @property + def roles(self) -> List[Role]: + return List(Role, ToContainersConverter.convert(self._object.roles, Role)) + + @property + def text_channels(self) -> List[TextChannel]: + return List(TextChannel, ToContainersConverter.convert(self._object.text_channels, TextChannel)) + + @property + def threads(self) -> List[TextChannel]: + return List(TextChannel, ToContainersConverter.convert(self._object.threads, TextChannel)) + + @property + def voice_channels(self) -> List[VoiceChannel]: + return List(VoiceChannel, ToContainersConverter.convert(self._object.voice_channels, VoiceChannel)) diff --git a/src/cpl_discord/container/member.py b/src/cpl_discord/container/member.py new file mode 100644 index 00000000..0fe775f3 --- /dev/null +++ b/src/cpl_discord/container/member.py @@ -0,0 +1,11 @@ +import discord + +from cpl_discord.container.container import Container +from cpl_discord.helper.ToContainersConverter import ToContainersConverter +from cpl_query.extension import List + + +class Member(discord.Member, Container): + + def __init__(self, _t: discord.Member): + Container.__init__(self, _t, Member) diff --git a/src/cpl_discord/container/role.py b/src/cpl_discord/container/role.py new file mode 100644 index 00000000..27251ef0 --- /dev/null +++ b/src/cpl_discord/container/role.py @@ -0,0 +1,16 @@ +import discord + +from cpl_discord.container.container import Container +from cpl_discord.container.member import Member +from cpl_discord.helper.ToContainersConverter import ToContainersConverter +from cpl_query.extension import List + + +class Role(discord.Role, Container): + + def __init__(self, _t: discord.Role): + Container.__init__(self, _t, Role) + + @property + def members(self) -> List[discord.Member]: + return List(discord.Member, ToContainersConverter.convert(self._object.members, Member)) diff --git a/src/cpl_discord/container/text_channel.py b/src/cpl_discord/container/text_channel.py new file mode 100644 index 00000000..1051a3a5 --- /dev/null +++ b/src/cpl_discord/container/text_channel.py @@ -0,0 +1,11 @@ +import discord + +from cpl_discord.container.container import Container +from cpl_discord.helper.ToContainersConverter import ToContainersConverter +from cpl_query.extension import List + + +class TextChannel(discord.TextChannel, Container): + + def __init__(self, _t: discord.TextChannel): + Container.__init__(self, _t, TextChannel) diff --git a/src/cpl_discord/container/voice_channel.py b/src/cpl_discord/container/voice_channel.py new file mode 100644 index 00000000..ccc5ac4f --- /dev/null +++ b/src/cpl_discord/container/voice_channel.py @@ -0,0 +1,11 @@ +import discord + +from cpl_discord.container.container import Container +from cpl_discord.helper.ToContainersConverter import ToContainersConverter +from cpl_query.extension import List + + +class VoiceChannel(discord.VoiceChannel, Container): + + def __init__(self, _t: discord.VoiceChannel): + Container.__init__(self, _t, VoiceChannel) diff --git a/src/cpl_discord/helper/ToContainersConverter.py b/src/cpl_discord/helper/ToContainersConverter.py new file mode 100644 index 00000000..70d8bfec --- /dev/null +++ b/src/cpl_discord/helper/ToContainersConverter.py @@ -0,0 +1,13 @@ +from typing import Union, Sequence + +from cpl_discord.container.container import Container + + +class ToContainersConverter: + + @staticmethod + def convert(_l: Union[list[object], Sequence[object]], _t: type) -> list[Container]: + values = [] + for e in _l: + values.append(_t(e)) + return values diff --git a/src/cpl_discord/helper/__init__.py b/src/cpl_discord/helper/__init__.py new file mode 100644 index 00000000..425ab6c1 --- /dev/null +++ b/src/cpl_discord/helper/__init__.py @@ -0,0 +1 @@ +# imports diff --git a/src/cpl_discord/service/discord_bot_service.py b/src/cpl_discord/service/discord_bot_service.py index 07682187..e2928c5e 100644 --- a/src/cpl_discord/service/discord_bot_service.py +++ b/src/cpl_discord/service/discord_bot_service.py @@ -5,8 +5,11 @@ from cpl_core.console import Console from cpl_core.environment import ApplicationEnvironmentABC from cpl_core.logging import LoggerABC, LoggingSettings, LoggingLevelEnum from cpl_discord.configuration.discord_bot_settings import DiscordBotSettings +from cpl_discord.container.guild import Guild +from cpl_discord.helper.ToContainersConverter import ToContainersConverter from cpl_discord.service.discord_bot_service_abc import DiscordBotServiceABC from cpl_discord.service.discord_service_abc import DiscordServiceABC +from cpl_query.extension.list import List class DiscordBotService(DiscordBotServiceABC): @@ -32,6 +35,7 @@ class DiscordBotService(DiscordBotServiceABC): # setup super DiscordBotServiceABC.__init__(self, command_prefix=self._discord_settings.prefix, help_command=None, intents=discord.Intents().all()) + self._base = super(DiscordBotServiceABC, self) @staticmethod def _is_string_invalid(x): @@ -77,3 +81,7 @@ class DiscordBotService(DiscordBotServiceABC): self._logger.debug(__name__, f'Finished syncing commands') await self._discord_service.on_ready() + + @property + def guilds(self) -> List[Guild]: + return List(Guild, ToContainersConverter.convert(self._base.guilds, Guild)) diff --git a/src/cpl_discord/service/discord_bot_service_abc.py b/src/cpl_discord/service/discord_bot_service_abc.py index 1f54f21e..b96a5ee8 100644 --- a/src/cpl_discord/service/discord_bot_service_abc.py +++ b/src/cpl_discord/service/discord_bot_service_abc.py @@ -1,8 +1,10 @@ -from abc import ABC, abstractmethod +from abc import abstractmethod -import discord from discord.ext import commands +from cpl_discord.container.guild import Guild +from cpl_query.extension.list import List + class DiscordBotServiceABC(commands.Bot): @@ -17,3 +19,7 @@ class DiscordBotServiceABC(commands.Bot): @abstractmethod async def on_ready(self): pass + + @property + @abstractmethod + def guilds(self) -> List[Guild]: pass diff --git a/src/cpl_query/_query/select.py b/src/cpl_query/_query/select.py new file mode 100644 index 00000000..0fb07ad2 --- /dev/null +++ b/src/cpl_query/_query/select.py @@ -0,0 +1,19 @@ +from typing import Callable + +from cpl_query.extension.iterable_abc import IterableABC + + +def select_query(_list: IterableABC, _f: Callable) -> any: + result = IterableABC() + result.extend(_f(_o) for _o in _list) + return result + + +def select_many_query(_list: IterableABC, _f: Callable) -> any: + result = IterableABC() + # The line below is pain. I don't understand anything of it... + # written on 09.11.2022 by Sven Heidemann + elements = [_a for _o in _list for _a in _f(_o)] + + result.extend(elements) + return result diff --git a/src/cpl_query/extension/iterable.py b/src/cpl_query/extension/iterable.py index ff835382..5c98fde7 100644 --- a/src/cpl_query/extension/iterable.py +++ b/src/cpl_query/extension/iterable.py @@ -14,6 +14,7 @@ from cpl_query._query.for_each import for_each_query from cpl_query._query.max_min import max_query, min_query from cpl_query._query.order_by import order_by_descending_query, order_by_query from cpl_query._query.reverse import reverse_query +from cpl_query._query.select import select_query, select_many_query from cpl_query._query.single import single_or_default_query, single_query from cpl_query._query.skip_take import (skip_last_query, skip_query, take_last_query, take_query) @@ -94,6 +95,12 @@ class Iterable(IterableABC): def single_or_default(self) -> Optional[any]: return single_or_default_query(self) + def select(self, _f: Callable) -> IterableABC: + return self.__to_self(select_query(self, _f)) + + def select_many(self, _f: Callable) -> IterableABC: + return self.__to_self(select_many_query(self, _f)) + def skip(self, index: int) -> IterableABC: return self.__to_self(skip_query(self, index)) diff --git a/src/tests/custom/discord/src/modules/hello_world/on_ready_event.py b/src/tests/custom/discord/src/modules/hello_world/on_ready_event.py index 306ccf98..10d3453c 100644 --- a/src/tests/custom/discord/src/modules/hello_world/on_ready_event.py +++ b/src/tests/custom/discord/src/modules/hello_world/on_ready_event.py @@ -1,12 +1,33 @@ +import discord + from cpl_core.logging import LoggerABC from cpl_discord.events.on_ready_abc import OnReadyABC +from cpl_discord.service.discord_bot_service_abc import DiscordBotServiceABC class OnReadyEvent(OnReadyABC): - def __init__(self, logger: LoggerABC): + def __init__(self, logger: LoggerABC, bot: DiscordBotServiceABC): OnReadyABC.__init__(self) self._logger = logger + self._bot = bot + + def _log(self, _t: str, _o: object, _type: type = None): + self._logger.debug(__name__, f'{_t} {_o} {_type}') async def on_ready(self): self._logger.info(__name__, 'Hello World') + for g in self._bot.guilds: + self._log('-Guild', g, type(g)) + for r in g.roles: + self._log('--Role', r, type(r)) + for rm in r.members: + self._log('---Rolemembers', rm, type(rm)) + + for m in g.members: + self._log('--Member', m, type(m)) + + select = self._bot.guilds.select(lambda guild: (guild.name, guild.id)) + self._logger.warn(__name__, f'Does cpl.query select work? {select}') + select_many = self._bot.guilds.select_many(lambda guild: guild.roles).where(lambda role: role.name == "Tester").first() + self._logger.warn(__name__, f'Does cpl.query select_many work? {select_many}') -- 2.45.2 From dbe1d370462c470db1093d4ca082e2c783582694 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 11 Sep 2022 22:02:10 +0200 Subject: [PATCH 2/5] Build version --- src/cpl_discord/__init__.py | 4 ++-- src/cpl_discord/application/__init__.py | 4 ++-- src/cpl_discord/command/__init__.py | 4 ++-- src/cpl_discord/configuration/__init__.py | 4 ++-- src/cpl_discord/container/__init__.py | 25 +++++++++++++++++++++++ src/cpl_discord/cpl-discord.json | 6 +++--- src/cpl_discord/events/__init__.py | 4 ++-- src/cpl_discord/helper/__init__.py | 25 +++++++++++++++++++++++ src/cpl_discord/service/__init__.py | 4 ++-- src/cpl_query/__init__.py | 4 ++-- src/cpl_query/_query/__init__.py | 4 ++-- src/cpl_query/cpl-query.json | 4 ++-- src/cpl_query/extension/__init__.py | 4 ++-- 13 files changed, 73 insertions(+), 23 deletions(-) diff --git a/src/cpl_discord/__init__.py b/src/cpl_discord/__init__.py index 1d2fa1bc..533a977e 100644 --- a/src/cpl_discord/__init__.py +++ b/src/cpl_discord/__init__.py @@ -15,7 +15,7 @@ __title__ = 'cpl_discord' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2021 - 2022 sh-edraft.de' -__version__ = '2022.10.1' +__version__ = '2022.10.2' from collections import namedtuple @@ -55,4 +55,4 @@ def get_discord_collection(services: 'ServiceCollectionABC') -> 'DiscordCollecti # build-ignore-end VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='2022', minor='10', micro='1') +version_info = VersionInfo(major='2022', minor='10', micro='2') diff --git a/src/cpl_discord/application/__init__.py b/src/cpl_discord/application/__init__.py index 545d8d4a..5d4e0947 100644 --- a/src/cpl_discord/application/__init__.py +++ b/src/cpl_discord/application/__init__.py @@ -15,7 +15,7 @@ __title__ = 'cpl_discord.application' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2021 - 2022 sh-edraft.de' -__version__ = '2022.10.1' +__version__ = '2022.10.2' from collections import namedtuple @@ -24,4 +24,4 @@ from collections import namedtuple from .discord_bot_application_abc import DiscordBotApplicationABC VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='2022', minor='10', micro='1') +version_info = VersionInfo(major='2022', minor='10', micro='2') diff --git a/src/cpl_discord/command/__init__.py b/src/cpl_discord/command/__init__.py index 50b66688..e8a9a8e6 100644 --- a/src/cpl_discord/command/__init__.py +++ b/src/cpl_discord/command/__init__.py @@ -15,7 +15,7 @@ __title__ = 'cpl_discord.command' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2021 - 2022 sh-edraft.de' -__version__ = '2022.10.1' +__version__ = '2022.10.2' from collections import namedtuple @@ -25,4 +25,4 @@ from .discord_command_abc import DiscordCommandABC from .discord_commands_meta import DiscordCogMeta VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='2022', minor='10', micro='1') +version_info = VersionInfo(major='2022', minor='10', micro='2') diff --git a/src/cpl_discord/configuration/__init__.py b/src/cpl_discord/configuration/__init__.py index 661c53de..84b94968 100644 --- a/src/cpl_discord/configuration/__init__.py +++ b/src/cpl_discord/configuration/__init__.py @@ -15,7 +15,7 @@ __title__ = 'cpl_discord.configuration' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2021 - 2022 sh-edraft.de' -__version__ = '2022.10.1' +__version__ = '2022.10.2' from collections import namedtuple @@ -24,4 +24,4 @@ from collections import namedtuple from .discord_bot_settings import DiscordBotSettings VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='2022', minor='10', micro='1') +version_info = VersionInfo(major='2022', minor='10', micro='2') diff --git a/src/cpl_discord/container/__init__.py b/src/cpl_discord/container/__init__.py index 425ab6c1..27c3f929 100644 --- a/src/cpl_discord/container/__init__.py +++ b/src/cpl_discord/container/__init__.py @@ -1 +1,26 @@ +# -*- coding: utf-8 -*- + +""" +cpl-discord sh-edraft Common Python library Discord +~~~~~~~~~~~~~~~~~~~ + +sh-edraft Common Python library link between discord.py and CPL + +:copyright: (c) 2021 - 2022 sh-edraft.de +:license: MIT, see LICENSE for more details. + +""" + +__title__ = 'cpl_discord.container' +__author__ = 'Sven Heidemann' +__license__ = 'MIT' +__copyright__ = 'Copyright (c) 2021 - 2022 sh-edraft.de' +__version__ = '2022.10.2' + +from collections import namedtuple + + # imports + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major='2022', minor='10', micro='2') diff --git a/src/cpl_discord/cpl-discord.json b/src/cpl_discord/cpl-discord.json index d82c1478..fc0b6fee 100644 --- a/src/cpl_discord/cpl-discord.json +++ b/src/cpl_discord/cpl-discord.json @@ -4,7 +4,7 @@ "Version": { "Major": "2022", "Minor": "10", - "Micro": "1" + "Micro": "2" }, "Author": "Sven Heidemann", "AuthorEmail": "sven.heidemann@sh-edraft.de", @@ -16,9 +16,9 @@ "LicenseName": "MIT", "LicenseDescription": "MIT, see LICENSE for more details.", "Dependencies": [ - "cpl-core>=2022.7.0.post2", + "cpl-core>=2022.10.1", "discord.py==2.0.1", - "cpl-query==2022.7.0" + "cpl-query==2022.10.2" ], "DevDependencies": [ "cpl-cli>=2022.7.0.post2" diff --git a/src/cpl_discord/events/__init__.py b/src/cpl_discord/events/__init__.py index 623c7882..fda503ce 100644 --- a/src/cpl_discord/events/__init__.py +++ b/src/cpl_discord/events/__init__.py @@ -15,7 +15,7 @@ __title__ = 'cpl_discord.events' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2021 - 2022 sh-edraft.de' -__version__ = '2022.10.1' +__version__ = '2022.10.2' from collections import namedtuple @@ -69,4 +69,4 @@ from .on_voice_state_update_abc import OnVoiceStateUpdateABC from .on_webhooks_update_abc import OnWebhooksUpdateABC VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='2022', minor='10', micro='1') +version_info = VersionInfo(major='2022', minor='10', micro='2') diff --git a/src/cpl_discord/helper/__init__.py b/src/cpl_discord/helper/__init__.py index 425ab6c1..1e69ed07 100644 --- a/src/cpl_discord/helper/__init__.py +++ b/src/cpl_discord/helper/__init__.py @@ -1 +1,26 @@ +# -*- coding: utf-8 -*- + +""" +cpl-discord sh-edraft Common Python library Discord +~~~~~~~~~~~~~~~~~~~ + +sh-edraft Common Python library link between discord.py and CPL + +:copyright: (c) 2021 - 2022 sh-edraft.de +:license: MIT, see LICENSE for more details. + +""" + +__title__ = 'cpl_discord.helper' +__author__ = 'Sven Heidemann' +__license__ = 'MIT' +__copyright__ = 'Copyright (c) 2021 - 2022 sh-edraft.de' +__version__ = '2022.10.2' + +from collections import namedtuple + + # imports + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major='2022', minor='10', micro='2') diff --git a/src/cpl_discord/service/__init__.py b/src/cpl_discord/service/__init__.py index f0cece4f..03612b5c 100644 --- a/src/cpl_discord/service/__init__.py +++ b/src/cpl_discord/service/__init__.py @@ -15,7 +15,7 @@ __title__ = 'cpl_discord.service' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2021 - 2022 sh-edraft.de' -__version__ = '2022.10.1' +__version__ = '2022.10.2' from collections import namedtuple @@ -29,4 +29,4 @@ from .discord_service import DiscordService from .discord_service_abc import DiscordServiceABC VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='2022', minor='10', micro='1') +version_info = VersionInfo(major='2022', minor='10', micro='2') diff --git a/src/cpl_query/__init__.py b/src/cpl_query/__init__.py index 295e0eac..072f84bc 100644 --- a/src/cpl_query/__init__.py +++ b/src/cpl_query/__init__.py @@ -15,7 +15,7 @@ __title__ = 'cpl_query' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2021 - 2022 sh-edraft.de' -__version__ = '2022.7.0' +__version__ = '2022.10.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports: VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='2022', minor='7', micro='0') +version_info = VersionInfo(major='2022', minor='10', micro='2') diff --git a/src/cpl_query/_query/__init__.py b/src/cpl_query/_query/__init__.py index 55afe7f2..248d4aa3 100644 --- a/src/cpl_query/_query/__init__.py +++ b/src/cpl_query/_query/__init__.py @@ -15,7 +15,7 @@ __title__ = 'cpl_query._query' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2021 - 2022 sh-edraft.de' -__version__ = '2022.7.0' +__version__ = '2022.10.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports: VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='2022', minor='7', micro='0') +version_info = VersionInfo(major='2022', minor='10', micro='2') diff --git a/src/cpl_query/cpl-query.json b/src/cpl_query/cpl-query.json index af1c55c5..75d297f7 100644 --- a/src/cpl_query/cpl-query.json +++ b/src/cpl_query/cpl-query.json @@ -3,8 +3,8 @@ "Name": "cpl-query", "Version": { "Major": "2022", - "Minor": "7", - "Micro": "0" + "Minor": "10", + "Micro": "2" }, "Author": "Sven Heidemann", "AuthorEmail": "sven.heidemann@sh-edraft.de", diff --git a/src/cpl_query/extension/__init__.py b/src/cpl_query/extension/__init__.py index cdfa93b1..dee0d4f3 100644 --- a/src/cpl_query/extension/__init__.py +++ b/src/cpl_query/extension/__init__.py @@ -15,7 +15,7 @@ __title__ = 'cpl_query.extension' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2021 - 2022 sh-edraft.de' -__version__ = '2022.7.0' +__version__ = '2022.10.2' from collections import namedtuple @@ -28,4 +28,4 @@ from .ordered_iterable_abc import OrderedIterableABC from .ordered_iterable import OrderedIterable VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='2022', minor='7', micro='0') +version_info = VersionInfo(major='2022', minor='10', micro='2') -- 2.45.2 From a5285117324cf7269b805dcab5d6b8403bfcbafd Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 12 Sep 2022 17:58:48 +0200 Subject: [PATCH 3/5] Added tests for select & select many --- src/cpl_query/extension/iterable_abc.py | 22 ++++++++++++- unittests/unittests_query/query_test_case.py | 33 ++++++++++++++++++-- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/cpl_query/extension/iterable_abc.py b/src/cpl_query/extension/iterable_abc.py index 45135c6e..e45d7815 100644 --- a/src/cpl_query/extension/iterable_abc.py +++ b/src/cpl_query/extension/iterable_abc.py @@ -158,7 +158,7 @@ class IterableABC(ABC, list): """ pass - def extend(self, __iterable: Iterable) -> None: + def extend(self, __iterable: Iterable) -> 'IterableABC': r"""Adds elements of given list to list Parameter @@ -169,6 +169,8 @@ class IterableABC(ABC, list): for value in __iterable: self.append(value) + return self + @abstractmethod def last(self) -> any: r"""Returns last element @@ -290,6 +292,24 @@ class IterableABC(ABC, list): """ pass + def select(self, _f: Callable) -> 'IterableABC': + r"""Formats each element of list to a given format + + Returns + ------- + :class: `cpl_query.extension.iterable_abc.IterableABC` + """ + pass + + def select_many(self, _f: Callable) -> 'IterableABC': + r"""Flattens resulting lists to one + + Returns + ------- + :class: `cpl_query.extension.iterable_abc.IterableABC` + """ + pass + @abstractmethod def single(self) -> any: r"""Returns one single element of list diff --git a/unittests/unittests_query/query_test_case.py b/unittests/unittests_query/query_test_case.py index 00d4fed8..74fff280 100644 --- a/unittests/unittests_query/query_test_case.py +++ b/unittests/unittests_query/query_test_case.py @@ -161,9 +161,9 @@ class QueryTestCase(unittest.TestCase): def test_for_each(self): users = [] self._tests.for_each(lambda user: ( - users.append(user) - ) + users.append(user) ) + ) self.assertEqual(len(users), len(self._tests)) @@ -239,6 +239,35 @@ class QueryTestCase(unittest.TestCase): self.assertEqual(l_res, res) + def test_select(self): + range_list = List(int, range(0, 100)) + selected_range = range_list.select(lambda x: x + 1) + + modulo_range = [] + for x in range(0, 100): + if x % 2 == 0: + modulo_range.append(x) + self.assertEqual(selected_range.to_list(), list(range(1, 101))) + self.assertEqual(range_list.where(lambda x: x % 2 == 0).to_list(), modulo_range) + + def test_select_many(self): + range_list = List(int, range(0, 100)) + selected_range = range_list.select(lambda x: [x, x]) + + self.assertEqual(selected_range, [[x, x] for x in range(0, 100)]) + self.assertEqual(selected_range.select_many(lambda x: x).to_list(), [_x for _l in [2 * [x] for x in range(0, 100)] for _x in _l]) + + class TestClass: + def __init__(self, i, is_sub=False): + self.i = i + if is_sub: + return + self.elements = [TestClass(x, True) for x in range(0, 10)] + + elements = List(TestClass, [TestClass(i) for i in range(0, 100)]) + selected_elements = elements.select_many(lambda x: x.elements).select(lambda x: x.i) + self.assertEqual(selected_elements.where(lambda x: x == 0).count(), 100) + def test_single(self): res = self._tests.where(lambda u: u.address.nr == self._t_user.address.nr) s_res = self._tests.where(lambda u: u.address.nr == self._t_user.address.nr).single() -- 2.45.2 From fc63f3616efffd4f0b9aaeaa319ba8da54a63ddb Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 12 Sep 2022 18:07:37 +0200 Subject: [PATCH 4/5] Added tests for select & select many --- unittests/unittests_cli/update_test_case.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/unittests/unittests_cli/update_test_case.py b/unittests/unittests_cli/update_test_case.py index 36c6fd4f..de21bf6b 100644 --- a/unittests/unittests_cli/update_test_case.py +++ b/unittests/unittests_cli/update_test_case.py @@ -21,7 +21,8 @@ class UpdateTestCase(unittest.TestCase): self._old_package_name = 'discord.py' self._old_package = f'{self._old_package_name}=={self._old_version}' - self._new_version = '1.7.3' + # todo: better way to do shit required + self._new_version = '2.0.1' self._new_package_name = 'discord.py' self._new_package = f'{self._new_package_name}=={self._new_version}' -- 2.45.2 From 53fe43710029f01bc4c54279a713ea2481a6b010 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 12 Sep 2022 22:18:49 +0200 Subject: [PATCH 5/5] Added more data containers --- src/cpl_discord/container/category_channel.py | 12 +++++++++++- src/cpl_discord/container/container.py | 1 + src/cpl_discord/container/guild.py | 2 +- src/cpl_discord/container/member.py | 9 +++++++-- src/cpl_discord/container/role.py | 11 ++++++----- src/cpl_discord/container/text_channel.py | 12 +++++++++++- src/cpl_discord/container/threads.py | 16 ++++++++++++++++ src/cpl_discord/container/voice_channel.py | 7 ++++++- ...rsConverter.py => to_containers_converter.py} | 0 src/cpl_discord/service/discord_bot_service.py | 2 +- .../src/modules/hello_world/on_ready_event.py | 6 +++++- 11 files changed, 65 insertions(+), 13 deletions(-) create mode 100644 src/cpl_discord/container/threads.py rename src/cpl_discord/helper/{ToContainersConverter.py => to_containers_converter.py} (100%) diff --git a/src/cpl_discord/container/category_channel.py b/src/cpl_discord/container/category_channel.py index 3c9ad3cf..52e93ac9 100644 --- a/src/cpl_discord/container/category_channel.py +++ b/src/cpl_discord/container/category_channel.py @@ -1,7 +1,9 @@ import discord from cpl_discord.container.container import Container -from cpl_discord.helper.ToContainersConverter import ToContainersConverter +from cpl_discord.container.text_channel import TextChannel +from cpl_discord.container.voice_channel import VoiceChannel +from cpl_discord.helper.to_containers_converter import ToContainersConverter from cpl_query.extension import List @@ -9,3 +11,11 @@ class CategoryChannel(discord.CategoryChannel, Container): def __init__(self, _t: discord.CategoryChannel): Container.__init__(self, _t, CategoryChannel) + + @property + def text_channels(self) -> List[TextChannel]: + return List(TextChannel, ToContainersConverter.convert(self._object.text_channels, TextChannel)) + + @property + def voice_channels(self) -> List[VoiceChannel]: + return List(VoiceChannel, ToContainersConverter.convert(self._object.voice_channels, VoiceChannel)) diff --git a/src/cpl_discord/container/container.py b/src/cpl_discord/container/container.py index a32479d6..b55e1d0d 100644 --- a/src/cpl_discord/container/container.py +++ b/src/cpl_discord/container/container.py @@ -1,3 +1,4 @@ +from abc import abstractmethod from typing import Callable diff --git a/src/cpl_discord/container/guild.py b/src/cpl_discord/container/guild.py index b69af4ed..83055d44 100644 --- a/src/cpl_discord/container/guild.py +++ b/src/cpl_discord/container/guild.py @@ -6,7 +6,7 @@ from cpl_discord.container.member import Member from cpl_discord.container.role import Role from cpl_discord.container.text_channel import TextChannel from cpl_discord.container.voice_channel import VoiceChannel -from cpl_discord.helper.ToContainersConverter import ToContainersConverter +from cpl_discord.helper.to_containers_converter import ToContainersConverter from cpl_query.extension import List diff --git a/src/cpl_discord/container/member.py b/src/cpl_discord/container/member.py index 0fe775f3..9f785422 100644 --- a/src/cpl_discord/container/member.py +++ b/src/cpl_discord/container/member.py @@ -1,11 +1,16 @@ import discord from cpl_discord.container.container import Container -from cpl_discord.helper.ToContainersConverter import ToContainersConverter -from cpl_query.extension import List +from cpl_discord.helper.to_containers_converter import ToContainersConverter +from cpl_query.extension.list import List class Member(discord.Member, Container): def __init__(self, _t: discord.Member): Container.__init__(self, _t, Member) + + @property + def roles(self) -> List['Role']: + from cpl_discord.container.role import Role + return List(Role, ToContainersConverter.convert(self._object.roles, Role)) diff --git a/src/cpl_discord/container/role.py b/src/cpl_discord/container/role.py index 27251ef0..74f8c874 100644 --- a/src/cpl_discord/container/role.py +++ b/src/cpl_discord/container/role.py @@ -1,9 +1,9 @@ import discord from cpl_discord.container.container import Container -from cpl_discord.container.member import Member -from cpl_discord.helper.ToContainersConverter import ToContainersConverter -from cpl_query.extension import List + +from cpl_discord.helper.to_containers_converter import ToContainersConverter +from cpl_query.extension.list import List class Role(discord.Role, Container): @@ -12,5 +12,6 @@ class Role(discord.Role, Container): Container.__init__(self, _t, Role) @property - def members(self) -> List[discord.Member]: - return List(discord.Member, ToContainersConverter.convert(self._object.members, Member)) + def members(self) -> List['Member']: + from cpl_discord.container.member import Member + return List(Member, ToContainersConverter.convert(self._object.members, Member)) diff --git a/src/cpl_discord/container/text_channel.py b/src/cpl_discord/container/text_channel.py index 1051a3a5..62347bbf 100644 --- a/src/cpl_discord/container/text_channel.py +++ b/src/cpl_discord/container/text_channel.py @@ -1,7 +1,9 @@ import discord from cpl_discord.container.container import Container -from cpl_discord.helper.ToContainersConverter import ToContainersConverter +from cpl_discord.container.member import Member +from cpl_discord.container.threads import Thread +from cpl_discord.helper.to_containers_converter import ToContainersConverter from cpl_query.extension import List @@ -9,3 +11,11 @@ class TextChannel(discord.TextChannel, Container): def __init__(self, _t: discord.TextChannel): Container.__init__(self, _t, TextChannel) + + @property + def members(self) -> List[discord.Member]: + return List(discord.Member, ToContainersConverter.convert(self._object.members, Member)) + + @property + def threads(self) -> List[Thread]: + return List(Thread, ToContainersConverter.convert(self._object.threads, Thread)) diff --git a/src/cpl_discord/container/threads.py b/src/cpl_discord/container/threads.py new file mode 100644 index 00000000..03b11ac4 --- /dev/null +++ b/src/cpl_discord/container/threads.py @@ -0,0 +1,16 @@ +import discord + +from cpl_discord.container.container import Container +from cpl_discord.container.member import Member +from cpl_discord.helper.to_containers_converter import ToContainersConverter +from cpl_query.extension import List + + +class Thread(discord.Thread, Container): + + def __init__(self, _t: discord.Thread): + Container.__init__(self, _t, Thread) + + @property + def members(self) -> List[Member]: + return List(Member, ToContainersConverter.convert(self._object.members, Member)) diff --git a/src/cpl_discord/container/voice_channel.py b/src/cpl_discord/container/voice_channel.py index ccc5ac4f..805ea4bb 100644 --- a/src/cpl_discord/container/voice_channel.py +++ b/src/cpl_discord/container/voice_channel.py @@ -1,7 +1,8 @@ import discord from cpl_discord.container.container import Container -from cpl_discord.helper.ToContainersConverter import ToContainersConverter +from cpl_discord.container.member import Member +from cpl_discord.helper.to_containers_converter import ToContainersConverter from cpl_query.extension import List @@ -9,3 +10,7 @@ class VoiceChannel(discord.VoiceChannel, Container): def __init__(self, _t: discord.VoiceChannel): Container.__init__(self, _t, VoiceChannel) + + @property + def members(self) -> List[Member]: + return List(Member, ToContainersConverter.convert(self._object.members, Member)) diff --git a/src/cpl_discord/helper/ToContainersConverter.py b/src/cpl_discord/helper/to_containers_converter.py similarity index 100% rename from src/cpl_discord/helper/ToContainersConverter.py rename to src/cpl_discord/helper/to_containers_converter.py diff --git a/src/cpl_discord/service/discord_bot_service.py b/src/cpl_discord/service/discord_bot_service.py index e2928c5e..6b8466d1 100644 --- a/src/cpl_discord/service/discord_bot_service.py +++ b/src/cpl_discord/service/discord_bot_service.py @@ -6,7 +6,7 @@ from cpl_core.environment import ApplicationEnvironmentABC from cpl_core.logging import LoggerABC, LoggingSettings, LoggingLevelEnum from cpl_discord.configuration.discord_bot_settings import DiscordBotSettings from cpl_discord.container.guild import Guild -from cpl_discord.helper.ToContainersConverter import ToContainersConverter +from cpl_discord.helper.to_containers_converter import ToContainersConverter from cpl_discord.service.discord_bot_service_abc import DiscordBotServiceABC from cpl_discord.service.discord_service_abc import DiscordServiceABC from cpl_query.extension.list import List diff --git a/src/tests/custom/discord/src/modules/hello_world/on_ready_event.py b/src/tests/custom/discord/src/modules/hello_world/on_ready_event.py index 10d3453c..d9a2fd5d 100644 --- a/src/tests/custom/discord/src/modules/hello_world/on_ready_event.py +++ b/src/tests/custom/discord/src/modules/hello_world/on_ready_event.py @@ -22,10 +22,14 @@ class OnReadyEvent(OnReadyABC): for r in g.roles: self._log('--Role', r, type(r)) for rm in r.members: - self._log('---Rolemembers', rm, type(rm)) + self._log('---Rolemember', rm, type(rm)) for m in g.members: self._log('--Member', m, type(m)) + for mr in m.roles: + self._log('--Memberole', mr, type(mr)) + for rm in mr.members: + self._log('---Rolemember', rm, type(rm)) select = self._bot.guilds.select(lambda guild: (guild.name, guild.id)) self._logger.warn(__name__, f'Does cpl.query select work? {select}') -- 2.45.2