diff --git a/src/miranda.py b/src/miranda.py index d443f5c..2df4179 100755 --- a/src/miranda.py +++ b/src/miranda.py @@ -39,19 +39,17 @@ def __init__(self, commands): # Traverses the list of available commands def traverse(self, tokens, tree): - retVal = [] + retVal = [] - # If there are no commands, or no user input, return null - if tree is None or len(tokens) == 0: - retVal = [] - # If there is only one word, only auto-complete the primary commands - elif len(tokens) == 1: - retVal = [x+' ' for x in tree if x.startswith(tokens[0])] - # Else auto-complete for the sub-commands - elif tokens[0] in tree.keys(): - retVal = self.traverse(tokens[1:],tree[tokens[0]]) + # If there are no commands, or no user input, return null + if tree is None or len(tokens) == 0: + retVal = [] + elif len(tokens) == 1: + retVal = [f'{x} ' for x in tree if x.startswith(tokens[0])] + elif tokens[0] in tree.keys(): + retVal = self.traverse(tokens[1:],tree[tokens[0]]) - return retVal + return retVal # Returns a list of possible commands that match the partial command that the user has entered def complete(self, text, state): @@ -182,23 +180,20 @@ def send(self,data,socket): #Receive network data def recv(self,size,socket): - if socket == False: - socket = self.ssock + if socket == False: + socket = self.ssock - if self.TIMEOUT: - socket.setblocking(0) - ready = select.select([socket], [], [], self.TIMEOUT)[0] - else: - socket.setblocking(1) - ready = True - - try: - if ready: - return socket.recv(size) - else: - return False - except: - return False + if self.TIMEOUT: + socket.setblocking(0) + ready = select.select([socket], [], [], self.TIMEOUT)[0] + else: + socket.setblocking(1) + ready = True + + try: + return socket.recv(size) if ready else False + except: + return False #Create new UDP socket on ip, bound to port def createNewListener(self,ip,port): @@ -225,39 +220,39 @@ def sender(self): #Parse a URL, return the host and the page def parseURL(self,url): - delim = '://' - host = False - page = False + delim = '://' + host = False + page = False - #Split the host and page - try: - (host,page) = url.split(delim)[1].split('/',1) - page = '/' + page - except: - #If '://' is not in the url, then it's not a full URL, so assume that it's just a relative path - page = url + #Split the host and page + try: + (host,page) = url.split(delim)[1].split('/',1) + page = f'/{page}' + except: + #If '://' is not in the url, then it's not a full URL, so assume that it's just a relative path + page = url - return (host,page) + return (host,page) #Pull the name of the device type from a device type string #The device type string looks like: 'urn:schemas-upnp-org:device:WANDevice:1' def parseDeviceTypeName(self,string): - delim1 = 'device:' - delim2 = ':' + delim1 = 'device:' + if delim1 in string and not string.endswith(delim1): + delim2 = ':' - if delim1 in string and not string.endswith(delim1): - return string.split(delim1)[1].split(delim2,1)[0] - return False + return string.split(delim1)[1].split(delim2,1)[0] + return False #Pull the name of the service type from a service type string #The service type string looks like: 'urn:schemas-upnp-org:service:Layer3Forwarding:1' def parseServiceTypeName(self,string): - delim1 = 'service:' - delim2 = ':' + delim1 = 'service:' + if delim1 in string and not string.endswith(delim1): + delim2 = ':' - if delim1 in string and not string.endswith(delim1): - return string.split(delim1)[1].split(delim2,1)[0] - return False + return string.split(delim1)[1].split(delim2,1)[0] + return False #Pull the header info for the specified HTTP header - case insensitive def parseHeader(self,data,header): @@ -280,18 +275,18 @@ def parseHeader(self,data,header): #Extract the contents of a single XML tag from the data def extractSingleTag(self,data,tag): - startTag = "<%s" % tag - endTag = "" % tag - - try: - tmp = data.split(startTag)[1] - index = tmp.find('>') - if index != -1: - index += 1 - return tmp[index:].split(endTag)[0].strip() - except: - pass - return None + startTag = f"<{tag}" + endTag = f"" + + try: + tmp = data.split(startTag)[1] + index = tmp.find('>') + if index != -1: + index += 1 + return tmp[index:].split(endTag)[0].strip() + except: + pass + return None #Parses SSDP notify and reply packets, and populates the ENUM_HOSTS dict def parseSSDPInfo(self,data,showUniq,verbose): @@ -1649,10 +1644,7 @@ def parseCliOpts(argc,argv,hp): #Toggle boolean values def toggleVal(val): - if val: - return False - else: - return True + return not val #Prompt for user input def getUserInput(hp,shellPrompt):