1 | import alsaaudio as aa |
---|
2 | import gutenbach.ipp |
---|
3 | from gutenbach.ipp.attribute import Attribute |
---|
4 | from .exceptions import InvalidJobException, \ |
---|
5 | InvalidPrinterStateException |
---|
6 | |
---|
7 | class 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 = -1 |
---|
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.jobid = jobid |
---|
43 | job.enqueue(self) |
---|
44 | return jobid |
---|
45 | |
---|
46 | def complete_job(self, jobid): |
---|
47 | job = self.active_jobs.pop(0) |
---|
48 | if job.jobid != jobid: |
---|
49 | raise InvalidJobException( |
---|
50 | "Completed job %d has unexpected job id %d!" % \ |
---|
51 | (job.jobid, jobid)) |
---|
52 | |
---|
53 | self.finished_jobs.append(job) |
---|
54 | job.finish() |
---|
55 | return job.jobid |
---|
56 | |
---|
57 | def start_job(self, jobid): |
---|
58 | job = self.active_jobs[0] |
---|
59 | if job.jobid != jobid: |
---|
60 | raise InvalidJobException( |
---|
61 | "Completed job %d has unexpected job id %d!" % \ |
---|
62 | (job.jobid, jobid)) |
---|
63 | |
---|
64 | if job.status == 'playing': |
---|
65 | raise InvalidPrinterStateException( |
---|
66 | "Next job in queue (id %d) is " + \ |
---|
67 | "already playing!" % jobid) |
---|
68 | |
---|
69 | job.play() |
---|
70 | |
---|
71 | def get_job(self, jobid): |
---|
72 | if jobid not in self.jobs: |
---|
73 | raise InvalidJobException(jobid) |
---|
74 | return self.jobs[jobid] |
---|
75 | |
---|
76 | def __repr__(self): |
---|
77 | return str(self) |
---|
78 | |
---|
79 | def __str__(self): |
---|
80 | return "<Printer '%s'>" % self.name |
---|