source: server/lib/gutenbach/ipp/attributegroup.py @ 5c5fe6d

no-cups
Last change on this file since 5c5fe6d was 5c5fe6d, checked in by Jessica B. Hamrick <jhamrick@…>, 12 years ago

Comment formatting standardization

  • Property mode set to 100644
File size: 3.2 KB
RevLine 
[8979f90]1#!/usr/bin/python
2
3import sys, struct, logging
[7a1c039]4from .attribute import Attribute
[8979f90]5
6# initialize logger
[7a1c039]7logger = logging.getLogger(__name__)
[8979f90]8
[a76f440]9class AttributeGroup(object):
[5c5fe6d]10    """An AttributeGroup consists of an attribute-group-tag, followed
11    by a sequence of Attributes. According to RFC 2565, 'Within an
[a76f440]12    attribute-sequence, if two attributes have the same name, the
[5c5fe6d]13    first occurrence MUST be ignored.', so we can effectively treat
[a76f440]14    this as an ordered dictionary.
[5c5fe6d]15   
[8979f90]16    """
17
[c269bc7]18    def __init__(self, attribute_group_tag=None, attributes=[]):
[5c5fe6d]19        """Initialize an AttributeGroup.  An AttributeGroup can be
[c269bc7]20        initialized in three ways:
21
22            AttributeGroup()
23            AttributeGroup(attribute_group_tag)
24            AttributeGroup(attribute_group_tag, attributes)
[8979f90]25
26        Arguments:
27
28            attribute_group_tag -- a signed char, holds the tag of the
29                                   attribute group
30
[c269bc7]31            attributes -- a list of attributes
[5c5fe6d]32
[8979f90]33        """
34
[c269bc7]35        if attribute_group_tag is not None:
[569c377]36            assert isinstance(attribute_group_tag, int), \
[c269bc7]37                   "attribute_group_tag must be a character!"
38           
[8979f90]39
40        self.attribute_group_tag = attribute_group_tag
[569c377]41        self.attributes = []
[a76f440]42        self.extend(attributes)
[8979f90]43
[a76f440]44    def __getitem__(self, name):
[5c5fe6d]45        """Returns a list of attributes which have name 'name'.
46       
[c269bc7]47        """
48       
[a76f440]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)
[8979f90]54
[a76f440]55    def __contains__(self, name):
56        return len(filter(lambda x: x.name == name, self.attributes))
[c269bc7]57
[a76f440]58    def __iter__(self):
59        return (a.name for a in self.attributes)
[776a659]60
[a76f440]61    iterkeys = __iter__
[c269bc7]62
[a76f440]63    def __setitem__(self, name, attribute):
[5c5fe6d]64        """Sets an attribute in the attribute group. Note that the key
65        is ignored and the attribute is queried for its name.
66       
[c269bc7]67        """
68
[a76f440]69        return self.append(attribute)
70
71    def __delitem__(self, name):
[776a659]72        self.attributes = filter(lambda x: x.name != name,
73                                 self.attributes)
[c269bc7]74
[a76f440]75    def append(self, attribute):
76        return self.extend([attribute])
77
78    def extend(self, attributes):
[5c5fe6d]79        """Sets the attributes for the attribute group.
80       
[c269bc7]81        """
82
83        for a in attributes:
84            assert isinstance(a, Attribute), \
85                   "attribute must be of type Attribute!"
86
[a76f440]87        for a in attributes:
88            # XXX: Instead of replacing the attribute, do we want to
89            # append the value to the attribute here?
90            del self[a.name]
91            self.attributes.append(a)
[c269bc7]92
[a76f440]93    @property
94    def packed_value(self):
[5c5fe6d]95        """Convert the AttributeGroup to binary.
96       
[8979f90]97        """
98
99        # conver the attribute_group_tag to binary
100        tag = struct.pack('>b', self.attribute_group_tag)
101
102        # convert each of the attributes to binary
[a76f440]103        attributes = [a.packed_value for a in self.attributes]
[8979f90]104
105        # concatenate everything and return
106        return tag + ''.join(attributes)
[94211df]107
108    def __repr__(self):
109        return '<IPPAttributeGroup (%r, %r)>' % (self.attribute_group_tag, self.attributes)
Note: See TracBrowser for help on using the repository browser.