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

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

Refactor code to handle the operations a little bit more logically

  • Property mode set to 100644
File size: 3.0 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 __getitem__(self, name):
44        """Returns a list of attributes which have name 'name'.
45       
46        """
47       
48        attribute = filter(lambda x: x.name == name, self.attributes)
49        if attribute:
50            return attribute[0]
51        else:
52            raise KeyError("Attribute %r not found" % name)
53
54    def __contains__(self, name):
55        return len(filter(lambda x: x.name == name, self.attributes))
56
57    def __iter__(self):
58        return (a.name for a in self.attributes)
59
60    iterkeys = __iter__
61
62    def __setitem__(self, name, attribute):
63        """Sets an attribute in the attribute group. Note that the key
64        is ignored and the attribute is queried for its name.
65       
66        """
67
68        return self.append(attribute)
69
70    def __delitem__(self, name):
71        self.attributes = filter(lambda x: x.name != name,
72                                 self.attributes)
73
74    def append(self, attribute):
75        return self.extend([attribute])
76
77    def extend(self, attributes):
78        """Sets the attributes for the attribute group.
79       
80        """
81
82        for a in attributes:
83            assert isinstance(a, Attribute), \
84                   "attribute must be of type Attribute!"
85
86        for a in attributes:
87            # XXX: Instead of replacing the attribute, do we want to
88            # append the value to the attribute here?
89            del self[a.name]
90            self.attributes.append(a)
91
92    @property
93    def packed_value(self):
94        """Convert the AttributeGroup to binary.
95       
96        """
97
98        # convert the tag to binary
99        tag = struct.pack('>b', self.tag)
100
101        # convert each of the attributes to binary
102        attributes = [a.packed_value for a in self.attributes]
103
104        # concatenate everything and return
105        return tag + ''.join(attributes)
106
107    def __repr__(self):
108        return '<IPPAttributeGroup (%r, %r)>' % (self.tag, self.attributes)
Note: See TracBrowser for help on using the repository browser.