Skip to content
Open
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
120 changes: 56 additions & 64 deletions src/miranda.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines -42 to +52
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CmdCompleter.traverse refactored with the following changes:

This removes the following comments ( why? ):

# If there is only one word, only auto-complete the primary commands
# Else auto-complete for the sub-commands


# Returns a list of possible commands that match the partial command that the user has entered
def complete(self, text, state):
Expand Down Expand Up @@ -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
Comment on lines -185 to +196
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function upnp.recv refactored with the following changes:


#Create new UDP socket on ip, bound to port
def createNewListener(self,ip,port):
Expand All @@ -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)
Comment on lines -228 to +235
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function upnp.parseURL refactored with the following changes:


#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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function upnp.parseDeviceTypeName refactored with the following changes:


#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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function upnp.parseServiceTypeName refactored with the following changes:


#Pull the header info for the specified HTTP header - case insensitive
def parseHeader(self,data,header):
Expand All @@ -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 = "</%s>" % 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"</{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
Comment on lines -283 to +289
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function upnp.extractSingleTag refactored with the following changes:


#Parses SSDP notify and reply packets, and populates the ENUM_HOSTS dict
def parseSSDPInfo(self,data,showUniq,verbose):
Expand Down Expand Up @@ -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
Comment on lines -1652 to +1647
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function toggleVal refactored with the following changes:


#Prompt for user input
def getUserInput(hp,shellPrompt):
Expand Down