Changeset ffbe41d


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

Location:
server/lib/gutenbach
Files:
7 edited

Legend:

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

    r793432f rffbe41d  
    5959        """
    6060
    61         if name is not None:
    62             assert isinstance(name, str), \
    63                    "Attribute name must be a string!"
     61        if name is not None and not isinstance(name, str):
     62            raise ValueError("attribute name must be a string")
    6463        if values is None:
    6564            values = []
    6665        for value in values:
    67             assert isinstance(value, Value), \
    68                    "Value %s must be of type Value" % (value,)
     66            if not isinstance(value, Value):
     67                raise ValueError("value %s must be of type Value" % (value,))
    6968
    7069        self.name = name
     
    8382        """
    8483
    85         assert self.name is not None, \
    86                "cannot pack unnamed attribute!"
    87         assert len(self.values) > 0, \
    88                "cannot pack empty attribute!"
     84        if self.name is None:
     85            raise ValueError, "cannot pack unnamed attribute"
     86        if len(self.values) == 0:
     87            raise ValueError, "cannot pack empty attribute"
    8988
    9089        # get the binary data for all the values
     
    9493            # get the name length (0 for everything but the first
    9594            # value)
    96             if i == 0:
    97                 name_length = len(self.name)
    98             else:
    99                 name_length = 0
     95            name_length = len(self.name) if i == 0 else 0
    10096
    10197            logger.debug("dumping name : %s" % self.name)
     
    113109            # the value tag in binary
    114110            tag_bin = struct.pack('>b', v.tag)
    115 
    116111            # the name length in binary
    117112            name_length_bin = struct.pack('>h', name_length)
    118 
    119113            # the name in binary
    120114            name_bin = self.name
    121 
    122115            # the value length in binary
    123116            value_length_bin = struct.pack('>h', value_length)
    124117
     118            # add the binary value to the list of values
     119            vlist = [tag_bin, name_length_bin, value_length_bin, value_bin]
    125120            if i == 0:
    126                 values.append(''.join([tag_bin,
    127                                        name_length_bin,
    128                                        name_bin,
    129                                        value_length_bin,
    130                                        value_bin]))
    131             else:
    132                 values.append(''.join([tag_bin,
    133                                        name_length_bin,
    134                                        value_length_bin,
    135                                        value_bin]))
     121                vlist.insert(2, name_bin)
     122            values.append(''.join(vlist))
    136123
    137124        # concatenate everything together and return it along with the
  • server/lib/gutenbach/ipp/core/attributegroup.py

    r793432f rffbe41d  
    3333        """
    3434
    35         if tag is not None:
    36             assert isinstance(tag, int), \
    37                    "tag must be a character!"
     35        if tag is not None and not isinstance(tag, int):
     36            raise ValueError("tag must be a character")
    3837
    3938        self.tag = tag
     
    8786
    8887        for a in attributes:
    89             assert isinstance(a, Attribute), \
    90                    "attribute must be of type Attribute!"
     88            if not isinstance(a, Attribute):
     89                raise ValueError("attribute must be of type Attribute")
    9190
    9291        for a in attributes:
  • 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
  • server/lib/gutenbach/ipp/core/value.py

    r793432f rffbe41d  
    4646
    4747        # make sure the arguments are valid
    48         if value is not None:
    49             assert tag is not None, \
    50                    "tag must not be null because " + \
    51                    "value is not null!"
     48        if value is not None and tag is None:
     49            raise ValueError("tag may not be None")
    5250
    5351        # initialize member variables
    54         self.tag = tag # one byte, the type of value
    55         self.value     = value     # non-binary value of self.value
     52        self.tag   = tag    # one byte, the type of value
     53        self.value = value  # non-binary value of self.value
    5654
    5755    def __cmp__(self, other):
     
    7674        """
    7775
    78         assert tag is not None, \
    79                "Cannot unpack values with unspecified value tag!"
    80         assert packed_value is not None, \
    81                "Cannot unpack null values!"
     76        if tag is None:
     77            raise ValueError("cannot unpack values with no value tag")
     78        if packed_value is None:
     79            raise ValueError("cannot unpack null values")
    8280
    8381        value = None
     
    172170
    173171        """
    174        
    175         assert self.tag is not None, \
    176                "cannot pack value with null value tag!"
    177         assert self.value is not None, \
    178                "cannot pack null value!"
     172
     173        if self.tag is None:
     174            raise ValueError("cannot pack value with null tag")
     175        if self.value is None:
     176            raise ValueError("cannot pack null value")
    179177
    180178        packed_value = None
     
    221219            # contains the units
    222220
    223             packed_value = truct.pack('>iib', *self.value)
     221            packed_value = struct.pack('>iib', *self.value)
    224222           
    225223        elif self.tag == OctetStringTags.RANGE_OF_INTEGER:
     
    291289        return self.packed_value_size + 1
    292290
    293     @property
    294     def pyobj(self):
    295         return (self.tag, self.value)
    296 
    297291    def __str__(self):
    298292        return str(self.value)
  • server/lib/gutenbach/server/printer.py

    rce2abc5 rffbe41d  
    1 #import alsaaudio as aa
    21from . import InvalidJobException, InvalidPrinterStateException
    32from . import Job
     
    4544       
    4645
    47     #def __init__(self, name, card, mixer):
    4846    def __init__(self, name):
    4947
     
    5250        self.time_created = int(time.time())
    5351        self.state = "idle"
    54 
    55         # if card >= len(aa.cards()):
    56         #     raise aa.ALSAAudioError(
    57         #       "Audio card at index %d does not exist!" % card)
    58         # elif mixer not in aa.mixers(card):
    59         #     raise aa.ALSAAudioError(
    60         #       "Audio mixer '%s' does not exist!" % mixer)
    61        
    62         # self.card = card
    63         # self.mixer = mixer
    6452
    6553        self.finished_jobs = []
  • server/lib/gutenbach/server/requests.py

    re58af05 rffbe41d  
    281281        """
    282282
    283 
    284283        operation = request.attribute_groups[0]
    285284
  • server/lib/gutenbach/server/server.py

    re58af05 rffbe41d  
    7979                chunk = self.rfile.read(size)
    8080                clrf = self.rfile.read(2)
    81                 assert clrf == "\r\n"
     81                if clrf != "\r\n":
     82                    raise ipp.errors.ServerErrorInternalError(
     83                        "clrf != \\r\\n (is '%s')" % clrf)
    8284                tmp.write(chunk)
    8385
Note: See TracChangeset for help on using the changeset viewer.