[e58af05] | 1 | from . import InvalidJobException, InvalidPrinterStateException |
---|
[776a659] | 2 | import os |
---|
[793432f] | 3 | import gutenbach.ipp as ipp |
---|
[ee8e6d0] | 4 | import logging |
---|
[ce2abc5] | 5 | import subprocess |
---|
| 6 | import time |
---|
[d04a689] | 7 | |
---|
| 8 | # initialize logger |
---|
| 9 | logger = logging.getLogger(__name__) |
---|
[776a659] | 10 | |
---|
| 11 | class Job(object): |
---|
| 12 | |
---|
[1a63bf7] | 13 | # for IPP |
---|
[b2e077a] | 14 | attributes = [ |
---|
| 15 | "job-id", |
---|
| 16 | "job-name", |
---|
| 17 | "job-originating-user-name", |
---|
| 18 | "job-k-octets", |
---|
| 19 | "job-state", |
---|
| 20 | "job-printer-uri" |
---|
[1a63bf7] | 21 | ] |
---|
[b2e077a] | 22 | |
---|
[ee8e6d0] | 23 | def __init__(self, jid, printer, creator="", name="", size=0): |
---|
[776a659] | 24 | """Initialize a Gutenbach job. |
---|
| 25 | |
---|
| 26 | This sets the status to 'initializing' and optionally sets the |
---|
| 27 | document to print to the value of document. |
---|
[e58af05] | 28 | |
---|
[776a659] | 29 | """ |
---|
[ee8e6d0] | 30 | |
---|
| 31 | self.jid = jid |
---|
| 32 | self.printer = printer |
---|
| 33 | |
---|
| 34 | self.creator = creator |
---|
| 35 | self.name = name |
---|
| 36 | self.size = size |
---|
| 37 | |
---|
[e58af05] | 38 | self.document = None |
---|
| 39 | self.document_name = None |
---|
[ce2abc5] | 40 | self.player = None |
---|
[e58af05] | 41 | |
---|
| 42 | self.status = ipp.JobStates.HELD |
---|
[1a63bf7] | 43 | |
---|
| 44 | def __getattr__(self, attr): |
---|
| 45 | try: |
---|
[ee8e6d0] | 46 | return self.__getattribute__(attr) |
---|
[1a63bf7] | 47 | except AttributeError: |
---|
| 48 | pass |
---|
[ee8e6d0] | 49 | return self.__getattribute__(attr.replace("-", "_")) |
---|
[1a63bf7] | 50 | |
---|
| 51 | def __hasattr__(self, attr): |
---|
[ee8e6d0] | 52 | try: |
---|
| 53 | getattr(self, attr) |
---|
| 54 | return True |
---|
| 55 | except AttributeError: |
---|
| 56 | return False |
---|
[1a63bf7] | 57 | |
---|
| 58 | #### Job attributes |
---|
[776a659] | 59 | |
---|
| 60 | @property |
---|
[b2e077a] | 61 | def job_id(self): |
---|
[793432f] | 62 | return ipp.JobId(self.jid) |
---|
[b2e077a] | 63 | |
---|
| 64 | @property |
---|
| 65 | def job_name(self): |
---|
[793432f] | 66 | return ipp.JobName(self.name) |
---|
[776a659] | 67 | |
---|
[b2e077a] | 68 | # XXX: we need to actually calculate this! |
---|
| 69 | @property |
---|
| 70 | def job_originating_user_name(self): |
---|
[ee8e6d0] | 71 | return ipp.JobOriginatingUserName(self.creator) |
---|
[776a659] | 72 | |
---|
[b2e077a] | 73 | # XXX: we need to actually calculate this! |
---|
| 74 | @property |
---|
| 75 | def job_k_octets(self): |
---|
[ee8e6d0] | 76 | return ipp.JobKOctets(self.size) |
---|
[b2e077a] | 77 | |
---|
| 78 | @property |
---|
| 79 | def job_state(self): |
---|
[793432f] | 80 | return ipp.JobState(self.status) |
---|
[b2e077a] | 81 | |
---|
| 82 | @property |
---|
| 83 | def job_printer_uri(self): |
---|
[793432f] | 84 | return ipp.JobPrinterUri(self.printer.uri) |
---|
[b2e077a] | 85 | |
---|
[ee8e6d0] | 86 | def get_job_attributes(self, request=None): |
---|
| 87 | if request and 'requested-attributes' in request: |
---|
| 88 | requested = [] |
---|
| 89 | for value in request['requested-attributes'].values: |
---|
| 90 | if value.value in self.attributes: |
---|
| 91 | requested.append(value.value) |
---|
| 92 | else: |
---|
| 93 | requested = self.attributes |
---|
| 94 | |
---|
| 95 | attributes = [getattr(self, attr) for attr in requested] |
---|
[b2e077a] | 96 | return attributes |
---|
| 97 | |
---|
| 98 | ####### |
---|
[776a659] | 99 | |
---|
| 100 | def play(self): |
---|
[e58af05] | 101 | logger.info("playing job %s" % str(self)) |
---|
[776a659] | 102 | # TODO: add external call to music player |
---|
[e58af05] | 103 | self.status = ipp.JobStates.PROCESSING |
---|
[ce2abc5] | 104 | self.player = subprocess.Popen( |
---|
| 105 | "/usr/bin/mplayer -quiet %s" % self.document.name, |
---|
| 106 | shell=True) |
---|
| 107 | #stderr=subprocess.PIPE, |
---|
| 108 | #stdout=subprocess.PIPE) |
---|
| 109 | while self.player.poll() is None: |
---|
| 110 | time.sleep(0.1) |
---|
| 111 | logger.info("mplayer finished with code %d" % self.player.returncode) |
---|
| 112 | #if self.player.returncode < 0: |
---|
| 113 | # logger.error(self.player.stderr) |
---|
| 114 | #logger.debug(self.player.stdout) |
---|
| 115 | self.player = None |
---|
[1a63bf7] | 116 | self.printer.complete_job(self.jid) |
---|
[776a659] | 117 | |
---|
[5d24a81] | 118 | def finish(self): |
---|
[e58af05] | 119 | logger.info("finished job %s" % str(self)) |
---|
| 120 | self.status = ipp.JobStates.COMPLETE |
---|
[776a659] | 121 | |
---|
| 122 | def __repr__(self): |
---|
| 123 | return str(self) |
---|
| 124 | |
---|
| 125 | def __str__(self): |
---|
[ee8e6d0] | 126 | return "<Job %d '%s'>" % (self.jid if self.jid is not None else -1, self.name) |
---|