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

Add formating #793

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ repos:
rev: v0.1.9
hooks:
- id: ruff # See pyproject.toml for args
- id: ruff-format

- repo: https://github.com/abravalheri/validate-pyproject
rev: v0.15
Expand Down
8 changes: 7 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ testpaths = "tests src"
exclude = ["test/lib/python/*"]
extend-select = [
"B",
"COM",
"E9",
"F63",
"F7",
Expand All @@ -85,12 +86,17 @@ extend-select = [
"UP",
"W",
]
ignore = []
# COM812: as recommended by ruff-format
ignore = ["COM812"]
line-length = 167

[tool.ruff.format]
exclude = ["examples/**/*.py"]

[tool.ruff.per-file-ignores]
"examples/**/*.py" = [
"B",
"COM",
"E402",
"E711",
"E741",
Expand Down
805 changes: 338 additions & 467 deletions src/paho/mqtt/client.py

Large diffs are not rendered by default.

28 changes: 15 additions & 13 deletions src/paho/mqtt/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class MQTTMatcher:
some topic name."""

class Node:
__slots__ = '_children', '_content'
__slots__ = "_children", "_content"

def __init__(self):
self._children = {}
Expand All @@ -20,15 +20,15 @@ def __setitem__(self, key, value):
"""Add a topic filter :key to the prefix tree
and associate it to :value"""
node = self._root
for sym in key.split('/'):
for sym in key.split("/"):
node = node._children.setdefault(sym, self.Node())
node._content = value

def __getitem__(self, key):
"""Retrieve the value associated with some topic filter :key"""
try:
node = self._root
for sym in key.split('/'):
for sym in key.split("/"):
node = node._children[sym]
if node._content is None:
raise KeyError(key)
Expand All @@ -41,24 +41,25 @@ def __delitem__(self, key):
lst = []
try:
parent, node = None, self._root
for k in key.split('/'):
parent, node = node, node._children[k]
lst.append((parent, k, node))
for k in key.split("/"):
parent, node = node, node._children[k]
lst.append((parent, k, node))
# TODO
node._content = None
except KeyError as ke:
raise KeyError(key) from ke
else: # cleanup
for parent, k, node in reversed(lst):
if node._children or node._content is not None:
break
break
del parent._children[k]

def iter_match(self, topic):
"""Return an iterator on all values associated with filters
that match the :topic"""
lst = topic.split('/')
normal = not topic.startswith('$')
lst = topic.split("/")
normal = not topic.startswith("$")

def rec(node, i=0):
if i == len(lst):
if node._content is not None:
Expand All @@ -68,11 +69,12 @@ def rec(node, i=0):
if part in node._children:
for content in rec(node._children[part], i + 1):
yield content
if '+' in node._children and (normal or i > 0):
for content in rec(node._children['+'], i + 1):
if "+" in node._children and (normal or i > 0):
for content in rec(node._children["+"], i + 1):
yield content
if '#' in node._children and (normal or i > 0):
content = node._children['#']._content
if "#" in node._children and (normal or i > 0):
content = node._children["#"]._content
if content is not None:
yield content

return rec(self._root)
26 changes: 19 additions & 7 deletions src/paho/mqtt/packettypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,26 @@ class PacketTypes:
indexes = range(1, 16)

# Packet types
CONNECT, CONNACK, PUBLISH, PUBACK, PUBREC, PUBREL, \
PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK, \
PINGREQ, PINGRESP, DISCONNECT, AUTH = indexes
CONNECT, CONNACK, PUBLISH, PUBACK, PUBREC, PUBREL, PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK, PINGREQ, PINGRESP, DISCONNECT, AUTH = indexes

# Dummy packet type for properties use - will delay only applies to will
WILLMESSAGE = 99

Names = [ "reserved", \
"Connect", "Connack", "Publish", "Puback", "Pubrec", "Pubrel", \
"Pubcomp", "Subscribe", "Suback", "Unsubscribe", "Unsuback", \
"Pingreq", "Pingresp", "Disconnect", "Auth"]
Names = [
"reserved",
"Connect",
"Connack",
"Publish",
"Puback",
"Pubrec",
"Pubrel",
"Pubcomp",
"Subscribe",
"Suback",
"Unsubscribe",
"Unsuback",
"Pingreq",
"Pingresp",
"Disconnect",
"Auth",
]
Loading
Loading