1 """GNUmed printing."""
2
3 __version__ = "$Revision: 1.4 $"
4 __author__ = "K.Hilbert <Karsten.Hilbert@gmx.net>"
5 __license__ = 'GPL (details at http://www.gnu.org)'
6
7
8 import logging, sys, os
9
10
11 if __name__ == '__main__':
12 sys.path.insert(0, '../../')
13 from Gnumed.pycommon import gmShellAPI
14 from Gnumed.pycommon import gmTools
15
16
17 _log = logging.getLogger('gm.printing')
18 _log.info(__version__)
19
20
21 known_printjob_types = [
22 u'medication_list',
23 u'generic_document'
24 ]
25
26 external_print_APIs = [
27 u'os_startfile',
28 u'gm-print_doc'
29 ]
30
31
32
33
34 -def print_file(filename=None, jobtype=None, print_api=None):
35
36 _log.debug('printing "%s": [%s]', jobtype, filename)
37
38 if jobtype not in known_printjob_types:
39 print "unregistered print job type <%s>" % jobtype
40 _log.warning('print job type "%s" not registered', jobtype)
41
42 if print_api not in external_print_APIs:
43 print "unknown print API <%s>" % print_api
44 _log.warning('print API "%s" unknown, trying all', print_api)
45
46 if print_api == u'os_startfile':
47 return __print_file_by_os_startfile(filename = filename)
48
49 if print_api == u'gm-print_doc':
50 return __print_file_by_shellscript(filename = filename, jobtype = jobtype)
51
52 if __print_file_by_os_startfile(filename = filename):
53 return True
54
55 if __print_file_by_shellscript(filename = filename, jobtype = jobtype):
56 return True
57
58 return False
59
60
61
63
64 _log.debug('printing [%s]', filename)
65
66 try:
67 os.startfile(filename, 'print')
68 except AttributeError:
69 _log.exception('platform does not support "os.startfile()", cannot print')
70 return False
71
72 return True
73
75
76 paths = gmTools.gmPaths()
77 local_script = os.path.join(paths.local_base_dir, '..', 'external-tools', 'gm-print_doc')
78
79 candidates = [u'gm-print_doc', u'gm-print_doc.bat', local_script, u'gm-print_doc.bat']
80 args = u' %s %s' % (jobtype, filename)
81
82 success = gmShellAPI.run_first_available_in_shell (
83 binaries = candidates,
84 args = args,
85 blocking = True,
86 run_last_one_anyway = True
87 )
88
89 if success:
90 return True
91
92 _log.error('print command failed')
93 return False
94
95
96
97 if __name__ == '__main__':
98
99 if len(sys.argv) < 2:
100 sys.exit()
101
102 if sys.argv[1] != 'test':
103 sys.exit()
104
105 from Gnumed.pycommon import gmLog2
106 from Gnumed.pycommon import gmI18N
107 gmI18N.activate_locale()
108 gmI18N.install_domain()
109
110
113
114
115 print test_print_file()
116
117
118