Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/pip/mattermostautodriver-approx-e…
Browse files Browse the repository at this point in the history
…q-2.0.0
  • Loading branch information
attzonko authored Dec 10, 2024
2 parents cd25c73 + 164ba5e commit dfef669
Show file tree
Hide file tree
Showing 20 changed files with 109 additions and 103 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ jobs:

steps:
- name: Cancel Outdated Runs
uses: styfle/[email protected].0
uses: styfle/[email protected].1
with:
access_token: ${{ github.token }}
- uses: actions/checkout@v4
- name: Set up Python 3.10
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: '3.10'
- uses: actions/cache@v3
- uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip3-${{ hashFiles('*requirements.txt') }}
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/pre-commit-autoupdate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ jobs:
- uses: actions/checkout@v4

- name: Configure Python 3.10
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: '3.10'

- name: Use Pip cache if available
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip3-${{ hashFiles('*requirements.txt') }}
Expand All @@ -34,7 +34,7 @@ jobs:
uses: browniebroke/pre-commit-autoupdate-action@main

- name: Create Pull request with pre-commit updates
uses: peter-evans/create-pull-request@v5
uses: peter-evans/create-pull-request@v6
with:
token: ${{ secrets.GITHUB_TOKEN }}
branch: update/pre-commit-hooks
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.10
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Install requirements
Expand Down
16 changes: 8 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ jobs:

steps:
- name: Cancel Outdated Runs
uses: styfle/[email protected].0
uses: styfle/[email protected].1
with:
access_token: ${{ github.token }}
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- uses: actions/cache@v3
- uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip3-${{ hashFiles('*requirements.txt') }}
Expand All @@ -47,20 +47,20 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- uses: actions/cache@v3
- uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip3-${{ hashFiles('*requirements.txt') }}
- name: Install dependencies
run: pip install -e .[dev]
- name: Launch test server
working-directory: tests/integration_tests
run: docker-compose up -d && sleep 30
- name: Print docker info
run: docker ps -a
run: docker compose up -d && sleep 30
- name: Print docker services info
run: docker compose ps -a
- name: Run integration tests
working-directory: tests/integration_tests
run: pytest . -vv -n auto
16 changes: 11 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v4.6.0
hooks:
- id: end-of-file-fixer
- id: fix-byte-order-marker
Expand All @@ -13,7 +13,7 @@ repos:
- id: check-merge-conflict
- repo: https://github.com/pycqa/isort
# Formants, sorts and reorganizes imports
rev: 5.12.0
rev: 5.13.2
hooks:
- id: isort
args:
Expand All @@ -22,16 +22,22 @@ repos:
- "--filter-files"
- repo: https://github.com/psf/black
# Code style formatting
rev: 23.11.0
rev: 24.8.0
hooks:
- id: black
exclude: (.*/)*snapshots/
- repo: https://github.com/PyCQA/flake8
# Checks the code for PEP8 violations and common pitfals
rev: 6.1.0
rev: 7.1.1
hooks:
- id: flake8
exclude: (.*/)*snapshots/
# Ruff is a "fastar than flake8" linter/formatter
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.9
hooks:
# Run the linter.
- id: ruff
- repo: https://github.com/mattseymour/pre-commit-pytype
rev: '2023.5.8'
hooks:
Expand All @@ -54,6 +60,6 @@ repos:
- "88"
- repo: https://github.com/pycqa/doc8
# sphinx rst style checker
rev: v1.1.1
rev: v1.1.2
hooks:
- id: doc8
2 changes: 0 additions & 2 deletions docker-compose.yml → compose.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: "3.7"

services:
app:
# This docker-compose file specifies a mmpy_bot
Expand Down
14 changes: 7 additions & 7 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
black==23.10.1
black==24.4.2
docformatter==1.7.5
filelock==3.13.1
flake8==6.1.0
isort==5.12.0
pytest==7.4.3
pytest-xdist==3.3.1
pytype==2022.10.26
filelock==3.15.4
isort==5.13.2
flake8==7.1.0
pytest==8.2.2
pytest-xdist==3.6.1
pytype==2024.4.11
snapshottest==0.6.0
4 changes: 3 additions & 1 deletion mmpy_bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ def _register_logger(self):
**{
"format": self.settings.LOG_FORMAT,
"datefmt": self.settings.LOG_DATE_FORMAT,
"level": logging.DEBUG if self.settings.DEBUG else logging.INFO,
"level": (
logging.DEBUG if self.settings.DEBUG else self.settings.LOG_LEVEL
),
"filename": self.settings.LOG_FILE,
"filemode": "w",
}
Expand Down
45 changes: 22 additions & 23 deletions mmpy_bot/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def create_post(
message: str,
file_paths: Optional[Sequence[str]] = None,
root_id: str = "",
props: Dict = {},
props: Optional[Dict] = None,
ephemeral_user_id: Optional[str] = None,
):
"""Create a post in the specified channel with the specified text.
Expand All @@ -52,8 +52,8 @@ def create_post(
paths are specified, those files will be uploaded to mattermost first and then
attached.
"""
if file_paths is None:
file_paths = []
file_paths = file_paths or []
props = props or {}

