This poorly commented piece of code will convert a MC speedwalk string to Zmud stacked dirs and vice-versa:
def fnConvertDirs(strDirs, strFormat = 0):
"""0: Zmud to MUSHclient
1: MUSHclient to Zmud"""
lstNewDirs = []
if strFormat == 0:
# Strip list items.
lstDirs = map(string.strip, strDirs.split(";"))
strRepeat = ""
# Repeat commands as many times as required.
for strCommand in lstDirs:
if strCommand[0] == "#":
for strLetter in strCommand[1:]:
if strLetter.isdigit():
strRepeat += strLetter
else:
for intCounter in range(int(strRepeat)):
lstNewDirs.append(strCommand[strCommand.find(strLetter):].strip())
strRepeat = ""
break
else:
lstNewDirs.append(strCommand)
# Enclose long commands in parentheses.
lstNewDirs = map(lambda x: len(x) > 1 and "(" + x + ")" or x, lstNewDirs)
intOccurrenceCounter = 1
lstDirs = []
# Append an empty element so the following algorithm works.
lstNewDirs.append("")
# Join adjacent same dirs in one ("n n" -> "2n").
for intCounter in range(1, len(lstNewDirs)):
if lstNewDirs[intCounter - 1] == lstNewDirs[intCounter]:
intOccurrenceCounter += 1
else:
if intOccurrenceCounter > 1:
lstDirs.append(str(intOccurrenceCounter) + lstNewDirs[intCounter - 1])
else:
lstDirs.append(lstNewDirs[intCounter - 1])
intOccurrenceCounter = 1
return(string.join(lstDirs))
else:
strTemp = ""
strTimes = ""
blnInCommand = False
blnEvaluate = True
for strLetter in strDirs:
if strLetter == "(":
blnInCommand = True
elif strLetter == "/" and blnInCommand:
blnEvaluate = False
elif strLetter == ")" and blnInCommand:
blnInCommand = False
if strTimes != "":
lstNewDirs.append("#" + strTimes + " " + strTemp + ";")
else:
lstNewDirs.append(strTemp + ";")
blnEvaluate = True
strTimes = ""
strTemp = ""
elif strLetter.isdigit() and not blnInCommand:
strTimes += strLetter
elif strLetter.isspace() and not blnInCommand:
pass
elif blnInCommand:
if blnEvaluate:
strTemp += strLetter
else:
if strTimes != "":
lstNewDirs.append("#" + strTimes + " " + strLetter + ";")
else:
lstNewDirs.append(strLetter + ";")
strTimes = ""
strTemp = ""
return(string.join(lstNewDirs)[:-1])