source: server/lib/gutenbach/ipp/attribute.py @ cad7502

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

Fix bugs; can now again do 'lpq' and get back a reasonable response

  • Property mode set to 100644
File size: 5.1 KB
Line 
1from .value import Value
2import sys
3import struct
4import logging
5
6# initialize logger
7logger = logging.getLogger(__name__)
8
9class Attribute(object):
10    """In addition to what the RFC reports, an attribute has an
11    'attribute tag', which specifies what type of attribute it is.  It
12    is 1 bytes long, and comes before the list of values.
13
14    From RFC 2565:
15
16    Each attribute consists of:
17    -----------------------------------------------
18    |                   value-tag                 |   1 byte
19    -----------------------------------------------
20    |               name-length  (value is u)     |   2 bytes
21    -----------------------------------------------
22    |                     name                    |   u bytes
23    -----------------------------------------------
24    |              value-length  (value is v)     |   2 bytes
25    -----------------------------------------------
26    |                     value                   |   v bytes
27    -----------------------------------------------
28
29    An additional value consists of:
30    -----------------------------------------------------------
31    |                   value-tag                 |   1 byte  |
32    -----------------------------------------------           |
33    |            name-length  (value is 0x0000)   |   2 bytes |
34    -----------------------------------------------           |-0 or more
35    |              value-length (value is w)      |   2 bytes |
36    -----------------------------------------------           |
37    |                     value                   |   w bytes |
38    -----------------------------------------------------------
39
40    """
41
42    def __init__(self, name=None, values=None):
43        """Initialize an Attribute.  This function can be called in
44        three different ways:
45
46            Attribute() -- creates an empty Attribute
47
48            Attribute(name) -- creates an empty Attribute with a name
49
50            Attribute(name, values) -- creates an Attribute
51            initialized with a name and list of values
52       
53        Arguments:
54
55            name -- the name of the attribute
56
57            values -- a list of Values.  May not be empty.
58
59        """
60
61        if name is not None:
62            assert isinstance(name, str), \
63                   "Attribute name must be a string!"
64        if values is None:
65            values = []
66        for value in values:
67            assert isinstance(value, Value), \
68                   "Value %s must be of type Value" % (value,)
69
70        self.name = name
71        self.values = values
72
73    @property
74    def packed_value(self):
75        """Packs the attribute data into binary data.
76       
77        """
78
79        assert self.name is not None, \
80               "cannot pack unnamed attribute!"
81        assert len(self.values) > 0, \
82               "cannot pack empty attribute!"
83
84        # get the binary data for all the values
85        values = []
86        for v, i in zip(self.values, xrange(len(self.values))):
87
88            # get the name length (0 for everything but the first
89            # value)
90            if i == 0:
91                name_length = len(self.name)
92            else:
93                name_length = 0
94
95            logger.debug("dumping name : %s" % self.name)
96            logger.debug("dumping name_length : %i" % name_length)
97            logger.debug("value tag : 0x%x" % v.tag)
98
99            # get the binary value
100            value_bin = v.packed_value
101            # get the value length
102            value_length = len(value_bin)
103
104            logger.debug("dumping value : %s" % v.value)
105            logger.debug("dumping value_length : %i" % value_length)
106
107            # the value tag in binary
108            tag_bin = struct.pack('>b', v.tag)
109
110            # the name length in binary
111            name_length_bin = struct.pack('>h', name_length)
112
113            # the name in binary
114            name_bin = self.name
115
116            # the value length in binary
117            value_length_bin = struct.pack('>h', value_length)
118
119            if i == 0:
120                values.append(''.join([tag_bin,
121                                       name_length_bin,
122                                       name_bin,
123                                       value_length_bin,
124                                       value_bin]))
125            else:
126                values.append(''.join([tag_bin,
127                                       name_length_bin,
128                                       value_length_bin,
129                                       value_bin]))
130
131        # concatenate everything together and return it along with the
132        # total length of the attribute
133        return ''.join(values)
134
135    @property
136    def packed_value_size(self):
137        """Gets the total size of the attribute.
138       
139        """
140
141        return len(self.packed_value)
142
143    total_size = packed_value_size
144
145    def __str__(self):
146        if len(self.values) > 0:
147            values = [str(v) for v in self.values]
148        else:
149            values = "None"
150
151        if self.name is None:
152            name = "None"
153        else:
154            name = self.name
155       
156        return "%s: %s" % (name, str(values))
157
158    def __repr__(self):
159        return '<IPPAttribute (%r, %r)>' % (self.name, self.values)
Note: See TracBrowser for help on using the repository browser.