-
Notifications
You must be signed in to change notification settings - Fork 37
/
ScribusGenerator.py
442 lines (376 loc) · 20.8 KB
/
ScribusGenerator.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
=================
Automatic document generation for Scribus.
=================
For further information (manual, description, etc.) please visit:
http://berteh.github.io/ScribusGenerator/
# v4.0 (2024-04-17): various bug fix, python3 branch made maste, MacOS compatible GUI
# v3.0 (2022-01-12): port to Python3 for Scribut 1.5.6+, some features (count, fill)
# v2.0 (2015-12-02): added features (merge, range, clean, save/load)
# v1.9 (2015-08-03): initial GUI version with PDF generation
This script is the GUI (TCL-tK) ScribusGenerator
=================
The MIT License
=================
Copyright (c) 2010-2014 Ekkehard Will (www.ekkehardwill.de), 2014-2024 Berteh (https://github.com/berteh/)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import ScribusGeneratorBackend
from ScribusGeneratorBackend import CONST, ScribusGenerator, GeneratorDataObject
import tkinter
from tkinter import Frame, LabelFrame, Label, Entry, Button, StringVar, OptionMenu, Checkbutton, IntVar, DISABLED, NORMAL, PhotoImage
import tkinter.messagebox
import tkinter.filedialog
import webbrowser
import scribus
import os
import sys
import inspect
import traceback
class GeneratorControl:
# Controller being the bridge between UI and Logic.
def __init__(self, root):
self.__dataSourceFileEntryVariable = StringVar()
self.__scribusSourceFileEntryVariable = StringVar()
self.__dataSeparatorEntryVariable = StringVar()
self.__dataSeparatorEntryVariable.set(CONST.CSV_SEP)
self.__dataEncodingEntryVariable = StringVar()
self.__dataEncodingEntryVariable.set(CONST.CSV_ENCODING)
self.__outputDirectoryEntryVariable = StringVar()
self.__outputFileNameEntryVariable = StringVar()
# SLA & PDF are valid output format
self.__outputFormatList = [CONST.FORMAT_PDF, CONST.FORMAT_SLA]
self.__selectedOutputFormat = StringVar()
self.__selectedOutputFormat.set(CONST.FORMAT_PDF)
self.__keepGeneratedScribusFilesCheckboxVariable = IntVar()
self.__mergeOutputCheckboxVariable = IntVar()
self.__saveCheckboxVariable = IntVar()
self.__fromVariable = StringVar()
self.__fromVariable.set(CONST.EMPTY)
self.__toVariable = StringVar()
self.__toVariable.set(CONST.EMPTY)
self.__closeDialogVariable = IntVar()
self.__root = root
if scribus.haveDoc():
doc = scribus.getDocName()
self.__scribusSourceFileEntryVariable.set(doc)
self.__outputDirectoryEntryVariable.set(os.path.split(doc)[0])
self.__dataSourceFileEntryVariable.set(
os.path.splitext(doc)[0]+".csv")
def getDataSourceFileEntryVariable(self):
return self.__dataSourceFileEntryVariable
def dataSourceFileEntryVariableHandler(self):
result = tkinter.filedialog.askopenfilename(title='Choose...', defaultextension='.csv', filetypes=[(
'CSV - comma separated values', '*.csv *.CSV'), ('TSV - tab separated values', '*.tsv *.TSV'), ('TXT - text', '*.txt *.TXT'), ('all', '*.*')], initialdir=os.path.dirname(self.__dataSourceFileEntryVariable.get()))
if result:
self.__dataSourceFileEntryVariable.set(result)
# todo: opt update separator to tab if tsv is selected?
def getScribusSourceFileEntryVariable(self):
return self.__scribusSourceFileEntryVariable
def scribusSourceFileEntryVariableHandler(self):
result = tkinter.filedialog.askopenfilename(
title='Choose...', defaultextension='.sla', filetypes=[('SLA', '*.sla *.SLA')], initialdir=os.path.dirname(self.__scribusSourceFileEntryVariable.get()))
if result:
self.__scribusSourceFileEntryVariable.set(result)
def getDataSeparatorEntryVariable(self):
return self.__dataSeparatorEntryVariable
def getDataEncodingEntryVariable(self):
return self.__dataEncodingEntryVariable
def getOutputDirectoryEntryVariable(self):
return self.__outputDirectoryEntryVariable
def outputDirectoryEntryVariableHandler(self):
result = tkinter.filedialog.askdirectory(initialdir=self.__outputDirectoryEntryVariable.get())
if result:
self.__outputDirectoryEntryVariable.set(result)
def getOutputFileNameEntryVariable(self):
return self.__outputFileNameEntryVariable
def getOutputFormatList(self):
return self.__outputFormatList
def getSelectedOutputFormat(self):
return self.__selectedOutputFormat
def getKeepGeneratedScribusFilesCheckboxVariable(self):
return self.__keepGeneratedScribusFilesCheckboxVariable
def getMergeOutputCheckboxVariable(self):
return self.__mergeOutputCheckboxVariable
def getSaveCheckboxVariable(self):
return self.__saveCheckboxVariable
def getFromVariable(self):
return self.__fromVariable
def getToVariable(self):
return self.__toVariable
def getCloseDialogVariable(self):
return self.__closeDialogVariable
def allValuesSet(self):
# Simple check whether input fields are NOT EMPTY.
# The user could fill in crap into the input fields. This would lead to an error, but crap in crap out!
result = 0
if((self.__scribusSourceFileEntryVariable.get() != CONST.EMPTY) and
(self.__dataSourceFileEntryVariable.get() != CONST.EMPTY) and
(self.__outputDirectoryEntryVariable.get() != CONST.EMPTY) and
(len(self.__dataEncodingEntryVariable.get()) >= 4 ) and
(len(self.__dataSeparatorEntryVariable.get()) == 1)):
result = 1
return result
def createGeneratorDataObject(self):
# Collect the settings the user has made and build the Data Object
result = GeneratorDataObject(
scribusSourceFile=self.__scribusSourceFileEntryVariable.get(),
dataSourceFile=self.__dataSourceFileEntryVariable.get(),
outputDirectory=self.__outputDirectoryEntryVariable.get(),
outputFileName=self.__outputFileNameEntryVariable.get(),
outputFormat=self.__selectedOutputFormat.get(),
keepGeneratedScribusFiles=self.__keepGeneratedScribusFilesCheckboxVariable.get(),
csvSeparator=self.__dataSeparatorEntryVariable.get(),
csvEncoding=self.__dataEncodingEntryVariable.get(),
singleOutput=self.__mergeOutputCheckboxVariable.get(),
firstRow=self.__fromVariable.get(),
lastRow=self.__toVariable.get(),
saveSettings=self.__saveCheckboxVariable.get(),
closeDialog=self.__closeDialogVariable.get()
)
return result
def buttonCancelHandler(self):
self.__root.destroy()
def buttonOkHandler(self):
if (CONST.TRUE == self.allValuesSet()):
dataObject = self.createGeneratorDataObject()
generator = ScribusGenerator(dataObject)
try:
generator.run()
if(dataObject.getCloseDialog()):
self.__root.destroy()
else:
tkinter.messagebox.showinfo(
'Scribus Generator', message='Done. Generated files are in '+dataObject.getOutputDirectory())
except IOError as e: # except FileNotFoundError as e:
tkinter.messagebox.showerror(
title='File Not Found', message="Could not find some input file, please verify your Scribus and Data file settings:\n\n %s" % e)
except ValueError as e:
tkinter.messagebox.showerror(
title='Variable Error', message="Could likely not replace a variable with its value,\nplease check your Data File and Data Separator settings:\n\n %s" % e)
except IndexError as e:
tkinter.messagebox.showerror(
title='Variable Error', message="Could not find the value for one variable.\nplease check your Data File and Data Separator settings.\n\n %s" % e)
except Exception:
tkinter.messagebox.showerror(title='Error Scribus Generator',
message="Something went wrong.\n\nRead the log file for more (in your home directory)."+traceback.format_exc())
else:
tkinter.messagebox.showerror(
title='Validation failed', message='Please check if all settings have been set correctly!')
def scribusLoadSettingsHandler(self):
slaFile = self.__scribusSourceFileEntryVariable.get()
if(slaFile is CONST.EMPTY):
tkinter.messagebox.showinfo(
'Choose a file', message="Set a valid scribus input file prior to loading its settings.")
return
dataObject = GeneratorDataObject(
scribusSourceFile=slaFile
)
generator = ScribusGenerator(dataObject)
saved = generator.get_saved_settings()
if (saved):
dataObject.loadFromString(saved)
# self.__scribusSourceFileEntryVariable = StringVar() #not loaded
self.__dataSourceFileEntryVariable.set(
dataObject.getDataSourceFile())
self.__dataSeparatorEntryVariable.set(dataObject.getCsvSeparator())
self.__dataEncodingEntryVariable.set(dataObject.getCsvEncoding())
self.__outputDirectoryEntryVariable.set(
dataObject.getOutputDirectory())
self.__outputFileNameEntryVariable.set(
dataObject.getOutputFileName())
self.__selectedOutputFormat.set(dataObject.getOutputFormat())
self.__keepGeneratedScribusFilesCheckboxVariable.set(
dataObject.getKeepGeneratedScribusFiles())
self.__mergeOutputCheckboxVariable.set(
dataObject.getSingleOutput())
# self.__saveCheckboxVariable = IntVar() #not loaded
self.__fromVariable.set(dataObject.getFirstRow())
self.__toVariable.set(dataObject.getLastRow())
self.__closeDialogVariable.set(dataObject.getCloseDialog())
else:
tkinter.messagebox.showinfo(
'No Settings', message="Input scribus file contains no former saved settings.")
class GeneratorDialog:
# The UI to configure the settings by the user
def __init__(self, root, ctrl):
self.__root = root
self.__ctrl = ctrl
self.__pluginDir = os.path.dirname(
os.path.abspath(inspect.stack()[0][1]))
for i in [self.__pluginDir + '/pic/ScribusGenerator_logo.gif', self.__pluginDir + '/ScribusGenerator_logo.gif']:
if os.path.exists(i):
try:
self.__ico = PhotoImage(file=i)
root.tk.call('wm', 'iconphoto', root._w, '-default', self.__ico)
except Exception as e:
pass
def show(self):
self.__root.title(CONST.APP_NAME)
mainFrame = Frame(self.__root)
top = mainFrame.winfo_toplevel()
top.rowconfigure(0, weight=1)
top.columnconfigure(0, weight=1)
mainFrame.rowconfigure(0, weight=1)
mainFrame.columnconfigure(0, weight=1)
mainFrame.grid(sticky='ew')
# Three Sections: Input-Settings, Output-Settings and Buttons
inputFrame = LabelFrame(mainFrame, text='Input Settings')
inputFrame.columnconfigure(2, weight=1)
inputFrame.grid(column=0, row=0, padx=5, pady=5, sticky='ew')
outputFrame = LabelFrame(mainFrame, text='Output Settings')
outputFrame.columnconfigure(2, weight=1)
outputFrame.grid(column=0, row=1, padx=5, pady=5, sticky='ew')
miscFrame = LabelFrame(mainFrame, text='Misc Settings')
miscFrame.columnconfigure(2, weight=1)
miscFrame.grid(column=0, row=2, padx=5, pady=5, sticky='ew')
buttonFrame = Frame(mainFrame)
buttonFrame.columnconfigure(3, weight=1)
buttonFrame.grid(column=0, row=3, padx=5, pady=5, sticky='ew')
# Input-Settings
scribusSourceFileLabel = Label(
inputFrame, text='Scribus File:', width=15, anchor='w')
scribusSourceFileLabel.grid(
column=0, row=0, padx=5, pady=5, sticky='w')
scribusSourceFileEntry = Entry(
inputFrame, textvariable=self.__ctrl.getScribusSourceFileEntryVariable())
scribusSourceFileEntry.grid(
column=1, columnspan=3, row=0, padx=5, pady=5, sticky='ew')
scribusSourceFileButton = Button(
inputFrame, text='⏏', command=self.__ctrl.scribusSourceFileEntryVariableHandler)
scribusSourceFileButton.grid(
column=4, row=0, padx=5, pady=5, sticky='e')
scribusLoadSettingsButton = Button(
inputFrame, text='↺', command=self.__ctrl.scribusLoadSettingsHandler) # ⟲ ⟳ ↻ ↺ ⌂ ⌘ ⎗
scribusLoadSettingsButton.grid(
column=5, row=0, padx=5, pady=5, sticky='e')
dataSourceFileLabel = Label(
inputFrame, text='Data File:', width=15, anchor='w')
dataSourceFileLabel.grid(column=0, row=1, padx=5, pady=5, sticky='w')
dataSourceFileEntry = Entry(
inputFrame, textvariable=self.__ctrl.getDataSourceFileEntryVariable())
dataSourceFileEntry.grid(
column=1, columnspan=4, row=1, padx=5, pady=5, sticky='ew')
dataSourceFileButton = Button(
inputFrame, text='⏏', command=self.__ctrl.dataSourceFileEntryVariableHandler)
dataSourceFileButton.grid(column=5, row=1, padx=5, pady=5, sticky='e')
dataSeparatorLabel = Label(
inputFrame, text='Data Field Separator:', width=15, anchor='w')
dataSeparatorLabel.grid(column=0, row=2, padx=5, pady=5, sticky='w')
dataSeparatorEntry = Entry(
inputFrame, width=3, textvariable=self.__ctrl.getDataSeparatorEntryVariable())
dataSeparatorEntry.grid(column=1, row=2, padx=5, pady=5, sticky='w')
dataEncodingLabel = Label(
inputFrame, text='Data Encoding:', width=15, anchor='w')
dataEncodingLabel.grid(column=0, row=3, padx=5, pady=5, sticky='w')
dataEncodingEntry = Entry(
inputFrame, width=15, textvariable=self.__ctrl.getDataEncodingEntryVariable())
dataEncodingEntry.grid(column=1, row=3, padx=5, pady=5, sticky='w')
fromLabel = Label(
inputFrame, text='(opt.) use partial data, only from:', anchor='e')
fromLabel.grid(column=2, row=2, padx=5, pady=5, sticky='e')
fromEntry = Entry(inputFrame, width=3,
textvariable=self.__ctrl.getFromVariable())
fromEntry.grid(column=3, row=2, padx=5, pady=5, sticky='w')
toLabel = Label(inputFrame, text='to:', width=3, anchor='e')
toLabel.grid(column=4, row=2, padx=5, pady=5, sticky='e')
toEntry = Entry(inputFrame, width=3,
textvariable=self.__ctrl.getToVariable())
toEntry.grid(column=5, row=2, padx=5, pady=5, sticky='w')
# Output-Settings
outputDirectoryLabel = Label(
outputFrame, text='Output Directory:', width=15, anchor='w')
outputDirectoryLabel.grid(column=0, row=0, padx=5, pady=5, sticky='w')
outputDirectoryEntry = Entry(
outputFrame, textvariable=self.__ctrl.getOutputDirectoryEntryVariable())
outputDirectoryEntry.grid(
column=1, columnspan=4, row=0, padx=5, pady=5, sticky='ew')
outputDirectoryButton = Button(
outputFrame, text='⏏', command=self.__ctrl.outputDirectoryEntryVariableHandler)
outputDirectoryButton.grid(column=5, row=0, padx=5, pady=5, sticky='w')
outputFileNameLabel = Label(
outputFrame, text='Output File Name:', width=15, anchor='w')
outputFileNameLabel.grid(column=0, row=1, padx=5, pady=5, sticky='w')
outputFileNameEntry = Entry(
outputFrame, textvariable=self.__ctrl.getOutputFileNameEntryVariable())
outputFileNameEntry.grid(
column=1, columnspan=3, row=1, padx=5, pady=5, sticky='ew')
outputFormatLabel = Label(
outputFrame, text='Format:', anchor='e')
outputFormatLabel.grid(column=4, row=1, padx=5, pady=5, sticky='e')
outputFormatListBox = OptionMenu(outputFrame, self.__ctrl.getSelectedOutputFormat(), *self.__ctrl.getOutputFormatList(),
command=lambda v=self.__ctrl.getSelectedOutputFormat(): self.updateState(v))
outputFormatListBox.grid(column=5, row=1, padx=5, pady=5, sticky='w')
mergeOutputLabel = Label(
outputFrame, text='Merge in Single File:', width=17, anchor='w')
mergeOutputLabel.grid(column=0, columnspan=2, row=2, padx=5, pady=5, sticky='w')
mergeOutputCheckbox = Checkbutton(
outputFrame, variable=self.__ctrl.getMergeOutputCheckboxVariable())
mergeOutputCheckbox.grid(column=2, row=2, padx=5, pady=5, sticky='w')
self.keepGeneratedScribusFilesLabel = Label(
outputFrame, text='Keep Scribus Files:', width=15, anchor='w')
self.keepGeneratedScribusFilesLabel.grid(
column=3, columnspan=2, row=2, padx=5, pady=5, sticky='w')
self.keepGeneratedScribusFilesCheckbox = Checkbutton(
outputFrame, variable=self.__ctrl.getKeepGeneratedScribusFilesCheckboxVariable(), anchor='w')
self.keepGeneratedScribusFilesCheckbox.grid(
column=5, row=2, padx=5, pady=5, sticky='w')
# Misc Settings
saveLabel = Label(miscFrame, text='Save Settings:',
width=12, anchor='w')
saveLabel.grid(column=0, row=1, padx=5, pady=5, sticky='w')
saveCheckbox = Checkbutton(
miscFrame, variable=self.__ctrl.getSaveCheckboxVariable())
saveCheckbox.grid(column=1, row=1, padx=5, pady=5, sticky='w')
closeLabel = Label(miscFrame, text='Close dialog on success:',
width=20, anchor='w')
closeLabel.grid(column=3, columnspan=2, row=1, padx=5, pady=5, sticky='e')
closeCheckbox = Checkbutton(
miscFrame, variable=self.__ctrl.getCloseDialogVariable())
closeCheckbox.grid(column=5, row=1, padx=5, pady=5, sticky='e')
# Bottom Buttons
generateButton = Button(
buttonFrame, text='✔\nGenerate', width=10, command=self.__ctrl.buttonOkHandler)
generateButton.grid(column=0, row=0, padx=5, pady=5, sticky='w')
cancelButton = Button(buttonFrame, text='✘\nCancel',
width=10, command=self.__ctrl.buttonCancelHandler)
cancelButton.grid(column=1, row=0, padx=5, pady=5, sticky='e')
helpButton = Button(buttonFrame, text='❓\nHelp',
width=7, command = lambda: webbrowser.open("https://github.com/berteh/ScribusGenerator/#how-to-use-scribus-generator"))
helpButton.grid(column=3, row=0, padx=5, pady=5, sticky='e')
# general layout
mainFrame.grid()
self.__root.grid()
def updateState(self, value):
if(value == CONST.FORMAT_PDF):
self.keepGeneratedScribusFilesLabel.configure(state=NORMAL)
self.keepGeneratedScribusFilesCheckbox.configure(state=NORMAL)
else:
self.keepGeneratedScribusFilesLabel.configure(state=DISABLED)
self.keepGeneratedScribusFilesCheckbox.configure(state=DISABLED)
def main(argv):
root = tkinter.Tk()
ctrl = GeneratorControl(root)
dlg = GeneratorDialog(root, ctrl)
dlg.show()
root.mainloop()
def main_wrapper(argv):
try:
if(scribus.haveDoc()):
scribus.setRedraw(False)
scribus.statusMessage(CONST.APP_NAME)
scribus.progressReset()
main(argv)
finally:
# Exit neatly even if the script terminated with an exception,
# so we leave the progress bar and status bar blank and make sure
# drawing is enabled.
if(scribus.haveDoc()):
scribus.setRedraw(True)
scribus.statusMessage('')
scribus.progressReset()
if __name__ == '__main__':
main_wrapper(sys.argv)