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

COAP-22 remove 32 code errors in python code #13

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
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
26 changes: 16 additions & 10 deletions coap/coap.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def close(self):

#===== client

def GET(self,uri,confirmable=True,options=[]):
def GET(self,uri,confirmable=True,options=None):
log.debug('GET {0}'.format(uri))
response = self._transmit(
uri = uri,
Expand All @@ -69,7 +69,7 @@ def GET(self,uri,confirmable=True,options=[]):
log.debug('response: {0}'.format(response))
return response['payload']

def PUT(self,uri,confirmable=True,options=[],payload=None):
def PUT(self,uri,confirmable=True,options=None,payload=None):
response = self._transmit(
uri = uri,
confirmable = confirmable,
Expand All @@ -80,7 +80,7 @@ def PUT(self,uri,confirmable=True,options=[],payload=None):
log.debug('response: {0}'.format(response))
return response['payload']

def POST(self,uri,confirmable=True,options=[],payload=None):
def POST(self,uri,confirmable=True,options=None,payload=None):
response = self._transmit(
uri = uri,
confirmable = confirmable,
Expand All @@ -91,7 +91,7 @@ def POST(self,uri,confirmable=True,options=[],payload=None):
log.debug('response: {0}'.format(response))
return response['payload']

def DELETE(self,uri,confirmable=True,options=[]):
def DELETE(self,uri,confirmable=True,options=None):
self._transmit(
uri = uri,
confirmable = confirmable,
Expand All @@ -113,7 +113,13 @@ def addResource(self,newResource):

#===== transmit

def _transmit(self,uri,confirmable,code,options=[],payload=[]):
def _transmit(self,uri,confirmable,code,options=None,payload=None):
if options is None:
options = []

if payload is None:
payload = []

assert code in d.METHOD_ALL
if code in [d.METHOD_GET,d.METHOD_DELETE]:
assert payload==[]
Expand Down Expand Up @@ -148,7 +154,7 @@ def _transmit(self,uri,confirmable,code,options=[],payload=[]):

def _getMessageID(self,destIp,destPort):
'''
\pre transmittersLock is already acquired.
pre transmittersLock is already acquired.
'''
with self.transmittersLock:
self._cleanupTransmitter()
Expand All @@ -166,7 +172,7 @@ def _getMessageID(self,destIp,destPort):

def _getToken(self,destIp,destPort):
'''
\pre transmittersLock is already acquired.
pre transmittersLock is already acquired.
'''
with self.transmittersLock:
self._cleanupTransmitter()
Expand Down Expand Up @@ -227,9 +233,9 @@ def _receive(self,timestamp,sender,rawbytes):
# find resource that matches this path
resource = None
with self.resourceLock:
for r in self.resources:
if r.matchesPath(path):
resource = r
for _r in self.resources:
if _r.matchesPath(path):
resource = _r
break
log.debug('resource={0}'.format(resource))

Expand Down
8 changes: 7 additions & 1 deletion coap/coapMessage.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ def sortOptions(options):
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
'''

def buildMessage(msgtype,token,code,messageId,options=[],payload=[]):
def buildMessage(msgtype,token,code,messageId,options=None,payload=None):
if options is None:
options = []

if payload is None:
payload = []

assert msgtype in d.TYPE_ALL
assert code in d.METHOD_ALL+d.COAP_RC_ALL

Expand Down
8 changes: 5 additions & 3 deletions coap/coapOption.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,10 @@ def getPayloadBytes(self):

class Block2(coapOption):

def __init__(self,num=None,m=None,szx=None,rawbytes=[]):

def __init__(self,num=None,m=None,szx=None,rawbytes=None):
if rawbytes is None:
rawbytes = []

if rawbytes:
assert num==None
assert m==None
Expand Down Expand Up @@ -181,7 +183,7 @@ def getPayloadBytes(self):
#============================ functions =======================================

def parseOption(message,previousOptionNumber):
'''
r'''
\brief Extract an option from the beginning of a message.

\param[in] message A list of bytes.
Expand Down
8 changes: 4 additions & 4 deletions coap/coapResource.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ def __init__(self,path):

#======================== abstract methods ================================

def GET(self,options=[]):
def GET(self,options=None):
raise e.coapRcMethodNotAllowed()

def PUT(self,options=[],payload=None):
def PUT(self,options=None,payload=None):
raise e.coapRcMethodNotAllowed()

def POST(self,options=[],payload=None):
def POST(self,options=None,payload=None):
raise e.coapRcMethodNotAllowed()

def DELETE(self,options=[]):
def DELETE(self,options=None):
raise e.coapRcMethodNotAllowed()

#======================== public ==========================================
Expand Down
10 changes: 5 additions & 5 deletions coap/coapTransmitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def emit(self, record):
import coapMessage as m

class coapTransmitter(threading.Thread):
'''
r'''
\brief A class which takes care of transmitting a CoAP message.

It handles:
Expand Down Expand Up @@ -48,7 +48,7 @@ class coapTransmitter(threading.Thread):
]

def __init__(self,sendFunc,srcIp,srcPort,destIp,destPort,confirmable,messageId,code,token,options,payload,ackTimeout,respTimeout):
'''
r'''
\brief Initilizer function.

This function initializes this instance by recording everything about
Expand Down Expand Up @@ -109,8 +109,8 @@ def __init__(self,sendFunc,srcIp,srcPort,destIp,destPort,confirmable,messageId,c
self.endLock = threading.Lock() # released when done communicating
self.stateLock = threading.RLock() # busy setting or getting FSM state
self.rxMsgEvent = threading.Event()
self.receivedACK = None
self.receivedResp = None
self.receivedACK = (None, None, None, None)
self.receivedResp = (None, None, None, None)
self.coapResponse = None
self.coapError = None
self.state = self.STATE_INIT # current state of the FSM
Expand Down Expand Up @@ -154,7 +154,7 @@ def __init__(self,sendFunc,srcIp,srcPort,destIp,destPort,confirmable,messageId,c
#======================== public ==========================================

def transmit(self):
'''
r'''
\brief Start the interaction with the destination, including waiting
for transport-level ACK (if needed), waiting for an app-level
response, and ACKing that (if needed)
Expand Down
10 changes: 5 additions & 5 deletions coap/coapUri.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def emit(self, record):
import coapDefines as d

def uri2options(uri):
'''
r'''
\brief Converts a coap URI into a list of CoAP options.

Examples:
Expand Down Expand Up @@ -62,29 +62,29 @@ def uri2options(uri):
hostPort = uri.split('/')[0]
if (not host) or (not port):
# try format [aaaa::1]:1244
m = re.match('\[([0-9a-fA-F:]+)\]:([0-9]+)',hostPort)
m = re.match(r'\[([0-9a-fA-F:]+)\]:([0-9]+)',hostPort)
if m:
host = m.group(1)
port = int(m.group(2))
if (not host) or (not port):
# try format [aaaa::1]
m = re.match('\[([0-9a-fA-F:]+)\]',hostPort)
m = re.match(r'\[([0-9a-fA-F:]+)\]',hostPort)
if m:
host = m.group(1)
port = d.DEFAULT_UDP_PORT
if (not host) or (not port):
# try formats:
# 123.123.123.123:1234
# www.example.com:1234
m = re.match('([0-9a-zA-Z.\-\_]+):([0-9]+)',hostPort)
m = re.match(r'([0-9a-zA-Z.\-_]+):([0-9]+)',hostPort)
if m:
host = m.group(1)
port = int(m.group(2))
if (not host) or (not port):
# try formats:
# 123.123.123.123
# www.example.com
m = re.match('([0-9a-zA-Z.\-\_]+)',hostPort)
m = re.match(r'([0-9a-zA-Z.\-_]+)',hostPort)
if m:
host = m.group(1)
port = d.DEFAULT_UDP_PORT
Expand Down