source: server/lib/gutenbach/server/printer.py @ f6e2532

no-cups
Last change on this file since f6e2532 was f6e2532, checked in by Jessica B. Hamrick <jhamrick@…>, 12 years ago

Constants; small changes in printer

  • Property mode set to 100644
File size: 5.3 KB
Line 
1#import alsaaudio as aa
2from .exceptions import InvalidJobException, InvalidPrinterStateException
3from gutenbach.ipp.attribute import Attribute
4import gutenbach.ipp as ipp
5import gutenbach.ipp.constants as const
6import logging
7import time
8
9# initialize logger
10logger = logging.getLogger(__name__)
11
12class 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        "multiple-operation-time-out",
36        "multiple-document-jobs-supported",
37    ]
38
39    operations = [
40        "print-job",
41        "complete-job",
42        "start-job",
43        "get-job",
44        "get-jobs",
45    ]
46       
47
48    #def __init__(self, name, card, mixer):
49    def __init__(self, name):
50
51        self.name = name
52        self.uri = "ipp://localhost:8000/printers/" + self.name
53        self.time_created = int(time.time())
54        self.state = "idle"
55
56        # if card >= len(aa.cards()):
57        #     raise aa.ALSAAudioError(
58        #       "Audio card at index %d does not exist!" % card)
59        # elif mixer not in aa.mixers(card):
60        #     raise aa.ALSAAudioError(
61        #       "Audio mixer '%s' does not exist!" % mixer)
62       
63        # self.card = card
64        # self.mixer = mixer
65
66        self.finished_jobs = []
67        self.active_jobs = []
68        self.jobs = {}
69
70        self._next_jobid = 0
71
72    def __getattr__(self, attr):
73        try:
74            return self.__getattribute__(attr)
75        except AttributeError:
76            pass
77        return self.__getattribute__(attr.replace("-", "_"))
78
79    def __hasattr__(self, attr):
80        try:
81            getattr(self, attr)
82            return True
83        except AttributeError:
84            return False
85
86    ## Printer attributes
87
88    @property
89    def printer_uri_supported(self):
90        return self.uri
91
92    @property
93    def uri_authentication_supported(self):
94        return "none"
95
96    @property
97    def uri_security_supported(self):
98        return "none"
99
100    @property
101    def printer_name(self):
102        return self.name
103
104    @property
105    def printer_state(self):
106        return 3 # idle
107
108    @property
109    def printer_state_reasons(self):
110        return "none"
111
112    @property
113    def ipp_versions_supported(self):
114        return ("1.0", "1.1")
115
116    # XXX: We should query ourself for the supported operations
117    @property
118    def operations_supported(self):
119        return ipp.Operations.GET_JOBS # get-jobs
120
121    @property
122    def charset_configured(self):
123        return "utf-8"
124
125    @property
126    def charset_supported(self):
127        return "utf-8"
128
129    @property
130    def natural_language_configured(self):
131        return "en-us"
132
133    @property
134    def generated_natural_language_supported(self):
135        return "en-us"
136
137    @property
138    def document_format_default(self):
139        return "application/octet-stream"
140
141    @property
142    def document_format_supported(self):
143        return ("application/octet-stream", "audio/mp3")
144
145    @property
146    def printer_is_accepting_jobs(self):
147        return True
148
149    @property
150    def queued_job_count(self):
151        return len(self.active_jobs)
152
153    @property
154    def pdl_override_supported(self):
155        return "not-attempted"
156
157    @property
158    def printer_up_time(self):
159        return int(time.time()) - self.time_created
160
161    @property
162    def compression_supported(self):
163        return "none"
164
165    @property
166    def multiple_operation_time_out(self):
167        return 240
168
169    @property
170    def multiple_document_jobs_supported(self):
171        return False
172
173    def get_printer_attributes(self, request):
174        attributes = [(attr, getattr(self, attr)) for attr in self.attributes]
175        attributes = map(lambda x: x if isinstance(x[1], (tuple, list)) else (x[0], [x[1]]),
176                         attributes)
177        return attributes
178
179    ## Printer operations
180
181    def print_job(self, job):
182        jobid = self._next_jobid
183        self._next_jobid += 1
184        self.active_jobs.append(jobid)
185        self.jobs[jobid] = job
186        job.enqueue(self, jobid)
187        return jobid
188
189    def complete_job(self, jobid):
190        job = self.jobs[self.active_jobs.pop(0)]
191        if job.jobid != jobid:
192            raise InvalidJobException(
193                "Completed job %d has unexpected job id %d!" % \
194                (job.jobid, jobid))
195       
196        self.finished_jobs.append(job)
197        job.finish()
198        return job.jobid
199
200    def start_job(self, jobid):
201        job = self.jobs[self.active_jobs[0]]
202        if job.jobid != jobid:
203            raise InvalidJobException(
204                "Completed job %d has unexpected job id %d!" % \
205                (job.jobid, jobid))
206
207        if job.status == 'playing':
208            raise InvalidPrinterStateException(
209                "Next job in queue (id %d) is " + \
210                "already playing!" % jobid)
211
212        job.play()
213
214    def get_job(self, jobid):
215        if jobid not in self.jobs:
216            raise InvalidJobException(jobid)
217        return self.jobs[jobid]
218
219    def get_jobs(self):
220        jobs = [self.jobs[job_id] for job_id in self.active_jobs]
221        return jobs           
222
223    def __repr__(self):
224        return str(self)
225
226    def __str__(self):
227        return "<Printer '%s'>" % self.name
Note: See TracBrowser for help on using the repository browser.