-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrello2pdf.py
104 lines (86 loc) · 3.05 KB
/
trello2pdf.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
import configparser
import trello
import datetime
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter, inch
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
def main():
config_files = ['secrets.cfg', 'trello2pdf.cfg']
# our configuration and the Trello card list to be worked with
config, target = init(config_files)
cards = read_cards(target)
table = gen_table(config, cards)
gen_pdf(config, table)
def gen_table(config, cards):
table = []
# set up a row of column headers, capitalized as in the config file
headers = [_.strip() for _ in config['Cards']['DescAttribs'].split(',')]
table.append(headers)
# set attribs, lowercased for consistency
attribs = [_.lower() for _ in headers]
for card in cards:
# one row for each card
row = []
for attrib in attribs:
# don't use a list comprehension because of potential missing keys
if attrib in card: # check if we have this one
row.append(card[attrib])
else:
row.append('') # a blank table cell
table.append(row)
return table
def gen_pdf(config, table):
filename = config['PDF']['Basename']
if config['PDF']['DateInName'] == 'yes':
filename += " " + datetime.date.today().isoformat()
filename += ".pdf"
doc = SimpleDocTemplate(filename, pagesize=letter)
elements = []
doctable = Table(table, len(table[0])*[0.4*inch], len(table)*[0.4*inch])
elements.append(doctable)
doc.build(elements)
def read_cards(target):
cards = []
for card in target.list_cards():
attribs = {}
for line in card.description.split('\n'):
if ':' in line:
attrib, text = [_.strip() for _ in line.split(':', 1)]
# lowercase for consistency for later scanning
attribs[attrib.lower()] = text
cards.append(attribs)
return cards
def init(config_files):
"""
Initialize things.
Parameters:
config_files : list of filenames to load
Returns:
config : a loaded configparser.ConfigParser
target : a trello.List of trello.Cards
"""
config = configparser.ConfigParser()
config.read(config_files)
api_key = config['TrelloAPI']['key']
api_secret = config['TrelloAPI']['secret']
oauth_token = config['OAuth']['token']
oauth_secret = config['OAuth']['secret']
client = trello.TrelloClient(api_key=api_key,
api_secret=api_secret,
token=oauth_token,
token_secret=oauth_secret)
try:
boardid = config['Trello']['BoardID']
board = client.get_board(boardid)
except:
print("ERROR: Couldn't get board with ID", boardid)
raise
try:
listid = config['Trello']['ListID']
target = board.get_list(listid)
except:
print("ERROR: Couldn't get list with ID", listid)
raise
return (config, target)
if __name__ == '__main__':
main()