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