Ignore:
Timestamp:
Jan 11, 2012, 12:51:51 AM (12 years ago)
Author:
Jessica B. Hamrick <jhamrick@…>
Branches:
no-cups
Children:
be6ff03
Parents:
ffbe41d
git-author:
Jessica B. Hamrick <jhamrick@…> (01/11/12 00:51:51)
git-committer:
Jessica B. Hamrick <jhamrick@…> (01/11/12 00:51:51)
Message:

Clean up printer, job, and requests code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • server/lib/gutenbach/server/job.py

    rce2abc5 rb01b6d1  
    11from . import InvalidJobException, InvalidPrinterStateException
     2from gutenbach.ipp import JobStates as States
    23import os
    34import gutenbach.ipp as ipp
     
    2122    ]
    2223
    23     def __init__(self, jid, printer, creator="", name="", size=0):
    24         """Initialize a Gutenbach job.
    25 
    26         This sets the status to 'initializing' and optionally sets the
    27         document to print to the value of document.
     24    def __init__(self, job_id=-1, printer=None, creator=None, name=None, size=None):
     25        """Create an empty Gutenbach job.
    2826
    2927        """
    3028
    31         self.jid      = jid
    32         self.printer  = printer
    33 
    34         self.creator  = creator
    35         self.name     = name
    36         self.size     = size
     29        self.printer = printer
     30        self.player = None
     31
     32        self.id = job_id
     33        self.creator = creator
     34        self.name = name
     35        self.size = size
     36        self.status = States.HELD
    3737
    3838        self.document = None
    3939        self.document_name = None
    40         self.player = None
    41 
    42         self.status   = ipp.JobStates.HELD
    43 
    44     def __getattr__(self, attr):
     40        self.document_format = None
     41        self.document_natural_language = None
     42        self.compression = None
     43   
     44    def __repr__(self):
     45        return str(self)
     46
     47    def __str__(self):
     48        return "<Job %d '%s'>" % (self.id, self.name)
     49
     50    ######################################################################
     51    ###                          Properties                            ###
     52    ######################################################################
     53
     54    @property
     55    def id(self):
     56        """Unique job identifier.  Should be a positive integer,
     57        except when unassigned, when it defaults to -1.
     58       
     59        """
     60        return self._id
     61    @id.setter
     62    def id(self, val):
    4563        try:
    46             return self.__getattribute__(attr)
    47         except AttributeError:
    48             pass
    49         return self.__getattribute__(attr.replace("-", "_"))
    50 
    51     def __hasattr__(self, attr):
     64            self._id = int(val)
     65        except TypeError:
     66            self._id = -1
     67
     68    @property
     69    def creator(self):
     70        """The user who created the job; analogous to the IPP
     71        requesting-user-name.
     72
     73        """
     74        return self._creator
     75    @creator.setter
     76    def creator(self, val):
     77        if val is None:
     78            self._creator = ""
     79        else:
     80            self._creator = str(val)
     81
     82    @property
     83    def name(self):
     84        """The job's name.
     85
     86        """
     87        return self._name
     88    @name.setter
     89    def name(self, val):
     90        if val is None:
     91            self._name = ""
     92        else:
     93            self._name = str(val)
     94
     95    @property
     96    def size(self):
     97        """The size of the job in bytes.
     98
     99        """
     100        if self.document:
     101            size = os.path.getsize(self.document.name)
     102        else:
     103            size = self._size
     104        return size
     105    @size.setter
     106    def size(self, val):
    52107        try:
    53             getattr(self, attr)
    54             return True
    55         except AttributeError:
    56             return False
    57 
    58     #### Job attributes
    59 
    60     @property
    61     def job_id(self):
    62         return ipp.JobId(self.jid)
    63 
    64     @property
    65     def job_name(self):
    66         return ipp.JobName(self.name)
    67 
    68     # XXX: we need to actually calculate this!
    69     @property
    70     def job_originating_user_name(self):
    71         return ipp.JobOriginatingUserName(self.creator)
    72 
    73     # XXX: we need to actually calculate this!
    74     @property
    75     def job_k_octets(self):
    76         return ipp.JobKOctets(self.size)
    77 
    78     @property
    79     def job_state(self):
    80         return ipp.JobState(self.status)
    81 
    82     @property
    83     def job_printer_uri(self):
    84         return ipp.JobPrinterUri(self.printer.uri)
    85 
    86     def get_job_attributes(self, request=None):
    87         if request and 'requested-attributes' in request:
    88             requested = []
    89             for value in request['requested-attributes'].values:
    90                 if value.value in self.attributes:
    91                     requested.append(value.value)
    92         else:
    93             requested = self.attributes
    94            
    95         attributes = [getattr(self, attr) for attr in requested]
    96         return attributes
    97    
    98     #######
     108            self._size = int(val)
     109        except TypeError:
     110            self._size = 0
     111
     112    ######################################################################
     113    ###                            Methods                             ###
     114    ######################################################################
    99115
    100116    def play(self):
    101117        logger.info("playing job %s" % str(self))
    102118        # TODO: add external call to music player
    103         self.status = ipp.JobStates.PROCESSING
     119        self.status = States.PROCESSING
    104120        self.player = subprocess.Popen(
    105121            "/usr/bin/mplayer -quiet %s" % self.document.name,
     
    114130        #logger.debug(self.player.stdout)
    115131        self.player = None
    116         self.printer.complete_job(self.jid)
     132        self.printer.complete_job(self.id)
    117133
    118134    def finish(self):
    119135        logger.info("finished job %s" % str(self))
    120         self.status = ipp.JobStates.COMPLETE
    121 
    122     def __repr__(self):
    123         return str(self)
    124 
    125     def __str__(self):
    126         return "<Job %d '%s'>" % (self.jid if self.jid is not None else -1, self.name)
     136        self.status = States.COMPLETE
     137
     138    ######################################################################
     139    ###                        IPP Attributes                          ###
     140    ######################################################################
     141
     142    @property
     143    def job_id(self):
     144        return ipp.JobId(self.id)
     145
     146    @property
     147    def job_name(self):
     148        return ipp.JobName(self.name)
     149
     150    # XXX: we need to actually calculate this!
     151    @property
     152    def job_originating_user_name(self):
     153        return ipp.JobOriginatingUserName(self.creator)
     154
     155    # XXX: we need to actually calculate this!
     156    @property
     157    def job_k_octets(self):
     158        return ipp.JobKOctets(self.size)
     159
     160    @property
     161    def job_state(self):
     162        return ipp.JobState(self.status)
     163
     164    @property
     165    def job_printer_uri(self):
     166        return ipp.JobPrinterUri(self.printer.uri)
     167
     168
     169    ######################################################################
     170    ###                        IPP Operations                          ###
     171    ######################################################################
     172
     173    def cancel_job(self):
     174        pass
     175
     176    def send_document(self,
     177                      document,
     178                      document_name=None,
     179                      document_format=None,
     180                      document_natural_language=None,
     181                      requesting_user_name=None,
     182                      compression=None,
     183                      last_document=None):
     184
     185        if self.status != States.HELD:
     186            raise InvalidJobStateException(self.status)
     187       
     188        self.document = document
     189        self.document_name = str(document_name)
     190        self.document_format = str(document_format)
     191        self.document_natural_language = str(document_natural_language)
     192        self.creator = str(requesting_user_name)
     193        self.compression = str(compression)
     194        self.status = States.PENDING
     195
     196        logger.debug("document for job %d is '%s'" % (self.id, self.document_name))
     197
     198    def send_uri(self):
     199        pass
     200
     201    def get_job_attributes(self, requested_attributes=None):
     202        if requested_attributes is None:
     203            requested = self.attributes
     204        else:
     205            requested = [a for a in self.attributes if a in requested_attributes]
     206
     207        _attributes = [attr.replace("-", "_") for attr in requested]
     208        attributes = [getattr(self, attr) for attr in _attributes]
     209        return attributes
     210
     211    def set_job_attributes(self):
     212        pass
     213
     214    def restart_job(self):
     215        pass
     216
     217    def promote_job(self):
     218        pass
Note: See TracChangeset for help on using the changeset viewer.