-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdisseminate.py
executable file
·279 lines (210 loc) · 7.34 KB
/
disseminate.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Reads a xml file , transforms it to a intermediate format (e.g. formatting objects: FO ) and converts to PDF subsequently
Usage:
disseminate.py <input_file> <path> --out-type=<FO_PDF> [options]
Options:
-d, --debug Enable debug output
-f --formatter=<electronic_or_print> Formatter
-m --medium=<filename> Stylesheet file
-s --saxon=<location_of_the_saxon_jar_file>
-x --xsl=<filename> Stylesheet file
Example
--------
python $BUILD_DIR/static/tools/disseminate.py
"""
__author__ = "Dulip Withanage"
from debug import Debuggable, Debug
from globals import GV
import sys
import os
import inspect
from docopt import docopt
from subprocess import Popen, PIPE
from settingsconfiguration import Settings
class Disseminate(Debuggable):
def __init__(self):
self.args = self.read_command_line()
self.debug = Debug()
self.settings = Settings(self.args)
self.gv = GV(self.settings)
Debuggable.__init__(self, 'Main')
if self.args.get('--debug'):
self.debug.enable_debug()
self.dr = self.args.get('<path>')
self.f = self.args.get('<input_file>')
self.out_type = self.args.get('--out-type').lower()
self.script_path = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
@staticmethod
def read_command_line():
"""
Reads and generates a docopt dictionary from the command line parameters.
Returns
-------
docopt : dictionary
A dictionary, where keys are names of command-line elements such as and values are theparsed values of those
elements.
"""
return docopt(__doc__, version='Disseminate 0.1')
def get_saxon_path(self):
"""Checks if saxon is available in the default path
Returns
--------
saxon : boolean
True, if saxon is available. False, if not.
"""
s = os.path.join(self.script_path, self.gv.apps.get('saxon'))
if os.path.isfile(s):
return s
elif self.args.get('--saxon'):
if os.path.isfile(self.args.get('--saxon')):
return self.args.get('--saxon')
else:
return False
else:
return False
def get_module_name(self):
"""
Reads the name of the module for debugging and logging
Returns
-------
name string
Name of the Module
"""
name = 'disseminate'
return name
def process(self, args):
"""Runs typesetter with given arguments
Creates the execution path for the conversion process. Output,exit-code and system error codes are captured and returned.
Parameters
----------
args : list
application arguments in the correct oder.
Returns
-------
output :str
system standard output.
err :str
system standard error.
exit_code: str
system exit_code.
See Also
--------
subprocess.Popen()
"""
m = ' '.join(args).strip().split(' ')
print(' '.join(args))
process = Popen(m, stdout=PIPE)
output, err = process.communicate()
exit_code = process.wait()
if exit_code == 1:
print(err)
sys.exit(1)
return output, err, exit_code
def run(self):
"""
Runs converters
See Also
--------
create_output, create_pdf
"""
self.create_output(self.out_type)
def create_output(self, out_type):
"""
Create FO output
Parameters
----------
out_type: str
Output Type
See Also
-------
run_saxon(), get_saxon_path()
"""
formatters = self.args.get('--formatter').split(',')
mediums = self.args.get('--medium').split(',')
for f in formatters:
f = f.lower()
for m in mediums:
m = m.lower()
self.gv.create_dirs_recursive(self.args.get('<path>').split(os.pathsep))
if self.out_type=='fo':
self.debug.print_console(self, self.gv.RUNNING_FO_CONVERSION)
saxon_path = self.get_saxon_path()
args = self.run_saxon(saxon_path,f, m)
if self.out_type=='pdf':
self.debug.print_console(self, self.gv.RUNNING_PDF_CONVERSION)
args = self.run_fop_processor(f, m)
output, err, exit_code = self.process(args)
def run_fop_processor(self, formatter, medium):
args = []
if formatter.lower() == 'fop':
pth = os.path.join(self.script_path, self.gv.apps.get('fop'))
if self.gv.check_program(pth):
args = self.run_apache_fop(pth,formatter, medium)
elif formatter.lower() == 'ah':
pth = self.gv.apps.get('ah')
if self.gv.check_program(pth):
args = self.run_ah_fop(pth,formatter, medium)
return args
def run_ah_fop(self, pth, formatter, medium):
args=[pth]
args.append('-d')
args.append('{}/{}.{}.{}.fo'.format(os.path.dirname(self.f), self.gv.uuid, formatter, medium))
args.append('-o')
args.append('{}/{}.{}.{}.pdf'.format(self.dr, self.gv.uuid, formatter, medium))
return args
def run_apache_fop(self, pth, formatter, medium):
style_path = '{}/configurations/fop/conf/{}.{}.xml'.format(self.script_path, formatter,medium)
args = [pth]
args.append('-fo')
args.append('{}/{}.{}.{}.fo'.format(os.path.dirname(self.f),self.gv.uuid, formatter, medium))
args.append('-pdf')
args.append('{}/{}.{}.{}.pdf'.format(self.dr,self.gv.uuid, formatter, medium))
args.append('-c')
args.append(style_path)
return args
def run_saxon(self, saxon_path, formatter, medium):
"""
Creates the executable path for saxon
Parameters
---------
saxon_path : str
absolute path of the saxon binary jar file
formatter : str
name of the FO formatter
medium : str
name of the medium
Returns
------
args:list
List of arguments for saxon execution path
"""
args = ["java", "-jar", saxon_path]
if self.args.get('--xsl'):
xsl = self.script_path.split(os.sep)
xsl.append('stylesheets')
xsl.append(self.args.get('--xsl'))
args.append("-xsl:" + os.sep.join(xsl))
s = self.args.get('<input_file>')
if os.path.exists(s):
args.append("-s:" + s)
else:
self.debug.print_debug(self, self.gv.PROJECT_INPUT_FILE_DOES_NOT_EXIST + ' ' + s)
sys.exit(1)
file_name = '.'.join([self.gv.uuid,formatter.lower(),medium.lower(),'fo'])
args.append("-o:" + os.path.join(self.args.get('<path>'), file_name))
args.append('formatter=' + formatter.lower())
args.append('medium=' + medium.lower())
return args
def main():
"""
Calls the conversion process
See Also
--------
run
"""
xp = Disseminate()
xp.run()
if __name__ == '__main__':
main()