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