[7a1c039] | 1 | from .attribute import Attribute |
---|
[d04a689] | 2 | import sys |
---|
| 3 | import struct |
---|
| 4 | import logging |
---|
[8979f90] | 5 | |
---|
| 6 | # initialize logger |
---|
[7a1c039] | 7 | logger = logging.getLogger(__name__) |
---|
[8979f90] | 8 | |
---|
[a76f440] | 9 | class 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 | |
---|
[b828a96] | 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 | |
---|
[a76f440] | 49 | def __getitem__(self, name): |
---|
[5c5fe6d] | 50 | """Returns a list of attributes which have name 'name'. |
---|
| 51 | |
---|
[c269bc7] | 52 | """ |
---|
| 53 | |
---|
[a76f440] | 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) |
---|
[8979f90] | 59 | |
---|
[a76f440] | 60 | def __contains__(self, name): |
---|
| 61 | return len(filter(lambda x: x.name == name, self.attributes)) |
---|
[c269bc7] | 62 | |
---|
[a76f440] | 63 | def __iter__(self): |
---|
| 64 | return (a.name for a in self.attributes) |
---|
[776a659] | 65 | |
---|
[a76f440] | 66 | iterkeys = __iter__ |
---|
[c269bc7] | 67 | |
---|
[a76f440] | 68 | def __setitem__(self, name, attribute): |
---|
[5c5fe6d] | 69 | """Sets an attribute in the attribute group. Note that the key |
---|
| 70 | is ignored and the attribute is queried for its name. |
---|
| 71 | |
---|
[c269bc7] | 72 | """ |
---|
| 73 | |
---|
[a76f440] | 74 | return self.append(attribute) |
---|
| 75 | |
---|
| 76 | def __delitem__(self, name): |
---|
[776a659] | 77 | self.attributes = filter(lambda x: x.name != name, |
---|
| 78 | self.attributes) |
---|
[c269bc7] | 79 | |
---|
[a76f440] | 80 | def append(self, attribute): |
---|
| 81 | return self.extend([attribute]) |
---|
| 82 | |
---|
| 83 | def extend(self, attributes): |
---|
[5c5fe6d] | 84 | """Sets the attributes for the attribute group. |
---|
| 85 | |
---|
[c269bc7] | 86 | """ |
---|
| 87 | |
---|
| 88 | for a in attributes: |
---|
| 89 | assert isinstance(a, Attribute), \ |
---|
| 90 | "attribute must be of type Attribute!" |
---|
| 91 | |
---|
[a76f440] | 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) |
---|
[c269bc7] | 97 | |
---|
[a76f440] | 98 | @property |
---|
| 99 | def packed_value(self): |
---|
[5c5fe6d] | 100 | """Convert the AttributeGroup to binary. |
---|
| 101 | |
---|
[8979f90] | 102 | """ |
---|
| 103 | |
---|
[b2e077a] | 104 | # convert the tag to binary |
---|
| 105 | tag = struct.pack('>b', self.tag) |
---|
[8979f90] | 106 | |
---|
| 107 | # convert each of the attributes to binary |
---|
[a76f440] | 108 | attributes = [a.packed_value for a in self.attributes] |
---|
[8979f90] | 109 | |
---|
| 110 | # concatenate everything and return |
---|
| 111 | return tag + ''.join(attributes) |
---|
[94211df] | 112 | |
---|
| 113 | def __repr__(self): |
---|
[b2e077a] | 114 | return '<IPPAttributeGroup (%r, %r)>' % (self.tag, self.attributes) |
---|