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