[b2e077a] | 1 | #import alsaaudio as aa |
---|
[d04a689] | 2 | from .exceptions import InvalidJobException, InvalidPrinterStateException |
---|
[b2e077a] | 3 | import gutenbach.ipp as ipp |
---|
| 4 | import logging |
---|
| 5 | import time |
---|
[d04a689] | 6 | |
---|
| 7 | # initialize logger |
---|
| 8 | logger = logging.getLogger(__name__) |
---|
[776a659] | 9 | |
---|
[b2e077a] | 10 | class GutenbachPrinter(object): |
---|
| 11 | |
---|
[1a63bf7] | 12 | # for IPP |
---|
[b2e077a] | 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", |
---|
[f6e2532] | 32 | "compression-supported", |
---|
| 33 | "multiple-operation-time-out", |
---|
| 34 | "multiple-document-jobs-supported", |
---|
[1a63bf7] | 35 | ] |
---|
[b2e077a] | 36 | |
---|
[f6e2532] | 37 | operations = [ |
---|
| 38 | "print-job", |
---|
| 39 | "complete-job", |
---|
| 40 | "start-job", |
---|
| 41 | "get-job", |
---|
| 42 | "get-jobs", |
---|
| 43 | ] |
---|
| 44 | |
---|
| 45 | |
---|
[b2e077a] | 46 | #def __init__(self, name, card, mixer): |
---|
| 47 | def __init__(self, name): |
---|
| 48 | |
---|
[1a63bf7] | 49 | self.name = name |
---|
| 50 | self.uri = "ipp://localhost:8000/printers/" + self.name |
---|
| 51 | self.time_created = int(time.time()) |
---|
| 52 | self.state = "idle" |
---|
[b2e077a] | 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) |
---|
[776a659] | 60 | |
---|
[b2e077a] | 61 | # self.card = card |
---|
| 62 | # self.mixer = mixer |
---|
[776a659] | 63 | |
---|
| 64 | self.finished_jobs = [] |
---|
| 65 | self.active_jobs = [] |
---|
| 66 | self.jobs = {} |
---|
| 67 | |
---|
[5d24a81] | 68 | self._next_jobid = 0 |
---|
[776a659] | 69 | |
---|
[1a63bf7] | 70 | def __getattr__(self, attr): |
---|
| 71 | try: |
---|
[6effd50] | 72 | return self.__getattribute__(attr) |
---|
[1a63bf7] | 73 | except AttributeError: |
---|
| 74 | pass |
---|
[6effd50] | 75 | return self.__getattribute__(attr.replace("-", "_")) |
---|
[1a63bf7] | 76 | |
---|
| 77 | def __hasattr__(self, attr): |
---|
[6effd50] | 78 | try: |
---|
| 79 | getattr(self, attr) |
---|
| 80 | return True |
---|
| 81 | except AttributeError: |
---|
| 82 | return False |
---|
[1a63bf7] | 83 | |
---|
[b2e077a] | 84 | ## Printer attributes |
---|
[1a63bf7] | 85 | |
---|
[b2e077a] | 86 | @property |
---|
| 87 | def printer_uri_supported(self): |
---|
[793432f] | 88 | return ipp.PrinterUriSupported(self.uri) |
---|
[1a63bf7] | 89 | |
---|
[b2e077a] | 90 | @property |
---|
| 91 | def uri_authentication_supported(self): |
---|
[793432f] | 92 | return ipp.UriAuthenticationSupported("none") |
---|
[b2e077a] | 93 | |
---|
| 94 | @property |
---|
| 95 | def uri_security_supported(self): |
---|
[793432f] | 96 | return ipp.UriSecuritySupported("none") |
---|
[b2e077a] | 97 | |
---|
| 98 | @property |
---|
| 99 | def printer_name(self): |
---|
[793432f] | 100 | return ipp.PrinterName(self.name) |
---|
[1a63bf7] | 101 | |
---|
[b2e077a] | 102 | @property |
---|
| 103 | def printer_state(self): |
---|
[793432f] | 104 | return ipp.PrinterState(ipp.constants.PrinterStates.IDLE) |
---|
[1a63bf7] | 105 | |
---|
[b2e077a] | 106 | @property |
---|
| 107 | def printer_state_reasons(self): |
---|
[793432f] | 108 | return ipp.PrinterStateReasons("none") |
---|
[b2e077a] | 109 | |
---|
| 110 | @property |
---|
| 111 | def ipp_versions_supported(self): |
---|
[793432f] | 112 | return ipp.IppVersionsSupported("1.0", "1.1") |
---|
[1a63bf7] | 113 | |
---|
[f6e2532] | 114 | # XXX: We should query ourself for the supported operations |
---|
[b2e077a] | 115 | @property |
---|
| 116 | def operations_supported(self): |
---|
[793432f] | 117 | return ipp.OperationsSupported(ipp.OperationCodes.GET_JOBS) |
---|
[b2e077a] | 118 | |
---|
| 119 | @property |
---|
| 120 | def charset_configured(self): |
---|
[793432f] | 121 | return ipp.CharsetConfigured("utf-8") |
---|
[b2e077a] | 122 | |
---|
| 123 | @property |
---|
| 124 | def charset_supported(self): |
---|
[793432f] | 125 | return ipp.CharsetSupported("utf-8") |
---|
[b2e077a] | 126 | |
---|
| 127 | @property |
---|
| 128 | def natural_language_configured(self): |
---|
[793432f] | 129 | return ipp.NaturalLanguageConfigured("en-us") |
---|
[b2e077a] | 130 | |
---|
| 131 | @property |
---|
| 132 | def generated_natural_language_supported(self): |
---|
[793432f] | 133 | return ipp.GeneratedNaturalLanguageSupported("en-us") |
---|
[b2e077a] | 134 | |
---|
| 135 | @property |
---|
| 136 | def document_format_default(self): |
---|
[793432f] | 137 | return ipp.DocumentFormatDefault("application/octet-stream") |
---|
[b2e077a] | 138 | |
---|
| 139 | @property |
---|
| 140 | def document_format_supported(self): |
---|
[793432f] | 141 | return ipp.DocumentFormatSupported("application/octet-stream", "audio/mp3") |
---|
[b2e077a] | 142 | |
---|
| 143 | @property |
---|
| 144 | def printer_is_accepting_jobs(self): |
---|
[793432f] | 145 | return ipp.PrinterIsAcceptingJobs(True) |
---|
[b2e077a] | 146 | |
---|
| 147 | @property |
---|
| 148 | def queued_job_count(self): |
---|
[793432f] | 149 | return ipp.QueuedJobCount(len(self.active_jobs)) |
---|
[b2e077a] | 150 | |
---|
| 151 | @property |
---|
| 152 | def pdl_override_supported(self): |
---|
[793432f] | 153 | return ipp.PdlOverrideSupported("not-attempted") |
---|
[b2e077a] | 154 | |
---|
| 155 | @property |
---|
| 156 | def printer_up_time(self): |
---|
[793432f] | 157 | return ipp.PrinterUpTime(int(time.time()) - self.time_created) |
---|
[b2e077a] | 158 | |
---|
| 159 | @property |
---|
| 160 | def compression_supported(self): |
---|
[793432f] | 161 | return ipp.CompressionSupported("none") |
---|
[b2e077a] | 162 | |
---|
[f6e2532] | 163 | @property |
---|
| 164 | def multiple_operation_time_out(self): |
---|
[793432f] | 165 | return ipp.MultipleOperationTimeOut(240) |
---|
[f6e2532] | 166 | |
---|
| 167 | @property |
---|
| 168 | def multiple_document_jobs_supported(self): |
---|
[793432f] | 169 | return ipp.MultipleDocumentJobsSupported(False) |
---|
[f6e2532] | 170 | |
---|
[b2e077a] | 171 | def get_printer_attributes(self, request): |
---|
[aded2d1] | 172 | attributes = [getattr(self, attr) for attr in self.attributes] |
---|
[b2e077a] | 173 | return attributes |
---|
| 174 | |
---|
| 175 | ## Printer operations |
---|
[776a659] | 176 | |
---|
| 177 | def print_job(self, job): |
---|
[f6e2532] | 178 | jobid = self._next_jobid |
---|
| 179 | self._next_jobid += 1 |
---|
[776a659] | 180 | self.active_jobs.append(jobid) |
---|
| 181 | self.jobs[jobid] = job |
---|
[5d24a81] | 182 | job.enqueue(self, jobid) |
---|
[776a659] | 183 | return jobid |
---|
| 184 | |
---|
| 185 | def complete_job(self, jobid): |
---|
[5d24a81] | 186 | job = self.jobs[self.active_jobs.pop(0)] |
---|
[776a659] | 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): |
---|
[5d24a81] | 197 | job = self.jobs[self.active_jobs[0]] |
---|
[776a659] | 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 | |
---|
[b2e077a] | 215 | def get_jobs(self): |
---|
| 216 | jobs = [self.jobs[job_id] for job_id in self.active_jobs] |
---|
| 217 | return jobs |
---|
| 218 | |
---|
[776a659] | 219 | def __repr__(self): |
---|
| 220 | return str(self) |
---|
| 221 | |
---|
| 222 | def __str__(self): |
---|
[1a63bf7] | 223 | return "<Printer '%s'>" % self.name |
---|