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

Source Code for Module Gnumed.pycommon.gmPrinting

  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 
  9  import sys 
 10  import os 
 11  import subprocess 
 12  import codecs 
 13  import time 
 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  _log.info(__version__) 
 24   
 25   
 26  known_printjob_types = [ 
 27          u'medication_list', 
 28          u'generic_document' 
 29  ] 
 30   
 31  external_print_APIs = [ 
 32          u'gm-print_doc', 
 33          u'os_startfile',                # win, mostly 
 34          u'gsprint',                             # win 
 35          u'acrobat_reader',              # win 
 36          u'gktlp',                               # Linux 
 37          u'Internet_Explorer',   # win 
 38          u'Mac_Preview'                  # MacOSX 
 39  ] 
 40   
 41  #======================================================================= 
 42  # internal print API 
 43  #----------------------------------------------------------------------- 
 91  #======================================================================= 
 92  # external print APIs 
 93  #----------------------------------------------------------------------- 
94 -def _print_file_by_mac_preview(filename=None):
95 96 if sys.platform != 'darwin': 97 # if os.name != 'mac': 98 _log.debug('MacOSX <open> only available under MacOSX/Darwin') 99 return False 100 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, stdout = subprocess.PIPE, stderr = subprocess.PIPE) 109 except OSError: 110 _log.debug('cannot run <open -a Preview>') 111 return False 112 #except ValueError: # invalid arguments == programming error 113 stdout, stderr = mac_preview.communicate() 114 if mac_preview.returncode != 0: 115 _log.error('<open -a Preview> returned [%s], failed to print', mac_preview.returncode) 116 return False 117 118 return True
119 #-----------------------------------------------------------------------
120 -def _print_file_by_IE(filename=None):
121 122 if os.name != 'nt': 123 _log.debug('Internet Explorer only available under Windows') 124 return False 125 126 try: 127 from win32com import client as dde_client 128 except ImportError: 129 _log.exception('<win32com> Python module not available for use in printing') 130 return False 131 132 i_explorer = dde_client.Dispatch("InternetExplorer.Application") 133 i_explorer.Navigate(os.path.normpath(filename)) 134 if i_explorer.Busy: 135 time.sleep(1) 136 i_explorer.Document.printAll() 137 i_explorer.Quit() 138 139 return True
140 #-----------------------------------------------------------------------
141 -def _print_file_by_gtklp(filename=None):
142 143 if sys.platform != 'linux2': 144 # if os.name != 'posix': 145 _log.debug('<gtklp> only available under Linux') 146 return False 147 148 cmd_line = [ 149 r'gtklp', # "gtklp" must be in the PATH 150 r'-i', # ignore STDIN garbage 151 r'-# 1', # 1 copy 152 filename 153 ] 154 _log.debug('printing with %s' % cmd_line) 155 try: 156 gtklp = subprocess.Popen(cmd_line, stdout = subprocess.PIPE, stderr = subprocess.PIPE) 157 except OSError: 158 _log.debug('cannot run <gtklp>') 159 return False 160 #except ValueError: # invalid arguments == programming error 161 stdout, stderr = gtklp.communicate() 162 if gtklp.returncode != 0: 163 _log.error('<gtklp> returned [%s], failed to print', gtklp.returncode) 164 return False 165 166 return True
167 #-----------------------------------------------------------------------
168 -def _print_file_by_gsprint_exe(filename=None):
169 """Use gsprint.exe from Ghostscript tools. Windows only. 170 171 - docs: http://pages.cs.wisc.edu/~ghost/gsview/gsprint.htm 172 - download: http://www.cs.wisc.edu/~ghost/ 173 """ 174 if os.name != 'nt': 175 _log.debug('<gsprint.exe> only available under Windows') 176 return False 177 178 conf_filename = gmTools.get_unique_filename(prefix = 'gm2gsprint-', suffix = '.cfg') 179 conf_file = codecs.open(conf_filename, 'wb', 'utf8') 180 conf_file.write('-color\n') 181 conf_file.write('-query\n') # printer setup dialog 182 conf_file.write('-all\n') # all pages 183 conf_file.write('-copies 1\n') 184 conf_file.write('%s\n' % os.path.normpath(filename)) 185 conf_file.close() 186 187 # "gsprint.exe" must be in the PATH 188 cmd_line = [ r'gsprint.exe', r'-config "%s"' % conf_filename ] 189 _log.debug('printing with %s' % cmd_line) 190 try: 191 gsprint = subprocess.Popen(cmd_line, stdout = subprocess.PIPE, stderr = subprocess.PIPE) 192 except OSError: 193 _log.debug('cannot run <gsprint.exe>') 194 return False 195 #except ValueError: # invalid arguments == programming error 196 stdout, stderr = gsprint.communicate() 197 if gsprint.returncode != 0: 198 _log.error('<gsprint.exe> returned [%s], failed to print', gsprint.returncode) 199 return False 200 201 return True
202 #-----------------------------------------------------------------------
203 -def _print_file_by_acroread_exe(filename):
204 """Use Adobe Acrobat Reader. Windows only. 205 206 - docs: http://www.robvanderwoude.com/printfiles.php#PrintPDF 207 """ 208 if os.name != 'nt': 209 _log.debug('Acrobat Reader only used under Windows') 210 return False 211 212 cmd_line = [ 213 r'AcroRd32.exe', # "AcroRd32.exe" must be in the PATH 214 r'/s', # no splash 215 r'/o', # no open-file dialog 216 r'/h', # minimized 217 r'/p', # go straight to printing dialog 218 os.path.normpath(filename) 219 ] 220 _log.debug('printing with %s' % cmd_line) 221 try: 222 acroread = subprocess.Popen(cmd_line, stdout = subprocess.PIPE, stderr = subprocess.PIPE) 223 except OSError: 224 _log.debug('cannot run <AcroRd32.exe>') 225 cmd_line[0] = r'acroread.exe' 226 _log.debug('printing with %s' % cmd_line) 227 try: 228 acroread = subprocess.Popen(cmd_line, stdout = subprocess.PIPE, stderr = subprocess.PIPE) 229 except OSError: 230 _log.debug('cannot run <acroread.exe>') 231 return False 232 233 stdout, stderr = acroread.communicate() 234 if acroread.returncode != 0: 235 _log.error('Acrobat Reader returned [%s], failed to print', acroread.returncode) 236 return False 237 238 return True
239 #-----------------------------------------------------------------------
240 -def _print_file_by_os_startfile(filename=None):
241 242 _log.debug('printing [%s]', filename) 243 244 try: 245 os.startfile(filename, 'print') 246 except AttributeError: 247 _log.exception('platform does not support "os.startfile()", cannot print') 248 return False 249 250 return True
251 #-----------------------------------------------------------------------
252 -def _print_file_by_shellscript(filename=None, jobtype=None):
253 254 paths = gmTools.gmPaths() 255 local_script = os.path.join(paths.local_base_dir, '..', 'external-tools', 'gm-print_doc') 256 257 candidates = [u'gm-print_doc', u'gm-print_doc.bat', local_script, u'gm-print_doc.bat'] 258 args = u' %s %s' % (jobtype, filename) 259 260 success = gmShellAPI.run_first_available_in_shell ( 261 binaries = candidates, 262 args = args, 263 blocking = True, 264 run_last_one_anyway = True 265 ) 266 267 if success: 268 return True 269 270 _log.error('print command failed') 271 return False
272 #======================================================================= 273 # main 274 #----------------------------------------------------------------------- 275 if __name__ == '__main__': 276 277 if len(sys.argv) < 2: 278 sys.exit() 279 280 if sys.argv[1] != 'test': 281 sys.exit() 282 283 from Gnumed.pycommon import gmLog2 284 from Gnumed.pycommon import gmI18N 285 gmI18N.activate_locale() 286 gmI18N.install_domain() 287 288
289 - def test_print_file():
290 return print_file(filename = sys.argv[2], jobtype = sys.argv[3])
291 #-------------------------------------------------------------------- 292 293 print test_print_file() 294 295 # ======================================================================= 296