3 import xml
.etree
.ElementTree
as ET
4 from collections
import defaultdict
9 def __init__(self
, name
=None, xml
=None, url
=None, txt
=None):
14 self
.parse_ponyhead_xml(xml
)
16 self
.parse_ponyhead_link(url
)
22 def description(self
):
24 colors
= defaultdict(int)
26 for card
in self
.cards
:
27 if card
['type'] == 'Mane':
28 mane
= '%s %s' % (card
['title'], mlpccg
.CardDb
.set_ids
[card
['set']].upper())
30 if card
['type'] == 'Friend':
31 colors
[card
['color']] += 1
33 colors_sorted
= filter(lambda c
: c
[1] >= 6, sorted(colors
.iteritems(), reverse
=True, key
=operator
.itemgetter(1)))
34 return '%s %s' % (mane
, '/'.join(map(lambda c
: c
[0], colors_sorted
)))
36 def parse_ponyhead_xml(self
, path
):
37 logging
.debug('loading decklist from xml')
40 root
= ET
.parse(path
).getroot()
41 card_names
= [element
.text
.strip() for element
in root
.findall('.//card/name')]
42 for card_name
in card_names
:
43 self
.cards
+= [mlpccg
.CardDb
.CARDDB
.by_name(card_name
)['id']]
45 logging
.exception('failed')
47 def parse_ponyhead_link(self
, link
):
48 logging
.debug('loading decklist by parsing url')
51 id_list
= [id.split('x') for id in link
.split('=')[-1].split('-')]
52 for id, amount
in id_list
:
53 self
.cards
+= [mlpccg
.CardDb
.CARDDB
.by_id(id)] * int(amount
)
55 logging
.exception('failed: %s', link
)
57 def parse_ponyhead_txt(self
):