Changeset c269bc7
- Timestamp:
- Mar 7, 2011, 7:47:42 PM (14 years ago)
- Branches:
- no-cups
- Children:
- fa0d0ef
- Parents:
- 5cfb358
- git-author:
- Jessica B. Hamrick <jhamrick@…> (03/07/11 19:47:42)
- git-committer:
- Jessica B. Hamrick <jhamrick@…> (03/07/11 19:47:42)
- Location:
- server/lib
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
server/lib/ippattribute.py
rebf327d rc269bc7 40 40 """ 41 41 42 def __init__(self, name, values): 43 """ 44 Initialize an Attribute. 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 45 53 46 54 Arguments: … … 51 59 """ 52 60 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): 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 def pack(self): 65 79 """ 66 80 Packs the attribute data into binary data. 67 81 """ 82 83 assert self.name is not None, \ 84 "cannot pack unnamed attribute!" 85 assert len(self.values) > 0, \ 86 "cannot pack empty attribute!" 68 87 69 88 # get the binary data for all the values 70 89 values = [] 90 length = 0 71 91 for v, i in zip(self.values, xrange(len(self.values))): 72 92 … … 79 99 80 100 # get the value length and binary value 81 value_length, value_bin = v. valueToBinary()101 value_length, value_bin = v.pack() 82 102 83 103 logger.debug("dumping name_length : %i" % name_length) 84 104 logger.debug("dumping name : %s" % self.name) 85 105 logger.debug("dumping value_length : %i" % value_length) 86 logger.debug("dumping value : %s" % v. value)106 logger.debug("dumping value : %s" % v.getValue()) 87 107 88 108 # the value tag in binary 89 value_tag_bin = struct.pack('>b', v. value_tag)109 value_tag_bin = struct.pack('>b', v.getValueTag()) 90 110 91 111 # the name length in binary … … 109 129 value_length_bin, 110 130 value_bin])) 131 132 length += 2 # name-length 133 length += name_length # name 134 length += value_length # value 111 135 112 # concatenate everything together and return it 113 return ''.join(values) 136 # concatenate everything together and return it along with the 137 # total length of the attribute 138 return length, ''.join(values) 139 140 def getName(self): 141 """ 142 Get the attribute's name. 143 """ 144 145 return self.name 146 147 def getValues(self): 148 """ 149 Get the list of values contained in the attribute. 150 """ 151 152 return self.values 153 154 def setName(self, name): 155 """ 156 Set the attribute's name. 157 """ 158 159 assert isinstance(name, str), \ 160 "name must be a string!" 161 162 self.name = name 163 164 def setValues(self, values): 165 """ 166 Set the list of values contained in the attribute. 167 """ 168 169 for v in values: 170 assert isinstance(v, Value), \ 171 "value must be of type Value!" 172 173 self.values = values 174 175 def addValue(self, value): 176 """ 177 Add a new value to the list of values contained in the 178 attribute." 179 180 assert isinstance(value, Value), \ 181 "value must be of type Value!" 182 183 self.values.append(value) 184 185 def getSize(self): 186 """ 187 Gets the total size of the attribute. 188 """ 189 190 size = 2 + len(self.name) 191 for v in self.values: 192 size += v.getTotalSize() 193 194 def verify(self): 195 """ 196 Check to make sure that the Attribute is valid. This means 197 that is should have a name and a list of values, and that its 198 binary representation should match up to the stored binary 199 representation. 200 """ 201 202 assert self.name is not None, \ 203 "attribute has no name!" 204 assert len(self.value) > 0, \ 205 "attribute has no values!" 206 207 size, bindata = self.pack() 208 209 assert size == self.getSize(), \ 210 "pack size does not match self.getSize()!" 211 assert bindata == self.binary_data, \ 212 "packed binary data does not match self.binary_data!" 213 214 def __str__(self): 215 if len(self.values) > 0: 216 values = [str(v) for v in self.values] 217 else: 218 values = "None" 219 220 if self.name is None: 221 name = "None" 222 else: 223 name = self.name 224 225 return "%s: %s" % (name, str(values)) -
server/lib/ippattributegroup.py
rebf327d rc269bc7 13 13 """ 14 14 15 def __init__(self, attribute_group_tag , attributes=[]):15 def __init__(self, attribute_group_tag=None, attributes=[]): 16 16 """ 17 Initialize an AttributeGroup. 17 Initialize an AttributeGroup. An AttributeGroup can be 18 initialized in three ways: 19 20 AttributeGroup() 21 AttributeGroup(attribute_group_tag) 22 AttributeGroup(attribute_group_tag, attributes) 18 23 19 24 Arguments: … … 22 27 attribute group 23 28 24 attributes -- (optional)a list of attributes29 attributes -- a list of attributes 25 30 """ 26 31 27 # make sure attribute_group_tag isn't empty 28 assert attribute_group_tag is not None 32 if attribute_group_tag is not None: 33 assert isinstance(attribute_group_tag, char), \ 34 "attribute_group_tag must be a character!" 35 29 36 30 # make sure attributes is a list or tuple of Attributes 31 assert isinstance(attributes, (list, tuple)) 32 for a in attributes: assert isinstance(a, Attribute) 37 if len(attributes) > 0: 38 for a in attributes: 39 assert isinstance(a, Attribute), \ 40 "attribute must be of type Attribute!" 33 41 34 42 self.attribute_group_tag = attribute_group_tag … … 36 44 37 45 def getAttribute(self, name): 46 """ 47 Returns a list of attributes which have name 'name'. 48 """ 49 38 50 return filter(lambda x: x.name == name, self.attributes) 39 51 40 def toBinaryData(self): 52 def getTag(self): 53 """ 54 Return the attribute group tag. 55 """ 56 57 return self.attribute_group_tag 58 59 def getAttributes(self): 60 """ 61 Return the list of attributes in this attribue group. 62 """ 63 64 return self.attributes 65 66 def setAttributes(self, attributes): 67 """ 68 Sets the attributes for the attribute group. 69 """ 70 71 for a in attributes: 72 assert isinstance(a, Attribute), \ 73 "attribute must be of type Attribute!" 74 75 self.attributes = attributes 76 77 def addAttribute(self, attribute): 78 """ 79 Adds an attribute to the list of attributes for the attribute 80 group. 81 """ 82 83 assert isinstance(attribute, Attribute), \ 84 "attribute must be of type Attribute!" 85 86 self.attributes.append(attribute) 87 88 def setTag(self, tag): 89 """ 90 Sets the attribute group tag. 91 """ 92 93 assert isinstance(tag, char), \ 94 "attribute tag must be a character!" 95 96 self.attribute_group_tag = tag 97 98 def pack(self): 41 99 """ 42 100 Convert the AttributeGroup to binary. -
server/lib/ippvalue.py
r5cfb358 rc269bc7 65 65 self.value_tag = None # one byte, the type of value 66 66 self.value = None # non-binary value of self.value 67 self.value_size = None # size of self.value 67 self.tag_size = 1 # length of the tag in bytes 68 self.value_size = None # size of self.value in bytes 68 69 self.binary_value = None # binary value of self.value 69 70 … … 72 73 self.value_tag = value_tag 73 74 self.binary_value = value 74 self.unpack() 75 self.pack() 75 self.value_size, self.value = self.unpack() 76 76 else: 77 77 self.value_tag = value_tag 78 78 self.value = value 79 self.pack() 80 self.unpack() 79 self.value_size, self.binary_value = self.pack() 80 81 self.verify() 81 82 82 83 def unpack(self): … … 196 197 "unpacked value is not the same as self.value!" 197 198 198 self.value_size = value_size199 self.value = value200 201 199 return value_size, value 202 200 … … 313 311 binary_value = self.value 314 312 315 if self.value_size is not None:316 assert value_size == self.value_size, \317 "packed value size is not the same as " + \318 "self.value_size!"319 if self.binary_value is not None:320 assert binary_value == self.binary_value, \321 "packed binary value is not the same as " + \322 "self.binary_value!"323 324 self.value_size = value_size325 self.binary_value = binary_value326 327 313 return value_size, binary_value 328 314 … … 346 332 "value type is unknown!" 347 333 348 self.pack() 349 self.unpack() 334 value_size1, binary_value = self.pack() 335 value_size2, value = self.unpack() 336 337 assert value_size1 == value_size2, \ 338 "packed value size is not the same as " + \ 339 "unpacked value size!" 340 assert value_size == self.value_size, \ 341 "packed value size is not the same as " + \ 342 "self.value_size!" 343 assert binary_value == self.binary_value, \ 344 "packed binary value is not the same as " + \ 345 "self.binary_value!" 350 346 351 347 def getValue(self): … … 371 367 return self.value_tag 372 368 373 def get Size(self):369 def getValueSize(self): 374 370 """ 375 371 Get the size of the value in bytes. … … 378 374 return self.value_size 379 375 376 def getTagSize(self): 377 """ 378 Get the size of the value tag in bytes. 379 """ 380 381 return self.tag_size 382 380 383 def setValue(self, value): 381 384 """ 382 385 Set the non-binary value. 383 386 """ 384 387 385 388 self.value = value 386 389 … … 389 392 Set the binary value. 390 393 """ 394 391 395 self.binary_value = binary_value 392 396 … … 399 403 self.value_tag = value_tag 400 404 401 def set Size(self, size):405 def setValueSize(self, size): 402 406 """ 403 407 Set the size of the value in bytes. … … 405 409 406 410 self.value_size = size 411 412 def getTotalSize(self): 413 """ 414 Get the total size of the IPP value. 415 """ 416 417 return self.value_size + self.tag_size 407 418 408 419 def __str__(self):
Note: See TracChangeset
for help on using the changeset viewer.