file_ids = (
self.upload_files(file_paths, channel_id) if len(file_paths) > 0 else []
Expand Down Expand Up @@ -88,9 +88,9 @@ def get_post_thread(self, post_id: str):
duplicate and wrongly ordered entries in the ordered list."""
thread_info = self.posts.get_post_thread(post_id)

id_stamps = []
for id, post in thread_info["posts"].items():
id_stamps.append((id, int(post["create_at"])))
id_stamps = (
(id, int(post["create_at"])) for id, post in thread_info["posts"].items()
)
# Sort the posts by their timestamps
sorted_stamps = sorted(id_stamps, key=lambda x: x[-1])
# Overwrite the order with the sorted list
Expand All @@ -116,7 +116,7 @@ def reply_to(
message: Message,
response: str,
file_paths: Optional[Sequence[str]] = None,
props: Dict = {},
props: Optional[Dict] = None,
ephemeral: bool = False,
direct: bool = False,
):
Expand All @@ -127,8 +127,8 @@ def reply_to(
Also supports replying privately by setting direct=True.
"""
if file_paths is None:
file_paths = []
file_paths = file_paths or []
props = props or {}

if direct and not message.is_direct_message:
# NOTE we explicitly don't pass root_id as it would refer to a
Expand Down Expand Up @@ -160,14 +160,15 @@ def direct_message(
message: str,
file_paths: Optional[Sequence[str]] = None,
root_id: str = "",
props: Dict = {},
props: Optional[Dict] = None,
ephemeral_user_id: Optional[str] = None,
):
# Private/direct messages are sent to a special channel that
# includes the bot and the recipient
direct_id = self.channels.create_direct_channel([self.user_id, receiver_id])[
"id"
]
props = props or {}

return self.create_post(
channel_id=direct_id,
Expand Down Expand Up @@ -199,22 +200,20 @@ def upload_files(
) -> List[str]:
"""Given a list of file paths and the channel id, uploads the corresponding
files and returns a list their internal file IDs."""
file_list = []
for path in file_paths:
path = Path(path)
# Note: 'files' should be a name of an expected attribute in the body
# but seems to be ignored when simply uploading files to mattermost
file_list.append(
# Note: 'files' should be a name of an expected attribute in the body
# but seems to be ignored when simply uploading files to mattermost
file_list = [
(
"files",
(
"files",
(
path.name,
Path(path).read_bytes(),
),
)
Path(path).name,
Path(path).read_bytes(),
),
)
for path in file_paths
]

result = self.files.upload_file(
files=file_list, data={"channel_id": channel_id}
)
return list(info["id"] for info in result["file_infos"])
return [info["id"] for info in result["file_infos"]]
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
8 changes: 4 additions & 4 deletions mmpy_bot/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __init__(
)

self.function = function
self.is_coroutine = asyncio.iscoroutinefunction(function)
self.is_coroutine = inspect.iscoroutinefunction(function)
self.is_click_function: bool = False
self.matcher = matcher
self.metadata = metadata
Expand Down Expand Up @@ -100,7 +100,7 @@ def __init__(

if self.is_click_function:
_function = self.function.callback
if asyncio.iscoroutinefunction(_function):
if inspect.iscoroutinefunction(_function):
raise ValueError(
"Combining click functions and coroutines is currently not supported!"
" Consider using a regular function, which will be threaded by default."
Expand Down Expand Up @@ -155,10 +155,10 @@ def __call__(self, message: Message, *args):
assert len(args) <= 1 # There is only one group, (.*)?
if len(args) == 1:
# Turn space-separated string into list
args = tuple(shlex.split(args[0]))
args = shlex.split(args[0])
try:
ctx = self.function.make_context(
info_name=self.plugin.__class__.__name__, args=list(args)
info_name=self.plugin.__class__.__name__, args=args
)
ctx.params.update({"self": self.plugin, "message": message})
return self.function.invoke(ctx)
Expand Down
Loading

0 comments on commit dfef669

Please sign in to comment.