1 | from exceptions import InvalidJobException, InvalidPrinterStateException |
---|
2 | import os |
---|
3 | import gutenbach.ipp as ipp |
---|
4 | |
---|
5 | # initialize logger |
---|
6 | logger = logging.getLogger(__name__) |
---|
7 | |
---|
8 | class Job(object): |
---|
9 | |
---|
10 | # for IPP |
---|
11 | attributes = [ |
---|
12 | "job-id", |
---|
13 | "job-name", |
---|
14 | "job-originating-user-name", |
---|
15 | "job-k-octets", |
---|
16 | "job-state", |
---|
17 | "job-printer-uri" |
---|
18 | ] |
---|
19 | |
---|
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 | |
---|
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 |
---|
50 | |
---|
51 | @property |
---|
52 | def job_id(self): |
---|
53 | return ipp.JobId(self.jid) |
---|
54 | |
---|
55 | @property |
---|
56 | def job_name(self): |
---|
57 | return ipp.JobName(self.name) |
---|
58 | |
---|
59 | # XXX: we need to actually calculate this! |
---|
60 | @property |
---|
61 | def job_originating_user_name(self): |
---|
62 | return ipp.JobOriginatingUserName("jhamrick") |
---|
63 | |
---|
64 | # XXX: we need to actually calculate this! |
---|
65 | @property |
---|
66 | def job_k_octets(self): |
---|
67 | return ipp.JobKOctets(1) |
---|
68 | |
---|
69 | @property |
---|
70 | def job_state(self): |
---|
71 | return ipp.JobState(self.status) |
---|
72 | |
---|
73 | @property |
---|
74 | def job_printer_uri(self): |
---|
75 | return ipp.JobPrinterUri(self.printer.uri) |
---|
76 | |
---|
77 | def get_job_attributes(self, request): |
---|
78 | attributes = [getattr(self, attr) for attr in self.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) |
---|