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 more linter #786

Closed
wants to merge 4 commits into from
Closed
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
12 changes: 12 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
# default_language_version:
# python: python3
repos:
- repo: https://github.com/psf/black
rev: 23.12.1
hooks:
- id: black

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
Expand Down Expand Up @@ -37,3 +42,10 @@ repos:
rev: v0.15
hooks:
- id: validate-pyproject

- repo: https://github.com/codespell-project/codespell
rev: v2.2.6
hooks:
- id: codespell
additional_dependencies:
- tomli
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Please read the [Eclipse Foundation policy on accepting contributions via Git](h
cherry-picked to the release branch.

The only changes that goes directly to the release branch (``1.4``,
``1.5``, ...) are bug fixe that does not apply to ``master`` (e.g. because
``1.5``, ...) are bug fixes that does not apply to ``master`` (e.g. because
there are fixed on master by a refactoring, or any other huge change we do
not want to cherry-pick to the release branch).
4. Create a new branch from the latest ```master``` branch
Expand Down
10 changes: 10 additions & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ v2.0.0 - 2023-xx-xx

- **BREAKING** Drop support for Python 2.7, Python 3.5 and Python 3.6
Minimum tested version is Python 3.7
- **BREAKING** connnect_srv changed it signature to take an additional bind_port parameter.
This is a breaking change, but in previous version connect_srv was broken anyway.
Closes #493.
- Add types to Client class, which caused few change which should be compatible.
Known risk of breaking changes:
- Use enum for returned error code (like MQTT_ERR_SUCCESS). It use an IntEnum
which should be a drop-in replacement. Excepted if someone is doing "rc is 0" instead of "rc == 0".
- reason in on_connect callback when using MQTTv5 is now always a ReasonCode object. It used to possibly be
an integer with the value 132.
- MQTTMessage field "dup" and "retain" used to be integer with value 0 and 1. They are now boolean.
- Add on_pre_connect() callback, which is called immediately before a
connection attempt is made.

Expand Down
36 changes: 25 additions & 11 deletions examples/aws_iot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


def get_amazon_auth_headers(access_key, secret_key, region, host, port, headers=None):
""" Get the amazon auth headers for working with the amazon websockets
"""Get the amazon auth headers for working with the amazon websockets
protocol

Requires a lot of extra stuff:
Expand Down Expand Up @@ -47,8 +47,8 @@ def getSignatureKey(key, dateStamp, regionName, serviceName):
algorithm = "AWS4-HMAC-SHA256"

t = datetime.datetime.utcnow()
amzdate = t.strftime('%Y%m%dT%H%M%SZ')
datestamp = t.strftime("%Y%m%d") # Date w/o time, used in credential scope
amzdate = t.strftime("%Y%m%dT%H%M%SZ")
datestamp = t.strftime("%Y%m%d") # Date w/o time, used in credential scope

if headers is None:
headers = {
Expand All @@ -61,12 +61,16 @@ def getSignatureKey(key, dateStamp, regionName, serviceName):
"Sec-Websocket-Protocol": "mqtt",
}

headers.update({
"X-Amz-Date": amzdate,
})
headers.update(
{
"X-Amz-Date": amzdate,
}
)

# get into 'canonical' form - lowercase, sorted alphabetically
canonical_headers = "\n".join(sorted("{}:{}".format(i.lower(), j).strip() for i, j in headers.items()))
canonical_headers = "\n".join(
sorted("{}:{}".format(i.lower(), j).strip() for i, j in headers.items())
)
# Headers to sign - alphabetical order
signed_headers = ";".join(sorted(i.lower().strip() for i in headers.keys()))

Expand All @@ -88,14 +92,24 @@ def getSignatureKey(key, dateStamp, regionName, serviceName):
# now actually hash request and sign
hashed_request = hashlib.sha256(canonical_request).hexdigest()

credential_scope = "{datestamp:s}/{region:s}/{service:s}/aws4_request".format(**locals())
string_to_sign = "{algorithm:s}\n{amzdate:s}\n{credential_scope:s}\n{hashed_request:s}".format(**locals())
credential_scope = "{datestamp:s}/{region:s}/{service:s}/aws4_request".format(
**locals()
)
string_to_sign = (
"{algorithm:s}\n{amzdate:s}\n{credential_scope:s}\n{hashed_request:s}".format(
**locals()
)
)

signing_key = getSignatureKey(secret_key, datestamp, region, service)
signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'), hashlib.sha256).hexdigest()
signature = hmac.new(
signing_key, (string_to_sign).encode("utf-8"), hashlib.sha256
).hexdigest()

# create auth header
authorization_header = "{algorithm:s} Credential={access_key:s}/{credential_scope:s}, SignedHeaders={signed_headers:s}, Signature={signature:s}".format(**locals())
authorization_header = "{algorithm:s} Credential={access_key:s}/{credential_scope:s}, SignedHeaders={signed_headers:s}, Signature={signature:s}".format(
**locals()
)

# get final header string
headers["Authorization"] = authorization_header
Expand Down
19 changes: 16 additions & 3 deletions examples/client_mqtt_clear_retain.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ def on_log(mqttc, userdata, level, string):

def print_usage():
print(
"mqtt_clear_retain.py [-d] [-h hostname] [-i clientid] [-k keepalive] [-p port] [-u username [-P password]] [-v] -t topic")
"mqtt_clear_retain.py [-d] [-h hostname] [-i clientid] [-k keepalive] [-p port] [-u username [-P password]] [-v] -t topic"
)


def main(argv):
Expand All @@ -70,8 +71,20 @@ def main(argv):
verbose = False

try:
opts, args = getopt.getopt(argv, "dh:i:k:p:P:t:u:v",
["debug", "id", "keepalive", "port", "password", "topic", "username", "verbose"])
opts, args = getopt.getopt(
argv,
"dh:i:k:p:P:t:u:v",
[
"debug",
"id",
"keepalive",
"port",
"password",
"topic",
"username",
"verbose",
],
)
except getopt.GetoptError:
print_usage()
sys.exit(2)
Expand Down
95 changes: 66 additions & 29 deletions examples/client_pub_opts.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,53 @@

parser = argparse.ArgumentParser()

parser.add_argument('-H', '--host', required=False, default="mqtt.eclipseprojects.io")
parser.add_argument('-t', '--topic', required=False, default="paho/test/opts")
parser.add_argument('-q', '--qos', required=False, type=int,default=0)
parser.add_argument('-c', '--clientid', required=False, default=None)
parser.add_argument('-u', '--username', required=False, default=None)
parser.add_argument('-d', '--disable-clean-session', action='store_true', help="disable 'clean session' (sub + msgs not cleared when client disconnects)")
parser.add_argument('-p', '--password', required=False, default=None)
parser.add_argument('-P', '--port', required=False, type=int, default=None, help='Defaults to 8883 for TLS or 1883 for non-TLS')
parser.add_argument('-N', '--nummsgs', required=False, type=int, default=1, help='send this many messages before disconnecting')
parser.add_argument('-S', '--delay', required=False, type=float, default=1, help='number of seconds to sleep between msgs')
parser.add_argument('-k', '--keepalive', required=False, type=int, default=60)
parser.add_argument('-s', '--use-tls', action='store_true')
parser.add_argument('--insecure', action='store_true')
parser.add_argument('-F', '--cacerts', required=False, default=None)
parser.add_argument('--tls-version', required=False, default=None, help='TLS protocol version, can be one of tlsv1.2 tlsv1.1 or tlsv1\n')
parser.add_argument('-D', '--debug', action='store_true')
parser.add_argument("-H", "--host", required=False, default="mqtt.eclipseprojects.io")
parser.add_argument("-t", "--topic", required=False, default="paho/test/opts")
parser.add_argument("-q", "--qos", required=False, type=int, default=0)
parser.add_argument("-c", "--clientid", required=False, default=None)
parser.add_argument("-u", "--username", required=False, default=None)
parser.add_argument(
"-d",
"--disable-clean-session",
action="store_true",
help="disable 'clean session' (sub + msgs not cleared when client disconnects)",
)
parser.add_argument("-p", "--password", required=False, default=None)
parser.add_argument(
"-P",
"--port",
required=False,
type=int,
default=None,
help="Defaults to 8883 for TLS or 1883 for non-TLS",
)
parser.add_argument(
"-N",
"--nummsgs",
required=False,
type=int,
default=1,
help="send this many messages before disconnecting",
)
parser.add_argument(
"-S",
"--delay",
required=False,
type=float,
default=1,
help="number of seconds to sleep between msgs",
)
parser.add_argument("-k", "--keepalive", required=False, type=int, default=60)
parser.add_argument("-s", "--use-tls", action="store_true")
parser.add_argument("--insecure", action="store_true")
parser.add_argument("-F", "--cacerts", required=False, default=None)
parser.add_argument(
"--tls-version",
required=False,
default=None,
help="TLS protocol version, can be one of tlsv1.2 tlsv1.1 or tlsv1\n",
)
parser.add_argument("-D", "--debug", action="store_true")

