[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 |
---|
| 6 | import logging |
---|
| 7 | import time |
---|
[d04a689] | 8 | |
---|
| 9 | # initialize logger |
---|
| 10 | logger = logging.getLogger(__name__) |
---|
[776a659] | 11 | |
---|
[b2e077a] | 12 | class GutenbachPrinter(object): |
---|
| 13 | |
---|
[1a63bf7] | 14 | # for IPP |
---|
[b2e077a] | 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", |
---|
[1a63bf7] | 34 | "compression-supported" |
---|
| 35 | ] |
---|
[b2e077a] | 36 | |
---|
| 37 | #def __init__(self, name, card, mixer): |
---|
| 38 | def __init__(self, name): |
---|
| 39 | |
---|
[1a63bf7] | 40 | self.name = name |
---|
| 41 | self.uri = "ipp://localhost:8000/printers/" + self.name |
---|
| 42 | self.time_created = int(time.time()) |
---|
| 43 | self.state = "idle" |
---|
[b2e077a] | 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) |
---|
[776a659] | 51 | |
---|
[b2e077a] | 52 | # self.card = card |
---|
| 53 | # self.mixer = mixer |
---|
[776a659] | 54 | |
---|
| 55 | self.finished_jobs = [] |
---|
| 56 | self.active_jobs = [] |
---|
| 57 | self.jobs = {} |
---|
| 58 | |
---|
[5d24a81] | 59 | self._next_jobid = 0 |
---|
[776a659] | 60 | |
---|
[1a63bf7] | 61 | def __getattr__(self, attr): |
---|
| 62 | try: |
---|
[6effd50] | 63 | return self.__getattribute__(attr) |
---|
[1a63bf7] | 64 | except AttributeError: |
---|
| 65 | pass |
---|
[6effd50] | 66 | return self.__getattribute__(attr.replace("-", "_")) |
---|
[1a63bf7] | 67 | |
---|
| 68 | def __hasattr__(self, attr): |
---|
[6effd50] | 69 | try: |
---|
| 70 | getattr(self, attr) |
---|
| 71 | return True |
---|
| 72 | except AttributeError: |
---|
| 73 | return False |
---|
[1a63bf7] | 74 | |
---|
[b2e077a] | 75 | ## Printer attributes |
---|
[1a63bf7] | 76 | |
---|
[b2e077a] | 77 | @property |
---|
| 78 | def printer_uri_supported(self): |
---|
[1a63bf7] | 79 | return self.uri |
---|
| 80 | |
---|
[b2e077a] | 81 | @property |
---|
| 82 | def uri_authentication_supported(self): |
---|
[1a63bf7] | 83 | return "none" |
---|
[b2e077a] | 84 | |
---|
| 85 | @property |
---|
| 86 | def uri_security_supported(self): |
---|
[1a63bf7] | 87 | return "none" |
---|
[b2e077a] | 88 | |
---|
| 89 | @property |
---|
| 90 | def printer_name(self): |
---|
[1a63bf7] | 91 | return self.name |
---|
| 92 | |
---|
[b2e077a] | 93 | @property |
---|
| 94 | def printer_state(self): |
---|
[cad7502] | 95 | return 3 # idle |
---|
[1a63bf7] | 96 | |
---|
[b2e077a] | 97 | @property |
---|
| 98 | def printer_state_reasons(self): |
---|
[1a63bf7] | 99 | return "none" |
---|
[b2e077a] | 100 | |
---|
| 101 | @property |
---|
| 102 | def ipp_versions_supported(self): |
---|
[1a63bf7] | 103 | return ("1.0", "1.1") |
---|
[b2e077a] | 104 | # XXX: We should query ourself for the supported operations |
---|
[1a63bf7] | 105 | |
---|
[b2e077a] | 106 | @property |
---|
| 107 | def operations_supported(self): |
---|
[cad7502] | 108 | return 0xa # get-jobs |
---|
[b2e077a] | 109 | |
---|
| 110 | @property |
---|
| 111 | def charset_configured(self): |
---|
[1a63bf7] | 112 | return "utf-8" |
---|
[b2e077a] | 113 | |
---|
| 114 | @property |
---|
| 115 | def charset_supported(self): |
---|
[1a63bf7] | 116 | return "utf-8" |
---|
[b2e077a] | 117 | |
---|
| 118 | @property |
---|
| 119 | def natural_language_configured(self): |
---|
[1a63bf7] | 120 | return "en-us" |
---|
[b2e077a] | 121 | |
---|
| 122 | @property |
---|
| 123 | def generated_natural_language_supported(self): |
---|
[1a63bf7] | 124 | return "en-us" |
---|
[b2e077a] | 125 | |
---|
| 126 | @property |
---|
| 127 | def document_format_default(self): |
---|
[1a63bf7] | 128 | return "application/octet-stream" |
---|
[b2e077a] | 129 | |
---|
| 130 | @property |
---|
| 131 | def document_format_supported(self): |
---|
[1a63bf7] | 132 | return ("application/octet-stream", "audio/mp3") |
---|
[b2e077a] | 133 | |
---|
| 134 | @property |
---|
| 135 | def printer_is_accepting_jobs(self): |
---|
[1a63bf7] | 136 | return True |
---|
[b2e077a] | 137 | |
---|
| 138 | @property |
---|
| 139 | def queued_job_count(self): |
---|
[1a63bf7] | 140 | return len(self.active_jobs) |
---|
[b2e077a] | 141 | |
---|
| 142 | @property |
---|
| 143 | def pdl_override_supported(self): |
---|
[1a63bf7] | 144 | return "not-attempted" |
---|
[b2e077a] | 145 | |
---|
| 146 | @property |
---|
| 147 | def printer_up_time(self): |
---|
[1a63bf7] | 148 | return int(time.time()) - self.time_created |
---|
[b2e077a] | 149 | |
---|
| 150 | @property |
---|
| 151 | def compression_supported(self): |
---|
[1a63bf7] | 152 | return "none" |
---|
[b2e077a] | 153 | |
---|
| 154 | def get_printer_attributes(self, request): |
---|
[1a63bf7] | 155 | attributes = [(attr, getattr(self, attr)) for attr in self.attributes] |
---|
[6effd50] | 156 | attributes = map(lambda x: x if isinstance(x[1], (tuple, list)) else (x[0], [x[1]]), |
---|
| 157 | attributes) |
---|
[b2e077a] | 158 | return attributes |
---|
| 159 | |
---|
| 160 | ## Printer operations |
---|
[776a659] | 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 |
---|
[5d24a81] | 170 | job.enqueue(self, jobid) |
---|
[776a659] | 171 | return jobid |
---|
| 172 | |
---|
| 173 | def complete_job(self, jobid): |
---|
[5d24a81] | 174 | job = self.jobs[self.active_jobs.pop(0)] |
---|
[776a659] | 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): |
---|
[5d24a81] | 185 | job = self.jobs[self.active_jobs[0]] |
---|
[776a659] | 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 | |
---|
[b2e077a] | 203 | def get_jobs(self): |
---|
| 204 | jobs = [self.jobs[job_id] for job_id in self.active_jobs] |
---|
| 205 | return jobs |
---|
| 206 | |
---|
[776a659] | 207 | def __repr__(self): |
---|
| 208 | return str(self) |
---|
| 209 | |
---|
| 210 | def __str__(self): |
---|
[1a63bf7] | 211 | return "<Printer '%s'>" % self.name |
---|