source: server/lib/gutenbach/server/job.py @ 793432f

no-cups
Last change on this file since 793432f was 793432f, checked in by Jessica B. Hamrick <jhamrick@…>, 12 years ago

Reorganization

  • Property mode set to 100644
File size: 2.6 KB
RevLine 
[d04a689]1from exceptions import InvalidJobException, InvalidPrinterStateException
[776a659]2import os
[793432f]3import gutenbach.ipp as ipp
[d04a689]4
5# initialize logger
6logger = logging.getLogger(__name__)
[776a659]7
8class Job(object):
9
[1a63bf7]10    # for IPP
[b2e077a]11    attributes = [
12        "job-id",
13        "job-name",
14        "job-originating-user-name",
15        "job-k-octets",
16        "job-state",
17        "job-printer-uri"
[1a63bf7]18    ]
[b2e077a]19
[776a659]20    def __init__(self, document=None):
21        """Initialize a Gutenbach job.
22
23        This sets the status to 'initializing' and optionally sets the
24        document to print to the value of document.
25        """
26         
[1a63bf7]27        self.jid = None
28        self.name = document
29        self.status = None
30        self.document = document
31        self.printer = None
32
33    def __getattr__(self, attr):
34        try:
35            return super(Job, self).__getattr__(attr)
36        except AttributeError:
37            pass
38
39        return super(Job, self).__getattr__(
40            attr.replace("-", "_"))
41
42    def __hasattr__(self, attr):
43        has = super(Job, self).__hasattr__(attr)
44        if not has:
45            has = super(Job, self).__hasattr__(
46                attr.replace("-", "_"))
47        return has
48
49    #### Job attributes
[776a659]50
51    @property
[b2e077a]52    def job_id(self):
[793432f]53        return ipp.JobId(self.jid)
[b2e077a]54
55    @property
56    def job_name(self):
[793432f]57        return ipp.JobName(self.name)
[776a659]58
[b2e077a]59    # XXX: we need to actually calculate this!
60    @property
61    def job_originating_user_name(self):
[793432f]62        return ipp.JobOriginatingUserName("jhamrick")
[776a659]63
[b2e077a]64    # XXX: we need to actually calculate this!
65    @property
66    def job_k_octets(self):
[793432f]67        return ipp.JobKOctets(1)
[b2e077a]68
69    @property
70    def job_state(self):
[793432f]71        return ipp.JobState(self.status)
[b2e077a]72
73    @property
74    def job_printer_uri(self):
[793432f]75        return ipp.JobPrinterUri(self.printer.uri)
[b2e077a]76
77    def get_job_attributes(self, request):
[aded2d1]78        attributes = [getattr(self, attr) for attr in self.attributes]
[b2e077a]79        return attributes
80   
81    #######
[776a659]82
[b2e077a]83    def enqueue(self, printer, job_id):
[1a63bf7]84        if self.status != None:
[776a659]85            raise InvalidJobException(
86                "Cannot enqueue a job that has " + \
87                "already been initialized!")
[1a63bf7]88        self.printer = printer
89        self.jid = job_id
90        self.status = const.JobStates.PENDING
[776a659]91
92    def play(self):
93        if self.status != 'active':
94            raise InvalidJobException(
95                "Cannot play an inactive job!")
96       
[1a63bf7]97        self.status = const.JobStates.PROCESSING
[776a659]98        # TODO: add external call to music player
[5d24a81]99        print "Playing job %s" % str(self)
[1a63bf7]100        self.printer.complete_job(self.jid)
[776a659]101
[5d24a81]102    def finish(self):
[1a63bf7]103        self.status = const.JobStates.COMPLETE
[776a659]104
105    def __repr__(self):
106        return str(self)
107
108    def __str__(self):
[5d24a81]109        return "<Job %d '%s'>" % \
[1a63bf7]110               (self.jid if self.jid is not None else -1, \
111                self.document)
Note: See TracBrowser for help on using the repository browser.