Package Gnumed :: Package pycommon :: Module gmPrinting
[frames] | no frames]

Source Code for Module Gnumed.pycommon.gmPrinting

  1  """GNUmed printing.""" 
  2  # ======================================================================= 
  3  __author__  = "K.Hilbert <Karsten.Hilbert@gmx.net>" 
  4  __license__ = 'GPL v2 or later (details at http://www.gnu.org)' 
  5   
  6  # ======================================================================= 
  7  import logging 
  8  import sys 
  9  import os 
 10  import subprocess 
 11  import codecs 
 12  import time 
 13   
 14   
 15  if __name__ == '__main__': 
 16          sys.path.insert(0, '../../') 
 17  from Gnumed.pycommon import gmShellAPI 
 18  from Gnumed.pycommon import gmTools 
 19   
 20   
 21  _log = logging.getLogger('gm.printing') 
 22   
 23   
 24  known_printjob_types = [ 
 25          u'medication_list', 
 26          u'generic_document' 
 27  ] 
 28   
 29  external_print_APIs = [ 
 30          u'gm-print_doc', 
 31          u'os_startfile',                # win, mostly 
 32          u'gsprint',                             # win 
 33          u'acrobat_reader',              # win 
 34          u'gtklp',                               # Linux 
 35          u'Internet_Explorer',   # win 
 36          u'Mac_Preview'                  # MacOSX 
 37  ] 
 38   
 39  #======================================================================= 
 40  # internal print API 
 41  #----------------------------------------------------------------------- 
 92  #======================================================================= 
 93  # external print APIs 
 94  #----------------------------------------------------------------------- 
