source: server/lib/gutenbach/server/requests.py @ 6ed9d7a

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

Add make_get_jobs_response to ipp/operations, add documentation to server/requests

  • Property mode set to 100644
File size: 14.0 KB
Line 
1from gutenbach.server.exceptions import MalformedIPPRequestException
2from gutenbach.server.printer import GutenbachPrinter
3import gutenbach.ipp as ipp
4import gutenbach.ipp.constants as const
5import logging
6
7# initialize logger
8logger = logging.getLogger(__name__)
9
10def handler_for(operation):
11    """A decorator method to mark a function with the operation id
12    that it handles.  This value will be stored in
13    'func.ipp_operation'.
14
15    """
16   
17    def f(func):
18        func.ipp_operation = operation
19        return func
20    return f
21
22class GutenbachRequestHandler(object):
23
24    def __init__(self):
25        self.printers = {
26            "test": GutenbachPrinter(name="test")
27            }
28        self.default = "test"
29   
30    def handle(self, request, response):
31        # look up the handler
32        handler = None
33        handler_name = None
34        for d in dir(self):
35            if getattr(getattr(self, d), "ipp_operation", None) == request.operation_id:
36                handler_name = d
37                break
38        # we couldn't find a handler, so default to unknown operation
39        if handler_name is None:
40            handler_name = "unknown_operation"
41        # call the handler
42        handler = getattr(self, handler_name)
43        logger.info("Handling request of type '%s'" % handler_name)
44        handler(request, response)
45
46    def unknown_operation(self, request, response):
47        logger.warning("Received unknown operation 0x%x" % request.operation_id)
48        response.operation_id = const.StatusCodes.OPERATION_NOT_SUPPORTED
49
50    ##### Helper functions
51
52    def _get_printer_attributes(self, printer, request, response):
53        response.attribute_groups.append(ipp.AttributeGroup(
54            const.AttributeTags.PRINTER,
55            printer.get_printer_attributes(request)))
56
57    def _get_job_attributes(self, job_id, printer, request, response):
58        response.attribute_groups.append(ipp.AttributeGroup(
59            const.AttributeTags.JOB,
60            job.get_job_attributes(request)))
61
62    def _get_printer_name(self, request):
63        # make sure the first group is an OPERATION group
64        group_tag = request.attribute_groups[0].tag
65        if group_tag != const.AttributeTags.OPERATION:
66            raise MalformedIPPRequestException, \
67                  "Expected OPERATION group tag, got %d\n", group_tag
68
69        # make sure the printer-uri value is appropriate
70        printer_name_attr = request.attribute_groups[0]['printer-uri']
71        printer_name_value_tag = printer_name_attr.values[0].value_tag
72        if printer_name_value_tag != const.CharacterStringTags.URI:
73            raise MalformedIPPRequestException, \
74                  "Expected URI value tag, got %s" % printer_name_value_tag
75
76        # actually get the printer name
77        printer_name_value = printer_name_attr.values[0].value
78        # XXX: hack -- CUPS will strip the port from the request, so
79        # we can't do an exact comparison (also the hostname might be
80        # different, depending on the CNAME or whether it's localhost)
81        printer_name = printer_name_value.split("/")[-1]
82
83        # make sure the printer name is valid
84        if printer_name not in self.printers:
85            raise ValueError, "Invalid printer uri: %s" % printer_name_value
86
87        return printer_name
88
89    def _get_job_id(self, request):
90        pass
91       
92    ##### Printer Commands
93
94    def print_job(self, request, response):
95        pass
96
97    def validate_job(self, request, response):
98        pass
99
100    @handler_for(const.Operations.GET_JOBS)
101    def get_jobs(self, request, response):
102        """RFC 2911: 3.2.6 Get-Jobs Operation
103       
104        This REQUIRED operation allows a client to retrieve the list
105        of Job objects belonging to the target Printer object. The
106        client may also supply a list of Job attribute names and/or
107        attribute group names. A group of Job object attributes will
108        be returned for each Job object that is returned.
109
110        This operation is similar to the Get-Job-Attributes operation,
111        except that this Get-Jobs operation returns attributes from
112        possibly more than one object.
113
114        """
115
116        printer_name = self._get_printer_name(request)
117        # Each job will append a new job attribute group.
118        for job in self.printers[printer_name].get_jobs():
119            self._get_job_attributes(job, request, response)
120
121    def print_uri(self, request, response):
122        pass
123
124    def create_job(self, request, response):
125        pass
126
127    def pause_printer(self, request, response):
128        pass
129
130    def resume_printer(self, request, response):
131        pass
132
133    @handler_for(const.Operations.GET_PRINTER_ATTRIBUTES)
134    def get_printer_attributes(self, request, response):
135        # this is just like cups_get_default, except the printer name
136        # is given
137        printer_name = self._get_printer_name(request)
138        self._get_printer_attributes(self.printers[printer_name], request, response)
139
140    def set_printer_attributes(self, request, response):
141        pass
142
143    ##### Job Commands
144
145    def cancel_job(self, request, response):
146        pass
147
148    def send_document(self, request, response):
149        pass
150
151    def send_uri(self, request, response):
152        pass
153
154    def get_job_attributes(self, request, response):
155        printer_name = self._get_printer_name(request)
156        job_id = self._get_job_id(request)
157        self._get_job_attributes(
158            self.printers[printer_name].get_job(job_id), request, response)
159
160    def set_job_attributes(self, request, response):
161        pass
162
163    def cups_get_document(self, request, response):
164        pass
165
166    def restart_job(self, request, response):
167        pass
168
169    def promote_job(self, request, response):
170        pass
171
172    ##### CUPS Specific Commands
173
174    @handler_for(const.Operations.CUPS_GET_DEFAULT)
175    def cups_get_default(self, request, response):
176        """The CUPS-Get-Default operation (0x4001) returns the default
177        printer URI and attributes.
178
179        CUPS-Get-Default Request
180        ------------------------
181
182        The following groups of attributes are supplied as part of the
183        CUPS-Get-Default request:
184
185        Group 1: Operation Attributes
186            Natural Language and Character Set:
187                The 'attributes-charset' and
188                'attributes-natural-language' attributes as described
189                in section 3.1.4.1 of the IPP Model and Semantics
190                document.
191            'requested-attributes' (1setOf keyword):
192                The client OPTIONALLY supplies a set of attribute
193                names and/or attribute group names in whose values the
194                requester is interested. If the client omits this
195                attribute, the server responds as if this attribute
196                had been supplied with a value of 'all'.
197       
198        CUPS-Get-Default Response
199        -------------------------
200
201        The following groups of attributes are send as part of the
202        CUPS-Get-Default Response:
203
204        Group 1: Operation Attributes
205            Status Message:
206                The standard response status message.
207            Natural Language and Character Set:
208                The 'attributes-charset' and
209                'attributes-natural-language' attributes as described
210                in section 3.1.4.2 of the IPP Model and Semantics
211                document.
212
213        Group 2: Printer Object Attributes
214            The set of requested attributes and their current values.
215
216        (Source: http://www.cups.org/documentation.php/spec-ipp.html#CUPS_GET_DEFAULT )
217
218        """
219           
220        self._get_printer_attributes(self.printers[self.default], request, response)
221
222    @handler_for(const.Operations.CUPS_GET_PRINTERS)
223    def cups_get_printers(self, request, response):
224        """
225        The CUPS-Get-Printers operation (0x4002) returns the printer
226        attributes for every printer known to the system. This may
227        include printers that are not served directly by the server.
228
229        CUPS-Get-Printers Request
230        -------------------------
231       
232        The following groups of attributes are supplied as part of the
233        CUPS-Get-Printers request:
234
235        Group 1: Operation Attributes
236            Natural Language and Character Set:
237                The 'attributes-charset' and
238                'attributes-natural-language' attributes as described
239                in section 3.1.4.1 of the IPP Model and Semantics
240                document.
241            'first-printer-name' (name(127)):CUPS 1.2/Mac OS X 10.5
242                The client OPTIONALLY supplies this attribute to
243                select the first printer that is returned.
244            'limit' (integer (1:MAX)):
245                The client OPTIONALLY supplies this attribute limiting
246                the number of printers that are returned.
247            'printer-location' (text(127)): CUPS 1.1.7
248                The client OPTIONALLY supplies this attribute to
249                select which printers are returned.
250            'printer-type' (type2 enum): CUPS 1.1.7
251                The client OPTIONALLY supplies a printer type
252                enumeration to select which printers are returned.
253            'printer-type-mask' (type2 enum): CUPS 1.1.7
254                The client OPTIONALLY supplies a printer type mask
255                enumeration to select which bits are used in the
256                'printer-type' attribute.
257            'requested-attributes' (1setOf keyword) :
258                The client OPTIONALLY supplies a set of attribute
259                names and/or attribute group names in whose values the
260                requester is interested. If the client omits this
261                attribute, the server responds as if this attribute
262                had been supplied with a value of 'all'.
263            'requested-user-name' (name(127)) : CUPS 1.2/Mac OS X 10.5
264                The client OPTIONALLY supplies a user name that is
265                used to filter the returned printers.
266
267        CUPS-Get-Printers Response
268        --------------------------
269
270        The following groups of attributes are send as part of the
271        CUPS-Get-Printers Response:
272
273        Group 1: Operation Attributes
274            Status Message:
275                The standard response status message.
276            Natural Language and Character Set:
277                The 'attributes-charset' and
278                'attributes-natural-language' attributes as described
279                in section 3.1.4.2 of the IPP Model and Semantics
280                document.
281               
282        Group 2: Printer Object Attributes
283            The set of requested attributes and their current values
284            for each printer.
285
286        (Source: http://www.cups.org/documentation.php/spec-ipp.html#CUPS_GET_PRINTERS )
287           
288        """
289
290        # Each printer will append a new printer attribute group.
291        for printer in self.printers:
292            self._get_printer_attributes(self.printers[printer], request, response)
293
294    @handler_for(const.Operations.CUPS_GET_CLASSES)
295    def cups_get_classes(self, request, response):
296        """The CUPS-Get-Classes operation (0x4005) returns the printer
297        attributes for every printer class known to the system. This
298        may include printer classes that are not served directly by
299        the server.
300
301        CUPS-Get-Classes Request
302        ------------------------
303
304        The following groups of attributes are supplied as part of the
305        CUPS-Get-Classes request:
306
307        Group 1: Operation Attributes
308            Natural Language and Character Set:
309                The 'attributes-charset' and
310                'attributes-natural-language' attributes as described
311                in section 3.1.4.1 of the IPP Model and Semantics
312                document.
313            'first-printer-name' (name(127)):CUPS 1.2/Mac OS X 10.5
314                The client OPTIONALLY supplies this attribute to
315                select the first printer that is returned.
316            'limit' (integer (1:MAX)):
317                The client OPTIONALLY supplies this attribute limiting
318                the number of printer classes that are returned.
319            'printer-location' (text(127)): CUPS 1.1.7
320                The client OPTIONALLY supplies this attribute to
321                select which printer classes are returned.
322            'printer-type' (type2 enum): CUPS 1.1.7
323                The client OPTIONALLY supplies a printer type
324                enumeration to select which printer classes are
325                returned.
326            'printer-type-mask' (type2 enum): CUPS 1.1.7
327                The client OPTIONALLY supplies a printer type mask
328                enumeration to select which bits are used in the
329                'printer-type' attribute.
330            'requested-attributes' (1setOf keyword) :
331                The client OPTIONALLY supplies a set of attribute
332                names and/or attribute group names in whose values the
333                requester is interested. If the client omits this
334                attribute, the server responds as if this attribute
335                had been supplied with a value of 'all'.
336            'requested-user-name' (name(127)) : CUPS 1.2/Mac OS X 10.5
337                The client OPTIONALLY supplies a user name that is
338                used to filter the returned printers.
339               
340        CUPS-Get-Classes Response
341        -------------------------
342
343        The following groups of attributes are send as part of the
344        CUPS-Get-Classes Response:
345
346        Group 1: Operation Attributes
347            Status Message:
348                The standard response status message.
349            Natural Language and Character Set:
350                The 'attributes-charset' and
351                'attributes-natural-language' attributes as described
352                in section 3.1.4.2 of the IPP Model and Semantics
353                document.
354
355        Group 2: Printer Class Object Attributes
356            The set of requested attributes and their current values
357            for each printer class.
358
359        (Source: http://www.cups.org/documentation.php/spec-ipp.html#CUPS_GET_CLASSES )
360
361        """
362       
363        # We have no printer classes, so we don't need to do anything
364        pass
Note: See TracBrowser for help on using the repository browser.