source: server/lib/gutenbach/server/job.py @ b01b6d1

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

Clean up printer, job, and requests code

  • Property mode set to 100644
File size: 6.0 KB
RevLine 
[e58af05]1from . import InvalidJobException, InvalidPrinterStateException
[b01b6d1]2from gutenbach.ipp import JobStates as States
[776a659]3import os
[793432f]4import gutenbach.ipp as ipp
[ee8e6d0]5import logging
[ce2abc5]6import subprocess
7import time
[d04a689]8
9# initialize logger
10logger = logging.getLogger(__name__)
[776a659]11
12class Job(object):
13
[1a63bf7]14    # for IPP
[b2e077a]15    attributes = [
16        "job-id",
17        "job-name",
18        "job-originating-user-name",
19        "job-k-octets",
20        "job-state",
21        "job-printer-uri"
[1a63bf7]22    ]
[b2e077a]23
[b01b6d1]24    def __init__(self, job_id=-1, printer=None, creator=None, name=None, size=None):
25        """Create an empty Gutenbach job.
[e58af05]26
[776a659]27        """
[ee8e6d0]28
[b01b6d1]29        self.printer = printer
30        self.player = None
[ee8e6d0]31
[b01b6d1]32        self.id = job_id
33        self.creator = creator
34        self.name = name
35        self.size = size
36        self.status = States.HELD
[ee8e6d0]37
[e58af05]38        self.document = None
39        self.document_name = None
[b01b6d1]40        self.document_format = None
41        self.document_natural_language = None
42        self.compression = None
43   
44    def __repr__(self):
45        return str(self)
46
47    def __str__(self):
48        return "<Job %d '%s'>" % (self.id, self.name)
[e58af05]49
[b01b6d1]50    ######################################################################
51    ###                          Properties                            ###
52    ######################################################################
[1a63bf7]53
[b01b6d1]54    @property
55    def id(self):
56        """Unique job identifier.  Should be a positive integer,
57        except when unassigned, when it defaults to -1.
58       
59        """
60        return self._id
61    @id.setter
62    def id(self, val):
[1a63bf7]63        try:
[b01b6d1]64            self._id = int(val)
65        except TypeError:
66            self._id = -1
67
68    @property
69    def creator(self):
70        """The user who created the job; analogous to the IPP
71        requesting-user-name.
72
73        """
74        return self._creator
75    @creator.setter
76    def creator(self, val):
77        if val is None:
78            self._creator = ""
79        else:
80            self._creator = str(val)
81
82    @property
83    def name(self):
84        """The job's name.
85
86        """
87        return self._name
88    @name.setter
89    def name(self, val):
90        if val is None:
91            self._name = ""
92        else:
93            self._name = str(val)
94
95    @property
96    def size(self):
97        """The size of the job in bytes.
[1a63bf7]98
[b01b6d1]99        """
100        if self.document:
101            size = os.path.getsize(self.document.name)
102        else:
103            size = self._size
104        return size
105    @size.setter
106    def size(self, val):
[ee8e6d0]107        try:
[b01b6d1]108            self._size = int(val)
109        except TypeError:
110            self._size = 0
111
112    ######################################################################
113    ###                            Methods                             ###
114    ######################################################################
115
116    def play(self):
117        logger.info("playing job %s" % str(self))
118        # TODO: add external call to music player
119        self.status = States.PROCESSING
120        self.player = subprocess.Popen(
121            "/usr/bin/mplayer -quiet %s" % self.document.name,
122            shell=True)
123            #stderr=subprocess.PIPE,
124            #stdout=subprocess.PIPE)
125        while self.player.poll() is None:
126            time.sleep(0.1)
127        logger.info("mplayer finished with code %d" % self.player.returncode)
128        #if self.player.returncode < 0:
129        #    logger.error(self.player.stderr)
130        #logger.debug(self.player.stdout)
131        self.player = None
132        self.printer.complete_job(self.id)
133
134    def finish(self):
135        logger.info("finished job %s" % str(self))
136        self.status = States.COMPLETE
[1a63bf7]137
[b01b6d1]138    ######################################################################
139    ###                        IPP Attributes                          ###
140    ######################################################################
[776a659]141
142    @property
[b2e077a]143    def job_id(self):
[b01b6d1]144        return ipp.JobId(self.id)
[b2e077a]145
146    @property
147    def job_name(self):
[793432f]148        return ipp.JobName(self.name)
[776a659]149
[b2e077a]150    # XXX: we need to actually calculate this!
151    @property
152    def job_originating_user_name(self):
[ee8e6d0]153        return ipp.JobOriginatingUserName(self.creator)
[776a659]154
[b2e077a]155    # XXX: we need to actually calculate this!
156    @property
157    def job_k_octets(self):
[ee8e6d0]158        return ipp.JobKOctets(self.size)
[b2e077a]159
160    @property
161    def job_state(self):
[793432f]162        return ipp.JobState(self.status)
[b2e077a]163
164    @property
165    def job_printer_uri(self):
[793432f]166        return ipp.JobPrinterUri(self.printer.uri)
[b2e077a]167
[b01b6d1]168
169    ######################################################################
170    ###                        IPP Operations                          ###
171    ######################################################################
172
173    def cancel_job(self):
174        pass
175
176    def send_document(self,
177                      document,
178                      document_name=None,
179                      document_format=None,
180                      document_natural_language=None,
181                      requesting_user_name=None,
182                      compression=None,
183                      last_document=None):
184
185        if self.status != States.HELD:
186            raise InvalidJobStateException(self.status)
187       
188        self.document = document
189        self.document_name = str(document_name)
190        self.document_format = str(document_format)
191        self.document_natural_language = str(document_natural_language)
192        self.creator = str(requesting_user_name)
193        self.compression = str(compression)
194        self.status = States.PENDING
195
196        logger.debug("document for job %d is '%s'" % (self.id, self.document_name))
197
198    def send_uri(self):
199        pass
200
201    def get_job_attributes(self, requested_attributes=None):
202        if requested_attributes is None:
[ee8e6d0]203            requested = self.attributes
[b01b6d1]204        else:
205            requested = [a for a in self.attributes if a in requested_attributes]
[776a659]206
[b01b6d1]207        _attributes = [attr.replace("-", "_") for attr in requested]
208        attributes = [getattr(self, attr) for attr in _attributes]
209        return attributes
[776a659]210
[b01b6d1]211    def set_job_attributes(self):
212        pass
[776a659]213
[b01b6d1]214    def restart_job(self):
215        pass
[776a659]216
[b01b6d1]217    def promote_job(self):
218        pass
Note: See TracBrowser for help on using the repository browser.