source: server/lib/ipp/attribute.py @ 7a1c039

no-cups
Last change on this file since 7a1c039 was 7a1c039, checked in by Quentin Smith <quentin@…>, 13 years ago

Move IPP modules into the 'ipp' namespace

  • Property mode set to 100644
File size: 5.1 KB
Line 
1#!/usr/bin/python
2
3import sys, struct, logging
4from .value import Value
5
6# initialize logger
7logger = logging.getLogger(__name__)
8
9class Attribute(object):
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=None, values=None):
43        """
44        Initialize an Attribute.  This function can be called in three
45        different ways:
46
47            Attribute() -- creates an empty Attribute
48
49            Attribute(name) -- creates an empty Attribute with a name
50
51            Attribute(name, values) -- creates an Attribute
52            initialized with a name and list of values
53       
54        Arguments:
55
56            name -- the name of the attribute
57
58            values -- a list of Values.  May not be empty.
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 %r must be of type Value" % (value)
69
70        self.name = name
71        self.values = values
72
73    @property
74    def packed_value(self):
75        """
76        Packs the attribute data into binary data.
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            # get the value length and binary value
96            value_bin = v.packed_value
97            value_length = len(value_bin)
98
99            logger.debug("dumping name_length : %i" % name_length)
100            logger.debug("dumping name : %s" % self.name)
101            logger.debug("dumping value_length : %i" % value_length)
102            logger.debug("dumping value : %s" % v.value)
103
104            # the value tag in binary
105            value_tag_bin = struct.pack('>b', v.value_tag)
106
107            # the name length in binary
108            name_length_bin = struct.pack('>h', name_length)
109
110            # the name in binary
111            name_bin = self.name
112
113            # the value length in binary
114            value_length_bin = struct.pack('>h', value_length)
115
116            if i == 0:
117                values.append(''.join([value_tag_bin,
118                                       name_length_bin,
119                                       name_bin,
120                                       value_length_bin,
121                                       value_bin]))
122            else:
123                values.append(''.join([value_tag_bin,
124                                       name_length_bin,
125                                       value_length_bin,
126                                       value_bin]))
127
128        # concatenate everything together and return it along with the
129        # total length of the attribute
130        return ''.join(values)
131
132    @property
133    def packed_value_size(self):
134        """
135        Gets the total size of the attribute.
136        """
137        return len(self.packed_value)
138
139    total_size = packed_value_size
140
141    def __str__(self):
142        if len(self.values) > 0:
143            values = [str(v) for v in self.values]
144        else:
145            values = "None"
146
147        if self.name is None:
148            name = "None"
149        else:
150            name = self.name
151       
152        return "%s: %s" % (name, str(values))
153
154    def __repr__(self):
155        return '<IPPAttribute (%r, %r)>' % (self.name, self.values)
Note: See TracBrowser for help on using the repository browser.