Changeset 33ea505 for server


Ignore:
Timestamp:
Jan 11, 2012, 6:58:43 PM (12 years ago)
Author:
Jessica B. Hamrick <jhamrick@…>
Branches:
no-cups
Children:
09790fe
Parents:
4126d3d
git-author:
Jessica B. Hamrick <jhamrick@…> (01/11/12 18:58:43)
git-committer:
Jessica B. Hamrick <jhamrick@…> (01/11/12 18:58:43)
Message:

Keep IPP code in GutenbachPrinter?, not in GutenbachJob?

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

Legend:

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

    rd21198f r33ea505  
    44__all__.extend(errors.__all__)
    55
    6 from job import Job
    7 __all__.append('Job')
     6from job import GutenbachJob
     7__all__.append('GutenbachJob')
    88
    99from printer import GutenbachPrinter
  • server/lib/gutenbach/server/job.py

    r34a4e5d r33ea505  
    1 from . import InvalidJobStateException, MissingDataException
     1from .errors import InvalidJobStateException, MissingDataException
    22from .player import Player
    33from gutenbach.ipp import JobStates as States
     4import logging
    45import os
    5 import gutenbach.ipp as ipp
    6 import logging
    76
    87# initialize logger
    98logger = logging.getLogger(__name__)
    109
    11 class Job(object):
     10class GutenbachJob(object):
    1211
    13     # for IPP
    14     attributes = [
    15         "job-id",
    16         "job-name",
    17         "job-originating-user-name",
    18         "job-k-octets",
    19         "job-state",
    20         "job-printer-uri"
    21     ]
    22 
    23     def __init__(self, job_id=-1, printer=None, creator=None, name=None, size=None):
     12    def __init__(self, job_id=None, creator=None, name=None):
    2413        """Create an empty Gutenbach job.
    2514
    2615        """
    2716
    28         self.printer = printer
    2917        self.player = None
    3018
     
    3220        self.creator = creator
    3321        self.name = name
    34         self.size = size
    3522        self.state = States.HELD
    3623        self.priority = 1
     24        self.document = None
    3725
    38         self.document_name = None
    39         self.document_format = None
    40         self.document_natural_language = None
    41         self.compression = None
    42    
    4326    def __repr__(self):
    4427        return str(self)
     
    6750        except TypeError:
    6851            self._id = -1
    69 
    70     @property
    71     def uri(self):
    72         return self.uris[0]
    73 
    74     @property
    75     def uris(self):
    76         return ["ipp://localhost/jobs/%d" % self.id,
    77                 "ipp://localhost:8000/jobs/%d" % self.id]
    7852
    7953    @property
     
    10983
    11084        """
    111         return self._size
    112     @size.setter
    113     def size(self, val):
    11485        try:
    115             self._size = int(val)
    116         except TypeError:
    117             self._size = 0
     86            size = os.path.getsize(self.document)
     87        except:
     88            size = 0
     89        return size
    11890
    11991    @property
     
    135107    ######################################################################
    136108
     109    def spool(self, document, username=None):
     110        if self.state != States.HELD:
     111            raise InvalidJobStateException(self.state)
     112
     113        self.document = document.name
     114        self.player = Player(document)
     115        self.creator = username
     116        self.state = States.PENDING
     117
     118        logger.debug("document for job %d is '%s'" % (self.id, self.document))
     119
     120
    137121    def play(self):
    138         """Non-blocking play function.
     122        """Non-blocking play function.  Sets the job state to
     123        PROCESSING.
     124
     125        Raises
     126        ------
     127        InvalidJobStateException
     128            If the job is not ready to be played.
    139129
    140130        """
     
    142132        # make sure the job is waiting to be played and that it's
    143133        # valid
    144         if self.state != States.PENDING:
     134        if not self.is_ready:
    145135            raise InvalidJobStateException(self.state)
    146136       
    147137        # and set the state to processing if we're good to go
    148138        logger.info("playing job %s" % str(self))
     139
     140        def _completed():
     141            logger.info("completed job %s" % str(self))
     142            self.state = States.COMPLETE
     143            self.player = None
     144
    149145        self.state = States.PROCESSING
    150         self.player.callback = self._completed
     146        self.player.callback = _completed
    151147        self.player.start()
    152148
    153149    def pause(self):
    154         if self.is_playing:
    155             self.player.mplayer_pause()
    156             self.state = States.STOPPED
     150        """Non-blocking pause function.  Sets the job state to
     151        STOPPED.
     152
     153        """
     154       
     155        if not self.is_playing:
     156            raise InvalidJobStateException(self.state)
     157       
     158        self.player.mplayer_pause()
     159        self.state = States.STOPPED
    157160
    158161    def cancel(self):
     162        def _canceled():
     163            logger.info("canceled job %s" % str(self))
     164            self.state = States.CANCELLED
     165            self.player = None
     166
    159167        if self.is_playing:
    160             self.player.callback = self._canceled
     168            self.player.callback = _canceled
    161169            self.player.mplayer_stop()
     170        elif self.is_finished:
     171            raise InvalidJobStateException(self.state)
     172       
    162173        self.state = States.CANCELLED
    163174
    164175    def abort(self):
     176        def _aborted():
     177            logger.info("aborted job %s" % str(self))
     178            self.state = States.ABORTED
     179            self.player = None
     180
    165181        if self.is_playing:
    166             self.player.callback = self._aborted
     182            self.player.callback = _aborted
    167183            self.player.mplayer_stop()
    168         self.state = states.ABORTED
    169 
    170     def _completed(self):
    171         logger.info("completed job %s" % str(self))
    172         self.state = States.COMPLETE
    173         self.player = None
    174 
    175     def _canceled(self):
    176         logger.info("canceled job %s" % str(self))
    177         self.state = States.CANCELLED
    178         self.player = None
    179 
    180     def _aborted(self):
    181         logger.info("aborted job %s" % str(self))
     184        elif self.is_finished:
     185            raise InvalidJobStateException(self.state)
     186       
    182187        self.state = States.ABORTED
    183         self.player = None
    184 
    185     ######################################################################
    186     ###                        IPP Attributes                          ###
    187     ######################################################################
    188 
    189     @property
    190     def job_id(self):
    191         return ipp.JobId(self.id)
    192 
    193     @property
    194     def job_name(self):
    195         return ipp.JobName(self.name)
    196 
    197     @property
    198     def job_originating_user_name(self):
    199         return ipp.JobOriginatingUserName(self.creator)
    200 
    201     @property
    202     def job_k_octets(self):
    203         return ipp.JobKOctets(self.size)
    204 
    205     @property
    206     def job_state(self):
    207         return ipp.JobState(self.state)
    208 
    209     @property
    210     def job_printer_uri(self):
    211         return ipp.JobPrinterUri(self.printer.uri)
    212188
    213189
    214     ######################################################################
    215     ###                        IPP Operations                          ###
    216     ######################################################################
    217 
    218     def cancel_job(self):
    219         pass
    220 
    221     def send_document(self, document, document_name=None,
    222                       document_format=None, document_natural_language=None,
    223                       requesting_user_name=None, compression=None,
    224                       last_document=None):
    225 
    226         if self.state != States.HELD:
    227             raise InvalidJobStateException(self.state)
    228 
    229         self.player = Player(document)
    230 
    231         if self.size == 0:
    232             self.size = os.path.getsize(document.name)
    233        
    234         self.document_name = str(document_name)
    235         self.document_format = str(document_format)
    236         self.document_natural_language = str(document_natural_language)
    237         self.creator = str(requesting_user_name)
    238         self.compression = str(compression)
    239         self.state = States.PENDING
    240 
    241         logger.debug("document for job %d is '%s'" % (self.id, self.document_name))
    242 
    243     def send_uri(self):
    244         pass
    245 
    246     def get_job_attributes(self, requested_attributes=None):
    247         if requested_attributes is None:
    248             requested = self.attributes
    249         else:
    250             requested = [a for a in self.attributes if a in requested_attributes]
    251 
    252         _attributes = [attr.replace("-", "_") for attr in requested]
    253         attributes = [getattr(self, attr) for attr in _attributes]
    254         return attributes
    255 
    256     def set_job_attributes(self):
    257         pass
    258 
    259     def restart_job(self):
    260         pass
    261 
    262     def promote_job(self):
    263         pass
  • server/lib/gutenbach/server/player.py

    reee389a r33ea505  
    4141                self.callback()
    4242
     43        self.fh.close()
     44        self.fh = None
     45
    4346    def mplayer_play(self):
    4447        logger.info("playing file '%s'" % self.fh.name)
  • server/lib/gutenbach/server/printer.py

    r34a4e5d r33ea505  
    1 from . import InvalidJobException, InvalidPrinterStateException, InvalidJobStateException
    2 from . import Job
     1from .errors import InvalidJobException, InvalidPrinterStateException, InvalidJobStateException
     2from .job import GutenbachJob
    33from gutenbach.ipp import PrinterStates as States
    44import gutenbach.ipp as ipp
     
    1717
    1818    # for IPP
    19     attributes = [
     19    printer_attributes = [
    2020        "printer-uri-supported",
    2121        "uri-authentication-supported",
     
    4141    ]
    4242
     43    job_attributes = [
     44        "job-id",
     45        "job-name",
     46        "job-originating-user-name",
     47        "job-k-octets",
     48        "job-state",
     49        "job-printer-uri"
     50    ]
     51
    4352    operations = [
    4453        "print-job",
    45         "complete-job",
    46         "start-job",
    47         "get-job",
     54        "validate-job",
    4855        "get-jobs",
     56        "print-uri",
     57        "create-job",
     58        "pause-printer",
     59        "resume-printer",
     60        "get-printer-attributes",
     61        "set-printer-attributes",
     62        "cancel-job",
     63        "send-document",
     64        "send-uri",
     65        "get-job-attributes",
     66        "set-job-attributes",
     67        "restart-job",
     68        "promote-job"
    4969    ]
    5070       
     
    7393    def __str__(self):
    7494        return "<Printer '%s'>" % self.name
    75 
    76     ######################################################################
    77     ###                          Properties                            ###
    78     ######################################################################
    79 
    80     @property
    81     def uris(self):
    82         uris = ["ipp://localhost:8000/printers/" + self.name,
    83                 "ipp://localhost/printers/" + self.name]
    84         return uris
    85    
    86     @property
    87     def uri(self):
    88         return self.uris[0]
    89 
    90     @property
    91     def state(self):
    92         with self.lock:
    93             if self.current_job is not None:
    94                 val = States.PROCESSING
    95             elif len(self.pending_jobs) == 0:
    96                 val = States.IDLE
    97             else:
    98                 val = States.STOPPED
    99         return val
    100 
    101     @property
    102     def active_jobs(self):
    103         with self.lock:
    104             jobs = self.pending_jobs[:]
    105             if self.current_job is not None:
    106                 jobs.insert(0, self.current_job.id)
    107         return jobs
    108 
    109     ######################################################################
    110     ###                            Methods                             ###
    111     ######################################################################
    11295
    11396    def run(self):
     
    124107                    sys.exit(1)
    125108            time.sleep(0.1)
     109
     110    ######################################################################
     111    ###                          Properties                            ###
     112    ######################################################################
     113
     114    @property
     115    def uris(self):
     116        uris = ["ipp://localhost:8000/printers/" + self.name,
     117                "ipp://localhost/printers/" + self.name]
     118        return uris
     119   
     120    @property
     121    def uri(self):
     122        return self.uris[0]
     123
     124    @property
     125    def state(self):
     126        with self.lock:
     127            if self.current_job is not None:
     128                val = States.PROCESSING
     129            elif len(self.pending_jobs) == 0:
     130                val = States.IDLE
     131            else:
     132                val = States.STOPPED
     133        return val
     134
     135    @property
     136    def active_jobs(self):
     137        with self.lock:
     138            jobs = self.pending_jobs[:]
     139            if self.current_job is not None:
     140                jobs.insert(0, self.current_job.id)
     141        return jobs
     142
     143    ######################################################################
     144    ###                            Methods                             ###
     145    ######################################################################
    126146
    127147    def start_job(self):
     
    150170                self.current_job = None
    151171
    152     def stop(self):
    153         pass
    154 
    155172    def get_job(self, job_id):
    156173        with self.lock:
     
    249266        return ipp.MultipleDocumentJobsSupported(False)
    250267
     268    ######################################################################
     269    ###                      Job IPP Attributes                        ###
     270    ######################################################################
     271
     272    def job_id(self, job_id):
     273        job = self.get_job(job_id)
     274        return ipp.JobId(job.id)
     275
     276    def job_name(self, job_id):
     277        job = self.get_job(job_id)
     278        return ipp.JobName(job.name)
     279
     280    def job_originating_user_name(self, job_id):
     281        job = self.get_job(job_id)
     282        return ipp.JobOriginatingUserName(job.creator)
     283
     284    def job_k_octets(self, job_id):
     285        job = self.get_job(job_id)
     286        return ipp.JobKOctets(job.size)
     287
     288    def job_state(self, job_id):
     289        job = self.get_job(job_id)
     290        return ipp.JobState(job.state)
     291
     292    def job_printer_uri(self, job_id):
     293        job = self.get_job(job_id)
     294        return ipp.JobPrinterUri(self.uri)
    251295
    252296    ######################################################################
     
    260304        pass
    261305
    262     def get_jobs(self, requesting_user_name="", which_jobs=None):
     306    def get_jobs(self, requesting_user_name=None, which_jobs=None,
     307                 requested_attributes=None):
     308       
    263309        # Filter by the which-jobs attribute
    264310        if which_jobs is None:
     
    278324        else:
    279325            user_jobs = [job for job in jobs if job.creator == requesting_user_name]
    280        
    281         return user_jobs
     326
     327        # Get the attributes of each job
     328        job_attrs = [self.get_job_attributes(
     329            job.id, requested_attributes=requested_attributes) for job in user_jobs]
     330       
     331        return job_attrs
    282332
    283333    def print_uri(self):
     
    288338        self._next_job_id += 1
    289339       
    290         job = Job(job_id,
    291                   self,
    292                   creator=requesting_user_name,
    293                   name=job_name,
    294                   size=job_k_octets)
     340        job = GutenbachJob(
     341            job_id,
     342            creator=requesting_user_name,
     343            name=job_name)
    295344       
    296345        self.jobs[job_id] = job
    297346        self.pending_jobs.append(job_id)
    298347       
    299         return job
     348        return job_id
    300349
    301350    def pause_printer(self):
     
    307356    def get_printer_attributes(self, requested_attributes=None):
    308357        if requested_attributes is None:
    309             requested = self.attributes
     358            requested = self.printer_attributes
    310359        else:
    311             requested = [a for a in self.attributes if a in requested_attributes]
     360            requested = [a for a in self.printer_attributes \
     361                         if a in requested_attributes]
    312362
    313363        _attributes = [attr.replace("-", "_") for attr in requested]
     
    317367    def set_printer_attributes(self):
    318368        pass
     369
     370    def cancel_job(self, job_id, requesting_user_name=None):
     371        job = self.get_job(job_id)
     372        try:
     373            job.cancel()
     374        except InvalidJobStateException:
     375            # XXX
     376            raise
     377
     378    def send_document(self, job_id, document, document_name=None,
     379                      document_format=None, document_natural_language=None,
     380                      requesting_user_name=None, compression=None,
     381                      last_document=None):
     382
     383        job = self.get_job(job_id)
     384        job.spool(document, username=requesting_user_name)
     385
     386    def send_uri(self):
     387        pass
     388
     389    def get_job_attributes(self, job_id, requested_attributes=None):
     390        if requested_attributes is None:
     391            requested = self.job_attributes
     392        else:
     393            requested = [a for a in self.job_attributes \
     394                         if a in requested_attributes]
     395
     396        _attributes = [attr.replace("-", "_") for attr in requested]
     397        attributes = [getattr(self, attr)(job_id) for attr in _attributes]
     398        return attributes
     399
     400    def set_job_attributes(self):
     401        pass
     402
     403    def restart_job(self):
     404        pass
     405
     406    def promote_job(self):
     407        pass
  • server/lib/gutenbach/server/requests.py

    r34a4e5d r33ea505  
    258258           
    259259        # get the job attributes and add them to the response
    260         jobs = self.printer.get_jobs(
     260        job_attrs = self.printer.get_jobs(
    261261            which_jobs=which_jobs,
    262             requesting_user_name=user)
    263         for job in jobs:
    264             attrs = job.get_job_attributes(requested_attributes=attributes)
     262            requesting_user_name=user,
     263            requested_attributes=attributes)
     264        for attrs in job_attrs:
    265265            response.attribute_groups.append(ipp.AttributeGroup(
    266266                ipp.AttributeTags.JOB, attrs))
     
    354354
    355355        # get attributes from the printer and add to response
    356         job = self.printer.create_job(
     356        job_id = self.printer.create_job(
    357357            requesting_user_name=requesting_user_name,
    358358            job_name=job_name,
    359359            job_k_octets=job_k_octets)
     360        attrs = self.printer.get_job_attributes(job_id)
    360361        response.attribute_groups.append(ipp.AttributeGroup(
    361             ipp.AttributeTags.JOB, job.get_job_attributes()))
     362            ipp.AttributeTags.JOB, attrs))
    362363   
    363364    @handler_for(ipp.OperationCodes.PAUSE_PRINTER)
     
    536537
    537538        if 'requesting-user-name' in operation:
    538             user_name = verify_attribute(
     539            requesting_user_name = verify_attribute(
    539540                operation['requesting-user-name'], ipp.RequestingUserName)[0]
    540541
    541542        try:
    542             job = self.printer.get_job(job_id)
    543             job.cancel()
     543            self.printer.cancel_job(job_id, requesting_user_name=requesting_user_name)
    544544        except InvalidJobException:
    545545            raise ipp.errors.ClientErrorNotFound("bad job: %d" % job_id)
     
    681681
    682682        try:
    683             job = self.printer.get_job(job_id)
    684             job.send_document(
     683            self.printer.send_document(
     684                job_id,
    685685                request.data,
    686                 requesting_user_name=user_name,
    687686                document_name=document_name,
    688                 compression=compression,
    689687                document_format=document_format,
    690688                document_natural_language=document_natural_language,
     689                requesting_user_name=user_name,
     690                compression=compression,
    691691                last_document=last_document)
    692             attrs = job.get_job_attributes()
     692            attrs = self.printer.get_job_attributes(job_id)
    693693        except InvalidJobException:
    694694            raise ipp.errors.ClientErrorNotFound("bad job: %d" % job_id)
     
    796796        # get the job attributes and add them to the response
    797797        try:
    798             job = self.printer.get_job(job_id)
    799             attrs = job.get_job_attributes(requested_attributes=requested_attributes)
     798            attrs = self.printer.get_job_attributes(
     799                job_id,
     800                requested_attributes=requested_attributes)
    800801        except InvalidJobException:
    801802            raise ipp.errors.ClientErrorNotFound("bad job: %d" % job_id)
Note: See TracChangeset for help on using the changeset viewer.