Signed-off-by: Mattias Andrée <maandree@operamail.com>
This commit is contained in:
Mattias Andrée 2013-04-04 01:30:38 +02:00
parent dc6afac7b7
commit de5f9c9d5a
3 changed files with 50 additions and 33 deletions

View file

@ -45,47 +45,53 @@ class Metadata():
''' '''
@staticmethod @staticmethod
def makeRestrictionLogic(restriction): def makeRestrictionLogic(restriction):
table = [(get_test((cell[:cell.index('=')],
cell[cell.index('=') + 1:]
))
for cell in clause.lower().replace('_', '').replace(' ', '').split('+'))
for clause in restriction
]
def get_test(cell): def get_test(cell):
strict = cell[0][-1] != '?' strict = cell[0][-1] != '?'
key = cell[0][:-2 if strict else -1] key = cell[0]
if not strict:
key = key[:-1]
invert = cell[1][0] == '!' invert = cell[1][0] == '!'
value = cell[1][1 if invert else 0:] value = cell[1][1 if invert else 0:]
class SITest(): class SITest():
def __init__(self, cellkey, cellvalue): def __init__(self, cellkey, cellvalue):
(self.cellkey, self.callvalue) = (key, value) (self.cellkey, self.cellvalue) = (cellkey, cellvalue)
def __call__(self, has): def __call__(self, has):
return False if key not in has else (value not in has[key]) return False if self.cellkey not in has else (self.cellvalue not in has[self.cellkey])
def __str__(self):
return 'si(%s : %s)' % (self.cellkey, self.callvalue)
class STest(): class STest():
def __init__(self, cellkey, cellvalue): def __init__(self, cellkey, cellvalue):
(self.cellkey, self.callvalue) = (key, value) (self.cellkey, self.cellvalue) = (cellkey, cellvalue)
def __call__(self, has): def __call__(self, has):
return False if key not in has else (value in has[key]) return False if self.cellkey not in has else (self.cellvalue in has[self.cellkey])
def __str__(self):
return 's(%s : %s)' % (self.cellkey, self.callvalue)
class ITest(): class ITest():
def __init__(self, cellkey, cellvalue): def __init__(self, cellkey, cellvalue):
(self.cellkey, self.callvalue) = (key, value) (self.cellkey, self.cellvalue) = (cellkey, cellvalue)
def __call__(self, has): def __call__(self, has):
return True if key not in has else (value not in has[key]) return True if self.cellkey not in has else (self.cellvalue not in has[self.cellkey])
def __str__(self):
return 'i(%s : %s)' % (self.cellkey, self.callvalue)
class NTest(): class NTest():
def __init__(self, cellkey, cellvalue): def __init__(self, cellkey, cellvalue):
(self.cellkey, self.callvalue) = (key, value) (self.cellkey, self.cellvalue) = (cellkey, cellvalue)
def __call__(self, has): def __call__(self, has):
return True if key not in has else (value in has[key]) return True if self.cellkey not in has else (self.cellvalue in has[self.cellkey])
def __str__(self):
return 'n(%s : %s)' % (self.cellkey, self.callvalue)
if strict and invert: return SITest(key, value) if strict and invert: return SITest(key, value)
if strict: return STest(key, value) if strict: return STest(key, value)
if invert: return ITest(key, value) if invert: return ITest(key, value)
return NTest(key, value) return NTest(key, value)
def logic(cells): class Logic():
for alternative in table: def __init__(self, table):
self.table = table
def __call__(self, cells):
for alternative in self.table:
ok = True ok = True
for cell in alternative: for cell in alternative:
if not cell(cells): if not cell(cells):
@ -95,14 +101,19 @@ class Metadata():
return True return True
return False return False
return logic table = [[get_test((cell[:cell.index('=')].upper(), cell[cell.index('=') + 1:]))
for cell in clause.replace('_', '').replace(' ', '').split('+')]
for clause in restriction
]
return Logic(table)
''' '''
Get ponies that pass restriction Get ponies that pass restriction
@param ponydir:str Pony directory, must end with `os.sep` @param ponydir:str Pony directory, must end with `os.sep`
@param logic:dict<str, str>bool Restriction test functor @param logic:(str)bool Restriction test functor
@return :list<str> Passed ponies @return :list<str> Passed ponies
''' '''
@staticmethod @staticmethod

View file

@ -635,7 +635,7 @@ class PonysayTool():
while data[sep] != '$$$': while data[sep] != '$$$':
sep += 1 sep += 1
meta = data[1 : sep] meta = data[1 : sep]
data = [] data = {}
for line in meta: for line in meta:
if ':' in line: if ':' in line:
key = line[:line.find(':')].strip() key = line[:line.find(':')].strip()
@ -644,7 +644,13 @@ class PonysayTool():
for c in 'ABCDEFGHIJKLMN OPQRSTUVWXYZ': for c in 'ABCDEFGHIJKLMN OPQRSTUVWXYZ':
test = test.replace(c, '') test = test.replace(c, '')
if (len(test) == 0) and (len(key) > 0): if (len(test) == 0) and (len(key) > 0):
data.append((key, makeset(value.replace(' ', '')))) vals = makeset(value.replace(' ', ''))
if key not in data:
data[key] = vals
else:
dset = data[key]
for val in vals:
dset.add(val)
everything.append((ponyfile[:-5], data)) everything.append((ponyfile[:-5], data))
import pickle import pickle
with open((ponydir + '/metadata').replace('//', '/'), 'wb') as file: with open((ponydir + '/metadata').replace('//', '/'), 'wb') as file:

View file

@ -509,7 +509,7 @@ class Ponysay():
ponies = {} ponies = {}
for ponydir in ponydirs: for ponydir in ponydirs:
for pony in Metadata.restrictedPonies(ponydir, logic): for pony in Metadata.restrictedPonies(ponydir, logic):
if (pony in ponies) and not (pony in ponies): # XXX and (pony not in passed) if (pony in oldponies) and not (pony in ponies): # XXX and (pony not in passed)
ponies[pony] = ponydir + pony + '.pony' ponies[pony] = ponydir + pony + '.pony'
if len(ponies) > 0: if len(ponies) > 0:
oldponies = ponies oldponies = ponies