args, unknown = parser.parse_known_args()

Expand All @@ -63,6 +94,7 @@ def on_subscribe(mqttc, obj, mid, granted_qos):
def on_log(mqttc, obj, level, string):
print(string)


usetls = args.use_tls

if args.cacerts:
Expand All @@ -75,27 +107,33 @@ def on_log(mqttc, obj, level, string):
else:
port = 1883

mqttc = mqtt.Client(args.clientid,clean_session = not args.disable_clean_session)
mqttc = mqtt.Client(args.clientid, clean_session=not args.disable_clean_session)

if usetls:
if args.tls_version == "tlsv1.2":
tlsVersion = ssl.PROTOCOL_TLSv1_2
tlsVersion = ssl.PROTOCOL_TLSv1_2
elif args.tls_version == "tlsv1.1":
tlsVersion = ssl.PROTOCOL_TLSv1_1
tlsVersion = ssl.PROTOCOL_TLSv1_1
elif args.tls_version == "tlsv1":
tlsVersion = ssl.PROTOCOL_TLSv1
tlsVersion = ssl.PROTOCOL_TLSv1
elif args.tls_version is None:
tlsVersion = None
tlsVersion = None
else:
print ("Unknown TLS version - ignoring")
tlsVersion = None
print("Unknown TLS version - ignoring")
tlsVersion = None

if not args.insecure:
cert_required = ssl.CERT_REQUIRED
else:
cert_required = ssl.CERT_NONE

mqttc.tls_set(ca_certs=args.cacerts, certfile=None, keyfile=None, cert_reqs=cert_required, tls_version=tlsVersion)
mqttc.tls_set(
ca_certs=args.cacerts,
certfile=None,
keyfile=None,
cert_reqs=cert_required,
tls_version=tlsVersion,
)

if args.insecure:
mqttc.tls_insecure_set(True)
Expand All @@ -111,18 +149,17 @@ def on_log(mqttc, obj, level, string):
if args.debug:
mqttc.on_log = on_log

print("Connecting to "+args.host+" port: "+str(port))
print("Connecting to " + args.host + " port: " + str(port))
mqttc.connect(args.host, port, args.keepalive)

mqttc.loop_start()

for x in range (0, args.nummsgs):
msg_txt = '{"msgnum": "'+str(x)+'"}'
print("Publishing: "+msg_txt)
for x in range(0, args.nummsgs):
msg_txt = '{"msgnum": "' + str(x) + '"}'
print("Publishing: " + msg_txt)
infot = mqttc.publish(args.topic, msg_txt, qos=args.qos)
infot.wait_for_publish()

