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