Changeset c269bc7


Ignore:
Timestamp:
Mar 7, 2011, 7:47:42 PM (13 years ago)
Author:
Jessica B. Hamrick <jhamrick@…>
Branches:
no-cups
Children:
fa0d0ef
Parents:
5cfb358
git-author:
Jessica B. Hamrick <jhamrick@…> (03/07/11 19:47:42)
git-committer:
Jessica B. Hamrick <jhamrick@…> (03/07/11 19:47:42)
Message:

Update API for Value, Attribute, and AttributeGroup?

Location:
server/lib
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • server/lib/ippattribute.py

    rebf327d rc269bc7  
    4040    """
    4141
    42     def __init__(self, name, values):
    43         """
    44         Initialize an Attribute.
     42    def __init__(self, name=None, values=[]):
     43        """
     44        Initialize an Attribute.  This function can be called in three
     45        different ways:
     46
     47            Attribute() -- creates an empty Attribute
     48
     49            Attribute(name) -- creates an empty Attribute with a name
     50
     51            Attribute(name, values) -- creates an Attribute
     52            initialized with a name and list of values
    4553       
    4654        Arguments:
     
    5159        """
    5260
    53         # make sure name isn't empty
    54         assert name is not None
    55          
    56         # make sure the list of values isn't empty
    57         assert len(values) > 0
    58         # make sure each value is a Value
    59         for value in values: assert isinstance(value, Value)
    60          
    61         self.name = name
    62         self.values = values
    63 
    64     def toBinaryData(self):
     61        if name is not None:
     62            assert isinstance(name, str), \
     63                   "Attribute name must be a string!"
     64        for value in values:
     65            assert isinstance(value, Value), \
     66                   "Value must be of type Value"
     67
     68        self.name = None
     69        self.values = None
     70
     71        if name is not None:
     72            self.name = name
     73        if name is not None and len(values) > 0:
     74            self.values = values
     75            self.binary = self.pack()
     76            self.verify()
     77
     78    def pack(self):
    6579        """
    6680        Packs the attribute data into binary data.
    6781        """
     82
     83        assert self.name is not None, \
     84               "cannot pack unnamed attribute!"
     85        assert len(self.values) > 0, \
     86               "cannot pack empty attribute!"
    6887
    6988        # get the binary data for all the values
    7089        values = []
     90        length = 0
    7191        for v, i in zip(self.values, xrange(len(self.values))):
    7292
     
    7999
    80100            # get the value length and binary value
    81             value_length, value_bin = v.valueToBinary()
     101            value_length, value_bin = v.pack()
    82102
    83103            logger.debug("dumping name_length : %i" % name_length)
    84104            logger.debug("dumping name : %s" % self.name)
    85105            logger.debug("dumping value_length : %i" % value_length)
    86             logger.debug("dumping value : %s" % v.value)
     106            logger.debug("dumping value : %s" % v.getValue())
    87107
    88108            # the value tag in binary
    89             value_tag_bin = struct.pack('>b', v.value_tag)
     109            value_tag_bin = struct.pack('>b', v.getValueTag())
    90110
    91111            # the name length in binary
     
    109129                                       value_length_bin,
    110130                                       value_bin]))
     131
     132            length += 2            # name-length
     133            length += name_length  # name
     134            length += value_length # value
    111135               
    112         # concatenate everything together and return it
    113         return ''.join(values)
     136        # concatenate everything together and return it along with the
     137        # total length of the attribute
     138        return length, ''.join(values)
     139
     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):
     186        """
     187        Gets the total size of the attribute.
     188        """
     189
     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!"
     213
     214    def __str__(self):
     215        if len(self.values) > 0:
     216            values = [str(v) for v in self.values]
     217        else:
     218            values = "None"
     219
     220        if self.name is None:
     221            name = "None"
     222        else:
     223            name = self.name
     224       
     225        return "%s: %s" % (name, str(values))
  • server/lib/ippattributegroup.py

    rebf327d rc269bc7  
    1313    """
    1414
    15     def __init__(self, attribute_group_tag, attributes=[]):
     15    def __init__(self, attribute_group_tag=None, attributes=[]):
    1616        """
    17         Initialize an AttributeGroup.
     17        Initialize an AttributeGroup.  An AttributeGroup can be
     18        initialized in three ways:
     19
     20            AttributeGroup()
     21            AttributeGroup(attribute_group_tag)
     22            AttributeGroup(attribute_group_tag, attributes)
    1823
    1924        Arguments:
     
    2227                                   attribute group
    2328
    24             attributes -- (optional) a list of attributes
     29            attributes -- a list of attributes
    2530        """
    2631
    27         # make sure attribute_group_tag isn't empty
    28         assert attribute_group_tag is not None
     32        if attribute_group_tag is not None:
     33            assert isinstance(attribute_group_tag, char), \
     34                   "attribute_group_tag must be a character!"
     35           
    2936
    30         # make sure attributes is a list or tuple of Attributes
    31         assert isinstance(attributes, (list, tuple))
    32         for a in attributes: assert isinstance(a, Attribute)
     37        if len(attributes) > 0:
     38            for a in attributes:
     39                assert isinstance(a, Attribute), \
     40                       "attribute must be of type Attribute!"
    3341
    3442        self.attribute_group_tag = attribute_group_tag
     
    3644
    3745    def getAttribute(self, name):
     46        """
     47        Returns a list of attributes which have name 'name'.
     48        """
     49       
    3850        return filter(lambda x: x.name == name, self.attributes)
    3951
    40     def toBinaryData(self):
     52    def getTag(self):
     53        """
     54        Return the attribute group tag.
     55        """
     56
     57        return self.attribute_group_tag
     58
     59    def getAttributes(self):
     60        """
     61        Return the list of attributes in this attribue group.
     62        """
     63
     64        return self.attributes
     65
     66    def setAttributes(self, attributes):
     67        """
     68        Sets the attributes for the attribute group.
     69        """
     70
     71        for a in attributes:
     72            assert isinstance(a, Attribute), \
     73                   "attribute must be of type Attribute!"
     74
     75        self.attributes = attributes
     76
     77    def addAttribute(self, attribute):
     78        """
     79        Adds an attribute to the list of attributes for the attribute
     80        group.
     81        """
     82       
     83        assert isinstance(attribute, Attribute), \
     84               "attribute must be of type Attribute!"
     85
     86        self.attributes.append(attribute)
     87
     88    def setTag(self, tag):
     89        """
     90        Sets the attribute group tag.
     91        """
     92
     93        assert isinstance(tag, char), \
     94               "attribute tag must be a character!"
     95
     96        self.attribute_group_tag = tag
     97
     98    def pack(self):
    4199        """
    42100        Convert the AttributeGroup to binary.
  • server/lib/ippvalue.py

    r5cfb358 rc269bc7  
    6565        self.value_tag    = None # one byte, the type of value
    6666        self.value        = None # non-binary value of self.value
    67         self.value_size   = None # size of self.value
     67        self.tag_size     = 1    # length of the tag in bytes
     68        self.value_size   = None # size of self.value in bytes
    6869        self.binary_value = None # binary value of self.value
    6970
     
    7273                self.value_tag = value_tag
    7374                self.binary_value = value
    74                 self.unpack()
    75                 self.pack()
     75                self.value_size, self.value = self.unpack()
    7676            else:
    7777                self.value_tag = value_tag
    7878                self.value = value
    79                 self.pack()
    80                 self.unpack()
     79                self.value_size, self.binary_value = self.pack()
     80
     81            self.verify()
    8182
    8283    def unpack(self):
     
    196197                   "unpacked value is not the same as self.value!"
    197198
    198         self.value_size = value_size
    199         self.value = value
    200        
    201199        return value_size, value
    202200
     
    313311            binary_value = self.value
    314312
    315         if self.value_size is not None:
    316             assert value_size == self.value_size, \
    317                    "packed value size is not the same as " + \
    318                    "self.value_size!"
    319         if self.binary_value is not None:
    320             assert binary_value == self.binary_value, \
    321                    "packed binary value is not the same as " + \
    322                    "self.binary_value!"
    323 
    324         self.value_size = value_size
    325         self.binary_value = binary_value
    326 
    327313        return value_size, binary_value
    328314
     
    346332               "value type is unknown!"
    347333
    348         self.pack()
    349         self.unpack()
     334        value_size1, binary_value = self.pack()
     335        value_size2, value = self.unpack()
     336
     337        assert value_size1 == value_size2, \
     338               "packed value size is not the same as " + \
     339               "unpacked value size!"
     340        assert value_size == self.value_size, \
     341               "packed value size is not the same as " + \
     342               "self.value_size!"
     343        assert binary_value == self.binary_value, \
     344               "packed binary value is not the same as " + \
     345               "self.binary_value!"
    350346
    351347    def getValue(self):
     
    371367        return self.value_tag
    372368
    373     def getSize(self):
     369    def getValueSize(self):
    374370        """
    375371        Get the size of the value in bytes.
     
    378374        return self.value_size
    379375
     376    def getTagSize(self):
     377        """
     378        Get the size of the value tag in bytes.
     379        """
     380
     381        return self.tag_size
     382
    380383    def setValue(self, value):
    381384        """
    382385        Set the non-binary value.
    383386        """
    384        
     387
    385388        self.value = value
    386389
     
    389392        Set the binary value.
    390393        """
     394
    391395        self.binary_value = binary_value
    392396
     
    399403        self.value_tag = value_tag
    400404
    401     def setSize(self, size):
     405    def setValueSize(self, size):
    402406        """
    403407        Set the size of the value in bytes.
     
    405409       
    406410        self.value_size = size
     411
     412    def getTotalSize(self):
     413        """
     414        Get the total size of the IPP value.
     415        """
     416
     417        return self.value_size + self.tag_size
    407418
    408419    def __str__(self):
Note: See TracChangeset for help on using the changeset viewer.