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