source: server/lib/ippattribute.py @ ebf327d

no-cups
Last change on this file since ebf327d was ebf327d, checked in by Jessica B. Hamrick <jhamrick@…>, 13 years ago

Change IPPValue, IPPAttribute, IPPAttributeGroup, and IPPRequest to Value, Attribute, AttributeGroup?, and Request, respectively, to make things a little less verbose

  • Property mode set to 100644
File size: 3.9 KB
Line 
1#!/usr/bin/python
2
3import sys, struct, logging
4from ippvalue import Value
5
6# initialize logger
7logger = logging.getLogger("ippLogger")
8
9class Attribute():
10    """
11    In addition to what the RFC reports, an attribute has an
12    'attribute tag', which specifies what type of attribute it is.
13    It is 1 bytes long, and comes before the list of values.
14
15    From RFC 2565:
16
17    Each attribute consists of:
18    -----------------------------------------------
19    |                   value-tag                 |   1 byte
20    -----------------------------------------------
21    |               name-length  (value is u)     |   2 bytes
22    -----------------------------------------------
23    |                     name                    |   u bytes
24    -----------------------------------------------
25    |              value-length  (value is v)     |   2 bytes
26    -----------------------------------------------
27    |                     value                   |   v bytes
28    -----------------------------------------------
29
30    An additional value consists of:
31    -----------------------------------------------------------
32    |                   value-tag                 |   1 byte  |
33    -----------------------------------------------           |
34    |            name-length  (value is 0x0000)   |   2 bytes |
35    -----------------------------------------------           |-0 or more
36    |              value-length (value is w)      |   2 bytes |
37    -----------------------------------------------           |
38    |                     value                   |   w bytes |
39    -----------------------------------------------------------
40    """
41
42    def __init__(self, name, values):
43        """
44        Initialize an Attribute.
45       
46        Arguments:
47
48            name -- the name of the attribute
49
50            values -- a list of Values.  May not be empty.
51        """
52
53        # make sure name isn't empty
54        assert name is not None
55         
56        # make sure the list of values isn't empty
57        assert len(values) > 0
58        # make sure each value is a Value
59        for value in values: assert isinstance(value, Value)
60         
61        self.name = name
62        self.values = values
63
64    def toBinaryData(self):
65        """
66        Packs the attribute data into binary data.
67        """
68
69        # get the binary data for all the values
70        values = []
71        for v, i in zip(self.values, xrange(len(self.values))):
72
73            # get the name length (0 for everything but the first
74            # value)
75            if i == 0:
76                name_length = len(self.name)
77            else:
78                name_length = 0
79
80            # get the value length and binary value
81            value_length, value_bin = v.valueToBinary()
82
83            logger.debug("dumping name_length : %i" % name_length)
84            logger.debug("dumping name : %s" % self.name)
85            logger.debug("dumping value_length : %i" % value_length)
86            logger.debug("dumping value : %s" % v.value)
87
88            # the value tag in binary
89            value_tag_bin = struct.pack('>b', v.value_tag)
90
91            # the name length in binary
92            name_length_bin = struct.pack('>h', name_length)
93
94            # the name in binary
95            name_bin = self.name
96
97            # the value length in binary
98            value_length_bin = struct.pack('>h', value_length)
99
100            if i == 0:
101                values.append(''.join([value_tag_bin,
102                                       name_length_bin,
103                                       name_bin,
104                                       value_length_bin,
105                                       value_bin]))
106            else:
107                values.append(''.join([value_tag_bin,
108                                       name_length_bin,
109                                       value_length_bin,
110                                       value_bin]))
111               
112        # concatenate everything together and return it
113        return ''.join(values)
Note: See TracBrowser for help on using the repository browser.