time.sleep(args.delay)

mqttc.disconnect()

22 changes: 11 additions & 11 deletions examples/client_rpc_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@
# This correlates the outbound request with the returned reply
corr_id = b"1"

# This is sent in the message callback when we get the respone
# This is sent in the message callback when we get the response
reply = None


# The MQTTv5 callback takes the additional 'props' parameter.
def on_connect(mqttc, userdata, flags, rc, props):
global client_id, reply_to

print("Connected: '"+str(flags)+"', '"+str(rc)+"', '"+str(props))
if hasattr(props, 'AssignedClientIdentifier'):
print("Connected: '" + str(flags) + "', '" + str(rc) + "', '" + str(props))
if hasattr(props, "AssignedClientIdentifier"):
client_id = props.AssignedClientIdentifier
reply_to = "replies/math/" + client_id
mqttc.subscribe(reply_to)
Expand All @@ -51,9 +52,9 @@ def on_connect(mqttc, userdata, flags, rc, props):
def on_message(mqttc, userdata, msg):
global reply

print(msg.topic+" "+str(msg.payload)+" "+str(msg.properties))
print(msg.topic + " " + str(msg.payload) + " " + str(msg.properties))
props = msg.properties
if not hasattr(props, 'CorrelationData'):
if not hasattr(props, "CorrelationData"):
print("No correlation ID")

# Match the response to the request correlation ID.
Expand All @@ -69,7 +70,7 @@ def on_message(mqttc, userdata, msg):
mqttc.on_message = on_message
mqttc.on_connect = on_connect

mqttc.connect(host='localhost', clean_start=True)
mqttc.connect(host="localhost", clean_start=True)
mqttc.loop_start()

# Wait for connection to set `client_id`, etc.
Expand All @@ -82,9 +83,9 @@ def on_message(mqttc, userdata, msg):
props.ResponseTopic = reply_to

# Uncomment to see what got set
#print("Client ID: "+client_id)
#print("Reply To: "+reply_to)
#print(props)
# print("Client ID: "+client_id)
# print("Reply To: "+reply_to)
# print(props)

# The requested operation, 'add' or 'mult'
func = sys.argv[1]
Expand All @@ -106,7 +107,6 @@ def on_message(mqttc, userdata, msg):

# Extract the response and print it.
rsp = json.loads(reply)
print("Response: "+str(rsp))
print("Response: " + str(rsp))

mqttc.loop_stop()

2 changes: 1 addition & 1 deletion examples/client_session_present.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def on_connect(mqttc, obj, flags, rc):
print("Second connection:")
elif obj == 2:
print("Third connection (with clean session=True):")
print(" Session present: " + str(flags['session present']))
print(" Session present: " + str(flags["session present"]))
print(" Connection result: " + str(rc))
mqttc.disconnect()

Expand Down
11 changes: 5 additions & 6 deletions examples/client_sub-class.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,20 @@


class MyMQTTClass(mqtt.Client):

def on_connect(self, mqttc, obj, flags, rc):
print("rc: "+str(rc))
print("rc: " + str(rc))

def on_connect_fail(self, mqttc, obj):
print("Connect failed")

def on_message(self, mqttc, obj, msg):
print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))
print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload))

def on_publish(self, mqttc, obj, mid):
print("mid: "+str(mid))
print("mid: " + str(mid))

def on_subscribe(self, mqttc, obj, mid, granted_qos):
print("Subscribed: "+str(mid)+" "+str(granted_qos))
print("Subscribed: " + str(mid) + " " + str(granted_qos))

def on_log(self, mqttc, obj, level, string):
print(string)
Expand All @@ -57,4 +56,4 @@ def run(self):
mqttc = MyMQTTClass()
rc = mqttc.run()

print("rc: "+str(rc))
print("rc: " + str(rc))
Loading