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