File: printer.py

package info (click to toggle)
tinyerp-client 3.4.2-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,832 kB
  • ctags: 1,024
  • sloc: python: 7,566; sh: 2,253; makefile: 81
file content (141 lines) | stat: -rw-r--r-- 4,529 bytes parent folder | download
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
##############################################################################
#
# Copyright (c) 2004 TINY SPRL. (http://tiny.be) All Rights Reserved.
#                    Fabien Pinckaers <fp@tiny.Be>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

# ------------------------------------------------------------------- #
# Module printer
# ------------------------------------------------------------------- #
#
# Supported formats: pdf
#
# Print or open a previewer
#

import os, base64, options
import gc

class Printer(object):

	def __init__(self):
		self.openers = {
			'pdf': self._findPDFOpener,
			'html': self._findHTMLOpener,
			'doc': self._findHTMLOpener,
		}

	def _findInPath(self, progs):
		lstprogs = progs[:]
		found = {}
		path = [dir for dir in os.environ['PATH'].split(':')
				if os.path.isdir(dir)]
		for dir in path:
			content = os.listdir(dir)
			for prog in progs[:]:
				if prog in content:
					progs.remove(prog)
					found[prog] = os.path.join(dir, prog)
		for prog in lstprogs:
			if prog in found:
				return found[prog]
		return ''

	def _findHTMLOpener(self):
		if os.name == 'nt':
			return lambda fn: os.startfile(fn)
		else:
			if options.options['printer.preview']:
				if options.options['printer.softpath_html'] == 'none':
					prog = self._findInPath(['ooffice2', 'openoffice', 'firefox', 'mozilla', 'galeon'])
					def opener(fn):
						os.spawnv(os.P_NOWAIT, prog, (os.path.basename(prog),fn))
					return opener
				else:
					def opener(fn):
						os.system(options.options['printer.softpath_html'] + ' ' + fn)
					return opener

	def _findPDFOpener(self):
		if os.name == 'nt':
			if options.options['printer.preview']:
				if options.options['printer.softpath'] == 'none':
					return lambda fn: os.startfile(fn)
				else:
					return lambda fn: os.system(options.options['printer.softpath'] + ' ' + fn)
			else:
				return lambda fn: print_w32_filename(fn)
		else:
			if options.options['printer.preview']:
				if options.options['printer.softpath'] == 'none':
					prog = self._findInPath(['xpdf', 'evince', 'gpdf', 'kpdf'])
					def opener(fn):
						os.spawnv(os.P_NOWAIT, prog, (os.path.basename(prog),fn))
					return opener
				else:
					def opener(fn):
						os.spawnv(os.P_NOWAIT, options.options['printer.softpath'], (os.path.basename(options.options['printer.softpath']),fn))
					return opener

	def print_file(self, fname, ftype):
		finderfunc = self.openers[ftype]
		opener = finderfunc()
		opener(fname)
		gc.collect()

printer = Printer()

def print_linux_filename(filename):
	print 'Linux Automatic Printing not implemented. Use XPDF !'

def print_w32_filename(filename):
	import win32api
	win32api.ShellExecute (0, "print", filename, None, ".", 0)

def print_data(data):
	if data.get('code','normal')=='zlib':
		import zlib
		content = zlib.decompress(base64.decodestring(data['result']))
	else:
		content = base64.decodestring(data['result'])

	if data['format'] in printer.openers.keys():
		import tempfile
		if data['format']=='html' and os.name=='nt':
			data['format']='doc'
		fp_name = tempfile.mktemp('.'+data['format'])
		fp = file(fp_name, 'wb+')
		fp.write(content)
		fp.close()
		printer.print_file(fp_name, data['format'])
	else:
		fname = common.file_selection(_('Write Report to File'), data['format'])
		try:
			fp = file(filename,'wb+')
			fp.write(content)
			fp.close()
		except:
			common.message(_('Error writing the file!'))