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