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