source: server/lib/gutenbach/ipp/attributegroup.py @ b828a96

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

Use classes for standard IPP attributes

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