source: server/lib/ippattribute.py @ fc427ef

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

Adopt property usage for attributes, too

  • Property mode set to 100644
File size: 5.2 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(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=[]):
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        for value in values:
65            assert isinstance(value, Value), \
66                   "Value must be of type Value"
67
68        self.name = None
69        self.values = None
70
71        if name is not None:
72            self.name = name
73        if name is not None and len(values) > 0:
74            self.values = values
75            self.binary = self.pack()
76            self.verify()
77
78    @property
79    def packed_value(self):
80        """
81        Packs the attribute data into binary data.
82        """
83
84        assert self.name is not None, \
85               "cannot pack unnamed attribute!"
86        assert len(self.values) > 0, \
87               "cannot pack empty attribute!"
88
89        # get the binary data for all the values
90        values = []
91        for v, i in zip(self.values, xrange(len(self.values))):
92
93            # get the name length (0 for everything but the first
94            # value)
95            if i == 0:
96                name_length = len(self.name)
97            else:
98                name_length = 0
99
100            # get the value length and binary value
101            value_bin = v.packed_value
102            value_length = len(value_bin)
103
104            logger.debug("dumping name_length : %i" % name_length)
105            logger.debug("dumping name : %s" % self.name)
106            logger.debug("dumping value_length : %i" % value_length)
107            logger.debug("dumping value : %s" % v.value)
108
109            # the value tag in binary
110            value_tag_bin = struct.pack('>b', v.value_tag)
111
112            # the name length in binary
113            name_length_bin = struct.pack('>h', name_length)
114
115            # the name in binary
116            name_bin = self.name
117
118            # the value length in binary
119            value_length_bin = struct.pack('>h', value_length)
120
121            if i == 0:
122                values.append(''.join([value_tag_bin,
123                                       name_length_bin,
124                                       name_bin,
125                                       value_length_bin,
126                                       value_bin]))
127            else:
128                values.append(''.join([value_tag_bin,
129                                       name_length_bin,
130                                       value_length_bin,
131                                       value_bin]))
132
133        # concatenate everything together and return it along with the
134        # total length of the attribute
135        return ''.join(values)
136
137    @property
138    def packed_value_size(self):
139        """
140        Gets the total size of the attribute.
141        """
142        return 2+len(self.name)+sum(v.total_size for v in self.values)
143
144    total_size = packed_value_size
145
146    def __str__(self):
147        if len(self.values) > 0:
148            values = [str(v) for v in self.values]
149        else:
150            values = "None"
151
152        if self.name is None:
153            name = "None"
154        else:
155            name = self.name
156       
157        return "%s: %s" % (name, str(values))
Note: See TracBrowser for help on using the repository browser.