script that clears old setinfo and rarity and assigns new one based on mtg-data.txt

This commit is contained in:
Maxmtg
2013-03-04 12:11:54 +00:00
parent a6a8cad45d
commit e6a3e6ea90
3 changed files with 261 additions and 0 deletions

142
res/assignSetInfo.py Normal file
View File

@@ -0,0 +1,142 @@
#!/usr/bin/env python
import os,sys,fnmatch,re
pathToMtgData = "mtg-data.txt"
pathToSetsMatchTable = "mtgdata-sets-to-forge.txt"
class cis: # CardInSet
def __init__(self):
self.edition = ""
self.rarity = "C"
self.arts = 1
def __str__(self):
return "{} {} {}".format(self.edition, self.rarity, self.arts)
def __repr__(self):
return self.__str__()
def rarityFull(self):
if (self.rarity == "B"):
return "Land"
if (self.rarity == "R"):
return "Rare"
if (self.rarity == "U"):
return "Uncommon"
if (self.rarity == "S"):
return "Special"
if (self.rarity == "M"):
return "Mythic"
return "Common"
if __name__ == '__main__':
if not os.path.exists(pathToMtgData) :
print("This script requires the text version of Arch's mtg-data to be present.You can download it from slightlymagic.net's forum and either place the text version next to this script or edit this script and provide the path to the file at the top.")
print("Press Enter to exit")
raw_input("")
sys.exit()
setCodes = []
setCodeToName = {}
setCodeToForge = {}
mtgDataCards = {}
hasFetchedSets = False
hasFetchedCardName = False
tmpName = ""
line = ""
prevline = ""
#Parse mtg-data
print("Parsing mtg-data...")
with open(pathToMtgData) as mtgdata :
for line in mtgdata :
# Parse the sets at the top of the mtgdata file
if not hasFetchedSets :
if line != "\n" :
splitLine = line.split(' ')
code = splitLine[0]
setCodeToName[code] = splitLine[-1].replace('\n', '')
#print splitLine, code, setCodeToName[code]
setCodes.append(code)
else :
hasFetchedSets = True
# Once all sets are parsed, time to parse the cards
elif hasFetchedSets :
if not hasFetchedCardName :
tmpName = line.rstrip()
hasFetchedCardName = True
oracle = ""
else:
oracle += line
if line == "\n" :
#mtgOracleCards[tmpName] = oracle.replace(prevline, '')
sets = prevline.split(", ")
editions = []
for i in range(len(sets)):
ee = sets[i].split(' ')
editions.append(cis())
editions[i].edition = ee[0]
editions[i].rarity = ee[1]
if len(ee) > 2:
editions[i].arts = int(ee[2][2:3])
#print sets
mtgDataCards[tmpName] = editions
hasFetchedCardName = False
prevline = line
print("Matching mtg-data and Forge sets")
with open(pathToSetsMatchTable) as setsMatch :
for line in setsMatch:
if line[0:3] == "---":
code = line[3:].split(" ")[0]
setCodeToForge[code] = None
elif line[0:3] == "===":
code = line[3:].split(" ")[0]
setCodeToForge[code] = code;
else:
code1 = line.split(" ")[0]
code2 = line.split(" ")[1]
setCodeToForge[code1] = code2
folder = "cardsfolder"
for root, dirnames, filenames in os.walk(folder):
for fileName in fnmatch.filter(filenames, '*.txt'):
if fileName.startswith('.'):
continue
cardfile = open(os.path.join(root, fileName), 'r')
firstLine = cardfile.readline().strip()
cardName = firstLine[5:]
print (cardName, fileName)
validLines = []
validLines.append(firstLine)
for line in cardfile.readlines():
if line[:8] != "SetInfo:" and line[:8] != "SVar:Rar":
validLines.append(line.strip())
cardfile.close()
for e in mtgDataCards[cardName]:
if not setCodeToForge[e.edition] is None:
validLines.append( ("SetInfo:{} {}" if e.arts <= 1 else "SetInfo:{} {} x{}" ).format(setCodeToForge[e.edition], e.rarityFull(), e.arts) )
toWrite = "\n".join(validLines)
cardfile = open(os.path.join(root, fileName), 'w')
cardfile.write(toWrite)
cardfile.close();