Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimization for loops and refac to py3 #528

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 24 additions & 29 deletions mmpy_bot/event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
log = logging.getLogger("mmpy.event_handler")


class EventHandler(object):
class EventHandler:
def __init__(
self,
driver: Driver,
Expand All @@ -37,10 +37,8 @@ def start(self):
def _should_ignore(self, message: Message):
# Ignore message from senders specified in settings, and maybe from ourself
return (
True
if message.sender_name.lower()
message.sender_name.lower()
in (name.lower() for name in self.settings.IGNORE_USERS)
else False
) or (self.ignore_own_messages and message.sender_name == self.driver.username)

async def _check_queue_loop(self, webhook_queue: queue.Queue):
Expand Down Expand Up @@ -75,37 +73,34 @@ async def _handle_post(self, post):

# Find all the listeners that match this message, and have their plugins handle
# the rest.
tasks = []
for matcher, functions in self.plugin_manager.message_listeners.items():
match = matcher.search(message.text)
if match:
groups = list([group for group in match.groups() if group != ""])
for function in functions:
# Create an asyncio task to handle this callback
tasks.append(
asyncio.create_task(
function.plugin.call_function(
function, message, groups=groups
)
)
)
tasks = [
asyncio.create_task(
function.plugin.call_function(
function,
message,
groups=[group for group in match.groups() if group != ""],
)
)
for matcher, functions in self.plugin_manager.message_listeners.items()
if (match := matcher.search(message.text))
for function in functions
]

# Execute the callbacks in parallel
asyncio.gather(*tasks)

async def _handle_webhook(self, event: WebHookEvent):
# Find all the listeners that match this webhook id, and have their plugins
# handle the rest.
tasks = []
for matcher, functions in self.plugin_manager.webhook_listeners.items():
match = matcher.search(event.webhook_id)
if match:
for function in functions:
# Create an asyncio task to handle this callback
tasks.append(
asyncio.create_task(
function.plugin.call_function(function, event)
)
)

tasks = [
# Create an asyncio task to handle this callback
asyncio.create_task(function.plugin.call_function(function, event))
for matcher, functions in self.plugin_manager.webhook_listeners.items()
if matcher.search(event.webhook_id)
for function in functions
]

# If this webhook doesn't correspond to any listeners, signal the WebHookServer
# to not wait for any response
if len(tasks) == 0:
Expand Down
2 changes: 1 addition & 1 deletion tests/integration_tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def expect_reply(driver: Driver, post: Dict, wait=RESPONSE_TIMEOUT, retries=1):
reply = thread_info["posts"][reply_id]
break

if not reply:
if reply is None:
raise ValueError("Expected a response, but didn't get any!")

return reply
Expand Down