1 | #import alsaaudio as aa |
---|
2 | from .exceptions import InvalidJobException, InvalidPrinterStateException |
---|
3 | from gutenbach.ipp.attribute import Attribute |
---|
4 | import gutenbach.ipp as ipp |
---|
5 | import gutenbach.ipp.constants as const |
---|
6 | import logging |
---|
7 | import time |
---|
8 | |
---|
9 | # initialize logger |
---|
10 | logger = logging.getLogger(__name__) |
---|
11 | |
---|
12 | class GutenbachPrinter(object): |
---|
13 | |
---|
14 | attributes = [ |
---|
15 | "printer-uri-supported", |
---|
16 | "uri-authentication-supported", |
---|
17 | "uri-security-supported", |
---|
18 | "printer-name", |
---|
19 | "printer-state", |
---|
20 | "printer-state-reasons", |
---|
21 | "ipp-versions-supported", |
---|
22 | "operations-supported", |
---|
23 | "charset-configured", |
---|
24 | "charset-supported", |
---|
25 | "natural-language-configured", |
---|
26 | "generated-natural-language-supported", |
---|
27 | "document-format-default", |
---|
28 | "document-format-supported", |
---|
29 | "printer-is-accepting-jobs", |
---|
30 | "queued-job-count", |
---|
31 | "pdl-override-supported", |
---|
32 | "printer-up-time", |
---|
33 | "compression-supported"] |
---|
34 | |
---|
35 | #def __init__(self, name, card, mixer): |
---|
36 | def __init__(self, name): |
---|
37 | |
---|
38 | self._name = name |
---|
39 | self._uri = "ipp://localhost:8000/printers/" + self._name |
---|
40 | self._time_created = int(time.time()) |
---|
41 | |
---|
42 | # if card >= len(aa.cards()): |
---|
43 | # raise aa.ALSAAudioError( |
---|
44 | # "Audio card at index %d does not exist!" % card) |
---|
45 | # elif mixer not in aa.mixers(card): |
---|
46 | # raise aa.ALSAAudioError( |
---|
47 | # "Audio mixer '%s' does not exist!" % mixer) |
---|
48 | |
---|
49 | # self.card = card |
---|
50 | # self.mixer = mixer |
---|
51 | |
---|
52 | self.finished_jobs = [] |
---|
53 | self.active_jobs = [] |
---|
54 | self.jobs = {} |
---|
55 | |
---|
56 | self._next_jobid = 0 |
---|
57 | |
---|
58 | ## Printer attributes |
---|
59 | @property |
---|
60 | def printer_uri_supported(self): |
---|
61 | return ipp.Attribute( |
---|
62 | "printer-uri-supported", |
---|
63 | [ipp.Value(ipp.Tags.URI, self._uri)]) |
---|
64 | |
---|
65 | @property |
---|
66 | def uri_authentication_supported(self): |
---|
67 | return ipp.Attribute( |
---|
68 | "uri-authentication-supported", |
---|
69 | [ipp.Value(ipp.Tags.KEYWORD, "none")]) |
---|
70 | |
---|
71 | @property |
---|
72 | def uri_security_supported(self): |
---|
73 | return ipp.Attribute( |
---|
74 | "uri-security-supported", |
---|
75 | [ipp.Value(ipp.Tags.KEYWORD, "none")]) |
---|
76 | |
---|
77 | @property |
---|
78 | def printer_name(self): |
---|
79 | return ipp.Attribute( |
---|
80 | "printer-name", |
---|
81 | [ipp.Value(ipp.Tags.NAME_WITHOUT_LANGUAGE, self._name)]) |
---|
82 | |
---|
83 | @property |
---|
84 | def printer_state(self): |
---|
85 | return ipp.Attribute( |
---|
86 | "printer-state", |
---|
87 | [ipp.Value(ipp.Tags.ENUM, const.PrinterStates.IDLE)]) |
---|
88 | |
---|
89 | @property |
---|
90 | def printer_state_reasons(self): |
---|
91 | return ipp.Attribute( |
---|
92 | "printer-state-reasons", |
---|
93 | [ipp.Value(ipp.Tags.KEYWORD, "none")]) |
---|
94 | |
---|
95 | @property |
---|
96 | def ipp_versions_supported(self): |
---|
97 | return ipp.Attribute( |
---|
98 | "ipp-versions-supported", |
---|
99 | [ipp.Value(ipp.Tags.KEYWORD, "1.0"), |
---|
100 | ipp.Value(ipp.Tags.KEYWORD, "1.1")]) |
---|
101 | |
---|
102 | # XXX: We should query ourself for the supported operations |
---|
103 | @property |
---|
104 | def operations_supported(self): |
---|
105 | return ipp.Attribute( |
---|
106 | "operations-supported", |
---|
107 | [ipp.Value(ipp.Tags.ENUM, const.Operations.GET_JOBS)]) |
---|
108 | |
---|
109 | @property |
---|
110 | def charset_configured(self): |
---|
111 | return ipp.Attribute( |
---|
112 | "charset-configured", |
---|
113 | [ipp.Value(ipp.Tags.CHARSET, "utf-8")]) |
---|
114 | |
---|
115 | @property |
---|
116 | def charset_supported(self): |
---|
117 | return ipp.Attribute( |
---|
118 | "charset-supported", |
---|
119 | [ipp.Value(ipp.Tags.CHARSET, "utf-8")]) |
---|
120 | |
---|
121 | @property |
---|
122 | def natural_language_configured(self): |
---|
123 | return ipp.Attribute( |
---|
124 | "natural-language-configured", |
---|
125 | [ipp.Value(ipp.Tags.NATURAL_LANGUAGE, "en-us")]) |
---|
126 | |
---|
127 | @property |
---|
128 | def generated_natural_language_supported(self): |
---|
129 | return ipp.Attribute( |
---|
130 | "generated-natural-language-supported", |
---|
131 | [ipp.Value(ipp.Tags.NATURAL_LANGUAGE, "en-us")]) |
---|
132 | |
---|
133 | @property |
---|
134 | def document_format_default(self): |
---|
135 | return ipp.Attribute( |
---|
136 | "document-format-default", |
---|
137 | [ipp.Value(ipp.Tags.MIME_MEDIA_TYPE, "application/octet-stream")]) |
---|
138 | |
---|
139 | @property |
---|
140 | def document_format_supported(self): |
---|
141 | return ipp.Attribute( |
---|
142 | "document-format-supported", |
---|
143 | [ipp.Value(ipp.Tags.MIME_MEDIA_TYPE, "application/octet-stream"), |
---|
144 | ipp.Value(ipp.Tags.MIME_MEDIA_TYPE, "audio/mp3")]) |
---|
145 | |
---|
146 | @property |
---|
147 | def printer_is_accepting_jobs(self): |
---|
148 | return ipp.Attribute( |
---|
149 | "printer-is-accepting-jobs", |
---|
150 | [ipp.Value(ipp.Tags.BOOLEAN, True)]) |
---|
151 | |
---|
152 | @property |
---|
153 | def queued_job_count(self): |
---|
154 | return ipp.Attribute( |
---|
155 | "queued-job-count", |
---|
156 | [ipp.Value(ipp.Tags.INTEGER, len(self.active_jobs))]) |
---|
157 | |
---|
158 | @property |
---|
159 | def pdl_override_supported(self): |
---|
160 | return ipp.Attribute( |
---|
161 | "pdl-override-supported", |
---|
162 | [ipp.Value(ipp.Tags.KEYWORD, "not-attempted")]) |
---|
163 | |
---|
164 | @property |
---|
165 | def printer_up_time(self): |
---|
166 | return ipp.Attribute( |
---|
167 | "printer-up-time", |
---|
168 | [ipp.Value(ipp.Tags.INTEGER, int(time.time()) - self._time_created)]) |
---|
169 | |
---|
170 | @property |
---|
171 | def compression_supported(self): |
---|
172 | return ipp.Attribute( |
---|
173 | "compression-supported", |
---|
174 | [ipp.Value(ipp.Tags.KEYWORD, "none")]) |
---|
175 | |
---|
176 | def get_printer_attributes(self, request): |
---|
177 | attributes = [getattr(self, attr.replace("-", "_")) for attr in self.attributes] |
---|
178 | return attributes |
---|
179 | |
---|
180 | ## Printer operations |
---|
181 | @property |
---|
182 | def next_jobid(self): |
---|
183 | self._next_jobid += 1 |
---|
184 | return self._next_jobid |
---|
185 | |
---|
186 | def print_job(self, job): |
---|
187 | jobid = self.next_jobid |
---|
188 | self.active_jobs.append(jobid) |
---|
189 | self.jobs[jobid] = job |
---|
190 | job.enqueue(self, jobid) |
---|
191 | return jobid |
---|
192 | |
---|
193 | def complete_job(self, jobid): |
---|
194 | job = self.jobs[self.active_jobs.pop(0)] |
---|
195 | if job.jobid != jobid: |
---|
196 | raise InvalidJobException( |
---|
197 | "Completed job %d has unexpected job id %d!" % \ |
---|
198 | (job.jobid, jobid)) |
---|
199 | |
---|
200 | self.finished_jobs.append(job) |
---|
201 | job.finish() |
---|
202 | return job.jobid |
---|
203 | |
---|
204 | def start_job(self, jobid): |
---|
205 | job = self.jobs[self.active_jobs[0]] |
---|
206 | if job.jobid != jobid: |
---|
207 | raise InvalidJobException( |
---|
208 | "Completed job %d has unexpected job id %d!" % \ |
---|
209 | (job.jobid, jobid)) |
---|
210 | |
---|
211 | if job.status == 'playing': |
---|
212 | raise InvalidPrinterStateException( |
---|
213 | "Next job in queue (id %d) is " + \ |
---|
214 | "already playing!" % jobid) |
---|
215 | |
---|
216 | job.play() |
---|
217 | |
---|
218 | def get_job(self, jobid): |
---|
219 | if jobid not in self.jobs: |
---|
220 | raise InvalidJobException(jobid) |
---|
221 | return self.jobs[jobid] |
---|
222 | |
---|
223 | def get_jobs(self): |
---|
224 | jobs = [self.jobs[job_id] for job_id in self.active_jobs] |
---|
225 | return jobs |
---|
226 | |
---|
227 | def __repr__(self): |
---|
228 | return str(self) |
---|
229 | |
---|
230 | def __str__(self): |
---|
231 | return "<Printer '%s'>" % self._name |
---|