-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscoresheet.py
executable file
·173 lines (140 loc) · 5.37 KB
/
scoresheet.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OK
'''
Reads text file with rows formatted as:
<Name>;<Team>;<Serie>;<Compensation>
and exports scoresheets in ods/pdf format.
The script is using LibreOffice UNO API. UNO server can be started with
command:
soffice --accept="socket,port=2002;urp;"
If the python interpreter running the script is not UNO-enabled, the path of
UNO-enabled python can be provided with -u argument
'''
import argparse
import csv
import os.path
import sys
import tempfile
import PyPDF2
import appy.pod.renderer
import argcomplete
import charset_normalizer
SEP = ';'
PLAYERS_PER_PAGE = 3
TEMPLATE_FILE_NAME = 'henkkari.ods'
OUTPUT_FILE_NAME = 'result.pdf'
UNO_PATH = '/usr/bin/python3'
def parse_arguments():
"""Parse commandline argumernts.
:returns: commandline arguments
"""
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawTextHelpFormatter
)
parser.add_argument("-f", "--input", help="Input file", required=True)
parser.add_argument("-t", "--template", help="Template file",
default=TEMPLATE_FILE_NAME)
parser.add_argument("-o", "--output", help="Output file",
default=OUTPUT_FILE_NAME)
parser.add_argument("-u", "--uno-path",
help="Uno path (something like: /usr/bin/python)",
default=UNO_PATH)
argcomplete.autocomplete(parser)
args = parser.parse_args()
if not os.path.exists(args.input):
sys.exit("Input file \"{}\" not found".format(args.input))
if not os.path.exists(args.template):
sys.exit("Template file \"{}\" not found".format(args.template))
if os.path.exists(args.output):
sys.exit("Output file \"{}\" already exists".format(args.output))
return args
def read_file(filename):
"""Read player information from CSV file. The file contains four
columns per line (player name, team, serie and age compensation). If
a field contains less than 4 columns, the missing values are set to
empty strings.
:param filename: path to the file
:returns: List of players
"""
# Read CSV file
encoding = charset_normalizer.from_path(filename).best().encoding
with open(filename, encoding=encoding) as input_file:
reader = csv.reader(input_file, delimiter=';', quotechar='"')
players = list(reader)
# Pad player information with empty strings
for player in players:
player += [''] * (4 - len(player))
return players
def merge_pdf(input_files, output_file):
"""Merge list of pdf files into one pdf.
:param input_files: list of filenames of pdfs to be merged
:param output_file: filename of merged pdf
:returns: None
"""
output_pdf = PyPDF2.PdfFileMerger()
for input_file in input_files:
output_pdf.append(input_file)
with open(output_file, "wb") as output_stream:
output_pdf.write(output_stream)
def create_pages(players, template, output_basename, uno_path,
output_directory='/tmp/'):
"""Create pages as individual files. The file format is decided from the
output file suffix.
:param players: list of players
:param template: template file path
:param output_basename: base name for output files
:param output_directory: directory where outfiles are created
:returns: list of filenames
"""
page_num = 0
player_num = 0
page_list = []
while player_num < len(players):
page_output_file_name = ''.join(output_basename.split('.')[:-1]) \
+ '_' + str(page_num) + '.' + output_basename.split('.')[-1]
page_output_path = os.path.join(output_directory,
page_output_file_name)
if os.path.exists(page_output_path):
raise IOError(
"File \"{}\" already exists".format(page_output_path)
)
page_list.append(page_output_path)
renderer = appy.pod.renderer.Renderer(
template,
{'players': players, 'player_num': player_num},
page_output_path,
pythonWithUnoPath=uno_path
)
try:
renderer.run()
except appy.pod.PodError as error:
if "Couldn't not connect to LibreOffice on port 2002" \
in str(error):
sys.exit(
str(error) + '\n\nStart LibreOffice first:'
'\n\n soffice --accept="socket,port=2002;urp;"'
)
page_num = page_num+1
player_num = page_num*PLAYERS_PER_PAGE
return page_list
def main():
"""Main function"""
args = parse_arguments()
filetype = args.output.split('.')[-1]
players = read_file(args.input)
# add empty lines to player list if needed
while len(players) % PLAYERS_PER_PAGE > 0:
players.append(['', '', '', ''])
# Write scoresheets one printable page per file. If output file is pdf, the
# pages will be merged into one document.
if filetype == 'pdf':
page_output_directory = tempfile.mkdtemp()
else:
page_output_directory = './'
page_list = create_pages(players, args.template, args.output,
args.uno_path, page_output_directory)
# merge output files into one file if output file type is pdf
if filetype == 'pdf':
merge_pdf(page_list, args.output)
if __name__ == "__main__":
main()