def __init__(self, constantJSON='data/constants.json'): with open(constantJSON, 'r', encoding='utf8') as file: constants = json.load(file) # set informations self.sets = constants['sets'] self.setIds = {} for id, setDetails in self.sets.items(): self.setIds[setDetails['name']] = id # classes self.classes = constants.get('classes', {}) # special keywords to replace self.__specials = {} for key, values in constants['specials'].items(): cards = [CardDB.cleanName(card) for card in values] self.__specials[CardDB.cleanName(key)] = cards self.specialNames = self.__specials.keys() # alternative card names self.__translations = {} for key, alts in constants['alternative_names'].items(): org = CardDB.cleanName(key) if isinstance(alts, list): for alt in alts: self.__translations[CardDB.cleanName(alt)] = org else: self.__translations[CardDB.cleanName(alts)] = org self.alternativeNames = self.__translations.keys()
def __getCards(self, text): """look for [[cardname]]s in text and collect them""" cards = [] if len(text) < 6: return cards # regex for escaped (new reddit and some apps) and unescaped brackets for card in re.finditer(r'\\?\[\\?\[([^\]\\]{1,30})\\?\]\\?\]', text): card = card.group(1) log.debug("adding a card: %s", card) cleanCard = CardDB.cleanName(card) if cleanCard: log.debug("cleaned card name: %s", cleanCard) # slight spelling error? checkedCard = self.spellChecker.correct(cleanCard) if cleanCard != checkedCard: log.info("spelling fixed: %s -> %s", cleanCard, checkedCard) # is alternative name? checkedCard = self.constants.translateAlt(checkedCard) # add cardname if checkedCard not in cards: cards.append(checkedCard) else: log.info("duplicate card: %s", card) # sometimes cards are removed, get more to fill limit if len(cards) >= self.constants.CARD_LIMIT * 2: break return cards
def getCards(self, text): """look for [[cardname]]s in text and collect them securely""" cards = [] if len(text) < 6: return cards open_bracket = False card = '' # could be regex, but I rather not parse everything evil users are sending for i in range(1, len(text)): c = text[i] if open_bracket and c != ']': card += c if c == '[' and text[i - 1] == '[': open_bracket = True if c == ']' and open_bracket: if len(card) > 0: log.debug("adding a card: %s", card) cleanCard = CardDB.cleanName(card) if cleanCard: log.debug("cleaned card name: %s", cleanCard) # slight spelling error? checkedCard = self.spellChecker.correct(cleanCard) if cleanCard != checkedCard: log.info("spelling fixed: %s -> %s", cleanCard, checkedCard) # is alternative name? checkedCard = self.constants.translateAlt(checkedCard) # add cardname if checkedCard not in cards: cards.append(checkedCard) else: log.info("duplicate card: %s", card) card = '' open_bracket = False if len(cards) >= self.constants.CARD_LIMIT: break if len(card) > 30: card = '' open_bracket = False return cards
def test_CleanName(self): self.assertEqual(CardDB.cleanName('Ab: 1c'), 'abc')
def test_CleanName(self): self.assertEqual(CardDB.cleanName('Ab: 1c'), 'abc')