View | Details | Raw Unified | Return to bug 51812
Collapse All | Expand All

(-)a/management/univention-directory-reports/modules/univention/directory/reports/document.py (-4 / +12 lines)
 Lines 52-58   _ = Translation('univention-directory-reports').translate Link Here 
52
52
53
53
54
class Document(object):
54
class Document(object):
55
	(TYPE_LATEX, TYPE_CSV, TYPE_RML, TYPE_UNKNOWN) = range(4)
55
	(TYPE_LATEX, TYPE_CSV, TYPE_RML, TYPE_SCRIPT, TYPE_UNKNOWN) = range(5)
56
56
57
	@classmethod
57
	@classmethod
58
	def get_type(cls, template):
58
	def get_type(cls, template):
 Lines 62-67   class Document(object): Link Here 
62
			return cls.TYPE_CSV
62
			return cls.TYPE_CSV
63
		elif template.endswith('.rml'):
63
		elif template.endswith('.rml'):
64
			return cls.TYPE_RML
64
			return cls.TYPE_RML
65
		elif template.endswith('.py') or template.endswith('.sh'):
66
			return cls.TYPE_SCRIPT
65
		return cls.TYPE_UNKNOWN
67
		return cls.TYPE_UNKNOWN
66
68
67
	def __init__(self, template, header=None, footer=None):
69
	def __init__(self, template, header=None, footer=None):
 Lines 88-94   class Document(object): Link Here 
88
		elif self._type == Document.TYPE_CSV:
90
		elif self._type == Document.TYPE_CSV:
89
			suffix = '.csv'
91
			suffix = '.csv'
90
		else:
92
		else:
91
			suffix = self._template.rsplit('.', 1)[1]
93
			suffix = '.' + self._template.rsplit('.', 1)[1]
92
		fd, filename = tempfile.mkstemp(suffix, 'univention-directory-reports-')
94
		fd, filename = tempfile.mkstemp(suffix, 'univention-directory-reports-')
93
		os.chmod(filename, 0o644)
95
		os.chmod(filename, 0o644)
94
		os.close(fd)
96
		os.close(fd)
 Lines 138-144   class Document(object): Link Here 
138
140
139
		return tmpfile
141
		return tmpfile
140
142
141
	def create_pdf(self, latex_file):
143
	def create_pdf(self, latex_file, objects):
142
		"""Run pdflatex on latex_file and return path to generated file or None on errors."""
144
		"""Run pdflatex on latex_file and return path to generated file or None on errors."""
143
		cmd = ['/usr/bin/pdflatex', '-interaction=nonstopmode', '-halt-on-error', '-output-directory=%s' % os.path.dirname(latex_file), latex_file]
145
		cmd = ['/usr/bin/pdflatex', '-interaction=nonstopmode', '-halt-on-error', '-output-directory=%s' % os.path.dirname(latex_file), latex_file]
144
		devnull = open(os.path.devnull, 'w')
146
		devnull = open(os.path.devnull, 'w')
 Lines 157-163   class Document(object): Link Here 
157
				except EnvironmentError:
159
				except EnvironmentError:
158
					pass
160
					pass
159
161
160
	def create_rml_pdf(self, rml_file):
162
	def create_rml_pdf(self, rml_file, objects):
161
		output = '%s.pdf' % (os.path.splitext(rml_file)[0],)
163
		output = '%s.pdf' % (os.path.splitext(rml_file)[0],)
162
		with open(rml_file, 'rb') as fd:
164
		with open(rml_file, 'rb') as fd:
163
			outputfile = trml2pdf.parseString(fd.read(), output)
165
			outputfile = trml2pdf.parseString(fd.read(), output)
 Lines 166-168   class Document(object): Link Here 
166
		except EnvironmentError:
168
		except EnvironmentError:
167
			pass
169
			pass
168
		return outputfile
170
		return outputfile
171
172
	def create_from_script(self, script_file, objects):
173
		output = '%s.csv' % (os.path.splitext(script_file)[0],)
174
		with open(output, 'w') as fd:
175
			fd.write(subprocess.check_output([sys.executable, script_file] + objects))
176
		return output
(-)a/management/univention-directory-reports/modules/univention/directory/reports/report.py (-3 / +12 lines)
 Lines 61-76   class Report(object): Link Here 
61
				raise ReportError(_('The report %r does not exists or is misconfigured.') % (report,))
61
				raise ReportError(_('The report %r does not exists or is misconfigured.') % (report,))
62
			raise ReportError(_('No %r report exists for the module %r.') % (report, module))
62
			raise ReportError(_('No %r report exists for the module %r.') % (report, module))
63
63
64
		suffix = '.rml' if Document.get_type(template) == Document.TYPE_RML else '.tex'
64
		suffix = {
65
			Document.TYPE_RML: '.rml',
66
			Document.TYPE_SCRIPT: '.py',
67
			#Document.TYPE_SCRIPT: '.sh',
68
69
		}.get(Document.get_type(template), '.tex')
65
		header = self.config.get_header(module, report, suffix)
70
		header = self.config.get_header(module, report, suffix)
66
		footer = self.config.get_footer(module, report, suffix)
71
		footer = self.config.get_footer(module, report, suffix)
67
		doc = Document(template, header=header, footer=footer)
72
		doc = Document(template, header=header, footer=footer)
68
73
69
		tmpfile = doc.create_source(objects)
74
		tmpfile = doc.create_source(objects)
70
		pdffile = tmpfile
75
		pdffile = tmpfile
71
		func = {Document.TYPE_RML: doc.create_rml_pdf, Document.TYPE_LATEX: doc.create_pdf}.get(doc._type)
76
		func = {
77
			Document.TYPE_RML: doc.create_rml_pdf,
78
			Document.TYPE_LATEX: doc.create_pdf,
79
			Document.TYPE_SCRIPT: doc.create_from_script,
80
		}.get(doc._type)
72
		if func:
81
		if func:
73
			pdffile = func(tmpfile)
82
			pdffile = func(tmpfile, objects)
74
		if not pdffile or not os.path.exists(pdffile):
83
		if not pdffile or not os.path.exists(pdffile):
75
			raise ReportError(_('The report could not be created.'))
84
			raise ReportError(_('The report could not be created.'))
76
		return pdffile
85
		return pdffile

Return to bug 51812