Changeset fc427ef


Ignore:
Timestamp:
Mar 10, 2011, 12:12:32 AM (13 years ago)
Author:
Quentin Smith <quentin@…>
Branches:
no-cups
Children:
a6a1f43
Parents:
fa0d0ef
git-author:
Quentin Smith <quentin@…> (03/10/11 00:12:32)
git-committer:
Quentin Smith <quentin@…> (03/10/11 00:12:32)
Message:

Adopt property usage for attributes, too

File:
1 edited

Legend:

Unmodified
Added
Removed
  • server/lib/ippattribute.py

    rc269bc7 rfc427ef  
    77logger = logging.getLogger("ippLogger")
    88
    9 class Attribute():
     9class Attribute(object):
    1010    """
    1111    In addition to what the RFC reports, an attribute has an
     
    7676            self.verify()
    7777
    78     def pack(self):
     78    @property
     79    def packed_value(self):
    7980        """
    8081        Packs the attribute data into binary data.
     
    8889        # get the binary data for all the values
    8990        values = []
    90         length = 0
    9191        for v, i in zip(self.values, xrange(len(self.values))):
    9292
     
    9999
    100100            # get the value length and binary value
    101             value_length, value_bin = v.pack()
     101            value_bin = v.packed_value
     102            value_length = len(value_bin)
    102103
    103104            logger.debug("dumping name_length : %i" % name_length)
    104105            logger.debug("dumping name : %s" % self.name)
    105106            logger.debug("dumping value_length : %i" % value_length)
    106             logger.debug("dumping value : %s" % v.getValue())
     107            logger.debug("dumping value : %s" % v.value)
    107108
    108109            # the value tag in binary
    109             value_tag_bin = struct.pack('>b', v.getValueTag())
     110            value_tag_bin = struct.pack('>b', v.value_tag)
    110111
    111112            # the name length in binary
     
    130131                                       value_bin]))
    131132
    132             length += 2            # name-length
    133             length += name_length  # name
    134             length += value_length # value
    135                
    136133        # concatenate everything together and return it along with the
    137134        # total length of the attribute
    138         return length, ''.join(values)
     135        return ''.join(values)
    139136
    140     def getName(self):
    141         """
    142         Get the attribute's name.
    143         """
    144        
    145         return self.name
    146 
    147     def getValues(self):
    148         """
    149         Get the list of values contained in the attribute.
    150         """
    151        
    152         return self.values
    153 
    154     def setName(self, name):
    155         """
    156         Set the attribute's name.
    157         """
    158 
    159         assert isinstance(name, str), \
    160                "name must be a string!"
    161        
    162         self.name = name
    163 
    164     def setValues(self, values):
    165         """
    166         Set the list of values contained in the attribute.
    167         """
    168 
    169         for v in values:
    170             assert isinstance(v, Value), \
    171                    "value must be of type Value!"
    172        
    173         self.values = values
    174 
    175     def addValue(self, value):
    176         """
    177         Add a new value to the list of values contained in the
    178         attribute."
    179 
    180         assert isinstance(value, Value), \
    181                "value must be of type Value!"
    182 
    183         self.values.append(value)
    184 
    185     def getSize(self):
     137    @property
     138    def packed_value_size(self):
    186139        """
    187140        Gets the total size of the attribute.
    188141        """
     142        return 2+len(self.name)+sum(v.total_size for v in self.values)
    189143
    190         size = 2 + len(self.name)
    191         for v in self.values:
    192             size += v.getTotalSize()
    193 
    194     def verify(self):
    195         """
    196         Check to make sure that the Attribute is valid.  This means
    197         that is should have a name and a list of values, and that its
    198         binary representation should match up to the stored binary
    199         representation.
    200         """
    201        
    202         assert self.name is not None, \
    203                "attribute has no name!"
    204         assert len(self.value) > 0, \
    205                "attribute has no values!"
    206 
    207         size, bindata = self.pack()
    208 
    209         assert size == self.getSize(), \
    210                "pack size does not match self.getSize()!"
    211         assert bindata == self.binary_data, \
    212                "packed binary data does not match self.binary_data!"
     144    total_size = packed_value_size
    213145
    214146    def __str__(self):
Note: See TracChangeset for help on using the changeset viewer.