Clean up printf((...) and some imports

This commit is contained in:
Björn Freise 2019-04-23 11:37:18 +02:00
parent db4c8ec5ed
commit 5da871355a
15 changed files with 25 additions and 33 deletions

View file

@ -59,7 +59,7 @@ def main(): # pragma: no cover
"payload_file="]) "payload_file="])
except getopt.GetoptError as err: except getopt.GetoptError as err:
# print help information and exit: # print help information and exit:
print((str(err))) # will print something like "option -a not recognized" print(str(err)) # will print something like "option -a not recognized"
usage() usage()
sys.exit(2) sys.exit(2)
for o, a in opts: for o, a in opts:
@ -107,7 +107,7 @@ def main(): # pragma: no cover
usage() usage()
sys.exit(2) sys.exit(2)
response = client.get(path) response = client.get(path)
print((response.pretty_print())) print(response.pretty_print())
client.stop() client.stop()
elif op == "OBSERVE": elif op == "OBSERVE":
if path is None: if path is None:
@ -122,7 +122,7 @@ def main(): # pragma: no cover
usage() usage()
sys.exit(2) sys.exit(2)
response = client.delete(path) response = client.delete(path)
print((response.pretty_print())) print(response.pretty_print())
client.stop() client.stop()
elif op == "POST": elif op == "POST":
if path is None: if path is None:
@ -134,7 +134,7 @@ def main(): # pragma: no cover
usage() usage()
sys.exit(2) sys.exit(2)
response = client.post(path, payload) response = client.post(path, payload)
print((response.pretty_print())) print(response.pretty_print())
client.stop() client.stop()
elif op == "PUT": elif op == "PUT":
if path is None: if path is None:
@ -146,11 +146,11 @@ def main(): # pragma: no cover
usage() usage()
sys.exit(2) sys.exit(2)
response = client.put(path, payload) response = client.put(path, payload)
print((response.pretty_print())) print(response.pretty_print())
client.stop() client.stop()
elif op == "DISCOVER": elif op == "DISCOVER":
response = client.discover() response = client.discover()
print((response.pretty_print())) print(response.pretty_print())
client.stop() client.stop()
else: else:
print("Operation not recognized") print("Operation not recognized")

View file

@ -11,7 +11,7 @@ class CoAPForwardProxy(CoAP):
def __init__(self, host, port, multicast=False, cache=False): def __init__(self, host, port, multicast=False, cache=False):
CoAP.__init__(self, (host, port), multicast=multicast, cache=cache) CoAP.__init__(self, (host, port), multicast=multicast, cache=cache)
print(("CoAP Proxy start on " + host + ":" + str(port))) print("CoAP Proxy start on " + host + ":" + str(port))
def usage(): # pragma: no cover def usage(): # pragma: no cover

View file

@ -51,7 +51,7 @@ if __name__ == '__main__':
ping_cnt = 0 # global ping cnt ping_cnt = 0 # global ping cnt
print('COAP ping script') print('COAP ping script')
print(('COAP ping to: %s:%s...' % (host, port))) print('COAP ping to: %s:%s...' % (host, port))
try: try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
@ -69,8 +69,8 @@ if __name__ == '__main__':
msg += struct.pack("B", ping_no) msg += struct.pack("B", ping_no)
try : try :
print(('[0x%08X] Send ping:' % (ping_cnt), [hex(ord(c)) for c in msg])) print('[0x%08X] Send ping:' % (ping_cnt), [hex(ord(c)) for c in msg])
#Set the whole string # Set the whole string
s.sendto(msg, (host, port)) s.sendto(msg, (host, port))
s.settimeout(2 + sleep_sec) s.settimeout(2 + sleep_sec)
@ -81,11 +81,10 @@ if __name__ == '__main__':
# We need to check if ping peyload counter is the same in reply # We need to check if ping peyload counter is the same in reply
status = bytes(msg)[3] == bytes(reply)[3] status = bytes(msg)[3] == bytes(reply)[3]
print(('[0x%08X] Recv ping:' % (ping_cnt), [hex(ord(c)) for c in reply], 'ok' if status else 'fail')) print('[0x%08X] Recv ping:' % (ping_cnt), [hex(ord(c)) for c in reply], 'ok' if status else 'fail')
except socket.error as e: except socket.error as e:
print(('Error: socket.error: ', str(e))) print('Error: socket.error: ', str(e))
#print 'Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sleep(3) # Waiting to recover ;) sleep(3) # Waiting to recover ;)
except socket.timeout: except socket.timeout:
print("Error: closing socket") print("Error: closing socket")

View file

@ -12,7 +12,7 @@ class CoAPReverseProxy(CoAP):
CoAP.__init__(self, (host, port), xml_file=xml_file, multicast=multicast, starting_mid=starting_mid, CoAP.__init__(self, (host, port), xml_file=xml_file, multicast=multicast, starting_mid=starting_mid,
cache=cache) cache=cache)
print(("CoAP Proxy start on " + host + ":" + str(port))) print("CoAP Proxy start on " + host + ":" + str(port))
def usage(): # pragma: no cover def usage(): # pragma: no cover

