59 lines
2.1 KiB
Python
59 lines
2.1 KiB
Python
|
import re
|
||
|
from mtg import *
|
||
|
|
||
|
class Database:
|
||
|
def getCard(name):
|
||
|
pass
|
||
|
|
||
|
""" Text file database class
|
||
|
|
||
|
Loads the card database files found at http://www.yawgatog.com/resources/oracle/
|
||
|
"""
|
||
|
class TextDB(Database):
|
||
|
def __init__(self, filename):
|
||
|
self.filename = filename
|
||
|
def getCard(self, name):
|
||
|
card = None
|
||
|
with open(self.filename, 'r') as f:
|
||
|
inRecord = False
|
||
|
recordName = False
|
||
|
for line in f:
|
||
|
line = line.strip()
|
||
|
if not inRecord:
|
||
|
recordName = line
|
||
|
inRecord = True
|
||
|
if recordName.lower() == name.lower():
|
||
|
card = {
|
||
|
'name': recordName,
|
||
|
'type': '',
|
||
|
'attributes': [],
|
||
|
'cost': '',
|
||
|
'power': 0,
|
||
|
'toughness': 0
|
||
|
}
|
||
|
elif card and line:
|
||
|
# Load the data into the card
|
||
|
if not card['cost'] and re.match('^X?\d*[WBURG]*$', line):
|
||
|
card['cost'] = line
|
||
|
continue
|
||
|
if not card['type']:
|
||
|
attributes = line.split(' -- ')
|
||
|
type = attributes.pop(0)
|
||
|
card['type'] = type
|
||
|
card['attributes'] = attributes
|
||
|
if not card['power'] and not card['toughness'] and re.match('^\d+([+-]?\*)?\/\d+([+-]?\*)?$', line):
|
||
|
(card['power'], card['toughness']) = line.split('/')
|
||
|
elif not line:
|
||
|
# Finished with current record
|
||
|
if card:
|
||
|
# We're done here
|
||
|
break
|
||
|
else:
|
||
|
# Prepare to read in the next record
|
||
|
inRecord = False
|
||
|
return Card(card['name'], card['type'], card['attributes'], card['cost'], card['power'], card['toughness'])
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
db = TextDB('db.txt')
|
||
|
c = db.getCard('Aura Gnarlid')
|
||
|
print c
|