[738d179] | 1 | from gutenbach.server.printer import GutenbachPrinter |
---|
[b2e077a] | 2 | import gutenbach.ipp as ipp |
---|
[287d6ec] | 3 | import gutenbach.ipp.constants as const |
---|
| 4 | import logging |
---|
| 5 | |
---|
[d04a689] | 6 | # initialize logger |
---|
| 7 | logger = logging.getLogger(__name__) |
---|
[287d6ec] | 8 | |
---|
| 9 | def handler_for(operation): |
---|
| 10 | """A decorator method to mark a function with the operation id |
---|
| 11 | that it handles. This value will be stored in |
---|
| 12 | 'func.ipp_operation'. |
---|
| 13 | |
---|
| 14 | """ |
---|
| 15 | |
---|
| 16 | def f(func): |
---|
| 17 | func.ipp_operation = operation |
---|
| 18 | return func |
---|
| 19 | return f |
---|
| 20 | |
---|
[b2e077a] | 21 | class GutenbachRequestHandler(object): |
---|
[287d6ec] | 22 | |
---|
[b2e077a] | 23 | def __init__(self): |
---|
| 24 | self.printers = { |
---|
| 25 | "test": GutenbachPrinter(name="test") |
---|
| 26 | } |
---|
| 27 | self.default = "test" |
---|
[287d6ec] | 28 | |
---|
[ef8df33] | 29 | def handle(self, request): |
---|
[b2e077a] | 30 | # look up the handler |
---|
| 31 | handler = None |
---|
[9eeab06] | 32 | handler_name = None |
---|
[287d6ec] | 33 | for d in dir(self): |
---|
| 34 | if getattr(getattr(self, d), "ipp_operation", None) == request.operation_id: |
---|
[9eeab06] | 35 | handler_name = d |
---|
[b2e077a] | 36 | break |
---|
| 37 | # we couldn't find a handler, so default to unknown operation |
---|
[9eeab06] | 38 | if handler_name is None: |
---|
| 39 | handler_name = "unknown_operation" |
---|
[b2e077a] | 40 | # call the handler |
---|
[9eeab06] | 41 | handler = getattr(self, handler_name) |
---|
[7bd1035] | 42 | logger.info("Handling request of type '%s'" % handler_name) |
---|
[ef8df33] | 43 | response = handler(request) |
---|
| 44 | return response |
---|
[287d6ec] | 45 | |
---|
[ef8df33] | 46 | def unknown_operation(self, request): |
---|
[9eeab06] | 47 | logger.warning("Received unknown operation 0x%x" % request.operation_id) |
---|
[ef8df33] | 48 | response = ipp.ops.make_empty_response(request) |
---|
[287d6ec] | 49 | response.operation_id = const.StatusCodes.OPERATION_NOT_SUPPORTED |
---|
[ef8df33] | 50 | return response |
---|
[0ede474] | 51 | |
---|
[9eeab06] | 52 | ##### Printer Commands |
---|
| 53 | |
---|
[ef8df33] | 54 | def print_job(self, request): |
---|
[9eeab06] | 55 | pass |
---|
| 56 | |
---|
[ef8df33] | 57 | def validate_job(self, request): |
---|
[9eeab06] | 58 | pass |
---|
| 59 | |
---|
| 60 | @handler_for(const.Operations.GET_JOBS) |
---|
[ef8df33] | 61 | def get_jobs(self, request): |
---|
[6ed9d7a] | 62 | """RFC 2911: 3.2.6 Get-Jobs Operation |
---|
| 63 | |
---|
| 64 | This REQUIRED operation allows a client to retrieve the list |
---|
| 65 | of Job objects belonging to the target Printer object. The |
---|
| 66 | client may also supply a list of Job attribute names and/or |
---|
| 67 | attribute group names. A group of Job object attributes will |
---|
| 68 | be returned for each Job object that is returned. |
---|
| 69 | |
---|
| 70 | This operation is similar to the Get-Job-Attributes operation, |
---|
| 71 | except that this Get-Jobs operation returns attributes from |
---|
| 72 | possibly more than one object. |
---|
| 73 | |
---|
| 74 | """ |
---|
[5fe360e] | 75 | |
---|
[ef8df33] | 76 | req_dict = ipp.ops.verify_get_jobs_request(request) |
---|
| 77 | printer_name = req_dict['printer-uri'] |
---|
[5fe360e] | 78 | if printer_name not in self.printers: |
---|
| 79 | raise ipp.errors.Attributes( |
---|
| 80 | "Invalid printer uri: %s" % printer_name, |
---|
| 81 | [request.attribute_groups[0].attributes[2]]) |
---|
[6ed9d7a] | 82 | |
---|
[9eeab06] | 83 | # Each job will append a new job attribute group. |
---|
[ef8df33] | 84 | jobs = [job.get_job_attributes(request) for job in \ |
---|
| 85 | self.printers[printer_name].get_jobs()] |
---|
| 86 | response = ipp.ops.make_get_jobs_response(jobs, request) |
---|
| 87 | return response |
---|
[287d6ec] | 88 | |
---|
[ef8df33] | 89 | def print_uri(self, request): |
---|
[9eeab06] | 90 | pass |
---|
| 91 | |
---|
[ef8df33] | 92 | def create_job(self, request): |
---|
[9eeab06] | 93 | pass |
---|
| 94 | |
---|
[ef8df33] | 95 | def pause_printer(self, request): |
---|
[9eeab06] | 96 | pass |
---|
| 97 | |
---|
[ef8df33] | 98 | def resume_printer(self, request): |
---|
[9eeab06] | 99 | pass |
---|
| 100 | |
---|
[287d6ec] | 101 | @handler_for(const.Operations.GET_PRINTER_ATTRIBUTES) |
---|
[ef8df33] | 102 | def get_printer_attributes(self, request): |
---|
[cad7502] | 103 | """RFC 2911: 3.2.5 Get-Printer-Attributes Operation |
---|
| 104 | |
---|
| 105 | This REQUIRED operation allows a client to request the values |
---|
| 106 | of the attributes of a Printer object. |
---|
| 107 | |
---|
| 108 | In the request, the client supplies the set of Printer |
---|
| 109 | attribute names and/or attribute group names in which the |
---|
| 110 | requester is interested. In the response, the Printer object |
---|
| 111 | returns a corresponding attribute set with the appropriate |
---|
| 112 | attribute values filled in. |
---|
| 113 | |
---|
| 114 | For Printer objects, the possible names of attribute groups are: |
---|
| 115 | |
---|
| 116 | - 'job-template': the subset of the Job Template attributes |
---|
| 117 | that apply to a Printer object (the last two columns of the |
---|
| 118 | table in Section 4.2) that the implementation supports for |
---|
| 119 | Printer objects. |
---|
| 120 | |
---|
| 121 | - 'printer-description': the subset of the attributes |
---|
| 122 | specified in Section 4.4 that the implementation supports |
---|
| 123 | for Printer objects. |
---|
| 124 | |
---|
| 125 | - 'all': the special group 'all' that includes all attributes |
---|
| 126 | that the implementation supports for Printer objects. |
---|
| 127 | |
---|
| 128 | Since a client MAY request specific attributes or named |
---|
| 129 | groups, there is a potential that there is some overlap. For |
---|
| 130 | example, if a client requests, 'printer-name' and 'all', the |
---|
| 131 | client is actually requesting the 'printer-name' attribute |
---|
| 132 | twice: once by naming it explicitly, and once by inclusion in |
---|
| 133 | the 'all' group. In such cases, the Printer object NEED NOT |
---|
| 134 | return each attribute only once in the response even if it is |
---|
| 135 | requested multiple times. The client SHOULD NOT request the |
---|
| 136 | same attribute in multiple ways. |
---|
| 137 | |
---|
| 138 | It is NOT REQUIRED that a Printer object support all |
---|
| 139 | attributes belonging to a group (since some attributes are |
---|
| 140 | OPTIONAL). However, it is REQUIRED that each Printer object |
---|
| 141 | support all group names. |
---|
| 142 | |
---|
| 143 | """ |
---|
| 144 | |
---|
[b2e077a] | 145 | # this is just like cups_get_default, except the printer name |
---|
| 146 | # is given |
---|
[ef8df33] | 147 | req_dict = ipp.ops.verify_get_printer_attributes_request(request) |
---|
| 148 | printer_name = req_dict['printer-uri'] |
---|
[5fe360e] | 149 | if printer_name not in self.printers: |
---|
| 150 | raise ipp.errors.Attributes( |
---|
| 151 | "Invalid printer uri: %s" % printer_name, |
---|
| 152 | [request.attribute_groups[0].attributes[2]]) |
---|
| 153 | |
---|
[ef8df33] | 154 | response = ipp.ops.make_get_printer_attributes_response( |
---|
| 155 | self.printers[printer_name].get_printer_attributes(request), request) |
---|
| 156 | return response |
---|
[9eeab06] | 157 | |
---|
[ef8df33] | 158 | def set_printer_attributes(self, request): |
---|
[9eeab06] | 159 | pass |
---|
| 160 | |
---|
| 161 | ##### Job Commands |
---|
| 162 | |
---|
[ef8df33] | 163 | def cancel_job(self, request): |
---|
[9eeab06] | 164 | pass |
---|
| 165 | |
---|
[ef8df33] | 166 | def send_document(self, request): |
---|
[9eeab06] | 167 | pass |
---|
| 168 | |
---|
[ef8df33] | 169 | def send_uri(self, request): |
---|
[9eeab06] | 170 | pass |
---|
| 171 | |
---|
[ef8df33] | 172 | def get_job_attributes(self, request): |
---|
| 173 | req_dict = ipp.ops.verify_get_jobs_request(request) |
---|
| 174 | printer_name = req_dict['printer-uri'] |
---|
| 175 | job_id = req_dict['job-id'] |
---|
[5fe360e] | 176 | |
---|
| 177 | if printer_name not in self.printers: |
---|
| 178 | raise ipp.errors.Attributes( |
---|
| 179 | "Invalid printer uri: %s" % printer_name, |
---|
| 180 | [request.attribute_groups[0].attributes[2]]) |
---|
| 181 | try: |
---|
| 182 | job = self.printers[printer_name].get_job(job_id) |
---|
| 183 | except InvalidJobException: |
---|
| 184 | raise ipp.errors.Attributes( |
---|
| 185 | "Invalid job id: %d" % job_id, |
---|
| 186 | [request.attribute_groups[0].attributes[2]]) # XXX: this is wrong |
---|
| 187 | |
---|
| 188 | # Each job will append a new job attribute group. |
---|
| 189 | # XXX: we need to honor the things that the request actually asks for |
---|
[ef8df33] | 190 | response = ipp.ops.make_get_job_attributes_response( |
---|
| 191 | job.get_job_attributes(request), request) |
---|
| 192 | return response |
---|
[9eeab06] | 193 | |
---|
[ef8df33] | 194 | def set_job_attributes(self, request): |
---|
[9eeab06] | 195 | pass |
---|
| 196 | |
---|
[ef8df33] | 197 | def cups_get_document(self, request): |
---|
[9eeab06] | 198 | pass |
---|
| 199 | |
---|
[ef8df33] | 200 | def restart_job(self, request): |
---|
[9eeab06] | 201 | pass |
---|
| 202 | |
---|
[ef8df33] | 203 | def promote_job(self, request): |
---|
[9eeab06] | 204 | pass |
---|
| 205 | |
---|
| 206 | ##### CUPS Specific Commands |
---|
| 207 | |
---|
| 208 | @handler_for(const.Operations.CUPS_GET_DEFAULT) |
---|
[ef8df33] | 209 | def cups_get_default(self, request): |
---|
[0ede474] | 210 | """The CUPS-Get-Default operation (0x4001) returns the default |
---|
| 211 | printer URI and attributes. |
---|
| 212 | |
---|
| 213 | (Source: http://www.cups.org/documentation.php/spec-ipp.html#CUPS_GET_DEFAULT ) |
---|
| 214 | |
---|
| 215 | """ |
---|
[287d6ec] | 216 | |
---|
[ef8df33] | 217 | req_dict = ipp.ops.verify_cups_get_default_request(request) |
---|
| 218 | response = ipp.ops.make_get_printer_attributes_response( |
---|
| 219 | self.printers[self.default].get_printer_attributes(request), request) |
---|
| 220 | return response |
---|
[0ede474] | 221 | |
---|
[ef8df33] | 222 | @handler_for(const.Operations.CUPS_GET_PRINTERS) |
---|
| 223 | def cups_get_printers(self, request): |
---|
| 224 | """The CUPS-Get-Printers operation (0x4002) returns the |
---|
| 225 | printer attributes for every printer known to the system. This |
---|
| 226 | may include printers that are not served directly by the |
---|
| 227 | server. |
---|
[0ede474] | 228 | |
---|
| 229 | (Source: http://www.cups.org/documentation.php/spec-ipp.html#CUPS_GET_PRINTERS ) |
---|
| 230 | |
---|
| 231 | """ |
---|
| 232 | |
---|
[ef8df33] | 233 | req_dict = ipp.ops.verify_cups_get_printers_request(request) |
---|
| 234 | attrs = [self.printers[printer].get_printer_attributes(request) \ |
---|
| 235 | for printer in self.printers] |
---|
| 236 | response = ipp.ops.make_cups_get_printers_response(attrs, request) |
---|
| 237 | return response |
---|
[287d6ec] | 238 | |
---|
| 239 | @handler_for(const.Operations.CUPS_GET_CLASSES) |
---|
[ef8df33] | 240 | def cups_get_classes(self, request): |
---|
[0ede474] | 241 | """The CUPS-Get-Classes operation (0x4005) returns the printer |
---|
| 242 | attributes for every printer class known to the system. This |
---|
| 243 | may include printer classes that are not served directly by |
---|
| 244 | the server. |
---|
[ef8df33] | 245 | |
---|
[0ede474] | 246 | (Source: http://www.cups.org/documentation.php/spec-ipp.html#CUPS_GET_CLASSES ) |
---|
| 247 | |
---|
| 248 | """ |
---|
[ef8df33] | 249 | |
---|
| 250 | req_dict = ipp.ops.verify_cups_get_classes_request(request) |
---|
| 251 | response = ipp.ops.make_cups_get_classes_response(request) |
---|
| 252 | return response |
---|