forked from tealefristoe/nsc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nsc.py
129 lines (110 loc) · 4.49 KB
/
nsc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import card_drawer
import constants
reload(constants)
import json
import os
import os.path
import sys
import optparse
from pygame.locals import *
output_target = constants.OT_HOME
# True: don't rotate the cards for printing
horizontal_cards = False
def main(options, args):
sheets = options.sheets
save_pdf = options.save_pdf
# If desired_cards is empty, all cards discovered will be included.
# Otherwise, only cards with names in the list will be included.
# Similarly for desired_types.
desired_cards = options.desired_cards
desired_types = options.desired_types
# Get card info from files
card_files = os.listdir(constants.INPUT_DIR)
all_cards = []
cards = []
input_files = options.input_files
if input_files:
# Add .json to input files that have that omitted
correct_input_files = []
for input_file in input_files:
if input_file[-5:] != '.json':
correct_input_files += [input_file + '.json']
else:
correct_input_files += [input_file]
input_files = correct_input_files
# Display error messages when an input file can't be found
for input_file in input_files:
if input_file not in card_files:
print("!!! Could not find card file " + input_file)
desired_card_files = []
for card_file in card_files:
if card_file in input_files:
desired_card_files += [card_file]
else:
desired_card_files = card_files
for card_file in desired_card_files:
if card_file[0] == "." or card_file[-1] == "~":
continue
file = open(os.path.join(constants.INPUT_DIR, card_file))
cards_info = json.load(file)
for card_info in cards_info:
if not desired_cards or card_info["name"] in desired_cards:
if not desired_types or card_info["type"] in desired_types:
all_cards += [card_info]
if "number" in card_info.keys():
cards += [card_info] * card_info["number"]
else:
cards += [card_info]
# Get template info
templates = {}
template_files = os.listdir(constants.TEMPLATE_DIR)
for template_file in template_files:
if template_file[0] == "." or template_file[-1] == "~":
continue
file = open(os.path.join(constants.TEMPLATE_DIR, template_file))
template_info = json.load(file)
template_info["name"] = template_file[:-5]
templates[template_info["name"]] = template_info
# Get font info
fonts = {}
file = open(constants.FONTS_FILE)
fonts_info = json.load(file)
cd = card_drawer.CardDrawer(templates, fonts_info, output_target)
while True:
for event in card_drawer.pygame.event.get():
if event.type == QUIT:
card_drawer.pygame.quit()
sys.exit()
if event.type == KEYUP:
if event.key == K_ESCAPE:
card_drawer.pygame.quit()
sys.exit()
elif event.key == ord('p'):
cd.printToFile()
if len(all_cards) > 0:
cd.drawCards(all_cards)
all_cards = []
if sheets or save_pdf:
cards = cd.drawSheet(cards)
card_drawer.pygame.display.update()
if len(cards) == 0:
if save_pdf:
cd.writePDF()
card_drawer.pygame.quit()
sys.exit()
else:
card_drawer.pygame.quit()
sys.exit()
if __name__ == "__main__":
parser = optparse.OptionParser(usage="""%prog [options]
Nothing Sacred Cards - Create and rapidly iterate cards while designing board game.""",
version = "%prog 1.0")
parser.add_option("-s", "--sheets", dest="sheets", action="store_true", help="Generate sheets of cards after creating cards. Note: Sheets will also be generated if you use the --pdf option.", default=False)
parser.add_option("-p", "--pdf", dest="save_pdf", action="store_true", help="Generate a pdf of all cards for printing. Note: Will generate sheets.", default=False)
parser.add_option("-c", "--card", dest="desired_cards", action="append", help="The name of a card to generate. Can be used multiple times to generate multiple cards. If none specified, all cards will be generated.")
parser.add_option("-t", "--type", dest="desired_types", action="append", help="""The type of card to generate. Can be used multiple times to generate multiple types of cards.
Types are templates, found in the templates/ directory. Types are specified in the 'type' field of cards in card files (found in the cards/ directory).
If none specified, all types will be generated.""")
parser.add_option("-i", "--input_file", dest="input_files", action="append", help="The input file (in the cards/ directory) to use for generating cards. Can be used multiple times to get cards from multiple input files. If none specified, all .json files in the cards/ directory will be used.")
(options, args) = parser.parse_args()
sys.exit(main(options, args))