def flatten(l): for el in l: if isinstance(el, Iterable) and not is_string(el): for sub in flatten(el): yield sub else: yield el
def __init__(self, card_source): self.card_source = card_source # needed for test suite if is_string(card_source): # check if user is looking for complete set: match = re.search(r'set=\+\[(?P<set>[\w]+)\]', card_source) self.expansions = match.groups() if match else None self._document = None
def extract(self): cards = [] for component in self.document.cssselect('td.cardComponentContainer'): if not component.getchildren(): continue # do not parse empty components labels = component.cssselect('div.label') values = component.cssselect('div.value') pairs = zip(labels, values) card = Card() attributes = {} for (label, value) in pairs: attr = label.text_content().strip(': \n\r') \ .replace(' ', '_').lower() attr = attribute_map.get(attr) or attr if attr == 'p/t': attributes['power'], attributes['toughness'] = \ self.pow_tgh(value) elif attr == 'rules_text': attributes[attr] = self.box_field(value, 'div.cardtextbox', ' ; ') elif attr == 'printings': attributes[attr] = self.printings(value, 'img') attributes['printings_full'] = self.printings(value, 'img', full=True) elif attr == 'rarity': continue elif attr == 'flavor_text': attributes[attr] = self.box_field(value, 'div.flavortextbox', '\n') elif attr == 'mana_cost': attributes[attr] = self.symbol_field(value, 'img') elif attr == 'types': attributes['types'], attributes['subtypes'] = self.types( value.text_content().strip()) elif attr == 'community_rating': attributes['community_rating'] = self.text_field( value, 'span.textRatingValue') attributes['community_votes'] = self.text_field( value, 'span.totalVotesValue') else: attributes[attr] = value.text_content().strip() for a, v in attributes.items(): if is_string(v): v = clean_dashes(v) setattr(card, a, v) for ruling in component.cssselect('tr.post'): date, text = ruling.cssselect('td') card.ruling_data.append( (date.text_content(), text.text_content())) cards.append(card) return cards
def get_conditions(self): conditions = [] for name, value in self.data.items(): if not is_string(value): continue if name == 'color': self.lexer = self.getlexer('single_character') else: self.lexer = self.getlexer('freeform') fl = SearchFilter(name, keywords=[]) fl.add_keywords(self.parse(value)) conditions.append(fl) return conditions
def get_conditions(self): conditions = {} for name, value in self.data.items(): if not is_string(value): continue if name == 'color': value = self.preprocess_color(value) self.lexer = self.getlexer('single_character') else: self.lexer = self.getlexer('freeform') fl = SearchFilter(name, keywords=[]) fl.add_keywords(self.parse(value)) conditions[fl.name] = fl return conditions
def extract(self): cards = [] for component in self.document.cssselect('td.cardComponentContainer'): if not component.getchildren(): continue # do not parse empty components labels = component.cssselect('div.label') values = component.cssselect('div.value') pairs = zip(labels, values) card = Card() attributes = {} for (label, value) in pairs: attr = label.text_content().strip(': \n\r') \ .replace(' ', '_').lower() attr = attribute_map.get(attr) or attr if attr == 'p/t': attributes['power'], attributes['toughness'] = \ self.pow_tgh(value) elif attr == 'rules_text': attributes[attr] = self.box_field(value, 'div.cardtextbox', ' ; ') elif attr == 'printings': attributes[attr] = self.printings(value, 'img') attributes['printings_full'] = self.printings(value, 'img', full=True) elif attr == 'rarity': continue elif attr == 'flavor_text': attributes[attr] = self.box_field(value, 'div.cardtextbox', '\n') elif attr == 'mana_cost': attributes[attr] = self.symbol_field(value, 'img') elif attr == 'types': attributes['types'], attributes['subtypes'] = self.types(value.text_content().strip()) elif attr == 'community_rating': attributes['community_rating'] = self.text_field( value, 'span.textRatingValue') attributes['community_votes'] = self.text_field( value, 'span.totalVotesValue') else: attributes[attr] = value.text_content().strip() for a, v in attributes.items(): if is_string(v): v = clean_dashes(v) setattr(card, a, v) for ruling in component.cssselect('tr.post'): date, text = ruling.cssselect('td') card.ruling_data.append((date.text_content(), text.text_content())) cards.append(card) return cards
def render_term(self): if is_string(self.term) and ' ' in self.term: return '"{0}"'.format(self.term) else: return self.term