Changeset b77e57e for server


Ignore:
Timestamp:
Jan 21, 2012, 9:03:09 PM (12 years ago)
Author:
Daniel Cooper <danny@…>
Branches:
no-cups
Children:
3cb6c7b
Parents:
079ef11 (diff), 7e29e6a (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
git-author:
Daniel Cooper <danny@…> (01/21/12 21:03:09)
git-committer:
Daniel Cooper <danny@…> (01/21/12 21:03:09)
Message:

Merge branch 'no-cups' of github.com:jhamrick/gutenbach into no-cups

Conflicts:

server/lib/gutenbach/server/requests.py

Location:
server
Files:
5 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/job.py

    rbd5bffc r0494098  
    415415            raise errors.InvalidJobStateException(self.state)
    416416
    417         logger.debug("restarting job %d" % (self.id, self.document))
     417        logger.debug("restarting job %d", self.id)
    418418
    419419        self._why_done = None
    420         self.spool(self.document)
     420        fh = self.player.fh
     421
     422        if not fh or fh.closed:
     423            raise RuntimeError, "file handler is closed"
     424
     425        self.player = Player(fh)
  • server/lib/gutenbach/server/printer.py

    r57bc2dc r7e29e6a  
    8585
    8686        self.lock = threading.RLock()
    87         self.running = False
     87        self._running = False
    8888        self.paused = False
    8989
     
    9191        self._next_job_id = 1
    9292
     93    @sync
    9394    def __repr__(self):
    9495        return str(self)
    9596
     97    @sync
    9698    def __str__(self):
    97         return "<Printer '%s'>" % self.name 
     99        return "<Printer '%s'>" % self.name
    98100
    99101    def run(self):
    100         self.running = True
    101         while self.running:
    102             with self.lock:
    103                 try:
    104                     if not self.paused and self.current_job is None:
     102        self._running = True
     103        while self._running:
     104            try:
     105                with self.lock:
     106                    if self.current_job is None:
    105107                        self.start_job()
    106108                    elif self.current_job.is_done:
    107109                        self.complete_job()
    108                 except:
    109                     logger.fatal(traceback.format_exc())
    110                     sys.exit(1)
     110            except:
     111                logger.fatal(traceback.format_exc())
     112                sys.exit(1)
    111113            time.sleep(0.1)
    112114
     115    def stop(self):
     116        with self.lock:
     117            self._running = False
     118        if self.ident is not None and self.isAlive():
     119            self.join()
     120
    113121    ######################################################################
    114122    ###                          Properties                            ###
    115123    ######################################################################
     124
     125    @property
     126    def name(self):
     127        return self._name
     128    @name.setter
     129    def name(self, val):
     130        try:
     131            self._name = str(val)
     132        except:
     133            self._name = "gutenbach-printer"
     134
     135    @property
     136    def config(self):
     137        return self._config
     138    @config.setter
     139    def config(self, val):
     140        try:
     141            _config = dict(val).copy()
     142        except:
     143            raise ValueError, "not a dictionary"
     144        if 'ipp-versions' not in _config:
     145            raise ValueError, "missing ipp-versions"
     146        self._config = _config
    116147
    117148    @property
     
    128159    @sync
    129160    def state(self):
    130         if self.current_job is not None:
    131             return States.PROCESSING
    132         elif len(self.pending_jobs) == 0:
    133             return States.IDLE
     161        if self.is_running and self.current_job is not None:
     162            state = States.PROCESSING
     163        elif self.is_running and len(self.pending_jobs) == 0:
     164            state = States.IDLE
    134165        else:
    135             return States.STOPPED
     166            state = States.STOPPED
     167        return state
    136168
    137169    @property
     
    143175        return jobs
    144176
     177    @property
     178    def is_running(self):
     179        running = self.ident is not None and self.isAlive() and self._running
     180        return running
     181
    145182    ######################################################################
    146183    ###                            Methods                             ###
     
    148185
    149186    @sync
     187    def assert_running(self):
     188        if not self.is_running:
     189            raise RuntimeError, "%s not started" % str(self)
     190
     191    @sync
    150192    def start_job(self):
    151         if self.current_job is None:
     193        self.assert_running()
     194        if not self.paused and self.current_job is None:
    152195            try:
    153196                job_id = heapq.heappop(self.pending_jobs)
     
    162205    @sync
    163206    def complete_job(self):
    164         if self.current_job is None:
    165             return
    166 
    167         try:
    168             if not self.current_job.is_done:
    169                 self.current_job.stop()
    170         finally:
    171             self.finished_jobs.append(self.current_job.id)
    172             self.current_job = None
     207        self.assert_running()
     208        if not self.paused and self.current_job is not None:
     209            try:
     210                if not self.current_job.is_done:
     211                    self.current_job.stop()
     212            finally:
     213                self.finished_jobs.append(self.current_job.id)
     214                self.current_job = None
    173215
    174216    @sync
    175217    def get_job(self, job_id):
     218        self.assert_running()
    176219        if job_id not in self.jobs:
    177220            raise InvalidJobException(job_id)
     
    184227    @property
    185228    def printer_uri_supported(self):
     229        self.assert_running()
    186230        return ipp.PrinterUriSupported(self.uri)
    187231    @printer_uri_supported.setter
    188232    def printer_uri_supported(self, val):
     233        self.assert_running()
    189234        raise ipp.errors.AttributesNotSettable("printer-uri-supported")
    190235
    191236    @property
    192237    def uri_authentication_supported(self):
     238        self.assert_running()
    193239        return ipp.UriAuthenticationSupported("none")
    194240    @uri_authentication_supported.setter
    195241    def uri_authentication_supported(self, val):
     242        self.assert_running()
    196243        raise ipp.errors.AttributesNotSettable("uri-authentication-supported")
    197244
    198245    @property
    199246    def uri_security_supported(self):
     247        self.assert_running()
    200248        return ipp.UriSecuritySupported("none")
    201249    @uri_security_supported.setter
    202250    def uri_security_supported(self, val):
     251        self.assert_running()
    203252        raise ipp.errors.AttributesNotSettable("uri-security-supported")
    204253
    205254    @property
    206255    def printer_name(self):
     256        self.assert_running()
    207257        return ipp.PrinterName(self.name)
    208258    @printer_name.setter
    209259    def printer_name(self, val):
     260        self.assert_running()
    210261        raise ipp.errors.AttributesNotSettable("printer-name")
    211262
    212263    @property
    213264    def printer_state(self):
     265        self.assert_running()
    214266        return ipp.PrinterState(self.state)
    215267    @printer_state.setter
    216268    def printer_state(self, val):
     269        self.assert_running()
    217270        raise ipp.errors.AttributesNotSettable("printer-state")
    218271
    219272    @property
    220273    def printer_state_reasons(self):
     274        self.assert_running()
    221275        return ipp.PrinterStateReasons("none")
    222276    @printer_state_reasons.setter
    223277    def printer_state_reasons(self, val):
     278        self.assert_running()
    224279        raise ipp.errors.AttributesNotSettable("printer-state-reasons")
    225280
    226281    @property
    227282    def ipp_versions_supported(self):
     283        self.assert_running()
    228284        return ipp.IppVersionsSupported(*self.config['ipp-versions'])
    229285    @ipp_versions_supported.setter
    230286    def ipp_versions_supported(self, val):
     287        self.assert_running()
    231288        raise ipp.errors.AttributesNotSettable("ipp-versions-supported")
    232289
     
    234291    @property
    235292    def operations_supported(self):
     293        self.assert_running()
    236294        return ipp.OperationsSupported(ipp.OperationCodes.GET_JOBS)
    237295    @operations_supported.setter
    238296    def operations_supported(self, val):
     297        self.assert_running()
    239298        raise ipp.errors.AttributesNotSettable("operations-supported")
    240299
    241300    @property
    242301    def charset_configured(self):
     302        self.assert_running()
    243303        return ipp.CharsetConfigured("utf-8") # XXX
    244304    @charset_configured.setter
    245305    def charset_configured(self, val):
     306        self.assert_running()
    246307        raise ipp.errors.AttributesNotSettable("charset-configured")
    247308       
    248309    @property
    249310    def charset_supported(self):
     311        self.assert_running()
    250312        return ipp.CharsetSupported("utf-8") # XXX
    251313    @charset_supported.setter
    252314    def charset_supported(self, val):
     315        self.assert_running()
    253316        raise ipp.errors.AttributesNotSettable("charset-supported")
    254317
    255318    @property
    256319    def natural_language_configured(self):
     320        self.assert_running()
    257321        return ipp.NaturalLanguageConfigured("en-us")
    258322    @natural_language_configured.setter
    259323    def natural_language_configured(self, val):
     324        self.assert_running()
    260325        raise ipp.errors.AttributesNotSettable("natural-language-configured")
    261326
    262327    @property
    263328    def generated_natural_language_supported(self):
     329        self.assert_running()
    264330        return ipp.GeneratedNaturalLanguageSupported("en-us")
    265331    @generated_natural_language_supported.setter
    266332    def generated_natural_language_supported(self, val):
     333        self.assert_running()
    267334        raise ipp.errors.AttributesNotSettable("generated-natural-language-supported")
    268335
    269336    @property
    270337    def document_format_default(self):
     338        self.assert_running()
    271339        return ipp.DocumentFormatDefault("application/octet-stream")
    272340    @document_format_default.setter
    273341    def document_format_default(self, val):
     342        self.assert_running()
    274343        raise ipp.errors.AttributesNotSettable("document-format-default")
    275344
    276345    @property
    277346    def document_format_supported(self):
     347        self.assert_running()
    278348        return ipp.DocumentFormatSupported("application/octet-stream", "audio/mp3")
    279349    @document_format_supported.setter
    280350    def document_format_supported(self, val):
     351        self.assert_running()
    281352        raise ipp.errors.AttributesNotSettable("document-format-supported")
    282353
    283354    @property
    284355    def printer_is_accepting_jobs(self):
     356        self.assert_running()
    285357        return ipp.PrinterIsAcceptingJobs(True)
    286358    @printer_is_accepting_jobs.setter
    287359    def printer_is_accepting_jobs(self, val):
     360        self.assert_running()
    288361        raise ipp.errors.AttributesNotSettable("printer-is-accepting-jobs")
    289362
    290363    @property
    291364    def queued_job_count(self):
     365        self.assert_running()
    292366        return ipp.QueuedJobCount(len(self.active_jobs))
    293367    @queued_job_count.setter
    294368    def queued_job_count(self, val):
     369        self.assert_running()
    295370        raise ipp.errors.AttributesNotSettable("queued-job-count")
    296371
    297372    @property
    298373    def pdl_override_supported(self):
     374        self.assert_running()
    299375        return ipp.PdlOverrideSupported("not-attempted")
    300376    @pdl_override_supported.setter
    301377    def pdl_override_supported(self, val):
     378        self.assert_running()
    302379        raise ipp.errors.AttributesNotSettable("pdl-override-supported")
    303380
    304381    @property
    305382    def printer_up_time(self):
     383        self.assert_running()
    306384        return ipp.PrinterUpTime(int(time.time()) - self.time_created)
    307385    @printer_up_time.setter
    308386    def printer_up_time(self, val):
     387        self.assert_running()
    309388        raise ipp.errors.AttributesNotSettable("printer-up-time")
    310389
    311390    @property
    312391    def compression_supported(self):
     392        self.assert_running()
    313393        return ipp.CompressionSupported("none")
    314394    @compression_supported.setter
    315395    def compression_supported(self, val):
     396        self.assert_running()
    316397        raise ipp.errors.AttributesNotSettable("compression-supported")
    317398
    318399    @property
    319400    def multiple_operation_time_out(self):
     401        self.assert_running()
    320402        return ipp.MultipleOperationTimeOut(240)
    321403    @multiple_operation_time_out.setter
    322404    def multiple_operation_time_out(self, val):
     405        self.assert_running()
    323406        raise ipp.errors.AttributesNotSettable("multiple-operation-time-out")
    324407
    325408    @property
    326409    def multiple_document_jobs_supported(self):
     410        self.assert_running()
    327411        return ipp.MultipleDocumentJobsSupported(False)
    328412    @multiple_document_jobs_supported.setter
    329413    def multiple_document_jobs_supported(self, val):
     414        self.assert_running()
    330415        raise ipp.errors.AttributesNotSettable("multiple-document-jobs-supported")
    331416
     
    335420
    336421    def job_id(self, job_id):
     422        self.assert_running()
    337423        job = self.get_job(job_id)
    338424        return ipp.JobId(job.id)
    339425
    340426    def job_name(self, job_id):
     427        self.assert_running()
    341428        job = self.get_job(job_id)
    342429        return ipp.JobName(job.name)
    343430
    344431    def job_originating_user_name(self, job_id):
     432        self.assert_running()
    345433        job = self.get_job(job_id)
    346434        return ipp.JobOriginatingUserName(job.creator)
    347435
    348436    def job_k_octets(self, job_id):
     437        self.assert_running()
    349438        job = self.get_job(job_id)
    350439        return ipp.JobKOctets(job.size)
    351440
    352441    def job_state(self, job_id):
     442        self.assert_running()
    353443        job = self.get_job(job_id)
    354444        return ipp.JobState(job.state)
    355445
    356446    def job_printer_uri(self, job_id):
     447        self.assert_running()
    357448        job = self.get_job(job_id)
    358449        return ipp.JobPrinterUri(self.uri)
     
    362453    ######################################################################
    363454
     455    @sync
    364456    def print_job(self, document, document_name=None, document_format=None,
    365457                  document_natural_language=None, requesting_user_name=None,
    366458                  compression=None, job_name=None, job_k_octets=None):
     459
     460        self.assert_running()
    367461
    368462        # create the job
     
    385479        return job_id
    386480
    387     def verify_job(self, document_name=None, document_format=None,
    388                   document_natural_language=None, requesting_user_name=None,
    389                   compression=None, job_name=None, job_k_octets=None):
     481    @sync
     482    def validate_job(self, document_name=None, document_format=None,
     483                     document_natural_language=None, requesting_user_name=None,
     484                     compression=None, job_name=None, job_k_octets=None):
     485
     486        self.assert_running()
    390487
    391488        job_id = self._next_job_id
     
    398495        del job
    399496
     497    @sync
    400498    def get_jobs(self, requesting_user_name=None, which_jobs=None,
    401499                 requested_attributes=None):
    402500       
     501        self.assert_running()
     502
    403503        # Filter by the which-jobs attribute
    404504        if which_jobs is None:
     
    425525        return job_attrs
    426526
     527    @sync
    427528    def print_uri(self):
    428         pass
    429 
     529        self.assert_running()
     530
     531    @sync
    430532    def create_job(self, requesting_user_name=None, job_name=None,
    431533                   job_k_octets=None):
     534
     535        self.assert_running()
    432536
    433537        job_id = self._next_job_id
     
    450554        Does nothing if the printer is already paused.
    451555        """
    452         if self.paused:
    453             return
    454 
    455         if self.current_job is not None and self.current_job.is_playing:
    456             self.current_job.pause()
    457 
    458         self.paused = True
     556       
     557        self.assert_running()
     558        if not self.paused:
     559            if self.current_job is not None and self.current_job.is_playing:
     560                self.current_job.pause()
     561            self.paused = True
     562            logger.info("%s paused", str(self))
    459563
    460564    @sync
     
    464568        Does nothing if the printer is not paused.
    465569        """
    466         if not self.paused:
    467             return
    468 
    469         if self.current_job is not None:
    470             self.current_job.resume()
    471 
    472         self.paused = False
     570       
     571        self.assert_running()
     572        if self.paused:
     573            if self.current_job is not None:
     574                self.current_job.resume()
     575            self.paused = False
     576            logger.info("%s unpaused", str(self))
    473577
    474578    @sync
    475579    def get_printer_attributes(self, requested_attributes=None):
     580        self.assert_running()
    476581        if requested_attributes is None:
    477582            requested = self.printer_attributes
     
    485590
    486591    @sync
    487     def set_printer_attributes(self, job_id, attributes):
     592    def set_printer_attributes(self, attributes):
     593        self.assert_running()
    488594        for attr in attributes:
    489595            try:
     
    494600    @sync
    495601    def cancel_job(self, job_id, requesting_user_name=None):
     602        self.assert_running()
    496603        job = self.get_job(job_id)
    497604        try:
     
    507614                      last_document=None):
    508615
     616        self.assert_running()
    509617        job = self.get_job(job_id)
    510618        job.spool(document)
     
    515623                 requesting_user_name=None, compression=None,
    516624                 last_document=None):
     625
     626        self.assert_running()
    517627        job = self.get_job(job_id)
    518628        # XXX: need to validate URI
     
    522632    @sync
    523633    def get_job_attributes(self, job_id, requested_attributes=None):
     634
     635        self.assert_running()
    524636        if requested_attributes is None:
    525637            requested = self.job_attributes
     
    534646    @sync
    535647    def set_job_attributes(self, job_id, attributes):
     648
     649        self.assert_running()
    536650        job = self.get_job(job_id)
    537651        for attr in attributes:
     
    545659    @sync
    546660    def restart_job(self, job_id, requesting_user_name=None):
     661
     662        self.assert_running()
    547663        job = self.get_job(job_id)
    548664        try:
     
    561677        # completes, this one will go next
    562678       
     679        self.assert_running()
    563680        job = self.get_job(job_id)
    564681        job.priority = 1 # XXX we need to actually do something
  • server/lib/gutenbach/server/requests.py

    r079ef11 rb77e57e  
    169169
    170170        """
     171
    171172        operation = request.attribute_groups[0]
    172173        document = request.data       
     
    178179        compression = None
    179180        last_document = None
    180 
    181 
    182181        # requested printer uri
    183182        if 'printer-uri' not in operation:
  • server/test/server/job.py

    rbd5bffc r0494098  
    201201
    202202    def testRestart(self):
    203         # XXX: Todo
    204         pass
     203        self.assertRaises(errors.InvalidJobStateException, self.job.restart)
     204
     205        self.job.play()
     206        self.assertTrue(self.job.is_playing)
     207        self.assertFalse(self.job.is_done)
     208
     209        self.assertRaises(errors.InvalidJobStateException, self.job.restart)
     210
     211        self.job.cancel()
     212        self.assertFalse(self.job.is_playing)
     213        self.assertTrue(self.job.is_done)
     214        self.assertTrue(self.job.is_cancelled)
     215        self.assertFalse(self.job.is_aborted)
     216
     217        self.job.restart()
     218        self.assertTrue(self.job.is_ready)
    205219
    206220if __name__ == "__main__":
Note: See TracChangeset for help on using the changeset viewer.