[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 | |
---|
[ebf327d] | 9 | class AttributeGroup(): |
---|
[8979f90] | 10 | """ |
---|
[ebf327d] | 11 | An AttributeGroup consists of an attribute-group-tag, followed by |
---|
| 12 | a sequence of Attributes. |
---|
[8979f90] | 13 | """ |
---|
| 14 | |
---|
| 15 | def __init__(self, attribute_group_tag, attributes=[]): |
---|
| 16 | """ |
---|
[ebf327d] | 17 | Initialize an AttributeGroup. |
---|
[8979f90] | 18 | |
---|
| 19 | Arguments: |
---|
| 20 | |
---|
| 21 | attribute_group_tag -- a signed char, holds the tag of the |
---|
| 22 | attribute group |
---|
| 23 | |
---|
| 24 | attributes -- (optional) a list of attributes |
---|
| 25 | """ |
---|
| 26 | |
---|
| 27 | # make sure attribute_group_tag isn't empty |
---|
| 28 | assert attribute_group_tag is not None |
---|
| 29 | |
---|
[ebf327d] | 30 | # make sure attributes is a list or tuple of Attributes |
---|
[8979f90] | 31 | assert isinstance(attributes, (list, tuple)) |
---|
[ebf327d] | 32 | for a in attributes: assert isinstance(a, Attribute) |
---|
[8979f90] | 33 | |
---|
| 34 | self.attribute_group_tag = attribute_group_tag |
---|
| 35 | self.attributes = attributes |
---|
| 36 | |
---|
| 37 | def getAttribute(self, name): |
---|
| 38 | return filter(lambda x: x.name == name, self.attributes) |
---|
| 39 | |
---|
| 40 | def toBinaryData(self): |
---|
| 41 | """ |
---|
[ebf327d] | 42 | Convert the AttributeGroup to binary. |
---|
[8979f90] | 43 | """ |
---|
| 44 | |
---|
| 45 | # conver the attribute_group_tag to binary |
---|
| 46 | tag = struct.pack('>b', self.attribute_group_tag) |
---|
| 47 | |
---|
| 48 | # convert each of the attributes to binary |
---|
| 49 | attributes = [a.toBinaryData() for a in self.attributes] |
---|
| 50 | |
---|
| 51 | # concatenate everything and return |
---|
| 52 | return tag + ''.join(attributes) |
---|