Ignore:
Timestamp:
Jan 10, 2012, 7:25:33 PM (12 years ago)
Author:
Jessica B. Hamrick <jhamrick@…>
Branches:
no-cups
Children:
b01b6d1
Parents:
ce2abc5
git-author:
Jessica B. Hamrick <jhamrick@…> (01/10/12 19:25:33)
git-committer:
Jessica B. Hamrick <jhamrick@…> (01/10/12 19:25:33)
Message:

Clean up core ipp code a bit

File:
1 edited

Legend:

Unmodified
Added
Removed
  • server/lib/gutenbach/ipp/core/request.py

    rce2abc5 rffbe41d  
    7171        if request is None:
    7272            # make sure the version number isn't empty
    73             assert version is not None
     73            if version is None:
     74                raise ValueError("version must not be None")
    7475            # make sure verison is a tuple of length 2
    75             assert isinstance(version, tuple)
    76             assert len(version) == 2
     76            if not hasattr(version, '__iter__'):
     77                raise ValueError("version must be iterable")
     78            if len(version) != 2:
     79                raise ValueError("version must be of length 2")
    7780            # make sure the operation id isn't empty
    78             assert operation_id is not None
     81            if operation_id is None:
     82                raise ValueError("operation_id may not be None")
    7983            # make sure the request id isn't empty
    80             assert request_id is not None
     84            if request_id is None:
     85                raise ValueError("request_id may not be None")
    8186            # make sure attribute_groups is a list of Attributes
    82             for a in attribute_groups: assert isinstance(a, AttributeGroup)
     87            for a in attribute_groups:
     88                if not isinstance(a, AttributeGroup):
     89                    raise ValueError("attribute not of type AttributeGroup")
    8390           
    8491        # if the request isn't None, then we'll read directly from
     
    9097           
    9198            # read the version-number (two signed chars)
    92             self.version        = struct.unpack('>bb', request.read(2))
     99            self.version = struct.unpack('>bb', request.read(2))
    93100            length -= 2
    94101            logger.debug("version-number : (0x%X, 0x%X)" % self.version)
     
    96103            # read the operation-id (or status-code, but that's only
    97104            # for a response) (signed short)
    98             self.operation_id   = struct.unpack('>h', request.read(2))[0]
     105            self.operation_id = struct.unpack('>h', request.read(2))[0]
    99106            length -= 2
    100107            logger.debug("operation-id : 0x%X" % self.operation_id)
    101108
    102109            # read the request-id (signed int)
    103             self.request_id     = struct.unpack('>i', request.read(4))[0]
     110            self.request_id = struct.unpack('>i', request.read(4))[0]
    104111            length -= 4
    105112            logger.debug("request-id : 0x%X" % self.request_id)
     
    130137                   
    131138                    # read in the value tag (signed char)
    132                     value_tag     = next_byte
     139                    value_tag = next_byte
    133140                    logger.debug("value-tag : 0x%X" % value_tag)
    134141                   
    135142                    # read in the length of the name (signed short)
    136                     name_length   = struct.unpack('>h', request.read(2))[0]
     143                    name_length = struct.unpack('>h', request.read(2))[0]
    137144                    length -= 2
    138145                    logger.debug("name-length : %i" % name_length)
     
    140147                    if name_length != AttributeTags.ZERO_NAME_LENGTH:
    141148                        # read the name (a string of name_length bytes)
    142                         name          = request.read(name_length)
     149                        name = request.read(name_length)
    143150                        length -= name_length
    144151                        logger.debug("name : %s" % name)
    145152                   
    146153                        # read in the length of the value (signed short)
    147                         value_length  = struct.unpack('>h', request.read(2))[0]
     154                        value_length = struct.unpack('>h', request.read(2))[0]
    148155                        length -= 2
    149156                        logger.debug("value-length : %i" % value_length)
    150157                   
    151158                        # read in the value (string of value_length bytes)
    152                         value         = request.read(value_length)
     159                        value = request.read(value_length)
    153160                        length -= value_length
    154161                       
     
    162169                    else:
    163170                        # read in the length of the value (signed short)
    164                         value_length  = struct.unpack('>h', request.read(2))[0]
     171                        value_length = struct.unpack('>h', request.read(2))[0]
    165172                        length -= 2
    166173                        logger.debug("value-length : %i" % value_length)
    167174                   
    168175                        # read in the value (string of value_length bytes)
    169                         value         = request.read(value_length)
     176                        value = request.read(value_length)
    170177                        length -= value_length
    171178
     
    214221
    215222        # make sure the version number isn't empty
    216         assert self.version is not None
     223        if self.version is None:
     224            raise ValueError("version is None")
    217225        # make sure verison is a tuple of length 2
    218         assert isinstance(self.version, tuple)
    219         assert len(self.version) == 2
     226        if not hasattr(self.version, '__iter__'):
     227            raise ValueError("version is not iterable")
     228        if len(self.version) != 2:
     229            raise ValueError("version is not of length 2")
    220230        # make sure the operation id isn't empty
    221         assert self.operation_id is not None
     231        if self.operation_id is None:
     232            raise ValueError("operation_id is None")
    222233        # make sure the request id isn't empty
    223         assert self.request_id is not None
     234        if self.request_id is None:
     235            raise ValueError("request_id is None")
    224236        # make sure attribute_groups is a list of Attributes
    225         assert len(self.attribute_groups) > 0
    226         for a in self.attribute_groups: assert isinstance(a, AttributeGroup)
     237        if len(self.attribute_groups) == 0:
     238            raise ValueError("no attribute groups")
     239        for a in self.attribute_groups:
     240            if not isinstance(a, AttributeGroup):
     241                raise ValueError("not of type AttributeGroup")
    227242
    228243        # convert the version, operation id, and request id to binary
Note: See TracChangeset for help on using the changeset viewer.