webexmemebot/app/meme.py
Luke Tainton 6421a3923f
Some checks failed
Security / sonarqube (push) Failing after 22s
Security / snyk (push) Successful in 54s
feat!(deps): upgrade dependencies (#488)
Okay, let's break down this Git diff. I'll explain the changes in the context of the overall project.

**Overall:**

This pull request appears to be updating dependencies, and making a corresponding code change to reflect an API change in one of those dependencies, `webexteamssdk`. The project seems to be a Webex bot application, utilizing the Webex Teams SDK to interact with Webex.

**File Breakdown:**

1.  `app/meme.py`

    ```diff
    --- a/app/meme.py
    +++ b/app/meme.py
    @@ -2,10 +2,10 @@

     from webex_bot.models.command import Command
     from webex_bot.models.response import Response, response_from_adaptive_card
-    from webexteamssdk.models.cards import (
+    from webexpythonsdk.models.cards import (
         AdaptiveCard,
         Choice,
-        Choices,
+        ChoiceSet,
         Column,
         ColumnSet,
         FontSize,
@@ -13,7 +13,7 @@ from webexteamssdk.models.cards import (
         Text,
         TextBlock,
     )
-    from webexteamssdk.models.cards.actions import OpenUrl, Submit
+    from webexpythonsdk.models.cards.actions import OpenUrl, Submit

     from app import img

    @@ -70,7 +70,7 @@ class MakeMemeCommand(Command):
                         Column(
                             width=1,
                             items=[
-                                Choices(
+                                ChoiceSet(
                                     id="meme_type",
                                     isMultiSelect=False,
                                     choices=[Choice(title=x["name"], value=x["choiceval"]) for x in TEMPLATES],
    ```

    *   **`import` statement update:**

        *   `webexteamssdk` is replaced with `webexpythonsdk`.  This indicates that the code is migrating to use a potentially renamed or reorganized SDK.
        *   The import paths for card models and actions are updated to reflect the new SDK structure (e.g., `webexteamssdk.models.cards` becomes `webexpythonsdk.models.cards`).
    *   **`Choices` to `ChoiceSet`:**

        *   The code changes from using a class named `Choices` to `ChoiceSet`. The `Choices` class was probably renamed to `ChoiceSet` in the new SDK.  This change is found in the `MakeMemeCommand` class, within the adaptive card definition.
        *   The purpose of this code is likely to present a user with a set of options to select a meme type, and the `ChoiceSet` renders a dropdown or radio button group in the adaptive card.

2.  `uv.lock`

    This file is a lock file for the uv package manager, similar to `requirements.txt` with hashes.  It specifies the exact versions and dependencies of Python packages used in the project. The changes in this file reflect an update to the project's dependencies. The major changes are:

    *   **Removal of `revision = 2`:** This line is removed, indicating a change in the lock file's metadata.
    *   **Version bumps:** Several packages have their versions updated. Some examples include:
        *   `certifi` updated from `2025.1.31` to `2025.4.26`
        *   `charset-normalizer` updated from `3.4.1` to `3.4.2`
        *   `click` updated from `8.1.8` to `8.2.1`
        *   `coverage` updated from `7.8.0` to `7.8.2`
        *   `dill` updated from `0.3.9` to `0.4.0`
        *   `identify` updated from `2.6.9` to `2.6.12`
        *   `mypy-extensions` updated from `1.0.0` to `1.1.0`
        *   `packaging` updated from `24.2` to `25.0`
        *   `pluggy` updated from `1.5.0` to `1.6.0`
        *   `pylint` updated from `3.3.6` to `3.3.7`
        *   `urllib3` updated to `2.4.0`
        *   `webex-bot` updated from `0.5.2` to `0.6.2`
        *   `webexteamssdk` replaced by `webexpythonsdk` updated to `2.0.4`
    *   **Removal of `future` package:** This package is removed from the dependencies.
    *   **Addition of `pygments` package:** This package is added as a dependency with version `2.19.1`.
    *   **Hash changes:**  The hashes for all the updated packages have also changed, which is expected since the package versions are different. The inclusion of hashes ensures that the correct, unaltered versions of the packages are installed.

**Impact and Justification:**

*   **Dependency Updates:** Keeping dependencies up-to-date is a standard security practice. Newer versions often include bug fixes, performance improvements, and security patches.
*   **API Alignment:** The code change in `app/meme.py` is essential. If `webexteamssdk` was indeed renamed or its API significantly altered in the newer version, the code needs to adapt to use the new class names and import paths. Failing to do so would likely break the meme-making functionality.
*   **Lockfile Integrity:** Updating the lockfile (`uv.lock`) is crucial.  It ensures that every environment where this project is deployed uses the *exact* same versions of the dependencies, preventing unexpected behavior.
*   **Removal of `future`:** The removal of `future` suggests that the code might have been updated to be fully compatible with Python 3, and the package is no longer needed.
*   **Addition of `pygments`:** The addition of `pygments` indicates that the code will now use this library.

**In Summary:**

This pull request updates dependencies, adapts the code to API changes in `webexpythonsdk`, adds `pygments` and removes the `future` package, while ensuring dependency consistency through the lockfile. It's a necessary step for maintaining the application's functionality, security, and compatibility with the latest libraries.

Reviewed-on: #488
2025-06-06 19:53:36 +02:00

152 lines
5.7 KiB
Python

"""Generates meme images using the memegen.link API."""
from webex_bot.models.command import Command
from webex_bot.models.response import Response, response_from_adaptive_card
from webexpythonsdk.models.cards import (
AdaptiveCard,
Choice,
ChoiceSet,
Column,
ColumnSet,
FontSize,
FontWeight,
Text,
TextBlock,
)
from webexpythonsdk.models.cards.actions import OpenUrl, Submit
from app import img
TEMPLATES = img.get_templates()
class MakeMemeCommand(Command):
"""Class for initial Webex interactive card."""
def __init__(self) -> None:
"""Initialize the MakeMemeCommand with command keyword and help message."""
super().__init__(
command_keyword="/meme",
help_message="Make a Meme",
chained_commands=[MakeMemeCallback()],
delete_previous_message=True,
)
def pre_execute(self, message, attachment_actions, activity) -> None: # pylint: disable=unused-argument
"""Pre-execution logic for the MakeMemeCommand."""
return
def execute(self, message, attachment_actions, activity) -> Response: # pylint: disable=unused-argument
"""Execute the MakeMemeCommand and return an adaptive card."""
card_body: list = [
ColumnSet(
columns=[
Column(
width=1,
items=[
TextBlock(
"Make a Meme",
weight=FontWeight.BOLDER,
size=FontSize.MEDIUM,
),
TextBlock(
"This bot uses memegen.link to generate memes. Click 'View Templates' to view available templates.", # pylint: disable=line-too-long
weight=FontWeight.LIGHTER,
size=FontSize.SMALL,
wrap=True,
),
TextBlock(
"Both fields are required. If you do not want to specify a value, please type a space.", # pylint: disable=line-too-long
weight=FontWeight.LIGHTER,
size=FontSize.SMALL,
wrap=True,
),
],
),
]
),
ColumnSet(
columns=[
Column(
width=1,
items=[
ChoiceSet(
id="meme_type",
isMultiSelect=False,
choices=[Choice(title=x["name"], value=x["choiceval"]) for x in TEMPLATES],
),
Text(id="text_top", placeholder="Top Text", maxLength=100),
Text(
id="text_bottom",
placeholder="Bottom Text",
maxLength=100,
),
],
),
]
),
]
card: AdaptiveCard = AdaptiveCard(
body=card_body,
actions=[
Submit(
title="Go!",
data={"callback_keyword": "make_meme_callback_rbamzfyx"},
),
OpenUrl(url="https://memegen.link/#templates", title="View Templates"),
Submit(title="Cancel", data={"command_keyword": "exit"}),
],
)
return response_from_adaptive_card(card)
class MakeMemeCallback(Command):
"""Class to process user data and return meme."""
def __init__(self) -> None:
"""Initialize the MakeMemeCallback with command keyword and help message."""
super().__init__(
card_callback_keyword="make_meme_callback_rbamzfyx",
delete_previous_message=True,
)
self.error: bool = False
self.text_top: str = ""
self.text_bottom: str = ""
self.meme: str = ""
self.meme_filename: str = ""
def pre_execute(self, message, attachment_actions, activity) -> str: # pylint: disable=unused-argument
"""Pre-execution logic for the MakeMemeCallback."""
self.meme: str = attachment_actions.inputs.get("meme_type")
self.text_top: str = attachment_actions.inputs.get("text_top")
self.text_bottom: str = attachment_actions.inputs.get("text_bottom")
if not self.text_top and not self.text_bottom:
self.error = True
return "Please provide at least one positional text argument."
if ".gif" in self.meme:
return "Generating your meme. GIF-based memes take a little longer. Please wait..."
return "Generating your meme..."
def execute(self, message, attachment_actions, activity) -> Response | None: # pylint: disable=unused-argument
"""Execute the MakeMemeCallback and return a response with the meme image."""
if self.error:
return None
self.meme_filename: str = img.generate_api_url(self.meme, self.text_top, self.text_bottom)
msg: Response = Response(
attributes={
"roomId": activity["target"]["globalId"],
"parentId": "",
"files": [self.meme_filename],
}
)
return msg
def post_execute(self, message, attachment_actions, activity) -> None: # pylint: disable=unused-argument
"""Post-execution logic for the MakeMemeCallback."""
return