source: server/lib/gutenbach/server/job.py @ b2e077a

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

Refactor code to handle the operations a little bit more logically

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