import re
LEVEL_RE = re.compile(r'^([0-9]{1,2})-([0-9]{1,2})( secret)?$')
COMBINER_RE = re.compile(r'[ ]*(and|or)[ ]*')
class UnlockParseError(ValueError):
# todo: is this the proper way to make an Error?
pass
def parseUnlockText(text):
return _parseUnlockBit(text.lower())
def _parseUnlockBit(text):
# blank criterion
if text == '':
return ('always',)
# is this a simple one...?
m = LEVEL_RE.match(text)
if m:
one, two, secret = m.groups()
return ('level', int(one), int(two), (secret != None))
# OK, let's parse parentheses
pLevel = 0
endAt = len(text) - 1
# this could be either AND or OR or nothing at all
# we won't know it until we finish parsing!
whatCombiner = None
subTerms = []
currentSubTermStart = None
skip = 0
for index, char in enumerate(text):
if skip > 0:
skip -= 1
continue
if char == '(':
if pLevel == 0:
currentSubTermStart = index
pLevel += 1
elif char == ')':
pLevel -= 1
if pLevel < 0:
raise UnlockParseError('close parenthesis without a matching open')
elif pLevel == 0:
subTerms.append((currentSubTermStart, index, text[currentSubTermStart+1:index]))
# are we expecting to see something else?
if index == endAt: break
m = COMBINER_RE.match(text, index + 1)
if not m:
raise UnlockParseError('something unexpected at position %d' % (index+1))
# what is it?
nextCombiner = m.group(1)
if whatCombiner is not None and nextCombiner != whatCombiner:
raise UnlockParseError('mixed %s and %s in one term. use more parentheses!' % (whatCombiner,nextCombiner))
whatCombiner = nextCombiner
# go right past this, to the next subterm
skip = len(m.group(0))
if (index + skip) == endAt:
raise UnlockParseError('%s what?!' % (whatCombiner.upper()))
else:
if pLevel == 0:
if index == 0:
raise UnlockParseError('that\'s not right')
else:
raise UnlockParseError('something unexpected at position %d' % index)
if pLevel > 0:
raise UnlockParseError('unclosed parenthesis')
# now that we're here, we must have parsed these subterms
# do we have a combiner?
if whatCombiner is None:
if len(subTerms) != 1:
raise UnlockParseError('unclosed parenthesis')
return _parseUnlockBit(subTerms[0][2])
else:
return (whatCombiner, map(lambda x: _parseUnlockBit(x[2]), subTerms))
def stringifyUnlockData(data):
kind = data[0]
if kind == 'always':
return ''
elif kind == 'level':
return '%02d-%02d%s' % (data[1], data[2], (' secret' if data[3] else ''))
elif kind == 'and' or kind == 'or':
return (' %s ' % kind).join(map(lambda x: '(%s)' % stringifyUnlockData(x), data[1]))
if __name__ == '__main__':
p1 = parseUnlockText('((01-01 secret) and (01-02)) or (02-99 secret) or (01-01)')
p2 = parseUnlockText('(1-1 secret) or ((1-2) and (1-3 secret)) or (2-1)')
print
print repr(p1)
print
print stringifyUnlockData(p1)
print
print repr(p2)
print
print stringifyUnlockData(p2)
from sys import exit
exit()
from common import *
class KPUnlockSpecDialog(QtGui.QDialog):
def __init__(self, forWhat, unlockAdjective):
QtGui.QDialog.__init__(self)
self.setWindowTitle('Set Unlock Criteria')
text = """You may enter various criteria that must be fulfilled for this {0} to be {1}.
Here are some examples of what you can use: