home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / hplip / copier / copier.py < prev    next >
Encoding:
Python Source  |  2007-04-04  |  13.5 KB  |  371 lines

  1. # -*- coding: utf-8 -*-
  2. #
  3. # (c) Copyright 2003-2007 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. from __future__ import generators
  23.  
  24. # Std Lib
  25. import sys, os, os.path, mmap, struct, time, threading, Queue, socket
  26. from cStringIO import StringIO
  27.  
  28. # Local
  29. from base.g import *
  30. from base.codes import *
  31. from base import device, utils, status, pml, msg
  32.  
  33. # Event queue values (UI ==> Copy thread)
  34. COPY_CANCELED = 1
  35.  
  36. # Update queue values (Copy thread ==> UI)
  37. STATUS_IDLE = 0
  38. STATUS_SETTING_UP = 1
  39. STATUS_WARMING_UP = 2
  40. STATUS_ACTIVE = 3
  41. STATUS_DONE = 4
  42. STATUS_ERROR = 5
  43.  
  44.  
  45. # PML Copier Only
  46. class PMLCopyDevice(device.Device):
  47.     def __init__(self, device_uri=None, printer_name=None,
  48.                  hpssd_sock=None, hpiod_sock=None, callback=None):
  49.  
  50.         device.Device.__init__(self, device_uri, printer_name,
  51.                                hpssd_sock, hpiod_sock, callback)
  52.  
  53.         self.copy_thread = None
  54.  
  55.     def copy(self, num_copies=1, contrast=0, reduction=100,
  56.              quality=pml.COPIER_QUALITY_NORMAL, 
  57.              fit_to_page=pml.COPIER_FIT_TO_PAGE_ENABLED,
  58.              scan_style=SCAN_STYLE_FLATBED,
  59.              update_queue=None, event_queue=None): 
  60.  
  61.         if not self.isCopyActive():
  62.             self.copy_thread = PMLCopyThread(self, num_copies, contrast, reduction, quality, 
  63.                                              fit_to_page, scan_style, update_queue, event_queue)
  64.             self.copy_thread.start()
  65.             return True
  66.         else:
  67.             return False
  68.  
  69.     def isCopyActive(self):
  70.         if self.copy_thread is not None:
  71.             return self.copy_thread.isAlive()
  72.         else:
  73.             return False
  74.  
  75.     def waitForCopyThread(self):
  76.         if self.copy_thread is not None and \
  77.             self.copy_thread.isAlive():
  78.  
  79.             self.copy_thread.join()
  80.  
  81.  
  82.  
  83. class PMLCopyThread(threading.Thread):
  84.     def __init__(self, dev, num_copies, contrast, reduction, quality, 
  85.                  fit_to_page, scan_style, 
  86.                  update_queue=None, event_queue=None):
  87.  
  88.         threading.Thread.__init__(self)
  89.         self.dev = dev
  90.         self.num_copies = num_copies
  91.         self.contrast = contrast
  92.         self.reduction = reduction
  93.         self.quality = quality
  94.         self.fit_to_page = fit_to_page
  95.         self.scan_style = scan_style
  96.         self.event_queue = event_queue
  97.         self.update_queue = update_queue
  98.         self.prev_update = ''
  99.         self.copy_type = self.dev.copy_type
  100.         log.debug("Copy-type = %d" % self.copy_type)
  101.  
  102.     def run(self):
  103.         STATE_DONE = 0
  104.         STATE_ERROR = 5
  105.         STATE_ABORTED = 10
  106.         STATE_SUCCESS = 20
  107.         STATE_BUSY = 25
  108.         STATE_SET_TOKEN = 30
  109.         STATE_SETUP_STATE = 40
  110.         STATE_SETUP_PARAMS = 50
  111.         STATE_START = 60
  112.         STATE_ACTIVE = 70
  113.         STATE_RESET_TOKEN = 80
  114.  
  115.         state = STATE_SET_TOKEN
  116.  
  117.         while state != STATE_DONE: # ------------------------- Copier Thread
  118.             if self.check_for_cancel():
  119.                 state = STATE_ABORTED
  120.  
  121.             if state == STATE_ABORTED:
  122.                 log.debug("%s State: Aborted" % ("*"*20))
  123.                 self.write_queue(STATUS_ERROR)
  124.                 state = STATE_RESET_TOKEN
  125.  
  126.             if state == STATE_ERROR:
  127.                 log.debug("%s State: Error" % ("*"*20))
  128.                 self.write_queue(STATUS_ERROR)
  129.                 state = STATE_RESET_TOKEN
  130.  
  131.             elif state == STATE_SUCCESS:
  132.                 log.debug("%s State: Success" % ("*"*20))
  133.                 self.write_queue(STATUS_DONE)
  134.                 state = STATE_RESET_TOKEN
  135.  
  136.             elif state == STATE_BUSY:
  137.                 log.debug("%s State: Busy" % ("*"*20))
  138.                 self.write_queue(STATUS_ERROR)
  139.                 state = STATE_RESET_TOKEN
  140.  
  141.             elif state == STATE_SET_TOKEN:
  142.                 log.debug("%s State: Acquire copy token" % ("*"*20))
  143.  
  144.                 self.write_queue(STATUS_SETTING_UP)
  145.  
  146.                 try:
  147.                     result_code, token = self.dev.getPML(pml.OID_COPIER_TOKEN)
  148.                 except Error:
  149.                     log.debug("Unable to acquire copy token (1).")
  150.                     state = STATE_SETUP_STATE
  151.                 else:
  152.                     if result_code > pml.ERROR_MAX_OK:
  153.                         state = STATE_SETUP_STATE
  154.                         log.debug("Skipping token acquisition.")
  155.                     else:
  156.                         token = time.strftime("%d%m%Y%H:%M:%S", time.gmtime())
  157.                         log.debug("Setting token: %s" % token)
  158.                         try:
  159.                             self.dev.setPML(pml.OID_COPIER_TOKEN, token)
  160.                         except Error:
  161.                             log.error("Unable to acquire copy token (2).")
  162.                             state = STATUS_ERROR
  163.                         else:
  164.                             result_code, check_token = self.dev.getPML(pml.OID_COPIER_TOKEN)
  165.  
  166.                             if check_token == token:
  167.                                 state = STATE_SETUP_STATE
  168.                             else:
  169.                                 log.error("Unable to acquire copy token (3).")
  170.                                 state = STATE_ERROR
  171.  
  172.             elif state == STATE_SETUP_STATE:
  173.                 log.debug("%s State: Setup state" % ("*"*20))
  174.  
  175.                 if self.copy_type == COPY_TYPE_DEVICE:
  176.                     result_code, copy_state = self.dev.getPML(pml.OID_COPIER_JOB)
  177.  
  178.                     if copy_state == pml.COPIER_JOB_IDLE:
  179.                         self.dev.setPML(pml.OID_COPIER_JOB, pml.COPIER_JOB_SETUP)
  180.                         state = STATE_SETUP_PARAMS
  181.  
  182.                     else:
  183.                         state = STATE_BUSY
  184.  
  185.                 elif self.copy_type == COPY_TYPE_AIO_DEVICE:
  186.                     result_code, copy_state = self.dev.getPML(pml.OID_SCAN_TO_PRINTER)
  187.  
  188.                     if copy_state == pml.SCAN_TO_PRINTER_IDLE:
  189.                         state = STATE_SETUP_PARAMS
  190.  
  191.                     else:
  192.                         state = STATE_BUSY
  193.  
  194.  
  195.  
  196.             elif state == STATE_SETUP_PARAMS:
  197.                 log.debug("%s State: Setup Params" % ("*"*20))
  198.  
  199.                 if self.num_copies < 0: self.num_copies = 1
  200.                 if self.num_copies > 99: self.num_copies = 99
  201.  
  202.                 if self.copy_type == COPY_TYPE_DEVICE: # MFP
  203.  
  204.                     # num_copies
  205.                     self.dev.setPML(pml.OID_COPIER_JOB_NUM_COPIES, self.num_copies)
  206.  
  207.                     # contrast
  208.                     self.dev.setPML(pml.OID_COPIER_JOB_CONTRAST, self.contrast)
  209.  
  210.                     # reduction
  211.                     self.dev.setPML(pml.OID_COPIER_JOB_REDUCTION, self.reduction)
  212.  
  213.                     # quality
  214.                     self.dev.setPML(pml.OID_COPIER_JOB_QUALITY, self.quality)
  215.  
  216.                     # fit_to_page
  217.                     if self.scan_style == SCAN_STYLE_FLATBED:
  218.                         self.dev.setPML(pml.OID_COPIER_JOB_FIT_TO_PAGE, self.fit_to_page)
  219.  
  220.                 else: # AiO
  221.                     # num_copies
  222.                     self.dev.setPML(pml.OID_COPIER_NUM_COPIES_AIO, self.num_copies)
  223.  
  224.                     # contrast
  225.                     self.contrast = (self.contrast * 10 / 25) + 50
  226.                     self.dev.setPML(pml.OID_COPIER_CONTRAST_AIO, self.contrast)
  227.  
  228.                     if self.fit_to_page == pml.COPIER_FIT_TO_PAGE_ENABLED:
  229.                         self.reduction = 0
  230.  
  231.                     # reduction
  232.                     self.dev.setPML(pml.OID_COPIER_REDUCTION_AIO, self.reduction)
  233.  
  234.                     # quality
  235.                     self.dev.setPML(pml.OID_COPIER_QUALITY_AIO, self.quality)
  236.  
  237.                     self.dev.setPML(pml.OID_PIXEL_DATA_TYPE, pml.PIXEL_DATA_TYPE_COLOR_24_BIT)
  238.                     self.dev.setPML(pml.OID_COPIER_SPECIAL_FEATURES, pml.COPY_FEATURE_NONE)
  239.                     self.dev.setPML(pml.OID_COPIER_PHOTO_MODE, pml.ENHANCE_LIGHT_COLORS | pml.ENHANCE_TEXT)
  240.                     
  241.                     # tray select
  242.                     self.dev.setPML(pml.OID_COPIER_JOB_INPUT_TRAY_SELECT, pml.COPIER_JOB_INPUT_TRAY_1)
  243.                     
  244.                     # media type
  245.                     self.dev.setPML(pml.OID_COPIER_MEDIA_TYPE, pml.COPIER_MEDIA_TYPE_AUTOMATIC)
  246.                     
  247.                     # pixel data type
  248.                     self.dev.setPML(pml.OID_PIXEL_DATA_TYPE, pml.PIXEL_DATA_TYPE_COLOR_24_BIT)
  249.                     
  250.                     # special features
  251.                     self.dev.setPML(pml.OID_COPIER_SPECIAL_FEATURES, pml.COPY_FEATURE_NONE)
  252.                     
  253.                     # media size
  254.                     self.dev.setPML(pml.OID_COPIER_JOB_MEDIA_SIZE, pml.COPIER_JOB_MEDIA_SIZE_US_LETTER)
  255.                     
  256.  
  257.                 
  258.                 
  259.                 log.debug("num_copies = %d" % self.num_copies)
  260.                 log.debug("contrast= %d" % self.contrast)
  261.                 log.debug("reduction = %d" % self.reduction)
  262.                 log.debug("quality = %d" % self.quality)
  263.                 log.debug("fit_to_page = %d" % self.fit_to_page)
  264.  
  265.                 state = STATE_START
  266.  
  267.             elif state == STATE_START:
  268.                 log.debug("%s State: Start" % ("*"*20))
  269.  
  270.                 if self.copy_type == COPY_TYPE_DEVICE:
  271.                     self.dev.setPML(pml.OID_COPIER_JOB, pml.COPIER_JOB_START)
  272.  
  273.                 elif self.copy_type == COPY_TYPE_AIO_DEVICE:
  274.                     self.dev.setPML(pml.OID_SCAN_TO_PRINTER, pml.SCAN_TO_PRINTER_START)
  275.  
  276.                 state = STATE_ACTIVE
  277.  
  278.             elif state == STATE_ACTIVE:
  279.                 log.debug("%s State: Active" % ("*"*20))
  280.  
  281.                 if self.copy_type == COPY_TYPE_DEVICE:
  282.                     while True:
  283.                         result_code, copy_state = self.dev.getPML(pml.OID_COPIER_JOB)
  284.  
  285.                         if self.check_for_cancel():
  286.                             self.dev.setPML(pml.OID_COPIER_JOB, pml.COPIER_JOB_IDLE) # cancel
  287.                             state = STATE_ABORTED
  288.                             break
  289.  
  290.                         if copy_state == pml.COPIER_JOB_START:
  291.                             log.debug("state = start")
  292.                             time.sleep(1)
  293.                             continue
  294.  
  295.                         if copy_state == pml.COPIER_JOB_ACTIVE:
  296.                             self.write_queue(STATUS_ACTIVE)
  297.                             log.debug("state = active")
  298.                             time.sleep(2)
  299.                             continue
  300.  
  301.                         elif copy_state == pml.COPIER_JOB_ABORTING:
  302.                             log.debug("state = aborting")
  303.                             state = STATE_ABORTED
  304.                             break
  305.  
  306.                         elif copy_state == pml.COPIER_JOB_IDLE:
  307.                             log.debug("state = idle")
  308.                             state = STATE_SUCCESS
  309.                             break
  310.  
  311.                 elif self.copy_type == COPY_TYPE_AIO_DEVICE:
  312.                     while True:
  313.                         result_code, copy_state = self.dev.getPML(pml.OID_SCAN_TO_PRINTER)
  314.  
  315.                         if self.check_for_cancel():
  316.                             self.dev.setPML(pml.OID_SCAN_TO_PRINTER, pml.SCAN_TO_PRINTER_IDLE) # cancel
  317.                             state = STATE_ABORTED
  318.                             break
  319.  
  320.                         if copy_state == pml.SCAN_TO_PRINTER_START:
  321.                             log.debug("state = start")
  322.                             time.sleep(1)
  323.                             continue
  324.  
  325.                         if copy_state == pml.SCAN_TO_PRINTER_ACTIVE:
  326.                             self.write_queue(STATUS_ACTIVE)
  327.                             log.debug("state = active")
  328.                             time.sleep(2)
  329.                             continue
  330.  
  331.                         elif copy_state == pml.SCAN_TO_PRINTER_ABORTED:
  332.                             log.debug("state = aborting")
  333.                             state = STATE_ABORTED
  334.                             break
  335.  
  336.                         elif copy_state == pml.SCAN_TO_PRINTER_IDLE:
  337.                             log.debug("state = idle")
  338.                             state = STATE_SUCCESS
  339.                             break
  340.  
  341.  
  342.             elif state == STATE_RESET_TOKEN:
  343.                 log.debug("%s State: Release copy token" % ("*"*20))
  344.  
  345.                 try:
  346.                     self.dev.setPML(pml.OID_COPIER_TOKEN, '\x00'*16)
  347.                 except Error:
  348.                     log.error("Unable to release copier token.")
  349.  
  350.                 state = STATE_DONE
  351.  
  352.  
  353.     def check_for_cancel(self):
  354.         canceled = False
  355.         while self.event_queue.qsize():
  356.             try:
  357.                 event = self.event_queue.get(0)
  358.                 if event == COPY_CANCELED:
  359.                     canceled = True
  360.                     log.debug("Cancel pressed!")
  361.             except Queue.Empty:
  362.                 break
  363.  
  364.         return canceled
  365.  
  366.     def write_queue(self, message):
  367.         if self.update_queue is not None and message != self.prev_update:
  368.             self.update_queue.put(message)
  369.             time.sleep(0)
  370.             self.prev_update = message
  371.