- Timestamp:
- 05/13/05 23:48:18 (20 years ago)
- Location:
- tea4cups/trunk
- Files:
-
- 2 modified
Legend:
- Unmodified
- Added
- Removed
-
tea4cups/trunk/NEWS
r630 r631 24 24 * 2.12alpha : 25 25 26 - Greatly improved the IPP parser. 27 26 28 - When printing test pages from CUPS' web interface, the empty username 27 29 is now overwritten with the username CUPS is running as. -
tea4cups/trunk/tea4cups
r630 r631 91 91 class IPPMessage : 92 92 """A class for IPP message files.""" 93 def __init__(self, data ) :93 def __init__(self, data, debug=0) : 94 94 """Initializes an IPP Message object.""" 95 self.debug = debug 95 96 self.data = data 96 self._attributes = {} 97 self.curname = None 97 self.operation_attributes = {} 98 self.job_attributes = {} 99 self.printer_attributes = {} 98 100 self.tags = [ None ] * 256 # by default all tags reserved 99 101 … … 141 143 self.parse() 142 144 143 def __getattr__(self, attrname) :144 """Allows self.attributes to return the attributes names."""145 if attrname == "attributes" :146 keys = self._attributes.keys()147 keys.sort()148 return keys149 raise AttributeError, attrname150 151 def __getitem__(self, ippattrname) :152 """Fakes a dictionnary d['key'] notation."""153 value = self._attributes.get(ippattrname)154 if value is not None :155 if len(value) == 1 :156 value = value[0]157 return value158 get = __getitem__159 160 145 def parseTag(self) : 161 146 """Extracts information from an IPP tag.""" 162 147 pos = self.position 163 148 valuetag = self.tags[ord(self.data[pos])] 164 # print valuetag.get("name")165 149 pos += 1 166 150 posend = pos2 = pos + 2 167 151 namelength = unpack(">H", self.data[pos:pos2])[0] 168 152 if not namelength : 169 name = self. curname153 name = self._curname 170 154 else : 171 155 posend += namelength 172 self. curname = name = self.data[pos2:posend]156 self._curname = name = self.data[pos2:posend] 173 157 pos2 = posend + 2 174 158 valuelength = unpack(">H", self.data[posend:pos2])[0] 175 159 posend = pos2 + valuelength 176 160 value = self.data[pos2:posend] 177 oldval = self._attributes.setdefault(name, []) 178 oldval.append(value) 161 oldval = self._curdict.setdefault(name, []) 162 oldval.append((valuetag, value)) 163 self.printInfo("%s(%s) %s" % (name, valuetag, value)) 179 164 return posend - self.position 180 165 181 166 def operation_attributes_tag(self) : 182 167 """Indicates that the parser enters into an operation-attributes-tag group.""" 168 self.printInfo("Start of operation_attributes_tag") 169 self._curdict = self.operation_attributes 183 170 return self.parseTag() 184 171 185 172 def job_attributes_tag(self) : 186 """Indicates that the parser enters into a job-attributes-tag group.""" 173 """Indicates that the parser enters into an operation-attributes-tag group.""" 174 self.printInfo("Start of job_attributes_tag") 175 self._curdict = self.job_attributes 187 176 return self.parseTag() 188 177 189 178 def printer_attributes_tag(self) : 190 """Indicates that the parser enters into a printer-attributes-tag group.""" 179 """Indicates that the parser enters into an operation-attributes-tag group.""" 180 self.printInfo("Start of printer_attributes_tag") 181 self._curdict = self.printer_attributes 191 182 return self.parseTag() 192 183 184 def printInfo(self, msg) : 185 """Prints a debug message.""" 186 if self.debug : 187 sys.stderr.write("%s\n" % msg) 188 sys.stderr.flush() 189 193 190 def parse(self) : 194 191 """Parses an IPP Message. … … 197 194 We are only interested in textual informations for now anyway. 198 195 """ 196 self._curname = None 197 self._curdict = None 199 198 self.version = "%s.%s" % (ord(self.data[0]), ord(self.data[1])) 200 199 self.operation_id = "0x%04x" % unpack(">H", self.data[2:4])[0] … … 216 215 except IndexError : 217 216 raise IPPError, "Unexpected end of IPP message." 217 218 # Now transform all one-element lists into single values 219 for attrtype in ("operation", "job", "printer") : 220 attrdict = getattr(self, "%s_attributes" % attrtype) 221 for (key, value) in attrdict.items() : 222 if len(value) == 1 : 223 attrdict[key] = value[0] 218 224 219 225 class FakeConfig : … … 423 429 (ippfilename, ippmessage) = self.parseIPPMessageFile() 424 430 self.ControlFile = ippfilename 425 self.ClientHost = ippmessage.get("job-originating-host-name") 426 self.JobBilling = ippmessage.get("job-billing") 431 (chtype, self.ClientHost) = ippmessage.operation_attributes.get("job-originating-host-name", \ 432 ippmessage.job_attributes.get("job-originating-host-name", (None, None))) 433 (jbtype, self.JobBilling) = ippmessage.job_attributes.get("job-billing", (None, None)) 427 434 428 435 def getCupsConfigDirectives(self, directives=[]) :