View file

@ -26,8 +26,8 @@ class CoAPServer(CoAP):
self.add_resource('advanced/', AdvancedResource()) self.add_resource('advanced/', AdvancedResource())
self.add_resource('advancedSeparate/', AdvancedResourceSeparate()) self.add_resource('advancedSeparate/', AdvancedResourceSeparate())
print(("CoAP Server start on " + host + ":" + str(port))) print("CoAP Server start on " + host + ":" + str(port))
print((self.root.dump())) print(self.root.dump())
def usage(): # pragma: no cover def usage(): # pragma: no cover

View file

@ -23,8 +23,7 @@ class CoapLRUCache(CoapCache):
:param element: :param element:
:return: :return:
""" """
logger.debug("updating cache, key: %s, element: %s", \ logger.debug("updating cache, key: %s, element: %s", key.hashkey, element)
key.hashkey, element)
self.cache.update([(key.hashkey, element)]) self.cache.update([(key.hashkey, element)])
def get(self, key): def get(self, key):

View file

@ -1,9 +1,9 @@
import logging.config import logging
import os
import random import random
import socket import socket
import threading import threading
import time import time
import collections
from coapthon import defines from coapthon import defines
from coapthon.layers.blocklayer import BlockLayer from coapthon.layers.blocklayer import BlockLayer
@ -14,7 +14,6 @@ from coapthon.messages.message import Message
from coapthon.messages.request import Request from coapthon.messages.request import Request
from coapthon.messages.response import Response from coapthon.messages.response import Response
from coapthon.serializer import Serializer from coapthon.serializer import Serializer
import collections
__author__ = 'Giacomo Tanganelli' __author__ = 'Giacomo Tanganelli'

View file

@ -1,4 +1,4 @@
import logging.config import logging
import random import random
import socket import socket
import struct import struct

View file

@ -73,7 +73,6 @@ class Option(object):
else: else:
if value is not None: if value is not None:
value = bytes(value, "utf-8") value = bytes(value, "utf-8")
self._value = value self._value = value
@property @property

View file

@ -1,4 +1,4 @@
import logging.config import logging
import random import random
import socket import socket
import struct import struct

View file

@ -200,7 +200,6 @@ class Serializer(object):
elif opt_type == defines.STRING: elif opt_type == defines.STRING:
fmt += str(len(bytes(option.value, "utf-8"))) + "s" fmt += str(len(bytes(option.value, "utf-8"))) + "s"
values.append(bytes(option.value, "utf-8")) values.append(bytes(option.value, "utf-8"))
else: # OPAQUE else: # OPAQUE
for b in option.value: for b in option.value:
fmt += "B" fmt += "B"
@ -225,9 +224,6 @@ class Serializer(object):
else: else:
fmt += str(len(bytes(payload, "utf-8"))) + "s" fmt += str(len(bytes(payload, "utf-8"))) + "s"
values.append(bytes(payload, "utf-8")) values.append(bytes(payload, "utf-8"))
# for b in str(payload):
# fmt += "c"
# values.append(bytes(b, "utf-8"))
datagram = None datagram = None
if values[1] is None: if values[1] is None:

View file

@ -4,6 +4,7 @@ import random
import socket import socket
import struct import struct
import threading import threading
import collections
from coapthon import defines from coapthon import defines
from coapthon.layers.blocklayer import BlockLayer from coapthon.layers.blocklayer import BlockLayer
@ -17,7 +18,6 @@ from coapthon.messages.response import Response
from coapthon.resources.resource import Resource from coapthon.resources.resource import Resource
from coapthon.serializer import Serializer from coapthon.serializer import Serializer
from coapthon.utils import Tree, create_logging from coapthon.utils import Tree, create_logging
import collections
__author__ = 'Giacomo Tanganelli' __author__ = 'Giacomo Tanganelli'

View file

@ -1138,7 +1138,7 @@ class Tests(unittest.TestCase):
expected.token = None expected.token = None
expected.payload = "<value>0</value>" expected.payload = "<value>0</value>"
print((expected.pretty_print())) print(expected.pretty_print())
exchange7 = (req, expected) exchange7 = (req, expected)
self.current_mid += 1 self.current_mid += 1

View file

@ -105,8 +105,8 @@ class Tests(unittest.TestCase):
if expected is not None: if expected is not None:
datagram, source = sock.recvfrom(4096) datagram, source = sock.recvfrom(4096)
received_message = serializer.deserialize(datagram, source) received_message = serializer.deserialize(datagram, source)
print((received_message.pretty_print())) print(received_message.pretty_print())
print((expected.pretty_print())) print(expected.pretty_print())
if expected.type is not None: if expected.type is not None:
self.assertEqual(received_message.type, expected.type) self.assertEqual(received_message.type, expected.type)
if expected.mid is not None: if expected.mid is not None: