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/printer.py

    rffbe41d rb01b6d1  
    11from . import InvalidJobException, InvalidPrinterStateException
    22from . import Job
     3from gutenbach.ipp import PrinterStates as States
    34import gutenbach.ipp as ipp
    45import logging
     
    4344    ]
    4445       
    45 
    4646    def __init__(self, name):
    4747
    4848        self.name = name
    49         self.uri = "ipp://localhost:8000/printers/" + self.name
    5049        self.time_created = int(time.time())
    51         self.state = "idle"
     50        self.state = States.IDLE
    5251
    5352        self.finished_jobs = []
     
    5655
    5756        # cups ignores jobs with id 0, so we have to start at 1
    58         self._next_jobid = 1
    59 
    60     def __getattr__(self, attr):
    61         try:
    62             return self.__getattribute__(attr)
    63         except AttributeError:
    64             pass
    65         return self.__getattribute__(attr.replace("-", "_"))
    66 
    67     def __hasattr__(self, attr):
    68         try:
    69             getattr(self, attr)
    70             return True
    71         except AttributeError:
    72             return False
    73 
    74     ## Printer attributes
    75 
    76     @property
    77     def printer_uri_supported(self):
    78         return ipp.PrinterUriSupported(self.uri)
    79 
    80     @property
    81     def uri_authentication_supported(self):
    82         return ipp.UriAuthenticationSupported("none")
    83 
    84     @property
    85     def uri_security_supported(self):
    86         return ipp.UriSecuritySupported("none")
    87 
    88     @property
    89     def printer_name(self):
    90         return ipp.PrinterName(self.name)
    91 
    92     @property
    93     def printer_state(self):
    94         return ipp.PrinterState(ipp.constants.PrinterStates.IDLE)
    95 
    96     @property
    97     def printer_state_reasons(self):
    98         return ipp.PrinterStateReasons("none")
    99 
    100     @property
    101     def ipp_versions_supported(self):
    102         return ipp.IppVersionsSupported("1.0", "1.1")
    103 
    104     # XXX: We should query ourself for the supported operations
    105     @property
    106     def operations_supported(self):
    107         return ipp.OperationsSupported(ipp.OperationCodes.GET_JOBS)
    108 
    109     @property
    110     def charset_configured(self):
    111         return ipp.CharsetConfigured("utf-8")
    112 
    113     @property
    114     def charset_supported(self):
    115         return ipp.CharsetSupported("utf-8")
    116 
    117     @property
    118     def natural_language_configured(self):
    119         return ipp.NaturalLanguageConfigured("en-us")
    120 
    121     @property
    122     def generated_natural_language_supported(self):
    123         return ipp.GeneratedNaturalLanguageSupported("en-us")
    124 
    125     @property
    126     def document_format_default(self):
    127         return ipp.DocumentFormatDefault("application/octet-stream")
    128 
    129     @property
    130     def document_format_supported(self):
    131         return ipp.DocumentFormatSupported("application/octet-stream", "audio/mp3")
    132 
    133     @property
    134     def printer_is_accepting_jobs(self):
    135         return ipp.PrinterIsAcceptingJobs(True)
    136 
    137     @property
    138     def queued_job_count(self):
    139         return ipp.QueuedJobCount(len(self.active_jobs))
    140 
    141     @property
    142     def pdl_override_supported(self):
    143         return ipp.PdlOverrideSupported("not-attempted")
    144 
    145     @property
    146     def printer_up_time(self):
    147         return ipp.PrinterUpTime(int(time.time()) - self.time_created)
    148 
    149     @property
    150     def compression_supported(self):
    151         return ipp.CompressionSupported("none")
    152 
    153     @property
    154     def multiple_operation_time_out(self):
    155         return ipp.MultipleOperationTimeOut(240)
    156 
    157     @property
    158     def multiple_document_jobs_supported(self):
    159         return ipp.MultipleDocumentJobsSupported(False)
    160 
    161     ## Printer operations
    162 
    163     def get_printer_attributes(self, request=None):
    164         if request and 'requested-attributes' in request:
    165             requested = []
    166             for value in request['requested-attributes'].values:
    167                 if value.value in self.attributes:
    168                     requested.append(value.value)
    169         else:
    170             requested = self.attributes
    171            
    172         attributes = [getattr(self, attr) for attr in requested]
    173         return attributes
    174 
    175     def create_job(self, request):
    176         operation = request.attribute_groups[0]
    177         kwargs = {}
    178        
    179         # requesting username
    180         if 'requesting-user-name' in operation:
    181             username_attr = operation['requesting-user-name']
    182             username = username_attr.values[0].value
    183             if username_attr != ipp.RequestingUserName(username):
    184                 raise ipp.errors.ClientErrorBadRequest(str(username_attr))
    185             kwargs['creator'] = username
    186 
    187         # job name
    188         if 'job-name' in operation:
    189             job_name_attr = operation['job-name']
    190             job_name = job_name_attr.values[0].value
    191             if job_name_attr != ipp.JobName(job_name):
    192                 raise ipp.errors.ClientErrorBadRequest(str(job_name_attr))
    193             kwargs['name'] = job_name
    194 
    195         # job size
    196         if 'job-k-octets' in operation:
    197             job_k_octets_attr = operation['job-k-octets']
    198             job_k_octets = job_k_octets_attr.values[0].value
    199             if job_k_octets_attr != ipp.JobKOctets(job_k_octets):
    200                 raise ipp.errors.ClientErrorBadRequest(str(job_k_octets_attr))
    201             kwargs['size'] = job_k_octets
    202 
    203         job_id = self._next_jobid
    204         self._next_jobid += 1
    205        
    206         job = Job(job_id, self, **kwargs)
    207         self.jobs[job_id] = job
    208         self.active_jobs.append(job_id)
     57        self._next_jobid = 1
     58
     59    def __repr__(self):
     60        return str(self)
     61
     62    def __str__(self):
     63        return "<Printer '%s'>" % self.name
     64
     65    ######################################################################
     66    ###                          Properties                            ###
     67    ######################################################################
     68
     69    @property
     70    def uris(self):
     71        uris = ["ipp://localhost:8000/printers/" + self.name,
     72                "ipp://localhost/printers/" + self.name]
     73        return uris
     74   
     75    @property
     76    def uri(self):
     77        return self.uris[0]
     78
     79    @property
     80    def next_job(self):
     81        if len(self.active_jobs) == 0:
     82            job = None
     83        else:
     84            job = self.active_jobs[0]
    20985        return job
    21086
    211     def send_document(self, jobid, document):
    212         job = self.jobs[jobid]
    213         if job.status != ipp.JobStates.HELD:
    214             raise InvalidPrinterStateException(
    215                 "Invalid job state: %d" % job.status)
    216         job.document = document
    217         logger.debug("document for job %d is '%s'" % (jobid, job.document.name))
    218         job.status = ipp.JobStates.PENDING
    219 
    220     def print_job(self, job):
    221         pass
     87    ######################################################################
     88    ###                            Methods                             ###
     89    ######################################################################
    22290
    22391    def complete_job(self, jobid):
     
    22593        self.finished_jobs.append(job)
    22694        job.finish()
    227         return job.jid
     95        return job.id
    22896
    22997    def start_job(self, jobid):
    23098        job = self.jobs[self.active_jobs[0]]
    23199        if job.status != ipp.JobStates.PENDING:
    232             raise InvalidPrinterStateException(
    233                 "Invalid job state: %s" % job.status)
     100            raise InvalidPrinterStateException(job.status)
    234101        job.play()
    235 
    236     @property
    237     def next_job(self):
    238         if len(self.active_jobs) == 0:
    239             job = None
    240         else:
    241             job = self.active_jobs[0]
    242         return job
    243102
    244103    def stop(self):
     
    255114        return self.jobs[jobid]
    256115
    257     def get_jobs(self):
    258         print self.active_jobs
    259         jobs = [self.jobs[job_id] for job_id in self.active_jobs]
    260         return jobs
    261 
    262     # def __repr__(self):
    263     #     return str(self)
    264 
    265     # def __str__(self):
    266     #     return "<Printer '%s'>" % self.name
     116    ######################################################################
     117    ###                        IPP Attributes                          ###
     118    ######################################################################
     119
     120    @property
     121    def printer_uri_supported(self):
     122        return ipp.PrinterUriSupported(self.uri)
     123
     124    @property
     125    def uri_authentication_supported(self):
     126        return ipp.UriAuthenticationSupported("none")
     127
     128    @property
     129    def uri_security_supported(self):
     130        return ipp.UriSecuritySupported("none")
     131
     132    @property
     133    def printer_name(self):
     134        return ipp.PrinterName(self.name)
     135
     136    @property
     137    def printer_state(self):
     138        return ipp.PrinterState(self.state)
     139
     140    @property
     141    def printer_state_reasons(self):
     142        return ipp.PrinterStateReasons("none")
     143
     144    @property
     145    def ipp_versions_supported(self):
     146        return ipp.IppVersionsSupported("1.0", "1.1")
     147
     148    # XXX: We should query ourself for the supported operations
     149    @property
     150    def operations_supported(self):
     151        return ipp.OperationsSupported(ipp.OperationCodes.GET_JOBS)
     152
     153    @property
     154    def charset_configured(self):
     155        return ipp.CharsetConfigured("utf-8")
     156
     157    @property
     158    def charset_supported(self):
     159        return ipp.CharsetSupported("utf-8")
     160
     161    @property
     162    def natural_language_configured(self):
     163        return ipp.NaturalLanguageConfigured("en-us")
     164
     165    @property
     166    def generated_natural_language_supported(self):
     167        return ipp.GeneratedNaturalLanguageSupported("en-us")
     168
     169    @property
     170    def document_format_default(self):
     171        return ipp.DocumentFormatDefault("application/octet-stream")
     172
     173    @property
     174    def document_format_supported(self):
     175        return ipp.DocumentFormatSupported("application/octet-stream", "audio/mp3")
     176
     177    @property
     178    def printer_is_accepting_jobs(self):
     179        return ipp.PrinterIsAcceptingJobs(True)
     180
     181    @property
     182    def queued_job_count(self):
     183        return ipp.QueuedJobCount(len(self.active_jobs))
     184
     185    @property
     186    def pdl_override_supported(self):
     187        return ipp.PdlOverrideSupported("not-attempted")
     188
     189    @property
     190    def printer_up_time(self):
     191        return ipp.PrinterUpTime(int(time.time()) - self.time_created)
     192
     193    @property
     194    def compression_supported(self):
     195        return ipp.CompressionSupported("none")
     196
     197    @property
     198    def multiple_operation_time_out(self):
     199        return ipp.MultipleOperationTimeOut(240)
     200
     201    @property
     202    def multiple_document_jobs_supported(self):
     203        return ipp.MultipleDocumentJobsSupported(False)
     204
     205
     206    ######################################################################
     207    ###                        IPP Operations                          ###
     208    ######################################################################
     209
     210    def print_job(self):
     211        pass
     212
     213    def validate_job(self):
     214        pass
     215
     216    def get_jobs(self, requesting_user_name="", which_jobs=None):
     217        # Filter by the which-jobs attribute
     218        if which_jobs is None:
     219            jobs = self.jobs.values()
     220        elif which_jobs == "completed":
     221            jobs = [self.jobs[job_id] for job_id in self.finished_jobs]
     222        elif which_jobs == "not-completed":
     223            jobs = [self.jobs[job_id] for job_id in self.active_jobs]
     224        else:
     225            raise ipp.errors.ClientErrorAttributes(
     226                which_jobs, ipp.WhichJobs(which_jobs))
     227
     228        # Filter by username
     229        if requesting_user_name is None:
     230            user_jobs = jobs
     231        else:
     232            user_jobs = [job for job in jobs if job.creator == requesting_user_name]
     233       
     234        return user_jobs
     235
     236    def print_uri(self):
     237        pass
     238
     239    def create_job(self, requesting_user_name="", job_name="", job_k_octets=0):
     240        job_id = self._next_jobid
     241        self._next_jobid += 1
     242       
     243        job = Job(job_id,
     244                  self,
     245                  creator=requesting_user_name,
     246                  name=job_name,
     247                  size=job_k_octets)
     248       
     249        self.jobs[job_id] = job
     250        self.active_jobs.append(job_id)
     251        self.state = States.PROCESSING
     252       
     253        return job
     254
     255    def pause_printer(self):
     256        pass
     257
     258    def resume_printer(self):
     259        pass
     260
     261    def get_printer_attributes(self, requested_attributes=None):
     262        if requested_attributes is None:
     263            requested = self.attributes
     264        else:
     265            requested = [a for a in self.attributes if a in requested_attributes]
     266
     267        _attributes = [attr.replace("-", "_") for attr in requested]
     268        attributes = [getattr(self, attr) for attr in _attributes]
     269        return attributes
     270
     271    def set_printer_attributes(self):
     272        pass
Note: See TracChangeset for help on using the changeset viewer.