[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 | |
---|
[ffbe41d] | 35 | if tag is not None and not isinstance(tag, int): |
---|
| 36 | raise ValueError("tag must be a character") |
---|
[8979f90] | 37 | |
---|
[b2e077a] | 38 | self.tag = tag |
---|
[569c377] | 39 | self.attributes = [] |
---|
[a76f440] | 40 | self.extend(attributes) |
---|
[8979f90] | 41 | |
---|
[b828a96] | 42 | def __cmp__(self, other): |
---|
| 43 | eq = self.tag == other.tag |
---|
| 44 | for a1, a2 in zip(self.attributes, other.attributes): |
---|
| 45 | eq = eq and (a1 == a2) |
---|
| 46 | return 0 if eq else 1 |
---|
| 47 | |
---|
[a76f440] | 48 | def __getitem__(self, name): |
---|
[5c5fe6d] | 49 | """Returns a list of attributes which have name 'name'. |
---|
| 50 | |
---|
[c269bc7] | 51 | """ |
---|
| 52 | |
---|
[a76f440] | 53 | attribute = filter(lambda x: x.name == name, self.attributes) |
---|
| 54 | if attribute: |
---|
| 55 | return attribute[0] |
---|
| 56 | else: |
---|
| 57 | raise KeyError("Attribute %r not found" % name) |
---|
[8979f90] | 58 | |
---|
[a76f440] | 59 | def __contains__(self, name): |
---|
| 60 | return len(filter(lambda x: x.name == name, self.attributes)) |
---|
[c269bc7] | 61 | |
---|
[a76f440] | 62 | def __iter__(self): |
---|
| 63 | return (a.name for a in self.attributes) |
---|
[776a659] | 64 | |
---|
[a76f440] | 65 | iterkeys = __iter__ |
---|
[c269bc7] | 66 | |
---|
[a76f440] | 67 | def __setitem__(self, name, attribute): |
---|
[5c5fe6d] | 68 | """Sets an attribute in the attribute group. Note that the key |
---|
| 69 | is ignored and the attribute is queried for its name. |
---|
| 70 | |
---|
[c269bc7] | 71 | """ |
---|
| 72 | |
---|
[a76f440] | 73 | return self.append(attribute) |
---|
| 74 | |
---|
| 75 | def __delitem__(self, name): |
---|
[776a659] | 76 | self.attributes = filter(lambda x: x.name != name, |
---|
| 77 | self.attributes) |
---|
[c269bc7] | 78 | |
---|
[a76f440] | 79 | def append(self, attribute): |
---|
| 80 | return self.extend([attribute]) |
---|
| 81 | |
---|
| 82 | def extend(self, attributes): |
---|
[5c5fe6d] | 83 | """Sets the attributes for the attribute group. |
---|
| 84 | |
---|
[c269bc7] | 85 | """ |
---|
| 86 | |
---|
| 87 | for a in attributes: |
---|
[ffbe41d] | 88 | if not isinstance(a, Attribute): |
---|
| 89 | raise ValueError("attribute must be of type Attribute") |
---|
[c269bc7] | 90 | |
---|
[a76f440] | 91 | for a in attributes: |
---|
| 92 | # XXX: Instead of replacing the attribute, do we want to |
---|
| 93 | # append the value to the attribute here? |
---|
| 94 | del self[a.name] |
---|
| 95 | self.attributes.append(a) |
---|
[c269bc7] | 96 | |
---|
[a76f440] | 97 | @property |
---|
| 98 | def packed_value(self): |
---|
[5c5fe6d] | 99 | """Convert the AttributeGroup to binary. |
---|
| 100 | |
---|
[8979f90] | 101 | """ |
---|
| 102 | |
---|
[b2e077a] | 103 | # convert the tag to binary |
---|
| 104 | tag = struct.pack('>b', self.tag) |
---|
[8979f90] | 105 | |
---|
| 106 | # convert each of the attributes to binary |
---|
[a76f440] | 107 | attributes = [a.packed_value for a in self.attributes] |
---|
[8979f90] | 108 | |
---|
| 109 | # concatenate everything and return |
---|
| 110 | return tag + ''.join(attributes) |
---|
[94211df] | 111 | |
---|
| 112 | def __repr__(self): |
---|
[b2e077a] | 113 | return '<IPPAttributeGroup (%r, %r)>' % (self.tag, self.attributes) |
---|