home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / hplip / prnt / cups.py < prev    next >
Encoding:
Python Source  |  2006-08-30  |  5.0 KB  |  187 lines

  1. # -*- coding: utf-8 -*-
  2. #
  3. # (c) Copyright 2003-2006 Hewlett-Packard Development Company, L.P.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  18. #
  19. # Author: Don Welch
  20. #
  21.  
  22. # Std Lib
  23. import os, os.path, gzip, re, time, urllib, tempfile, glob
  24.  
  25. # Local
  26. from base.g import *
  27. import cupsext
  28.  
  29. # PPD parsing patterns
  30. mfg_pat = re.compile(r'\*\s*Manufacturer:\s*\".*?(.*?)\"', re.IGNORECASE)
  31. model_pat = re.compile(r'\*\s*Product:\s*\"\(.*?(.*?)\)\"', re.IGNORECASE)
  32.  
  33. IPP_PRINTER_STATE_IDLE = 3
  34. IPP_PRINTER_STATE_PROCESSING = 4
  35. IPP_PRINTER_STATE_STOPPED = 5
  36.  
  37. def restartCUPS(): # must be root. How do you check for this?
  38.     os.system('killall -HUP cupsd')
  39.  
  40. def getPPDPath(addtional_paths=[]):
  41.     search_paths = [prop.ppd_search_path.split(';')] + addtional_paths
  42.     for path in search_paths:
  43.         ppd_path = os.path.join(path, 'cups/model')
  44.         if os.path.exists(ppd_path):
  45.             return ppd_path
  46.  
  47.  
  48. def collectPPDs(ppd_path):
  49.     from base import utils
  50.     ppds = {} # { <model> : <PPD file> , ... }
  51.  
  52.     for f in utils.walkFiles(ppd_path, recurse=True, abs_paths=True,
  53.                               return_folders=False , pattern=prop.ppd_search_pattern):
  54.  
  55.         if f.endswith('.gz'):
  56.             g = gzip.open(f, 'r')
  57.         else:
  58.             g = open(f, 'r')
  59.  
  60.         try:
  61.             d = g.read(4096)
  62.         except IOError:
  63.             g.close()
  64.             continue
  65.         try:
  66.             mfg = mfg_pat.search(d).group(1).lower()
  67.         except ValueError:
  68.             g.close()
  69.             continue
  70.  
  71.         if mfg != 'hp':
  72.             continue
  73.  
  74.         try:
  75.             model = model_pat.search(d).group(1).replace(' ', '_')
  76.         except ValueError:
  77.             g.close()
  78.             continue
  79.  
  80.         ppds[model] = f
  81.  
  82.         g.close()
  83.  
  84.     return ppds
  85.  
  86.  
  87. def downloadPPD(lporg_model_name, driver='hpijs', url=prop.ppd_download_url):
  88.     # model name must match model name on lp.org
  89.     u = urllib.urlopen(url, urllib.urlencode({'driver' : driver,
  90.                                               'printer' : urllib.quote(lporg_model_name),
  91.                                               'show' : '0'}))
  92.  
  93.     ppd_file = os.path.join(tempfile.gettempdir(), lporg_model_name + prop.ppd_file_suffix)
  94.     f = file(ppd_file, 'w')
  95.     f.write(u.read())
  96.     f.close()
  97.  
  98.     return ppd_file
  99.  
  100.  
  101. def getAllowableMIMETypes():    
  102.     # Scan all /etc/cups/*.convs files for allowable file formats
  103.     files = glob.glob("/etc/cups/*.convs")
  104.     
  105.     allowable_mime_types = []
  106.     
  107.     for f in files:
  108.         #log.debug( "Capturing allowable MIME types from: %s" % f )
  109.         conv_file = file(f, 'r')
  110.     
  111.         for line in conv_file:
  112.             if not line.startswith("#") and len(line) > 1:
  113.                 try:
  114.                     source, dest, cost, prog =  line.split()
  115.                 except ValueError:
  116.                     continue
  117.     
  118.                 allowable_mime_types.append(source)
  119.             
  120.     return allowable_mime_types
  121.  
  122.  
  123. # cupsext wrapper
  124.  
  125. def getDefault():
  126.     return cupsext.getDefault()
  127.  
  128. def openPPD(printer):
  129.     return cupsext.openPPD(printer)
  130.  
  131. def closePPD():
  132.     return cupsext.closePPD()
  133.     
  134. def getPPD(printer):
  135.     return cupsext.getPPD(printer)
  136.  
  137. def getPPDOption(option):
  138.     return cupsext.getPPDOption(option)
  139.  
  140. def getPPDPageSize():
  141.     return cupsext.getPPDPageSize()
  142.  
  143. def getPrinters():
  144.     return cupsext.getPrinters()
  145.  
  146. def getJobs(my_job=0, completed=0):
  147.     return cupsext.getJobs(my_job, completed)
  148.  
  149. def getAllJobs(my_job=0):
  150.     return cupsext.getJobs(my_job, 0) + cupsext.getJobs(my_job, 1)
  151.  
  152. def getVersion():
  153.     return cupsext.getVersion()
  154.  
  155. def getServer():
  156.     return cupsext.getServer()
  157.  
  158. def cancelJob(jobid, dest=None):
  159.     if dest is not None:
  160.         return cupsext.cancelJob(dest, jobid)
  161.     else:
  162.         jobs = cupsext.getJobs(0, 0)
  163.         for j in jobs:
  164.             if j.id == jobid:
  165.                 return cupsext.cancelJob(j.dest, jobid)
  166.  
  167.     return False
  168.     
  169. def resetOptions():
  170.     return cupsext.resetOptions()
  171.     
  172. def addOption(option):
  173.     return cupsext.addOption(option)
  174.     
  175. def printFile(printer, filename, title):
  176.     if os.path.exists(filename):
  177.         return cupsext.printFileWithOptions(printer, filename, title)
  178.     else:
  179.         return -1
  180.         
  181. def addPrinter(printer_name, device_uri, location, ppd_file, info):
  182.     return cupsext.addPrinter(printer_name, device_uri, location, ppd_file, info)
  183.     
  184. def delPrinter(printer_name):
  185.     return cupsext.delPrinter(printer_name)
  186.         
  187.