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

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

Implement send-document operation and add threading so that the gutenbach server can play jobs

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