| 1216 | |
| 1217 | def genBanner(self, bannerfileorcommand) : |
| 1218 | """Reads a banner or generates one through an external command. |
| 1219 | |
| 1220 | Returns the banner's content in a format which MUST be accepted |
| 1221 | by the printer. |
| 1222 | """ |
| 1223 | banner = "" # no banner by default |
| 1224 | if bannerfileorcommand : |
| 1225 | if (os.access(bannerfileorcommand, os.X_OK)) : |
| 1226 | self.logdebug("Launching %s to generate a banner." % bannerfileorcommand) |
| 1227 | child = popen2.Popen3(bannerfileorcommand, capturestderr=1) |
| 1228 | child.tochild.close() |
| 1229 | child.childerr.close() |
| 1230 | banner = child.fromchild.read() |
| 1231 | child.fromchild.close() |
| 1232 | status = child.wait() |
| 1233 | if os.WIFEXITED(status) : |
| 1234 | status = os.WEXITSTATUS(status) |
| 1235 | self.printInfo(_("Banner generator %s exit code is %s") % (bannerfileorcommand, str(status))) |
| 1236 | else : |
| 1237 | self.logdebug("Using %s as the banner." % bannerfileorcommand) |
| 1238 | try : |
| 1239 | fh = open(bannerfileorcommand, 'r') |
| 1240 | except IOError, msg : |
| 1241 | self.printInfo("Impossible to open %s : %s" % (bannerfileorcommand, msg), "error") |
| 1242 | else : |
| 1243 | banner = fh.read() |
| 1244 | fh.close() |
| 1245 | return cStringIO.StringIO(banner) |
| 1246 | |
| 1247 | def startingBanner(self, printername) : |
| 1248 | """Retrieves a starting banner for current printer and returns its content.""" |
| 1249 | self.logdebug("Retrieving starting banner...") |
| 1250 | return self.genBanner(self.config.getStartingBanner(printername)) |
| 1251 | |
| 1252 | def endingBanner(self, printername) : |
| 1253 | """Retrieves an ending banner for current printer and returns its content.""" |
| 1254 | self.logdebug("Retrieving ending banner...") |
| 1255 | return self.genBanner(self.config.getEndingBanner(printername)) |