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