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
Line 
1from exceptions import InvalidJobException, InvalidPrinterStateException
2import os
3
4# initialize logger
5logger = logging.getLogger(__name__)
6
7class Job(object):
8
9    # for IPP
10    attributes = [
11        "job-id",
12        "job-name",
13        "job-originating-user-name",
14        "job-k-octets",
15        "job-state",
16        "job-printer-uri"
17    ]
18
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         
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
49
50    @property
51    def job_id(self):
52        return self.jid
53
54    @property
55    def job_name(self):
56        return self.name
57
58    # XXX: we need to actually calculate this!
59    @property
60    def job_originating_user_name(self):
61        return "jhamrick"
62
63    # XXX: we need to actually calculate this!
64    @property
65    def job_k_octets(self):
66        return 1
67
68    @property
69    def job_state(self):
70        return self.status
71
72    @property
73    def job_printer_uri(self):
74        return self.printer.uri
75
76    def get_job_attributes(self, request):
77        attributes = [(attr, getattr(self, attr)) for attr in self.attributes]
78        attributes = map(lambda x: x if isinstance(x, (tuple, list)) else [x], attributes)
79        return attributes
80   
81    #######
82
83    def enqueue(self, printer, job_id):
84        if self.status != None:
85            raise InvalidJobException(
86                "Cannot enqueue a job that has " + \
87                "already been initialized!")
88        self.printer = printer
89        self.jid = job_id
90        self.status = const.JobStates.PENDING
91
92    def play(self):
93        if self.status != 'active':
94            raise InvalidJobException(
95                "Cannot play an inactive job!")
96       
97        self.status = const.JobStates.PROCESSING
98        # TODO: add external call to music player
99        print "Playing job %s" % str(self)
100        self.printer.complete_job(self.jid)
101
102    def finish(self):
103        self.status = const.JobStates.COMPLETE
104
105    def __repr__(self):
106        return str(self)
107
108    def __str__(self):
109        return "<Job %d '%s'>" % \
110               (self.jid if self.jid is not None else -1, \
111                self.document)
Note: See TracBrowser for help on using the repository browser.