- Timestamp:
- Nov 6, 2010, 7:56:41 PM (14 years ago)
- Branches:
- no-cups
- Children:
- aaa1da3
- Parents:
- 478ca74
- git-author:
- Jessica B. Hamrick <jhamrick@…> (11/06/10 19:56:28)
- git-committer:
- Jessica B. Hamrick <jhamrick@…> (11/06/10 19:56:41)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
server/lib/ipprequest.py
r8403f61 r8e43aa8 86 86 assert value is not None 87 87 88 self.value_tag = hex(value_tag)89 self.value = str(value)88 self.value_tag = value_tag 89 self.value = value 90 90 91 91 class IPPAttribute(): … … 128 128 Arguments: 129 129 130 name -- the name of the attribute131 132 values -- a list of IPPValues. May not be empty.130 name -- the name of the attribute 131 132 values -- a list of IPPValues. May not be empty. 133 133 """ 134 134 … … 141 141 for value in values: assert isinstance(value, IPPValue) 142 142 143 self.name = str(name)143 self.name = name 144 144 self.values = values 145 145 … … 152 152 values = [] 153 153 for v, i in zip(self.values, xrange(len(self.values))): 154 name_length = len(self.name) 155 if i > 0: name_length = 0 156 value_length = len(v.value) 157 158 logger.debug("dumping name_length : %i" % name_length) 159 logger.debug("dumping name : %s" % self.name) 160 logger.debug("dumping value_length : %i" % value_length) 161 logger.debug("dumping value : %s" % v.value) 162 163 # the value tag in binary 164 value_tag_bin = struct.pack('>b', v.value_tag) 165 166 # the name length in binary 167 name_length_bin = struct.pack('>h', name_length) 168 169 # the name in binary 170 name_bin = self.name 171 172 # the value length in binary 173 value_length_bin = struct.pack('>h', value_length) 174 175 # the value in binary 176 value_bin = v.value 177 154 178 if i == 0: 155 name_length = sys.getsizeof(self.name) 156 value_length = sys.getsizeof(v.value) 157 values.append(struct.pack('>bh%ssh%ss' % (name_length, value_length), 158 v.value_tag, 159 name_length, 160 self.name, 161 value_length, 162 v.value)) 179 values.append(''.join([value_tag_bin, 180 name_length_bin, 181 name_bin, 182 value_length_bin, 183 value_bin])) 163 184 else: 164 value_length = sys.getsizeof(v.value) 165 values.append(struct.pack('>bhh%ss' % (value_length), 166 v.value_tag, 167 name_length, 168 value_length, 169 v.value)) 185 values.append(''.join([value_tag_bin, 186 name_length_bin, 187 value_length_bin, 188 value_bin])) 170 189 171 190 # concatenate everything together and return it 172 191 return ''.join(values) 192 193 class IPPAttributeGroup(): 194 """ 195 An IPPAttributeGroup consists of an attribute-group-tag, followed 196 by a sequence of IPPAttributes. 197 """ 198 199 def __init__(self, attribute_group_tag, attributes=[]): 200 """ 201 Initialize an IPPAttributeGroup. 202 203 Arguments: 204 205 attribute_group_tag -- a signed char, holds the tag of the 206 attribute group 207 208 attributes -- (optional) a list of attributes 209 """ 210 211 # make sure attribute_group_tag isn't empty 212 assert attribute_group_tag is not None 213 214 # make sure attributes is a list or tuple of IPPAttributes 215 assert isinstance(attributes, (list, tuple)) 216 for a in attributes: assert isinstance(a, IPPAttribute) 217 218 self.attribute_group_tag = attribute_group_tag 219 self.attributes = attributes 220 221 def toBinaryData(self): 222 """ 223 Convert the IPPAttributeGroup to binary. 224 """ 225 226 # conver the attribute_group_tag to binary 227 tag = struct.pack('>b', self.attribute_group_tag) 228 229 # convert each of the attributes to binary 230 attributes = [a.toBinaryData() for a in self.attributes] 231 232 # concatenate everything and return 233 return tag + ''.join(attributes) 173 234 174 235 class IPPRequest(): … … 199 260 # attribute_sequence, and data, or a file handler (request) which 200 261 # can be read from to get the request 201 def __init__(self, version=None, operation_id=None, request_id=None, attributes=[], data=None, request=None): 262 def __init__(self, version=None, operation_id=None, request_id=None, 263 attribute_groups=[], data=None, request=None): 202 264 """ 203 265 Create an IPPRequest. Takes either the segments of the … … 218 280 request itself. 219 281 220 attribute s -- (optional) a list of IPPAttributes282 attribute_groups -- a list of IPPAttributes, at least length 1 221 283 222 284 data -- (optional) variable length, containing the actual … … 239 301 # make sure the request id isn't empty 240 302 assert request_id is not None 303 # make sure attribute_groups is a list of IPPAttributes 304 assert len(attribute_groups) > 0 305 for a in attribute_groups: assert isinstance(a, IPPAttribute) 241 306 242 307 # if the request isn't None, then we'll read directly from … … 258 323 # now we have to read in the attributes. Each attribute 259 324 # has a tag (1 byte) and a sequence of values (n bytes) 260 self.attribute s= []325 self.attribute_groups = [] 261 326 262 327 # read in the next byte … … 271 336 logger.debug("attribute-tag : %i" % attribute_group_tag) 272 337 338 attributes = [] 339 273 340 next_byte = struct.unpack('>b', request.read(1))[0] 274 341 logger.debug("next byte : 0x%X" % next_byte) … … 299 366 # create a new IPPAttribute from the data we just 300 367 # read in, and add it to our attributes list 301 self.attributes.append(IPPAttribute(name,302 368 attributes.append(IPPAttribute(name, 369 [IPPValue(value_tag, value)])) 303 370 304 371 else: … … 312 379 313 380 # add another value to the last attribute 314 self.attributes[-1].values.append(IPPValue(value_tag, value))381 attributes[-1].values.append(IPPValue(value_tag, value)) 315 382 316 383 # read another byte 317 384 next_byte = struct.unpack('>b', request.read(1))[0] 385 386 self.attribute_groups.append(IPPAttributeGroup(attribute_group_tag, attributes)) 318 387 319 388 # once we hit the end-of-attributes tag, the only thing … … 325 394 # arguments passed in 326 395 else: 327 self.version = ( int(version[0]), int(version[1]))328 self.operation_id = int(operation_id)329 self.request_id = int(request_id)330 self.attribute s = attributes396 self.version = (version[0], version[1]) 397 self.operation_id = operation_id 398 self.request_id = request_id 399 self.attribute_groups = attribute_groups 331 400 self.data = data 332 401 … … 343 412 self.request_id) 344 413 345 # convert the attribute s to binary346 attribute s = ''.join([a.toBinaryData() for a in self.attributes])414 # convert the attribute groups to binary 415 attribute_groups = ''.join([a.toBinaryData() for a in self.attribute_groups]) 347 416 348 417 # conver the end-of-attributes-tag to binary … … 356 425 357 426 # append everything together and return it 358 return preattributes + attribute s + end_of_attributes_tag + data427 return preattributes + attribute_groups + end_of_attributes_tag + data
Note: See TracChangeset
for help on using the changeset viewer.