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.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2007-04-29  |  8.7 KB  |  289 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. from __future__ import generators
  5. import sys
  6. import os
  7. import os.path as os
  8. import mmap
  9. import struct
  10. import time
  11. import threading
  12. import Queue
  13. import socket
  14. from cStringIO import StringIO
  15. from base.g import *
  16. from base.codes import *
  17. from base import device, utils, status, pml, msg
  18. COPY_CANCELED = 1
  19. STATUS_IDLE = 0
  20. STATUS_SETTING_UP = 1
  21. STATUS_WARMING_UP = 2
  22. STATUS_ACTIVE = 3
  23. STATUS_DONE = 4
  24. STATUS_ERROR = 5
  25.  
  26. class PMLCopyDevice(device.Device):
  27.     
  28.     def __init__(self, device_uri = None, printer_name = None, hpssd_sock = None, hpiod_sock = None, callback = None):
  29.         device.Device.__init__(self, device_uri, printer_name, hpssd_sock, hpiod_sock, callback)
  30.         self.copy_thread = None
  31.  
  32.     
  33.     def copy(self, num_copies = 1, contrast = 0, reduction = 100, quality = pml.COPIER_QUALITY_NORMAL, fit_to_page = pml.COPIER_FIT_TO_PAGE_ENABLED, scan_style = SCAN_STYLE_FLATBED, update_queue = None, event_queue = None):
  34.         if not self.isCopyActive():
  35.             self.copy_thread = PMLCopyThread(self, num_copies, contrast, reduction, quality, fit_to_page, scan_style, update_queue, event_queue)
  36.             self.copy_thread.start()
  37.             return True
  38.         else:
  39.             return False
  40.  
  41.     
  42.     def isCopyActive(self):
  43.         if self.copy_thread is not None:
  44.             return self.copy_thread.isAlive()
  45.         else:
  46.             return False
  47.  
  48.     
  49.     def waitForCopyThread(self):
  50.         if self.copy_thread is not None and self.copy_thread.isAlive():
  51.             self.copy_thread.join()
  52.         
  53.  
  54.  
  55.  
  56. class PMLCopyThread(threading.Thread):
  57.     
  58.     def __init__(self, dev, num_copies, contrast, reduction, quality, fit_to_page, scan_style, update_queue = None, event_queue = None):
  59.         threading.Thread.__init__(self)
  60.         self.dev = dev
  61.         self.num_copies = num_copies
  62.         self.contrast = contrast
  63.         self.reduction = reduction
  64.         self.quality = quality
  65.         self.fit_to_page = fit_to_page
  66.         self.scan_style = scan_style
  67.         self.event_queue = event_queue
  68.         self.update_queue = update_queue
  69.         self.prev_update = ''
  70.         self.copy_type = self.dev.copy_type
  71.         log.debug('Copy-type = %d' % self.copy_type)
  72.  
  73.     
  74.     def run(self):
  75.         STATE_DONE = 0
  76.         STATE_ERROR = 5
  77.         STATE_ABORTED = 10
  78.         STATE_SUCCESS = 20
  79.         STATE_BUSY = 25
  80.         STATE_SET_TOKEN = 30
  81.         STATE_SETUP_STATE = 40
  82.         STATE_SETUP_PARAMS = 50
  83.         STATE_START = 60
  84.         STATE_ACTIVE = 70
  85.         STATE_RESET_TOKEN = 80
  86.         state = STATE_SET_TOKEN
  87.         while state != STATE_DONE:
  88.             if self.check_for_cancel():
  89.                 state = STATE_ABORTED
  90.             
  91.             if state == STATE_ABORTED:
  92.                 log.debug('%s State: Aborted' % '********************')
  93.                 self.write_queue(STATUS_ERROR)
  94.                 state = STATE_RESET_TOKEN
  95.             
  96.             if state == STATE_ERROR:
  97.                 log.debug('%s State: Error' % '********************')
  98.                 self.write_queue(STATUS_ERROR)
  99.                 state = STATE_RESET_TOKEN
  100.                 continue
  101.             if state == STATE_SUCCESS:
  102.                 log.debug('%s State: Success' % '********************')
  103.                 self.write_queue(STATUS_DONE)
  104.                 state = STATE_RESET_TOKEN
  105.                 continue
  106.             if state == STATE_BUSY:
  107.                 log.debug('%s State: Busy' % '********************')
  108.                 self.write_queue(STATUS_ERROR)
  109.                 state = STATE_RESET_TOKEN
  110.                 continue
  111.             if state == STATE_SET_TOKEN:
  112.                 log.debug('%s State: Acquire copy token' % '********************')
  113.                 self.write_queue(STATUS_SETTING_UP)
  114.                 
  115.                 try:
  116.                     (result_code, token) = self.dev.getPML(pml.OID_COPIER_TOKEN)
  117.                 except Error:
  118.                     log.debug('Unable to acquire copy token (1).')
  119.                     state = STATE_SETUP_STATE
  120.  
  121.             None if result_code > pml.ERROR_MAX_OK else check_token == token
  122.             if state == STATE_SETUP_STATE:
  123.                 log.debug('%s State: Setup state' % '********************')
  124.                 if self.copy_type == COPY_TYPE_DEVICE:
  125.                     (result_code, copy_state) = self.dev.getPML(pml.OID_COPIER_JOB)
  126.                     if copy_state == pml.COPIER_JOB_IDLE:
  127.                         self.dev.setPML(pml.OID_COPIER_JOB, pml.COPIER_JOB_SETUP)
  128.                         state = STATE_SETUP_PARAMS
  129.                     else:
  130.                         state = STATE_BUSY
  131.                 elif self.copy_type == COPY_TYPE_AIO_DEVICE:
  132.                     (result_code, copy_state) = self.dev.getPML(pml.OID_SCAN_TO_PRINTER)
  133.                     if copy_state == pml.SCAN_TO_PRINTER_IDLE:
  134.                         state = STATE_SETUP_PARAMS
  135.                     else:
  136.                         state = STATE_BUSY
  137.                 
  138.             self.copy_type == COPY_TYPE_DEVICE
  139.             if state == STATE_SETUP_PARAMS:
  140.                 log.debug('%s State: Setup Params' % '********************')
  141.                 if self.num_copies < 0:
  142.                     self.num_copies = 1
  143.                 
  144.                 if self.num_copies > 99:
  145.                     self.num_copies = 99
  146.                 
  147.                 if self.copy_type == COPY_TYPE_DEVICE:
  148.                     self.dev.setPML(pml.OID_COPIER_JOB_NUM_COPIES, self.num_copies)
  149.                     self.dev.setPML(pml.OID_COPIER_JOB_CONTRAST, self.contrast)
  150.                     self.dev.setPML(pml.OID_COPIER_JOB_REDUCTION, self.reduction)
  151.                     self.dev.setPML(pml.OID_COPIER_JOB_QUALITY, self.quality)
  152.                     if self.scan_style == SCAN_STYLE_FLATBED:
  153.                         self.dev.setPML(pml.OID_COPIER_JOB_FIT_TO_PAGE, self.fit_to_page)
  154.                     
  155.                 else:
  156.                     self.dev.setPML(pml.OID_COPIER_NUM_COPIES_AIO, self.num_copies)
  157.                     self.contrast = self.contrast * 10 / 25 + 50
  158.                     self.dev.setPML(pml.OID_COPIER_CONTRAST_AIO, self.contrast)
  159.                     if self.fit_to_page == pml.COPIER_FIT_TO_PAGE_ENABLED:
  160.                         self.reduction = 0
  161.                     
  162.                     self.dev.setPML(pml.OID_COPIER_REDUCTION_AIO, self.reduction)
  163.                     self.dev.setPML(pml.OID_COPIER_QUALITY_AIO, self.quality)
  164.                     self.dev.setPML(pml.OID_PIXEL_DATA_TYPE, pml.PIXEL_DATA_TYPE_COLOR_24_BIT)
  165.                     self.dev.setPML(pml.OID_COPIER_SPECIAL_FEATURES, pml.COPY_FEATURE_NONE)
  166.                     self.dev.setPML(pml.OID_COPIER_PHOTO_MODE, pml.ENHANCE_LIGHT_COLORS | pml.ENHANCE_TEXT)
  167.                     self.dev.setPML(pml.OID_COPIER_JOB_INPUT_TRAY_SELECT, pml.COPIER_JOB_INPUT_TRAY_1)
  168.                     self.dev.setPML(pml.OID_COPIER_MEDIA_TYPE, pml.COPIER_MEDIA_TYPE_AUTOMATIC)
  169.                     self.dev.setPML(pml.OID_PIXEL_DATA_TYPE, pml.PIXEL_DATA_TYPE_COLOR_24_BIT)
  170.                     self.dev.setPML(pml.OID_COPIER_SPECIAL_FEATURES, pml.COPY_FEATURE_NONE)
  171.                     self.dev.setPML(pml.OID_COPIER_JOB_MEDIA_SIZE, pml.COPIER_JOB_MEDIA_SIZE_US_LETTER)
  172.                 log.debug('num_copies = %d' % self.num_copies)
  173.                 log.debug('contrast= %d' % self.contrast)
  174.                 log.debug('reduction = %d' % self.reduction)
  175.                 log.debug('quality = %d' % self.quality)
  176.                 log.debug('fit_to_page = %d' % self.fit_to_page)
  177.                 state = STATE_START
  178.                 continue
  179.             if state == STATE_START:
  180.                 log.debug('%s State: Start' % '********************')
  181.                 if self.copy_type == COPY_TYPE_DEVICE:
  182.                     self.dev.setPML(pml.OID_COPIER_JOB, pml.COPIER_JOB_START)
  183.                 elif self.copy_type == COPY_TYPE_AIO_DEVICE:
  184.                     self.dev.setPML(pml.OID_SCAN_TO_PRINTER, pml.SCAN_TO_PRINTER_START)
  185.                 
  186.                 state = STATE_ACTIVE
  187.                 continue
  188.             if state == STATE_ACTIVE:
  189.                 log.debug('%s State: Active' % '********************')
  190.                 if self.copy_type == COPY_TYPE_DEVICE:
  191.                     while True:
  192.                         (result_code, copy_state) = self.dev.getPML(pml.OID_COPIER_JOB)
  193.                         if self.check_for_cancel():
  194.                             self.dev.setPML(pml.OID_COPIER_JOB, pml.COPIER_JOB_IDLE)
  195.                             state = STATE_ABORTED
  196.                             break
  197.                         
  198.                         if copy_state == pml.COPIER_JOB_START:
  199.                             log.debug('state = start')
  200.                             time.sleep(1)
  201.                             continue
  202.                         
  203.                         if copy_state == pml.COPIER_JOB_ACTIVE:
  204.                             self.write_queue(STATUS_ACTIVE)
  205.                             log.debug('state = active')
  206.                             time.sleep(2)
  207.                             continue
  208.                             continue
  209.                         if copy_state == pml.COPIER_JOB_ABORTING:
  210.                             log.debug('state = aborting')
  211.                             state = STATE_ABORTED
  212.                             break
  213.                             continue
  214.                         if copy_state == pml.COPIER_JOB_IDLE:
  215.                             log.debug('state = idle')
  216.                             state = STATE_SUCCESS
  217.                             break
  218.                             continue
  219.                 elif self.copy_type == COPY_TYPE_AIO_DEVICE:
  220.                     while True:
  221.                         (result_code, copy_state) = self.dev.getPML(pml.OID_SCAN_TO_PRINTER)
  222.                         if self.check_for_cancel():
  223.                             self.dev.setPML(pml.OID_SCAN_TO_PRINTER, pml.SCAN_TO_PRINTER_IDLE)
  224.                             state = STATE_ABORTED
  225.                             break
  226.                         
  227.                         if copy_state == pml.SCAN_TO_PRINTER_START:
  228.                             log.debug('state = start')
  229.                             time.sleep(1)
  230.                             continue
  231.                         
  232.                         if copy_state == pml.SCAN_TO_PRINTER_ACTIVE:
  233.                             self.write_queue(STATUS_ACTIVE)
  234.                             log.debug('state = active')
  235.                             time.sleep(2)
  236.                             continue
  237.                             continue
  238.                         if copy_state == pml.SCAN_TO_PRINTER_ABORTED:
  239.                             log.debug('state = aborting')
  240.                             state = STATE_ABORTED
  241.                             break
  242.                             continue
  243.                         if copy_state == pml.SCAN_TO_PRINTER_IDLE:
  244.                             log.debug('state = idle')
  245.                             state = STATE_SUCCESS
  246.                             break
  247.                             continue
  248.                 
  249.             self.copy_type == COPY_TYPE_DEVICE
  250.             if state == STATE_RESET_TOKEN:
  251.                 log.debug('%s State: Release copy token' % '********************')
  252.                 
  253.                 try:
  254.                     self.dev.setPML(pml.OID_COPIER_TOKEN, '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
  255.                 except Error:
  256.                     log.error('Unable to release copier token.')
  257.  
  258.                 state = STATE_DONE
  259.                 continue
  260.  
  261.     
  262.     def check_for_cancel(self):
  263.         canceled = False
  264.         while self.event_queue.qsize():
  265.             
  266.             try:
  267.                 event = self.event_queue.get(0)
  268.                 if event == COPY_CANCELED:
  269.                     canceled = True
  270.                     log.debug('Cancel pressed!')
  271.             continue
  272.             except Queue.Empty:
  273.                 break
  274.                 continue
  275.             
  276.  
  277.             None<EXCEPTION MATCH>Queue.Empty
  278.         return canceled
  279.  
  280.     
  281.     def write_queue(self, message):
  282.         if self.update_queue is not None and message != self.prev_update:
  283.             self.update_queue.put(message)
  284.             time.sleep(0)
  285.             self.prev_update = message
  286.         
  287.  
  288.  
  289.