Ignore:
Timestamp:
Dec 23, 2011, 8:20:29 PM (12 years ago)
Author:
Jessica B. Hamrick <jhamrick@…>
Branches:
no-cups
Children:
b828a96
Parents:
f6e2532
git-author:
Jessica B. Hamrick <jhamrick@…> (12/23/11 20:20:29)
git-committer:
Jessica B. Hamrick <jhamrick@…> (12/23/11 20:20:29)
Message:

Checkpoint, creating classes for specific IPP attributes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • server/lib/gutenbach/ipp/operations/__init__.py

    re70c020 raded2d1  
    55from .. import errors
    66from .. import constants as consts
     7from .. import object_attributes
    78
    89def verify_operations(request):
     
    1617    # XXX: check version
    1718    if False:
    18         raise errors.VersionNotSupported(str(request.version))
     19        raise errors.ClientErrorVersionNotSupported(str(request.version))
    1920
    2021    # XXX: check operation id
    2122    if False:
    22         raise errors.OperationNotSupported(str(request.operation_id))
     23        raise errors.ClientErrorOperationNotSupported(str(request.operation_id))
    2324
    2425    # check operation attributes tag
    2526    op_attrs = request.attribute_groups[0]
    2627    if op_attrs.tag != consts.AttributeTags.OPERATION:
    27         raise errors.BadRequest(
     28        raise errors.ClientErrorBadRequest(
    2829            "Attribute group does not have OPERATION tag: 0x%x" % op_attrs.tag)
    2930
     
    3233    # # check compression
    3334    # if False:
    34     #     raise errors.CompressionNotSupported
     35    #     raise errors.ClientErrorCompressionNotSupported
    3536
    3637    # # check document format
    3738    # if False:
    38     #     raise errors.DocumentFormatNotSupported
     39    #     raise errors.ClientErrorDocumentFormatNotSupported
    3940
    4041    # # check document uri
    4142    # if False:
    42     #     raise errors.UriSchemeNotSupported
     43    #     raise errors.ClientErrorUriSchemeNotSupported
    4344
    4445    # check charset
    4546    charset_attr = op_attrs.attributes[0]
    46     if charset_attr.name != 'attributes-charset':
    47         raise errors.BadRequest(
    48             "Attribute is not attributes-charset: %s" % charset_attr.name)
    49     if len(charset_attr.values) != 1:
    50         raise errors.BadRequest(
    51             "Too many values for attributes-charset: %d" % len(charset_attr.values))
    52     # check charset value
    53     charset_value = charset_attr.values[0]
    54     if charset_value.tag != consts.operations_attribute_value_tags['attributes-charset']:
    55         raise errors.BadRequest(
    56             "Wrong tag for charset value: 0x%x" % charset_value.tag)
    57     if charset_value.value != 'utf-8':
    58         raise errors.CharsetNotSupported(str(charset_value.value))
     47    if charset_attr != object_attributes.AttributesCharset('utf-8'):
     48        raise errors.ClientErrorBadRequest
    5949
    6050    # check for attributes-natural-language
    6151    natlang_attr = op_attrs.attributes[1]
    62     if natlang_attr.name != 'attributes-natural-language':
    63         raise errors.BadRequest(
    64             "Attribute is not attributes-natural-language: %s" % natlang_attr.name)
    65     if len(charset_attr.values) != 1:
    66         raise errors.BadRequest(
    67             "Too many values for attributes-natural-language: %s" % len(natlang_attr.values))
    68     # check natural language value
    69     natlang_value = natlang_attr.values[0]
    70     if natlang_value.tag != consts.operations_attribute_value_tags['attributes-natural-language']:
    71         raise errors.BadRequest(
    72             "Natural language value does not have NATURAL_LANGUAGE tag: 0x%x" % natlang_value.tag)
    73     if natlang_value.value != 'en-us':
    74         raise errors.Attributes(
    75             "Invalid natural language value: %s" % natlang_value.value, [natlang_attr])
     52    if natlang_attr != object_attributes.AttributesNaturalLanguage('en-us'):
     53        raise errors.ClientErrorBadRequest
    7654
    77     return dict([(attr.name, attr.values) for attr in op_attrs.attributes])
     55    return dict([(attr.name, attr) for attr in op_attrs.attributes])
    7856
    79 def verify_printer_uri(values):
    80     if len(values) != 1:
    81         raise errors.BadRequest(
    82             "Requesting printer uri attribute has too many values: %d" % len(values))
    83     uri_value = values[0]
    84     if uri_value.tag != consts.operations_attribute_value_tags['printer-uri']:
    85         raise errors.BadRequest(
    86             "Bad value tag (expected URI): 0x%x" % uri_value_tag)
     57def verify_printer_uri(uri_attr):
     58    if uri_attr != object_attributes.PrinterUri(uri_attr.values[0].value):
     59        raise errors.ClientErrorBadRequest
    8760   
    8861    # actually get the printer name
     
    9063    # we can't do an exact comparison (also the hostname might be
    9164    # different, depending on the CNAME or whether it's localhost)
    92     uri = uri_value.value.split("/")[-1]
     65    uri = uri_attr.values[0].value.split("/")[-1]
    9366    return uri
    9467
    95 def verify_requesting_username(values):
    96     if len(values) != 1:
    97         raise errors.BadRequest(
    98             "Requesting user name attribute has too many values: %d" % len(values))
    99     requser_value = values[0]
    100     if requser_value.tag != consts.operations_attribute_value_tags['requesting-user-name']:
    101         raise errors.BadRequest(
    102             "Bad value tag (expected NAME_WITHOUT_LANGUAGE): 0x%x" % requser_value.tag)
    103    
    104     return requser_value.value
     68def verify_requesting_username(username_attr):
     69    if username_attr != object_attributes.RequestingUserName(username_attr.values[0].value):
     70        raise errors.ClientErrorBadRequest
     71    return username_attr.values[0].value
    10572
    10673def make_empty_response(request):
    10774    # Operation attributes -- typically the same for any request
    108     attributes = [
    109         Attribute(
    110             'attributes-charset',
    111             [Value(consts.operations_attribute_value_tags['attributes-charset'], 'utf-8')]),
    112         Attribute(
    113             'attributes-natural-language',
    114             [Value(consts.operations_attribute_value_tags['attributes-natural-language'],
    115                    'en-us')])
    116         ]
    117     # Put the operation attributes in a group
    118     attribute_group = AttributeGroup(
     75    attributes = AttributeGroup(
    11976        consts.AttributeTags.OPERATION,
    120         attributes)
     77        [object_attributes.AttributesCharset('utf-8'),
     78         object_attributes.AttributesNaturalLanguage('en-us')])
    12179
    12280    # Set up the default response -- handlers will override these
     
    12684    response_kwargs['operation_id']     = consts.StatusCodes.OK
    12785    response_kwargs['request_id']       = request.request_id
    128     response_kwargs['attribute_groups'] = [attribute_group]
     86    response_kwargs['attribute_groups'] = [attributes]
    12987    response = Request(**response_kwargs)
    13088
     
    13290
    13391def make_job_attributes(attrs, request, response):
    134     ipp_attrs = []
    135     for attr, vals in attrs:
    136         ipp_vals = [Value(
    137             tag=consts.job_attribute_value_tags[attr],
    138             value=val) for val in vals]
    139         ipp_attrs.append(Attribute(name=attr, values=ipp_vals))
    14092    response.attribute_groups.append(AttributeGroup(
    141         consts.AttributeTags.JOB, ipp_attrs))
     93        consts.AttributeTags.JOB, attrs))
    14294
    14395def make_printer_attributes(attrs, request, response):
    144     ipp_attrs = []
    145     for attr, vals in attrs:
    146         ipp_vals = [Value(
    147             tag=consts.printer_attribute_value_tags[attr],
    148             value=val) for val in vals]
    149         ipp_attrs.append(Attribute(name=attr, values=ipp_vals))
    15096    response.attribute_groups.append(AttributeGroup(
    151         consts.AttributeTags.PRINTER, ipp_attrs))
     97        consts.AttributeTags.PRINTER, attrs))
    15298
    15399from cups_get_classes import verify_cups_get_classes_request, make_cups_get_classes_response
Note: See TracChangeset for help on using the changeset viewer.