Changeset a76f440 for server/lib


Ignore:
Timestamp:
Mar 10, 2011, 1:28:20 AM (14 years ago)
Author:
Quentin Smith <quentin@…>
Branches:
no-cups
Children:
0e5cdb3
Parents:
75c0cab
git-author:
Quentin Smith <quentin@…> (03/10/11 01:28:20)
git-committer:
Quentin Smith <quentin@…> (03/10/11 01:28:20)
Message:

ippattributegroup is a mix between a list and a dict (sort of an ordereddict, but the keys are actually the attributes' names

File:
1 edited

Legend:

Unmodified
Added
Removed
  • server/lib/ippattributegroup.py

    rc269bc7 ra76f440  
    77logger = logging.getLogger("ippLogger")
    88
    9 class AttributeGroup():
     9class AttributeGroup(object):
    1010    """
    1111    An AttributeGroup consists of an attribute-group-tag, followed by
    12     a sequence of Attributes.
     12    a sequence of Attributes. According to RFC 2565, "Within an
     13    attribute-sequence, if two attributes have the same name, the
     14    first occurrence MUST be ignored.", so we can effectively treat
     15    this as an ordered dictionary.
    1316    """
    1417
     
    3538           
    3639
    37         if len(attributes) > 0:
    38             for a in attributes:
    39                 assert isinstance(a, Attribute), \
    40                        "attribute must be of type Attribute!"
     40        self.attribute_group_tag = attribute_group_tag
     41        self.attributes[]
     42        self.extend(attributes)
    4143
    42         self.attribute_group_tag = attribute_group_tag
    43         self.attributes = attributes
    44 
    45     def getAttribute(self, name):
     44    def __getitem__(self, name):
    4645        """
    4746        Returns a list of attributes which have name 'name'.
    4847        """
    4948       
    50         return filter(lambda x: x.name == name, self.attributes)
     49        attribute = filter(lambda x: x.name == name, self.attributes)
     50        if attribute:
     51            return attribute[0]
     52        else:
     53            raise KeyError("Attribute %r not found" % name)
    5154
    52     def getTag(self):
     55    def __contains__(self, name):
     56        return len(filter(lambda x: x.name == name, self.attributes))
     57
     58    def __iter__(self):
     59        return (a.name for a in self.attributes)
     60    iterkeys = __iter__
     61
     62    def __setitem__(self, name, attribute):
    5363        """
    54         Return the attribute group tag.
     64        Sets an attribute in the attribute group. Note that the key is
     65        ignored and the attribute is queried for its name.
    5566        """
    5667
    57         return self.attribute_group_tag
     68        return self.append(attribute)
    5869
    59     def getAttributes(self):
    60         """
    61         Return the list of attributes in this attribue group.
    62         """
     70    def __delitem__(self, name):
     71        self.attributes = filter(lambda x: x.name != name, self.attributes)
    6372
    64         return self.attributes
     73    def append(self, attribute):
     74        return self.extend([attribute])
    6575
    66     def setAttributes(self, attributes):
     76    def extend(self, attributes):
    6777        """
    6878        Sets the attributes for the attribute group.
     
    7383                   "attribute must be of type Attribute!"
    7484
    75         self.attributes = attributes
     85        for a in attributes:
     86            # XXX: Instead of replacing the attribute, do we want to
     87            # append the value to the attribute here?
     88            del self[a.name]
     89            self.attributes.append(a)
    7690
    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):
     91    @property
     92    def packed_value(self):
    9993        """
    10094        Convert the AttributeGroup to binary.
     
    10599
    106100        # convert each of the attributes to binary
    107         attributes = [a.toBinaryData() for a in self.attributes]
     101        attributes = [a.packed_value for a in self.attributes]
    108102
    109103        # concatenate everything and return
Note: See TracChangeset for help on using the changeset viewer.