pep8
This commit is contained in:
parent
a7cc600ef7
commit
9a5dac3a55
159
dominion_tabs.py
159
dominion_tabs.py
@ -24,6 +24,7 @@ def split(l, n):
|
||||
|
||||
|
||||
class Card:
|
||||
|
||||
def __init__(self, name, cardset, types, cost, description='', potcost=0):
|
||||
self.name = name.strip()
|
||||
self.cardset = cardset.strip()
|
||||
@ -62,6 +63,7 @@ class Card:
|
||||
|
||||
|
||||
class BlankCard(Card):
|
||||
|
||||
def __init__(self, num):
|
||||
Card.__init__(self, str(num), 'extra', ('Blank',), 0)
|
||||
|
||||
@ -70,6 +72,7 @@ class BlankCard(Card):
|
||||
|
||||
|
||||
class CardType:
|
||||
|
||||
def __init__(self, typeNames, tabImageFile, tabTextHeightOffset=0, tabCostHeightOffset=-1):
|
||||
self.typeNames = typeNames
|
||||
self.tabImageFile = tabImageFile
|
||||
@ -95,6 +98,7 @@ class CardType:
|
||||
def getTabCostHeightOffset(self):
|
||||
return self.tabCostHeightOffset
|
||||
|
||||
|
||||
class DominionTabs:
|
||||
cardTypes = [
|
||||
CardType(('Action',), 'action.png'),
|
||||
@ -188,11 +192,14 @@ class DominionTabs:
|
||||
|
||||
def add_inline_images(self, text, fontsize):
|
||||
path = os.path.join(self.filedir, 'images')
|
||||
replace = '<img src='"'%s/coin_small_\\1.png'"' width=%d height='"'100%%'"' valign='"'middle'"'/>' % (path,fontsize*1.2)
|
||||
replace = '<img src='"'%s/coin_small_\\1.png'"' width=%d height='"'100%%'"' valign='"'middle'"'/>' % (
|
||||
path, fontsize * 1.2)
|
||||
text = re.sub('(\d)\s(c|C)oin(s)?', replace, text)
|
||||
replace = '<img src='"'%s/coin_small_question.png'"' width=%d height='"'100%%'"' valign='"'middle'"'/>' % (path,fontsize*1.2)
|
||||
replace = '<img src='"'%s/coin_small_question.png'"' width=%d height='"'100%%'"' valign='"'middle'"'/>' % (
|
||||
path, fontsize * 1.2)
|
||||
text = re.sub('\?\s(c|C)oin(s)?', replace, text)
|
||||
replace = '<img src='"'%s/victory_emblem.png'"' width=%d height='"'120%%'"' valign='"'middle'"'/>' % (path,fontsize*1.5)
|
||||
replace = '<img src='"'%s/victory_emblem.png'"' width=%d height='"'120%%'"' valign='"'middle'"'/>' % (
|
||||
path, fontsize * 1.5)
|
||||
text = re.sub('\<VP\>', replace, text)
|
||||
return text
|
||||
|
||||
@ -206,7 +213,8 @@ class DominionTabs:
|
||||
self.canvas.translate(self.tabWidth, 0)
|
||||
self.canvas.scale(-1, 1)
|
||||
if not self.options.cropmarks and not isBack:
|
||||
#don't draw outline on back, in case lines don't line up with front
|
||||
# don't draw outline on back, in case lines don't line up with
|
||||
# front
|
||||
if isExpansionDivider and self.options.centre_expansion_dividers:
|
||||
outline = self.expansionTabOutline
|
||||
else:
|
||||
@ -223,9 +231,11 @@ class DominionTabs:
|
||||
self.canvas.scale(-1, 1)
|
||||
if cropmarksleft or cropmarksright:
|
||||
self.canvas.line(-2 * cmw, 0, -cmw, 0)
|
||||
self.canvas.line(-2*cmw,self.tabBaseHeight,-cmw,self.tabBaseHeight)
|
||||
self.canvas.line(-2 * cmw,
|
||||
self.tabBaseHeight, -cmw, self.tabBaseHeight)
|
||||
if y > 0:
|
||||
self.canvas.line(-2*cmw,self.tabHeight,-cmw,self.tabHeight)
|
||||
self.canvas.line(-2 * cmw,
|
||||
self.tabHeight, -cmw, self.tabHeight)
|
||||
if mirror:
|
||||
self.canvas.restoreState()
|
||||
|
||||
@ -269,9 +279,11 @@ class DominionTabs:
|
||||
potHeight = y - 3
|
||||
potSize = 11
|
||||
|
||||
self.canvas.drawImage(os.path.join(self.filedir,'images','coin_small.png'),x,coinHeight,16,16,preserveAspectRatio=True,mask='auto')
|
||||
self.canvas.drawImage(os.path.join(self.filedir, 'images', 'coin_small.png'),
|
||||
x, coinHeight, 16, 16, preserveAspectRatio=True, mask='auto')
|
||||
if card.potcost:
|
||||
self.canvas.drawImage(os.path.join(self.filedir,'images','potion.png'),x+17,potHeight,potSize,potSize,preserveAspectRatio=True,mask=[255,255,255,255,255,255])
|
||||
self.canvas.drawImage(os.path.join(self.filedir, 'images', 'potion.png'), x + 17,
|
||||
potHeight, potSize, potSize, preserveAspectRatio=True, mask=[255, 255, 255, 255, 255, 255])
|
||||
width += potSize
|
||||
|
||||
self.canvas.setFont('MinionPro-Bold', 12)
|
||||
@ -283,7 +295,8 @@ class DominionTabs:
|
||||
|
||||
def drawSetIcon(self, setImage, x, y):
|
||||
# set image
|
||||
self.canvas.drawImage(os.path.join(self.filedir,'images',setImage), x, y, 14, 12, mask='auto')
|
||||
self.canvas.drawImage(
|
||||
os.path.join(self.filedir, 'images', setImage), x, y, 14, 12, mask='auto')
|
||||
|
||||
@classmethod
|
||||
def nameWidth(self, name, fontSize):
|
||||
@ -293,7 +306,8 @@ class DominionTabs:
|
||||
if i != 0:
|
||||
w += pdfmetrics.stringWidth(' ', 'MinionPro-Regular', fontSize)
|
||||
w += pdfmetrics.stringWidth(part[0], 'MinionPro-Regular', fontSize)
|
||||
w += pdfmetrics.stringWidth(part[1:],'MinionPro-Regular',fontSize-2)
|
||||
w += pdfmetrics.stringWidth(part[1:],
|
||||
'MinionPro-Regular', fontSize - 2)
|
||||
return w
|
||||
|
||||
def drawTab(self, card, rightSide):
|
||||
@ -308,14 +322,17 @@ class DominionTabs:
|
||||
else:
|
||||
self.canvas.translate(0, self.tabHeight - self.tabLabelHeight)
|
||||
|
||||
textWidth = self.tabLabelWidth - 6 # allow for 3 pt border on each side
|
||||
textHeight = self.tabLabelHeight/2-7+card.getType().getTabTextHeightOffset()
|
||||
# allow for 3 pt border on each side
|
||||
textWidth = self.tabLabelWidth - 6
|
||||
textHeight = self.tabLabelHeight / 2 - 7 + \
|
||||
card.getType().getTabTextHeightOffset()
|
||||
|
||||
# draw banner
|
||||
img = card.getType().getNoCoinTabImageFile()
|
||||
if img:
|
||||
self.canvas.drawImage(os.path.join(self.filedir, 'images', img), 1, 0,
|
||||
self.tabLabelWidth - 2, self.tabLabelHeight - 1,
|
||||
self.tabLabelWidth -
|
||||
2, self.tabLabelHeight - 1,
|
||||
preserveAspectRatio=False, anchor='n', mask='auto')
|
||||
|
||||
# draw cost
|
||||
@ -376,6 +393,7 @@ class DominionTabs:
|
||||
words = line.split()
|
||||
if rightSide or not self.options.edge_align_name:
|
||||
w = textInset
|
||||
|
||||
def drawWordPiece(text, fontSize):
|
||||
self.canvas.setFont('MinionPro-Regular', fontSize)
|
||||
if text != ' ':
|
||||
@ -393,6 +411,7 @@ class DominionTabs:
|
||||
|
||||
w = self.tabLabelWidth - textInsetRight - 3
|
||||
words.reverse()
|
||||
|
||||
def drawWordPiece(text, fontSize):
|
||||
self.canvas.setFont('MinionPro-Regular', fontSize)
|
||||
if text != ' ':
|
||||
@ -481,7 +500,8 @@ class DominionTabs:
|
||||
|
||||
# actual drawing
|
||||
if not self.options.tabs_only:
|
||||
self.drawOutline(x, y, rightSide, useExtra, card.getType().getTypeNames() == ('Expansion',))
|
||||
self.drawOutline(
|
||||
x, y, rightSide, useExtra, card.getType().getTypeNames() == ('Expansion',))
|
||||
self.drawTab(card, rightSide)
|
||||
if not self.options.tabs_only:
|
||||
self.drawText(card, useExtra)
|
||||
@ -549,7 +569,8 @@ class DominionTabs:
|
||||
# To solve:
|
||||
|
||||
# 1)
|
||||
# try to figure out if this a 'basic action' like +X Cards or +Y Actions
|
||||
# try to figure out if this a 'basic action' like +X Cards or +Y
|
||||
# Actions
|
||||
descriptions = [card.description]
|
||||
while True:
|
||||
m = self.baseactionRE.match(line, re.UNICODE)
|
||||
@ -585,11 +606,13 @@ class DominionTabs:
|
||||
potcost = 0
|
||||
currentCard = Card(m.groupdict()["name"].strip(),
|
||||
m.groupdict()["set"].lower().strip(),
|
||||
tuple([t.strip() for t in m.groupdict()["type"].split("-")]),
|
||||
tuple(
|
||||
[t.strip() for t in m.groupdict()["type"].split("-")]),
|
||||
m.groupdict()["cost"],
|
||||
'',
|
||||
potcost)
|
||||
self.add_definition_line(currentCard, m.groupdict()["description"])
|
||||
self.add_definition_line(
|
||||
currentCard, m.groupdict()["description"])
|
||||
cards.append(currentCard)
|
||||
elif line:
|
||||
assert currentCard
|
||||
@ -620,7 +643,8 @@ class DominionTabs:
|
||||
'width': self.paperheight}]
|
||||
|
||||
for layout in layouts:
|
||||
availableMargin = layout['totalMarginHeight'] - layout['minMarginHeight']
|
||||
availableMargin = layout[
|
||||
'totalMarginHeight'] - layout['minMarginHeight']
|
||||
fontsize = availableMargin / fontHeightRelative
|
||||
fontsize = min(maxFontsize, fontsize)
|
||||
if fontsize >= minFontsize:
|
||||
@ -656,7 +680,8 @@ class DominionTabs:
|
||||
cards = split(cards, self.numTabsVertical * self.numTabsHorizontal)
|
||||
self.odd = True
|
||||
for pageNum, pageCards in enumerate(cards):
|
||||
#remember whether we start with odd or even divider for tab location
|
||||
# remember whether we start with odd or even divider for tab
|
||||
# location
|
||||
pageStartOdd = self.odd
|
||||
if not self.options.tabs_only and self.options.order != "global":
|
||||
self.drawSetNames(pageCards)
|
||||
@ -683,7 +708,8 @@ class DominionTabs:
|
||||
x = (self.numTabsHorizontal - 1 - i) % self.numTabsHorizontal
|
||||
y = i / self.numTabsHorizontal
|
||||
self.canvas.saveState()
|
||||
self.drawDivider(card,x,self.numTabsVertical-1-y,useExtra=True)
|
||||
self.drawDivider(
|
||||
card, x, self.numTabsVertical - 1 - y, useExtra=True)
|
||||
self.canvas.restoreState()
|
||||
self.odd = not self.odd
|
||||
self.canvas.showPage()
|
||||
@ -700,7 +726,8 @@ class DominionTabs:
|
||||
parser.add_option("--orientation", type="choice", choices=["horizontal", "vertical"],
|
||||
dest="orientation", default="horizontal",
|
||||
help="horizontal or vertical, default:horizontal")
|
||||
parser.add_option("--sleeved", action="store_true", dest="sleeved", help="use --size=sleeved instead")
|
||||
parser.add_option("--sleeved", action="store_true",
|
||||
dest="sleeved", help="use --size=sleeved instead")
|
||||
parser.add_option("--size", type="string", dest="size", default='normal',
|
||||
help="'<%f>x<%f>' (size in cm), or 'normal' = '9.1x5.9', or 'sleeved' = '9.4x6.15'")
|
||||
parser.add_option("--minmargin", type="string", dest="minmargin", default="1x1",
|
||||
@ -756,9 +783,12 @@ class DominionTabs:
|
||||
help='centre the tabs on expansion dividers')
|
||||
parser.add_option("--num_pages", type="int", default=-1,
|
||||
help="stop generating after this many pages, -1 for all")
|
||||
parser.add_option("--language", default='en_us', help="language of card texts")
|
||||
parser.add_option("--include_blanks", action="store_true", help="include a few dividers with extra text")
|
||||
parser.add_option("--exclude_events", action="store_true", default=False, help="exclude individual dividers for events")
|
||||
parser.add_option(
|
||||
"--language", default='en_us', help="language of card texts")
|
||||
parser.add_option("--include_blanks", action="store_true",
|
||||
help="include a few dividers with extra text")
|
||||
parser.add_option("--exclude_events", action="store_true",
|
||||
default=False, help="exclude individual dividers for events")
|
||||
|
||||
options, args = parser.parse_args(argstring)
|
||||
if not options.cost:
|
||||
@ -838,7 +868,8 @@ class DominionTabs:
|
||||
self.verticalBorderSpace = 0.01 * cm
|
||||
fixedMargins = True
|
||||
else:
|
||||
minmarginwidth, minmarginheight = self.parseDimensions(self.options.minmargin)
|
||||
minmarginwidth, minmarginheight = self.parseDimensions(
|
||||
self.options.minmargin)
|
||||
self.tabLabelWidth = self.options.tabwidth * cm
|
||||
self.tabLabelHeight = .9 * cm
|
||||
self.horizontalBorderSpace = 0 * cm
|
||||
@ -846,7 +877,8 @@ class DominionTabs:
|
||||
|
||||
self.tabHeight = self.tabBaseHeight + self.tabLabelHeight
|
||||
|
||||
#note: this is convenient, but somewhat inaccurate as the border space isn't actually part of the tab width
|
||||
# note: this is convenient, but somewhat inaccurate as the border space
|
||||
# isn't actually part of the tab width
|
||||
self.totalTabWidth = self.tabWidth + self.horizontalBorderSpace
|
||||
self.totalTabHeight = self.tabHeight + self.verticalBorderSpace
|
||||
|
||||
@ -855,10 +887,14 @@ class DominionTabs:
|
||||
|
||||
# as we don't draw anything in the final border, it shouldn't count towards how many tabs we can fit
|
||||
# so it gets added back in to the page size here
|
||||
numTabsVerticalP = int ((self.paperheight - 2*minmarginheight + self.verticalBorderSpace) / self.totalTabHeight)
|
||||
numTabsHorizontalP = int ((self.paperwidth - 2*minmarginwidth + self.horizontalBorderSpace) / self.totalTabWidth)
|
||||
numTabsVerticalL = int ((self.paperwidth - 2*minmarginwidth + self.verticalBorderSpace) / self.totalTabHeight)
|
||||
numTabsHorizontalL = int ((self.paperheight - 2*minmarginheight + self.horizontalBorderSpace) / self.totalTabWidth)
|
||||
numTabsVerticalP = int(
|
||||
(self.paperheight - 2 * minmarginheight + self.verticalBorderSpace) / self.totalTabHeight)
|
||||
numTabsHorizontalP = int(
|
||||
(self.paperwidth - 2 * minmarginwidth + self.horizontalBorderSpace) / self.totalTabWidth)
|
||||
numTabsVerticalL = int(
|
||||
(self.paperwidth - 2 * minmarginwidth + self.verticalBorderSpace) / self.totalTabHeight)
|
||||
numTabsHorizontalL = int(
|
||||
(self.paperheight - 2 * minmarginheight + self.horizontalBorderSpace) / self.totalTabWidth)
|
||||
|
||||
if numTabsVerticalL * numTabsHorizontalL > numTabsVerticalP * numTabsHorizontalP and not fixedMargins:
|
||||
self.numTabsVertical, self.numTabsHorizontal\
|
||||
@ -874,8 +910,10 @@ class DominionTabs:
|
||||
|
||||
if not fixedMargins:
|
||||
# dynamically max margins
|
||||
self.horizontalMargin = (self.paperwidth-self.numTabsHorizontal*self.totalTabWidth)/2
|
||||
self.verticalMargin = (self.paperheight-self.numTabsVertical*self.totalTabHeight)/2
|
||||
self.horizontalMargin = (
|
||||
self.paperwidth - self.numTabsHorizontal * self.totalTabWidth) / 2
|
||||
self.verticalMargin = (
|
||||
self.paperheight - self.numTabsVertical * self.totalTabHeight) / 2
|
||||
else:
|
||||
self.horizontalMargin = minmarginwidth
|
||||
self.verticalMargin = minmarginheight
|
||||
@ -895,7 +933,8 @@ class DominionTabs:
|
||||
(0, self.tabBaseHeight, 0, 0)]
|
||||
|
||||
self.expansionTabOutline = [(0, 0, self.tabWidth, 0),
|
||||
(self.tabWidth,0,self.tabWidth,self.tabBaseHeight),
|
||||
(self.tabWidth, 0, self.tabWidth,
|
||||
self.tabBaseHeight),
|
||||
(self.tabWidth, self.tabBaseHeight,
|
||||
self.tabWidth / 2 + self.tabLabelWidth / 2, self.tabBaseHeight),
|
||||
(self.tabWidth / 2 + self.tabLabelWidth / 2, self.tabBaseHeight,
|
||||
@ -910,21 +949,30 @@ class DominionTabs:
|
||||
|
||||
try:
|
||||
dirn = os.path.join(self.filedir, 'fonts')
|
||||
pdfmetrics.registerFont(TTFont('MinionPro-Regular',os.path.join(dirn,'MinionPro-Regular.ttf')))
|
||||
pdfmetrics.registerFont(TTFont('MinionPro-Bold',os.path.join(dirn,'MinionPro-Bold.ttf')))
|
||||
pdfmetrics.registerFont(
|
||||
TTFont('MinionPro-Regular', os.path.join(dirn, 'MinionPro-Regular.ttf')))
|
||||
pdfmetrics.registerFont(
|
||||
TTFont('MinionPro-Bold', os.path.join(dirn, 'MinionPro-Bold.ttf')))
|
||||
except:
|
||||
raise
|
||||
pdfmetrics.registerFont(TTFont('MinionPro-Regular','OptimusPrincepsSemiBold.ttf'))
|
||||
pdfmetrics.registerFont(TTFont('MinionPro-Bold','OptimusPrinceps.ttf'))
|
||||
pdfmetrics.registerFont(
|
||||
TTFont('MinionPro-Regular', 'OptimusPrincepsSemiBold.ttf'))
|
||||
pdfmetrics.registerFont(
|
||||
TTFont('MinionPro-Bold', 'OptimusPrinceps.ttf'))
|
||||
if options.read_yaml:
|
||||
cardfile = open(os.path.join(self.filedir, "card_db", options.language, "cards.yaml"), "r")
|
||||
cardfile = open(
|
||||
os.path.join(self.filedir, "card_db", options.language, "cards.yaml"), "r")
|
||||
cards = yaml.load(cardfile)
|
||||
else:
|
||||
cards = self.read_card_defs(os.path.join(self.filedir, "card_db", options.language, "dominion_cards.txt"))
|
||||
self.read_card_extras(os.path.join(self.filedir, "card_db", options.language, "dominion_card_extras.txt"), cards)
|
||||
DominionTabs.language_mapping = yaml.load(open(os.path.join(self.filedir, "card_db", options.language, "mapping.yaml")))
|
||||
cards = self.read_card_defs(
|
||||
os.path.join(self.filedir, "card_db", options.language, "dominion_cards.txt"))
|
||||
self.read_card_extras(os.path.join(
|
||||
self.filedir, "card_db", options.language, "dominion_card_extras.txt"), cards)
|
||||
DominionTabs.language_mapping = yaml.load(
|
||||
open(os.path.join(self.filedir, "card_db", options.language, "mapping.yaml")))
|
||||
|
||||
baseCards = [card.name for card in cards if card.cardset.lower() == 'base']
|
||||
baseCards = [
|
||||
card.name for card in cards if card.cardset.lower() == 'base']
|
||||
|
||||
def isBaseExpansionCard(card):
|
||||
return card.cardset.lower() != 'base' and card.name in baseCards
|
||||
@ -935,9 +983,12 @@ class DominionTabs:
|
||||
cards = [card for card in cards if not isBaseExpansionCard(card)]
|
||||
|
||||
if self.options.expansions:
|
||||
self.options.expansions = [o.lower() for o in self.options.expansions]
|
||||
reverseMapping = {v: k for k, v in DominionTabs.language_mapping.iteritems()}
|
||||
self.options.expansions = [reverseMapping.get(e, e) for e in self.options.expansions]
|
||||
self.options.expansions = [o.lower()
|
||||
for o in self.options.expansions]
|
||||
reverseMapping = {
|
||||
v: k for k, v in DominionTabs.language_mapping.iteritems()}
|
||||
self.options.expansions = [
|
||||
reverseMapping.get(e, e) for e in self.options.expansions]
|
||||
filteredCards = []
|
||||
knownExpansions = set()
|
||||
for c in cards:
|
||||
@ -959,14 +1010,17 @@ class DominionTabs:
|
||||
for c in cards:
|
||||
if isBaseExpansionCard(c):
|
||||
continue
|
||||
cardnamesByExpansion.setdefault(c.cardset, []).append(c.name.strip())
|
||||
cardnamesByExpansion.setdefault(
|
||||
c.cardset, []).append(c.name.strip())
|
||||
for exp, names in cardnamesByExpansion.iteritems():
|
||||
c = Card(exp, exp, ("Expansion",), None, ' | '.join(sorted(names)))
|
||||
c = Card(
|
||||
exp, exp, ("Expansion",), None, ' | '.join(sorted(names)))
|
||||
cards.append(c)
|
||||
|
||||
if options.write_yaml:
|
||||
out = yaml.dump(cards)
|
||||
open(os.path.join(self.filedir, "card_db", options.language, "cards.yaml"), 'w').write(out)
|
||||
open(os.path.join(
|
||||
self.filedir, "card_db", options.language, "cards.yaml"), 'w').write(out)
|
||||
|
||||
# When sorting cards, want to always put "base" cards after all
|
||||
# kingdom cards, and order the base cards in a set order - the
|
||||
@ -980,14 +1034,17 @@ class DominionTabs:
|
||||
return -1
|
||||
|
||||
if options.order == "global":
|
||||
sortKey = lambda x: (int(x.isExpansion()), baseIndex(x.name), x.name)
|
||||
sortKey = lambda x: (
|
||||
int(x.isExpansion()), baseIndex(x.name), x.name)
|
||||
else:
|
||||
sortKey = lambda x: (x.cardset, int(x.isExpansion()), baseIndex(x.name), x.name)
|
||||
sortKey = lambda x: (
|
||||
x.cardset, int(x.isExpansion()), baseIndex(x.name), x.name)
|
||||
cards.sort(key=sortKey)
|
||||
|
||||
if not f:
|
||||
f = "dominion_tabs.pdf"
|
||||
self.canvas = canvas.Canvas(f, pagesize=(self.paperwidth, self.paperheight))
|
||||
self.canvas = canvas.Canvas(
|
||||
f, pagesize=(self.paperwidth, self.paperheight))
|
||||
self.drawDividers(cards)
|
||||
self.canvas.save()
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user