source: server/lib/gutenbach/server/printer.py @ 5fe360e

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

Converting server code to rely more on the ipp/ code

  • Property mode set to 100644
File size: 5.1 KB
RevLine 
[b2e077a]1#import alsaaudio as aa
[d04a689]2from .exceptions import InvalidJobException, InvalidPrinterStateException
3from gutenbach.ipp.attribute import Attribute
[b2e077a]4import gutenbach.ipp as ipp
5import gutenbach.ipp.constants as const
6import logging
7import time
[d04a689]8
9# initialize logger
10logger = logging.getLogger(__name__)
[776a659]11
[b2e077a]12class 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:
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
[b2e077a]77    ## Printer attributes
[1a63bf7]78
[b2e077a]79    @property
80    def printer_uri_supported(self):
[1a63bf7]81        return self.uri
82
[b2e077a]83    @property
84    def uri_authentication_supported(self):
[1a63bf7]85        return "none"
[b2e077a]86
87    @property
88    def uri_security_supported(self):
[1a63bf7]89        return "none"
[b2e077a]90
91    @property
92    def printer_name(self):
[1a63bf7]93        return self.name
94
[b2e077a]95    @property
96    def printer_state(self):
[1a63bf7]97        return self.state
98
[b2e077a]99    @property
100    def printer_state_reasons(self):
[1a63bf7]101        return "none"
[b2e077a]102
103    @property
104    def ipp_versions_supported(self):
[1a63bf7]105        return ("1.0", "1.1")
[b2e077a]106    # XXX: We should query ourself for the supported operations
[1a63bf7]107
[b2e077a]108    @property
109    def operations_supported(self):
[1a63bf7]110        return "get-jobs"
[b2e077a]111
112    @property
113    def charset_configured(self):
[1a63bf7]114        return "utf-8"
[b2e077a]115
116    @property
117    def charset_supported(self):
[1a63bf7]118        return "utf-8"
[b2e077a]119
120    @property
121    def natural_language_configured(self):
[1a63bf7]122        return "en-us"
[b2e077a]123
124    @property
125    def generated_natural_language_supported(self):
[1a63bf7]126        return "en-us"
[b2e077a]127
128    @property
129    def document_format_default(self):
[1a63bf7]130        return "application/octet-stream"
[b2e077a]131
132    @property
133    def document_format_supported(self):
[1a63bf7]134        return ("application/octet-stream", "audio/mp3")
[b2e077a]135
136    @property
137    def printer_is_accepting_jobs(self):
[1a63bf7]138        return True
[b2e077a]139
140    @property
141    def queued_job_count(self):
[1a63bf7]142        return len(self.active_jobs)
[b2e077a]143
144    @property
145    def pdl_override_supported(self):
[1a63bf7]146        return "not-attempted"
[b2e077a]147
148    @property
149    def printer_up_time(self):
[1a63bf7]150        return int(time.time()) - self.time_created
[b2e077a]151
152    @property
153    def compression_supported(self):
[1a63bf7]154        return "none"
[b2e077a]155
156    def get_printer_attributes(self, request):
[1a63bf7]157        attributes = [(attr, getattr(self, attr)) for attr in self.attributes]
[5fe360e]158        attributes = map(lambda x: x if isinstance(x, (tuple, list)) else [x], attributes)
[b2e077a]159        return attributes
160
161    ## Printer operations
[776a659]162    @property
163    def next_jobid(self):
164        self._next_jobid += 1
165        return self._next_jobid
166
167    def print_job(self, job):
168        jobid = self.next_jobid
169        self.active_jobs.append(jobid)
170        self.jobs[jobid] = job
[5d24a81]171        job.enqueue(self, jobid)
[776a659]172        return jobid
173
174    def complete_job(self, jobid):
[5d24a81]175        job = self.jobs[self.active_jobs.pop(0)]
[776a659]176        if job.jobid != jobid:
177            raise InvalidJobException(
178                "Completed job %d has unexpected job id %d!" % \
179                (job.jobid, jobid))
180       
181        self.finished_jobs.append(job)
182        job.finish()
183        return job.jobid
184
185    def start_job(self, jobid):
[5d24a81]186        job = self.jobs[self.active_jobs[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        if job.status == 'playing':
193            raise InvalidPrinterStateException(
194                "Next job in queue (id %d) is " + \
195                "already playing!" % jobid)
196
197        job.play()
198
199    def get_job(self, jobid):
200        if jobid not in self.jobs:
201            raise InvalidJobException(jobid)
202        return self.jobs[jobid]
203
[b2e077a]204    def get_jobs(self):
205        jobs = [self.jobs[job_id] for job_id in self.active_jobs]
206        return jobs           
207
[776a659]208    def __repr__(self):
209        return str(self)
210
211    def __str__(self):
[1a63bf7]212        return "<Printer '%s'>" % self.name
Note: See TracBrowser for help on using the repository browser.