Guest User

Untitled

a guest
Jul 17th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. #!/usr/bin/env python2
  2.  
  3. """Convert a list of card names (with rarity) to MWS spoiler format"""
  4.  
  5. # -*- coding: utf-8 -*-
  6.  
  7. from __future__ import print_function
  8. import sys
  9. from lxml import objectify
  10.  
  11. def get_card_color(card):
  12. """Given a card lxml.objectify object, determine the color as named by the
  13. MWS spoiler format"""
  14. try:
  15. if len(card.color) >= 2:
  16. # More than one color: Gold card
  17. return 'Gld'
  18. else:
  19. return card.color
  20. except AttributeError:
  21. # Card has no color: Artifact
  22. return 'Art'
  23.  
  24. def get_card(oracle, cardname):
  25. """Given a cardname, return the oracle data on it"""
  26. try:
  27. return filter(lambda c: c.name == cardname, oracle.cards.card)[0]
  28. except IndexError:
  29. raise ValueError('No card named "' + cardname + '"')
  30.  
  31. def get_card_pt(card):
  32. try:
  33. return card.pt
  34. except AttributeError:
  35. return ''
  36.  
  37. def card_to_mws(oracle, cardname, rarity=None):
  38. """Given a card name, create the MWS spoiler for it"""
  39. #MWS Format example:
  40. #NB: space after colon is \t
  41. # Card Name: Aerie Mystics
  42. # Card Color: W
  43. # Mana Cost: 4W
  44. # Type & Class: Creature - Bird Cleric
  45. # Pow/Tou: 3/3
  46. # Card Text: Flying %1%G%U, %T: Creatures you control gain shroud until end of turn.
  47. # Flavor Text:
  48. # Artist:
  49. # Rarity: U
  50. # Card #: 1/145
  51. if cardname[0:3] in ('C: ', 'U: ', 'R: '):
  52. rarity = cardname[0]
  53. cardname = cardname[3:]
  54. card = get_card(oracle, cardname)
  55. _name = card.name
  56. _color = get_card_color(_name)
  57. _cost = unicode(card.manacost)
  58. _type = card.type
  59. _pt = get_card_pt(card)
  60. #_text = get_card_text(card)
  61. _text = unicode(card.Text)
  62. _rarity = rarity or 'C'
  63. _num = '79/79'
  64. print('Card Name: ' + _name)
  65. print('Card Color: ' + _color)
  66. print('Mana Cost: ' + _cost)
  67. print('Type & Class: ' + _type)
  68. print('Pow/Tou: ' + _pt)
  69. print('Card Text: ' + unicode(str(_text), errors='ignore'))
  70. print('Flavor Text: ')
  71. print('Artist: ')
  72. print('Rarity: ' + _rarity)
  73. print('Card #: 79/79')
  74.  
  75. if __name__ == '__main__':
  76. # NB: cards.xml must be massaged: Rename all <text> tags to <Text> or lxml will eat them
  77. with open('cards.xml') as f:
  78. oracle = objectify.fromstring(f.read())
  79.  
  80. with open('cardlist.txt') as f:
  81. for card in f:
  82. sys.stderr.write(card)
  83. card_to_mws(oracle, card.rstrip())
Add Comment
Please, Sign In to add comment