Changeset 5e70cc2


Ignore:
Timestamp:
Jan 21, 2012, 7:57:40 PM (12 years ago)
Author:
Jessica B. Hamrick <jhamrick@…>
Branches:
no-cups
Children:
7e29e6a
Parents:
33528b4
git-author:
Jessica B. Hamrick <jhamrick@…> (01/21/12 19:57:40)
git-committer:
Jessica B. Hamrick <jhamrick@…> (01/21/12 19:57:40)
Message:

Make threading in printer.py more robust

Location:
server/lib/gutenbach/server
Files:
2 edited

Legend:

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

    r951ab1b r5e70cc2  
    1313
    1414class InvalidPrinterStateException(Exception):
     15    errstr = {
     16        3: "idle",
     17        4: "processing",
     18        5: "stopped"
     19        }
     20   
    1521    def __init__(self, state):
    16         self.state = hex(state)
     22        self.state = int(state)
    1723    def __str__(self):
    18         return "Invalid printer state: %s" % self.state
     24        return "Invalid printer state: %s (%s)" % \
     25               (self.errstr[self.state], hex(self.state))
    1926
    2027class InvalidJobStateException(Exception):
  • server/lib/gutenbach/server/printer.py

    r33528b4 r5e70cc2  
    136136    @sync
    137137    def state(self):
    138         if self.current_job is not None:
    139             return States.PROCESSING
    140         elif len(self.pending_jobs) == 0:
    141             return States.IDLE
     138        if self.is_running and self.current_job is not None:
     139            state = States.PROCESSING
     140        elif self.is_running and len(self.pending_jobs) == 0:
     141            state = States.IDLE
    142142        else:
    143             return States.STOPPED
     143            state = States.STOPPED
     144        return state
    144145
    145146    @property
     
    161162
    162163    @sync
     164    def assert_running(self):
     165        if not self.is_running:
     166            raise RuntimeError, "%s not started" % str(self)
     167
     168    @sync
    163169    def start_job(self):
     170        self.assert_running()
    164171        if not self.paused and self.current_job is None:
    165172            try:
     
    175182    @sync
    176183    def complete_job(self):
     184        self.assert_running()
    177185        if not self.paused and self.current_job is not None:
    178186            try:
     
    185193    @sync
    186194    def get_job(self, job_id):
     195        self.assert_running()
    187196        if job_id not in self.jobs:
    188197            raise InvalidJobException(job_id)
     
    195204    @property
    196205    def printer_uri_supported(self):
     206        self.assert_running()
    197207        return ipp.PrinterUriSupported(self.uri)
    198208    @printer_uri_supported.setter
    199209    def printer_uri_supported(self, val):
     210        self.assert_running()
    200211        raise ipp.errors.AttributesNotSettable("printer-uri-supported")
    201212
    202213    @property
    203214    def uri_authentication_supported(self):
     215        self.assert_running()
    204216        return ipp.UriAuthenticationSupported("none")
    205217    @uri_authentication_supported.setter
    206218    def uri_authentication_supported(self, val):
     219        self.assert_running()
    207220        raise ipp.errors.AttributesNotSettable("uri-authentication-supported")
    208221
    209222    @property
    210223    def uri_security_supported(self):
     224        self.assert_running()
    211225        return ipp.UriSecuritySupported("none")
    212226    @uri_security_supported.setter
    213227    def uri_security_supported(self, val):
     228        self.assert_running()
    214229        raise ipp.errors.AttributesNotSettable("uri-security-supported")
    215230
    216231    @property
    217232    def printer_name(self):
     233        self.assert_running()
    218234        return ipp.PrinterName(self.name)
    219235    @printer_name.setter
    220236    def printer_name(self, val):
     237        self.assert_running()
    221238        raise ipp.errors.AttributesNotSettable("printer-name")
    222239
    223240    @property
    224241    def printer_state(self):
     242        self.assert_running()
    225243        return ipp.PrinterState(self.state)
    226244    @printer_state.setter
    227245    def printer_state(self, val):
     246        self.assert_running()
    228247        raise ipp.errors.AttributesNotSettable("printer-state")
    229248
    230249    @property
    231250    def printer_state_reasons(self):
     251        self.assert_running()
    232252        return ipp.PrinterStateReasons("none")
    233253    @printer_state_reasons.setter
    234254    def printer_state_reasons(self, val):
     255        self.assert_running()
    235256        raise ipp.errors.AttributesNotSettable("printer-state-reasons")
    236257
    237258    @property
    238259    def ipp_versions_supported(self):
     260        self.assert_running()
    239261        return ipp.IppVersionsSupported(*self.config['ipp-versions'])
    240262    @ipp_versions_supported.setter
    241263    def ipp_versions_supported(self, val):
     264        self.assert_running()
    242265        raise ipp.errors.AttributesNotSettable("ipp-versions-supported")
    243266
     
    245268    @property
    246269    def operations_supported(self):
     270        self.assert_running()
    247271        return ipp.OperationsSupported(ipp.OperationCodes.GET_JOBS)
    248272    @operations_supported.setter
    249273    def operations_supported(self, val):
     274        self.assert_running()
    250275        raise ipp.errors.AttributesNotSettable("operations-supported")
    251276
    252277    @property
    253278    def charset_configured(self):
     279        self.assert_running()
    254280        return ipp.CharsetConfigured("utf-8") # XXX
    255281    @charset_configured.setter
    256282    def charset_configured(self, val):
     283        self.assert_running()
    257284        raise ipp.errors.AttributesNotSettable("charset-configured")
    258285       
    259286    @property
    260287    def charset_supported(self):
     288        self.assert_running()
    261289        return ipp.CharsetSupported("utf-8") # XXX
    262290    @charset_supported.setter
    263291    def charset_supported(self, val):
     292        self.assert_running()
    264293        raise ipp.errors.AttributesNotSettable("charset-supported")
    265294
    266295    @property
    267296    def natural_language_configured(self):
     297        self.assert_running()
    268298        return ipp.NaturalLanguageConfigured("en-us")
    269299    @natural_language_configured.setter
    270300    def natural_language_configured(self, val):
     301        self.assert_running()
    271302        raise ipp.errors.AttributesNotSettable("natural-language-configured")
    272303
    273304    @property
    274305    def generated_natural_language_supported(self):
     306        self.assert_running()
    275307        return ipp.GeneratedNaturalLanguageSupported("en-us")
    276308    @generated_natural_language_supported.setter
    277309    def generated_natural_language_supported(self, val):
     310        self.assert_running()
    278311        raise ipp.errors.AttributesNotSettable("generated-natural-language-supported")
    279312
    280313    @property
    281314    def document_format_default(self):
     315        self.assert_running()
    282316        return ipp.DocumentFormatDefault("application/octet-stream")
    283317    @document_format_default.setter
    284318    def document_format_default(self, val):
     319        self.assert_running()
    285320        raise ipp.errors.AttributesNotSettable("document-format-default")
    286321
    287322    @property
    288323    def document_format_supported(self):
     324        self.assert_running()
    289325        return ipp.DocumentFormatSupported("application/octet-stream", "audio/mp3")
    290326    @document_format_supported.setter
    291327    def document_format_supported(self, val):
     328        self.assert_running()
    292329        raise ipp.errors.AttributesNotSettable("document-format-supported")
    293330
    294331    @property
    295332    def printer_is_accepting_jobs(self):
     333        self.assert_running()
    296334        return ipp.PrinterIsAcceptingJobs(True)
    297335    @printer_is_accepting_jobs.setter
    298336    def printer_is_accepting_jobs(self, val):
     337        self.assert_running()
    299338        raise ipp.errors.AttributesNotSettable("printer-is-accepting-jobs")
    300339
    301340    @property
    302341    def queued_job_count(self):
     342        self.assert_running()
    303343        return ipp.QueuedJobCount(len(self.active_jobs))
    304344    @queued_job_count.setter
    305345    def queued_job_count(self, val):
     346        self.assert_running()
    306347        raise ipp.errors.AttributesNotSettable("queued-job-count")
    307348
    308349    @property
    309350    def pdl_override_supported(self):
     351        self.assert_running()
    310352        return ipp.PdlOverrideSupported("not-attempted")
    311353    @pdl_override_supported.setter
    312354    def pdl_override_supported(self, val):
     355        self.assert_running()
    313356        raise ipp.errors.AttributesNotSettable("pdl-override-supported")
    314357
    315358    @property
    316359    def printer_up_time(self):
     360        self.assert_running()
    317361        return ipp.PrinterUpTime(int(time.time()) - self.time_created)
    318362    @printer_up_time.setter
    319363    def printer_up_time(self, val):
     364        self.assert_running()
    320365        raise ipp.errors.AttributesNotSettable("printer-up-time")
    321366
    322367    @property
    323368    def compression_supported(self):
     369        self.assert_running()
    324370        return ipp.CompressionSupported("none")
    325371    @compression_supported.setter
    326372    def compression_supported(self, val):
     373        self.assert_running()
    327374        raise ipp.errors.AttributesNotSettable("compression-supported")
    328375
    329376    @property
    330377    def multiple_operation_time_out(self):
     378        self.assert_running()
    331379        return ipp.MultipleOperationTimeOut(240)
    332380    @multiple_operation_time_out.setter
    333381    def multiple_operation_time_out(self, val):
     382        self.assert_running()
    334383        raise ipp.errors.AttributesNotSettable("multiple-operation-time-out")
    335384
    336385    @property
    337386    def multiple_document_jobs_supported(self):
     387        self.assert_running()
    338388        return ipp.MultipleDocumentJobsSupported(False)
    339389    @multiple_document_jobs_supported.setter
    340390    def multiple_document_jobs_supported(self, val):
     391        self.assert_running()
    341392        raise ipp.errors.AttributesNotSettable("multiple-document-jobs-supported")
    342393
     
    346397
    347398    def job_id(self, job_id):
     399        self.assert_running()
    348400        job = self.get_job(job_id)
    349401        return ipp.JobId(job.id)
    350402
    351403    def job_name(self, job_id):
     404        self.assert_running()
    352405        job = self.get_job(job_id)
    353406        return ipp.JobName(job.name)
    354407
    355408    def job_originating_user_name(self, job_id):
     409        self.assert_running()
    356410        job = self.get_job(job_id)
    357411        return ipp.JobOriginatingUserName(job.creator)
    358412
    359413    def job_k_octets(self, job_id):
     414        self.assert_running()
    360415        job = self.get_job(job_id)
    361416        return ipp.JobKOctets(job.size)
    362417
    363418    def job_state(self, job_id):
     419        self.assert_running()
    364420        job = self.get_job(job_id)
    365421        return ipp.JobState(job.state)
    366422
    367423    def job_printer_uri(self, job_id):
     424        self.assert_running()
    368425        job = self.get_job(job_id)
    369426        return ipp.JobPrinterUri(self.uri)
     
    373430    ######################################################################
    374431
     432    @sync
    375433    def print_job(self, document, document_name=None, document_format=None,
    376434                  document_natural_language=None, requesting_user_name=None,
    377435                  compression=None, job_name=None, job_k_octets=None):
     436
     437        self.assert_running()
    378438
    379439        # create the job
     
    396456        return job_id
    397457
     458    @sync
    398459    def verify_job(self, document_name=None, document_format=None,
    399460                  document_natural_language=None, requesting_user_name=None,
    400461                  compression=None, job_name=None, job_k_octets=None):
     462
     463        self.assert_running()
    401464
    402465        job_id = self._next_job_id
     
    409472        del job
    410473
     474    @sync
    411475    def get_jobs(self, requesting_user_name=None, which_jobs=None,
    412476                 requested_attributes=None):
    413477       
     478        self.assert_running()
     479
    414480        # Filter by the which-jobs attribute
    415481        if which_jobs is None:
     
    436502        return job_attrs
    437503
     504    @sync
    438505    def print_uri(self):
    439         pass
    440 
     506        self.assert_running()
     507
     508    @sync
    441509    def create_job(self, requesting_user_name=None, job_name=None,
    442510                   job_k_octets=None):
     511
     512        self.assert_running()
    443513
    444514        job_id = self._next_job_id
     
    462532        """
    463533       
     534        self.assert_running()
    464535        if not self.paused:
    465536            if self.current_job is not None and self.current_job.is_playing:
     
    475546        """
    476547       
     548        self.assert_running()
    477549        if self.paused:
    478550            if self.current_job is not None:
     
    483555    @sync
    484556    def get_printer_attributes(self, requested_attributes=None):
     557        self.assert_running()
    485558        if requested_attributes is None:
    486559            requested = self.printer_attributes
     
    495568    @sync
    496569    def set_printer_attributes(self, job_id, attributes):
     570        self.assert_running()
    497571        for attr in attributes:
    498572            try:
     
    503577    @sync
    504578    def cancel_job(self, job_id, requesting_user_name=None):
     579        self.assert_running()
    505580        job = self.get_job(job_id)
    506581        try:
     
    516591                      last_document=None):
    517592
     593        self.assert_running()
    518594        job = self.get_job(job_id)
    519595        job.spool(document)
     
    524600                 requesting_user_name=None, compression=None,
    525601                 last_document=None):
     602
     603        self.assert_running()
    526604        job = self.get_job(job_id)
    527605        # XXX: need to validate URI
     
    531609    @sync
    532610    def get_job_attributes(self, job_id, requested_attributes=None):
     611
     612        self.assert_running()
    533613        if requested_attributes is None:
    534614            requested = self.job_attributes
     
    543623    @sync
    544624    def set_job_attributes(self, job_id, attributes):
     625
     626        self.assert_running()
    545627        job = self.get_job(job_id)
    546628        for attr in attributes:
     
    554636    @sync
    555637    def restart_job(self, job_id, requesting_user_name=None):
     638
     639        self.assert_running()
    556640        job = self.get_job(job_id)
    557641        try:
     
    570654        # completes, this one will go next
    571655       
     656        self.assert_running()
    572657        job = self.get_job(job_id)
    573658        job.priority = 1 # XXX we need to actually do something
Note: See TracChangeset for help on using the changeset viewer.