1 | #!/usr/bin/python |
---|
2 | |
---|
3 | import sys, struct, logging |
---|
4 | from ippattribute import Attribute |
---|
5 | |
---|
6 | # initialize logger |
---|
7 | logger = logging.getLogger("ippLogger") |
---|
8 | |
---|
9 | class AttributeGroup(): |
---|
10 | """ |
---|
11 | An AttributeGroup consists of an attribute-group-tag, followed by |
---|
12 | a sequence of Attributes. |
---|
13 | """ |
---|
14 | |
---|
15 | def __init__(self, attribute_group_tag=None, attributes=[]): |
---|
16 | """ |
---|
17 | Initialize an AttributeGroup. An AttributeGroup can be |
---|
18 | initialized in three ways: |
---|
19 | |
---|
20 | AttributeGroup() |
---|
21 | AttributeGroup(attribute_group_tag) |
---|
22 | AttributeGroup(attribute_group_tag, attributes) |
---|
23 | |
---|
24 | Arguments: |
---|
25 | |
---|
26 | attribute_group_tag -- a signed char, holds the tag of the |
---|
27 | attribute group |
---|
28 | |
---|
29 | attributes -- a list of attributes |
---|
30 | """ |
---|
31 | |
---|
32 | if attribute_group_tag is not None: |
---|
33 | assert isinstance(attribute_group_tag, char), \ |
---|
34 | "attribute_group_tag must be a character!" |
---|
35 | |
---|
36 | |
---|
37 | if len(attributes) > 0: |
---|
38 | for a in attributes: |
---|
39 | assert isinstance(a, Attribute), \ |
---|
40 | "attribute must be of type Attribute!" |
---|
41 | |
---|
42 | self.attribute_group_tag = attribute_group_tag |
---|
43 | self.attributes = attributes |
---|
44 | |
---|
45 | def getAttribute(self, name): |
---|
46 | """ |
---|
47 | Returns a list of attributes which have name 'name'. |
---|
48 | """ |
---|
49 | |
---|
50 | return filter(lambda x: x.name == name, self.attributes) |
---|
51 | |
---|
52 | def getTag(self): |
---|
53 | """ |
---|
54 | Return the attribute group tag. |
---|
55 | """ |
---|
56 | |
---|
57 | return self.attribute_group_tag |
---|
58 | |
---|
59 | def getAttributes(self): |
---|
60 | """ |
---|
61 | Return the list of attributes in this attribue group. |
---|
62 | """ |
---|
63 | |
---|
64 | return self.attributes |
---|
65 | |
---|
66 | def setAttributes(self, attributes): |
---|
67 | """ |
---|
68 | Sets the attributes for the attribute group. |
---|
69 | """ |
---|
70 | |
---|
71 | for a in attributes: |
---|
72 | assert isinstance(a, Attribute), \ |
---|
73 | "attribute must be of type Attribute!" |
---|
74 | |
---|
75 | self.attributes = attributes |
---|
76 | |
---|
77 | def addAttribute(self, attribute): |
---|
78 | """ |
---|
79 | Adds an attribute to the list of attributes for the attribute |
---|
80 | group. |
---|
81 | """ |
---|
82 | |
---|
83 | assert isinstance(attribute, Attribute), \ |
---|
84 | "attribute must be of type Attribute!" |
---|
85 | |
---|
86 | self.attributes.append(attribute) |
---|
87 | |
---|
88 | def setTag(self, tag): |
---|
89 | """ |
---|
90 | Sets the attribute group tag. |
---|
91 | """ |
---|
92 | |
---|
93 | assert isinstance(tag, char), \ |
---|
94 | "attribute tag must be a character!" |
---|
95 | |
---|
96 | self.attribute_group_tag = tag |
---|
97 | |
---|
98 | def pack(self): |
---|
99 | """ |
---|
100 | Convert the AttributeGroup to binary. |
---|
101 | """ |
---|
102 | |
---|
103 | # conver the attribute_group_tag to binary |
---|
104 | tag = struct.pack('>b', self.attribute_group_tag) |
---|
105 | |
---|
106 | # convert each of the attributes to binary |
---|
107 | attributes = [a.toBinaryData() for a in self.attributes] |
---|
108 | |
---|
109 | # concatenate everything and return |
---|
110 | return tag + ''.join(attributes) |
---|