source: server/lib/gutenbach/server/job.py @ 5fe360e

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

Converting server code to rely more on the ipp/ code

  • Property mode set to 100644
File size: 2.6 KB
RevLine 
[d04a689]1from exceptions import InvalidJobException, InvalidPrinterStateException
[776a659]2import os
[d04a689]3
4# initialize logger
5logger = logging.getLogger(__name__)
[776a659]6
7class Job(object):
8
[1a63bf7]9    # for IPP
[b2e077a]10    attributes = [
11        "job-id",
12        "job-name",
13        "job-originating-user-name",
14        "job-k-octets",
15        "job-state",
16        "job-printer-uri"
[1a63bf7]17    ]
[b2e077a]18
[776a659]19    def __init__(self, document=None):
20        """Initialize a Gutenbach job.
21
22        This sets the status to 'initializing' and optionally sets the
23        document to print to the value of document.
24        """
25         
[1a63bf7]26        self.jid = None
27        self.name = document
28        self.status = None
29        self.document = document
30        self.printer = None
31
32    def __getattr__(self, attr):
33        try:
34            return super(Job, self).__getattr__(attr)
35        except AttributeError:
36            pass
37
38        return super(Job, self).__getattr__(
39            attr.replace("-", "_"))
40
41    def __hasattr__(self, attr):
42        has = super(Job, self).__hasattr__(attr)
43        if not has:
44            has = super(Job, self).__hasattr__(
45                attr.replace("-", "_"))
46        return has
47
48    #### Job attributes
[776a659]49
50    @property
[b2e077a]51    def job_id(self):
[1a63bf7]52        return self.jid
[b2e077a]53
54    @property
55    def job_name(self):
[1a63bf7]56        return self.name
[776a659]57
[b2e077a]58    # XXX: we need to actually calculate this!
59    @property
60    def job_originating_user_name(self):
[1a63bf7]61        return "jhamrick"
[776a659]62
[b2e077a]63    # XXX: we need to actually calculate this!
64    @property
65    def job_k_octets(self):
[5fe360e]66        return 1
[b2e077a]67
68    @property
69    def job_state(self):
[1a63bf7]70        return self.status
[b2e077a]71
72    @property
73    def job_printer_uri(self):
[1a63bf7]74        return self.printer.uri
[b2e077a]75
76    def get_job_attributes(self, request):
[1a63bf7]77        attributes = [(attr, getattr(self, attr)) for attr in self.attributes]
[5fe360e]78        attributes = map(lambda x: x if isinstance(x, (tuple, list)) else [x], 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.