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
RevLine 
[7a1c039]1from .attribute import Attribute
[d04a689]2import sys
3import struct
4import logging
[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
[b2e077a]18    def __init__(self, tag=None, attributes=[]):
[5c5fe6d]19        """Initialize an AttributeGroup.  An AttributeGroup can be
[c269bc7]20        initialized in three ways:
21
22            AttributeGroup()
[b2e077a]23            AttributeGroup(tag)
24            AttributeGroup(tag, attributes)
[8979f90]25
26        Arguments:
27
[b2e077a]28            tag -- a signed char, holds the tag of the
29                   attribute group
[8979f90]30
[c269bc7]31            attributes -- a list of attributes
[5c5fe6d]32
[8979f90]33        """
34
[b2e077a]35        if tag is not None:
36            assert isinstance(tag, int), \
37                   "tag must be a character!"
[8979f90]38
[b2e077a]39        self.tag = tag
[569c377]40        self.attributes = []
[a76f440]41        self.extend(attributes)
[8979f90]42
[a76f440]43    def __getitem__(self, name):
[5c5fe6d]44        """Returns a list of attributes which have name 'name'.
45       
[c269bc7]46        """
47       
[a76f440]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)
[8979f90]53
[a76f440]54    def __contains__(self, name):
55        return len(filter(lambda x: x.name == name, self.attributes))
[c269bc7]56
[a76f440]57    def __iter__(self):
58        return (a.name for a in self.attributes)
[776a659]59
[a76f440]60    iterkeys = __iter__
[c269bc7]61
[a76f440]62    def __setitem__(self, name, attribute):
[5c5fe6d]63        """Sets an attribute in the attribute group. Note that the key
64        is ignored and the attribute is queried for its name.
65       
[c269bc7]66        """
67
[a76f440]68        return self.append(attribute)
69
70    def __delitem__(self, name):
[776a659]71        self.attributes = filter(lambda x: x.name != name,
72                                 self.attributes)
[c269bc7]73
[a76f440]74    def append(self, attribute):
75        return self.extend([attribute])
76
77    def extend(self, attributes):
[5c5fe6d]78        """Sets the attributes for the attribute group.
79       
[c269bc7]80        """
81
82        for a in attributes:
83            assert isinstance(a, Attribute), \
84                   "attribute must be of type Attribute!"
85
[a76f440]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)
[c269bc7]91
[a76f440]92    @property
93    def packed_value(self):
[5c5fe6d]94        """Convert the AttributeGroup to binary.
95       
[8979f90]96        """
97
[b2e077a]98        # convert the tag to binary
99        tag = struct.pack('>b', self.tag)
[8979f90]100
101        # convert each of the attributes to binary
[a76f440]102        attributes = [a.packed_value for a in self.attributes]
[8979f90]103
104        # concatenate everything and return
105        return tag + ''.join(attributes)
[94211df]106
107    def __repr__(self):
[b2e077a]108        return '<IPPAttributeGroup (%r, %r)>' % (self.tag, self.attributes)
Note: See TracBrowser for help on using the repository browser.