95 -def _print_files_by_mac_preview(filenames=None):
96 97 # if os.name != 'mac': # does not work 98 if sys.platform != 'darwin': 99 _log.debug('MacOSX <open> only available under MacOSX/Darwin') 100 return False 101 102 for filename in filenames: 103 cmd_line = [ 104 r'open', # "open" must be in the PATH 105 r'-a Preview', # action = Preview 106 filename 107 ] 108 _log.debug('printing with %s' % cmd_line) 109 try: 110 mac_preview = subprocess.Popen(cmd_line) 111 except OSError: 112 _log.debug('cannot run <open -a Preview>') 113 return False 114 mac_preview.communicate() 115 if mac_preview.returncode != 0: 116 _log.error('<open -a Preview> returned [%s], failed to print', mac_preview.returncode) 117 return False 118 119 return True
120 #-----------------------------------------------------------------------
121 -def _print_files_by_IE(filenames=None):
122 123 if os.name != 'nt': 124 _log.debug('Internet Explorer only available under Windows') 125 return False 126 127 try: 128 from win32com import client as dde_client 129 except ImportError: 130 _log.exception('<win32com> Python module not available for use in printing') 131 return False 132 133 i_explorer = dde_client.Dispatch("InternetExplorer.Application") 134 135 for filename in filenames: 136 if i_explorer.Busy: 137 time.sleep(1) 138 i_explorer.Navigate(os.path.normpath(filename)) 139 if i_explorer.Busy: 140 time.sleep(1) 141 i_explorer.Document.printAll() 142 143 i_explorer.Quit() 144 return True
145 #-----------------------------------------------------------------------
146 -def _print_files_by_gtklp(filenames=None):
147 148 # if os.name != 'posix': 149 if sys.platform != 'linux2': 150 _log.debug('<gtklp> only available under Linux') 151 return False 152 153 cmd_line = [ 154 r'gtklp', 155 r'-i', 156 r'-# 1' 157 ] 158 cmd_line.extend(filenames) 159 _log.debug('printing with %s' % cmd_line) 160 try: 161 gtklp = subprocess.Popen(cmd_line) 162 except OSError: 163 _log.debug('cannot run <gtklp>') 164 return False 165 gtklp.communicate() 166 if gtklp.returncode != 0: 167 _log.error('<gtklp> returned [%s], failed to print', gtklp.returncode) 168 return False 169 170 return True
171 #-----------------------------------------------------------------------
172 -def _print_files_by_gsprint_exe(filenames=None):
173 """Use gsprint.exe from Ghostscript tools. Windows only. 174 175 - docs: http://pages.cs.wisc.edu/~ghost/gsview/gsprint.htm 176 - download: http://www.cs.wisc.edu/~ghost/ 177 """ 178 if os.name != 'nt': 179 _log.debug('<gsprint.exe> only available under Windows') 180 return False 181 182 conf_filename = gmTools.get_unique_filename ( 183 prefix = 'gm2gsprint-', 184 suffix = '.cfg' 185 ).encode(sys.getfilesystemencoding()) 186 187 for filename in filenames: 188 conf_file = codecs.open(conf_filename, 'wb', 'utf8') 189 conf_file.write('-color\n') 190 conf_file.write('-query\n') # printer setup dialog 191 conf_file.write('-all\n') # all pages 192 conf_file.write('-copies 1\n') 193 conf_file.write('%s\n' % os.path.normpath(filename)) 194 conf_file.close() 195 196 cmd_line = [ 197 r'gsprint.exe', # "gsprint.exe" must be in the PATH 198 r'-config "%s"' % conf_filename 199 ] 200 _log.debug('printing with %s' % cmd_line) 201 try: 202 gsprint = subprocess.Popen(cmd_line) 203 except OSError: 204 _log.debug('cannot run <gsprint.exe>') 205 return False 206 gsprint.communicate() 207 if gsprint.returncode != 0: 208 _log.error('<gsprint.exe> returned [%s], failed to print', gsprint.returncode) 209 return False 210 211 return True
212 #-----------------------------------------------------------------------
213 -def _print_files_by_acroread_exe(filenames):
214 """Use Adobe Acrobat Reader. Windows only. 215 216 - docs: http://www.robvanderwoude.com/printfiles.php#PrintPDF 217 """ 218 if os.name != 'nt': 219 _log.debug('Acrobat Reader only used under Windows') 220 return False 221 222 for filename in filenames: 223 cmd_line = [ 224 r'AcroRd32.exe', # "AcroRd32.exe" must be in the PATH 225 r'/s', # no splash 226 r'/o', # no open-file dialog 227 r'/h', # minimized 228 r'/p', # go straight to printing dialog 229 os.path.normpath(filename) 230 ] 231 _log.debug('printing with %s' % cmd_line) 232 try: 233 acroread = subprocess.Popen(cmd_line) 234 except OSError: 235 _log.debug('cannot run <AcroRd32.exe>') 236 cmd_line[0] = r'acroread.exe' # "acroread.exe" must be in the PATH 237 _log.debug('printing with %s' % cmd_line) 238 try: 239 acroread = subprocess.Popen(cmd_line) 240 except OSError: 241 _log.debug('cannot run <acroread.exe>') 242 return False 243 244 acroread.communicate() 245 if acroread.returncode != 0: 246 _log.error('Acrobat Reader returned [%s], failed to print', acroread.returncode) 247 return False 248 249 return True
250 #-----------------------------------------------------------------------
251 -def _print_files_by_os_startfile(filenames=None):
252 253 try: 254 os.startfile 255 except AttributeError: 256 _log.error('platform does not support "os.startfile()"') 257 return False 258 259 _log.debug('printing [%s]', filenames) 260 261 for filename in filenames: 262 fname = os.path.normcase(os.path.normpath(filename)) 263 _log.debug('%s -> %s', filename, fname) 264 try: 265 os.startfile(fname, 'print') 266 except OSError: 267 _log.exception('cannot os.startfile()') 268 269 return True
270 #-----------------------------------------------------------------------
271 -def _print_files_by_shellscript(filenames=None, jobtype=None):
272 273 paths = gmTools.gmPaths() 274 local_script = os.path.join(paths.local_base_dir, '..', 'external-tools', 'gm-print_doc') 275 276 #candidates = [u'gm-print_doc', u'gm-print_doc.bat', local_script, u'gm-print_doc.bat'] 277 candidates = [u'gm-print_doc', local_script, u'gm-print_doc.bat'] 278 found, binary = gmShellAPI.find_first_binary(binaries = candidates) 279 if not found: 280 binary = r'gm-print_doc.bat' 281 282 cmd_line = [ 283 binary, 284 jobtype 285 ] 286 cmd_line.extend(filenames) 287 _log.debug('printing with %s', cmd_line) 288 try: 289 gm_print_doc = subprocess.Popen(cmd_line) 290 except OSError: 291 _log.debug('cannot run <gm_print_doc(.bat)>') 292 return False 293 gm_print_doc.communicate() 294 if gm_print_doc.returncode != 0: 295 _log.error('<gm_print_doc> returned [%s], failed to print', gm_print_doc.returncode) 296 return False 297 298 return True
299 300 # args = u' %s %s' % (jobtype, filename) 301 # success = gmShellAPI.run_first_available_in_shell ( 302 # binaries = candidates, 303 # args = args, 304 # blocking = True, 305 # run_last_one_anyway = True 306 # ) 307 # 308 # if success: 309 # return True 310 # 311 # _log.error('print command failed') 312 # return False 313 #======================================================================= 314 # main 315 #----------------------------------------------------------------------- 316 if __name__ == '__main__': 317 318 if len(sys.argv) < 2: 319 sys.exit() 320 321 if sys.argv[1] != 'test': 322 sys.exit() 323 324 from Gnumed.pycommon import gmLog2 325 from Gnumed.pycommon import gmI18N 326 gmI18N.activate_locale() 327 gmI18N.install_domain() 328 329 #--------------------------------------------------------------------
330 - def test_print_files():
331 return print_files(filenames = [sys.argv[2]], jobtype = sys.argv[3])
332 #--------------------------------------------------------------------
333 - def test_print_files_by_shellscript():
334 print_files(filenames = [sys.argv[2], sys.argv[2]], jobtype = u'generic_document', print_api = 'gm-print_doc')
335 #--------------------------------------------------------------------
336 - def test_print_files_by_gtklp():
337 print_files(filenames = [sys.argv[2], sys.argv[2]], jobtype = u'generic_document', print_api = u'gtklp')
338 #--------------------------------------------------------------------
339 - def test_print_files_by_mac_preview():
340 print "testing printing via Mac Preview" 341 _print_files_by_mac_preview(filenames = [sys.argv[0]])
342 #-------------------------------------------------------------------- 343 #print test_print_files() 344 #test_print_files_by_gtklp() 345 test_print_files_by_mac_preview() 346 347 # ======================================================================= 348