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

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

Fix bugs in Job and Printer

  • Property mode set to 100644
File size: 1.8 KB
Line 
1import alsaaudio as aa
2import gutenbach.ipp
3from gutenbach.ipp.attribute import Attribute
4from .exceptions import InvalidJobException, \
5     InvalidPrinterStateException
6
7class Printer(object):
8
9    def __init__(self, name, card, mixer):
10
11        self.name = name
12
13        if card >= len(aa.cards()):
14            raise aa.ALSAAudioError(
15                "Audio card at index %d does not exist!" % card)
16        elif mixer not in aa.mixers(card):
17            raise aa.ALSAAudioError(
18                "Audio mixer '%s' does not exist!" % mixer)
19       
20        self.card = card
21        self.mixer = mixer
22
23        self.finished_jobs = []
24        self.active_jobs = []
25        self.jobs = {}
26
27        self._next_jobid = 0
28
29    @property
30    def next_jobid(self):
31        self._next_jobid += 1
32        return self._next_jobid
33
34    @next_jobid.setter
35    def next_jobid(self, val):
36        raise AttributeError("Setting next_jobid is illegal!")
37
38    def print_job(self, job):
39        jobid = self.next_jobid
40        self.active_jobs.append(jobid)
41        self.jobs[jobid] = job
42        job.enqueue(self, jobid)
43        return jobid
44
45    def complete_job(self, jobid):
46        job = self.jobs[self.active_jobs.pop(0)]
47        if job.jobid != jobid:
48            raise InvalidJobException(
49                "Completed job %d has unexpected job id %d!" % \
50                (job.jobid, jobid))
51       
52        self.finished_jobs.append(job)
53        job.finish()
54        return job.jobid
55
56    def start_job(self, jobid):
57        job = self.jobs[self.active_jobs[0]]
58        if job.jobid != jobid:
59            raise InvalidJobException(
60                "Completed job %d has unexpected job id %d!" % \
61                (job.jobid, jobid))
62
63        if job.status == 'playing':
64            raise InvalidPrinterStateException(
65                "Next job in queue (id %d) is " + \
66                "already playing!" % jobid)
67
68        job.play()
69
70    def get_job(self, jobid):
71        if jobid not in self.jobs:
72            raise InvalidJobException(jobid)
73        return self.jobs[jobid]
74
75    def __repr__(self):
76        return str(self)
77
78    def __str__(self):
79        return "<Printer '%s'>" % self.name
Note: See TracBrowser for help on using the repository browser.