Changeset 3413 for pykota/trunk/bin/pkprinters
- Timestamp:
- 09/27/08 22:02:37 (16 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/bin/pkprinters
r3411 r3413 9 9 # the Free Software Foundation, either version 3 of the License, or 10 10 # (at your option) any later version. 11 # 11 # 12 12 # This program is distributed in the hope that it will be useful, 13 13 # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 15 # GNU General Public License for more details. 16 # 16 # 17 17 # You should have received a copy of the GNU General Public License 18 18 # along with this program. If not, see <http://www.gnu.org/licenses/>. … … 47 47 -v | --version Prints pkprinters's version number then exits. 48 48 -h | --help Prints this message then exits. 49 50 -a | --add Adds printers if they don't exist on the Quota 49 50 -a | --add Adds printers if they don't exist on the Quota 51 51 Storage Server. If they exist, they are modified 52 52 unless -s|--skipexisting is also used. 53 53 54 54 -d | --delete Deletes printers from the quota storage. 55 55 56 56 -D | --description d Adds a textual description to printers. 57 57 … … 62 62 If both are to be set, separate them with a comma. 63 63 Floating point and negative values are allowed. 64 65 -g | --groups pg1[,pg2...] Adds or Remove the printer(s) to the printer 64 65 -g | --groups pg1[,pg2...] Adds or Remove the printer(s) to the printer 66 66 groups pg1, pg2, etc... which must already exist. 67 67 A printer group is just like a normal printer, 68 68 only that it is usually unknown from the printing 69 69 system. Create printer groups exactly the same 70 way that you create printers, then add other 70 way that you create printers, then add other 71 71 printers to them with this option. 72 72 Accounting is done on a printer and on all … … 74 74 is done on a printer and on all the printer groups 75 75 it belongs to. 76 If the --remove option below is not used, the 76 If the --remove option below is not used, the 77 77 default action is to add printers to the specified 78 78 printer groups. 79 79 80 80 -l | --list List informations about the printer(s) and the 81 81 printers groups it is a member of. 82 83 -r | --remove In combination with the --groups option above, 82 83 -r | --remove In combination with the --groups option above, 84 84 remove printers from the specified printers groups. 85 85 86 86 -s | --skipexisting In combination with the --add option above, tells 87 87 pkprinters to not modify existing printers. 88 88 89 89 -m | --maxjobsize s Sets the maximum job size allowed on the printer 90 90 to s pages. 91 91 92 92 -p | --passthrough Activate passthrough mode for the printer. In this 93 93 mode, users are allowed to print without any impact 94 94 on their quota or account balance. 95 95 96 96 -n | --nopassthrough Deactivate passthrough mode for the printer. 97 Without -p or -n, printers are created in 97 Without -p or -n, printers are created in 98 98 normal mode, i.e. no passthrough. 99 100 printer1 through printerN can contain wildcards if the --add option 99 100 printer1 through printerN can contain wildcards if the --add option 101 101 is not set. 102 103 examples : 102 103 examples : 104 104 105 105 $ pkprinters --add -D "HP Printer" --charge 0.05,0.1 hp2100 hp2200 hp8000 106 106 107 107 Will create three printers named hp2100, hp2200 and hp8000. 108 108 Their price per page will be set at 0.05 unit, and their price … … 110 110 or whatever you want them to mean. 111 111 All of their descriptions will be set to the string "HP Printer". 112 If any of these printers already exists, it will also be modified 112 If any of these printers already exists, it will also be modified 113 113 unless the -s|--skipexisting command line option is also used. 114 114 115 115 $ pkprinters --delete "*" 116 116 117 117 This will completely delete all printers and associated quota information, 118 118 as well as their job history. USE WITH CARE ! 119 119 120 120 $ pkprinters --groups Laser,HP "hp*" 121 122 This will put all printers which name matches "hp*" into printers groups 121 122 This will put all printers which name matches "hp*" into printers groups 123 123 Laser and HP, which MUST already exist. 124 124 125 125 $ pkprinters --groups LexMark --remove hp2200 126 126 127 127 This will remove the hp2200 printer from the LexMark printer group. 128 128 """) 129 130 class PKPrinters(PyKotaTool) : 129 130 class PKPrinters(PyKotaTool) : 131 131 """A class for a printers manager.""" 132 132 def modifyPrinter(self, printer, charges, perpage, perjob, description, passthrough, nopassthrough, maxjobsize) : 133 133 if charges : 134 printer.setPrices(perpage, perjob) 134 printer.setPrices(perpage, perjob) 135 135 if description is not None : # NB : "" is allowed ! 136 136 printer.setDescription(description) 137 if nopassthrough : 137 if nopassthrough : 138 138 printer.setPassThrough(False) 139 if passthrough : 139 if passthrough : 140 140 printer.setPassThrough(True) 141 141 if maxjobsize is not None : 142 142 printer.setMaxJobSize(maxjobsize) 143 144 def managePrintersGroups(self, pgroups, printer, remove) : 143 144 def managePrintersGroups(self, pgroups, printer, remove) : 145 145 """Manage printer group membership.""" 146 146 for pgroup in pgroups : … … 148 148 pgroup.delPrinterFromGroup(printer) 149 149 else : 150 pgroup.addPrinterToGroup(printer) 151 152 def getPrinterDeviceURI(self, printername) : 150 pgroup.addPrinterToGroup(printer) 151 152 def getPrinterDeviceURI(self, printername) : 153 153 """Returns the Device URI attribute for a particular printer.""" 154 154 if not printername : … … 159 159 try : 160 160 return cups.doRequest(req).printer["device-uri"][0][1] 161 except : 161 except : 162 162 self.printInfo(_("Impossible to retrieve %(printername)s's DeviceURI") % locals(), "warn") 163 163 return "" 164 164 165 165 def isPrinterCaptured(self, printername=None, deviceuri=None) : 166 166 """Returns True if the printer is already redirected through PyKota's backend, else False.""" 167 167 if (deviceuri or self.getPrinterDeviceURI(printername)).find("cupspykota:") != -1 : 168 168 return True 169 else : 169 else : 170 170 return False 171 172 def reroutePrinterThroughPyKota(self, printer) : 171 172 def reroutePrinterThroughPyKota(self, printer) : 173 173 """Reroutes a CUPS printer through PyKota.""" 174 174 uri = self.getPrinterDeviceURI(printer.Name) … … 177 177 os.system('lpadmin -p "%s" -v "%s"' % (printer.Name, newuri)) 178 178 self.logdebug("Printer %s rerouted to %s" % (printer.Name, newuri)) 179 180 def deroutePrinterFromPyKota(self, printer) : 179 180 def deroutePrinterFromPyKota(self, printer) : 181 181 """Deroutes a PyKota printer through CUPS only.""" 182 182 uri = self.getPrinterDeviceURI(printer.Name) … … 187 187 os.system('lpadmin -p "%s" -v "%s"' % (printer.Name, newuri)) 188 188 self.logdebug("Printer %s rerouted to %s" % (printer.Name, newuri)) 189 189 190 190 def main(self, names, options) : 191 191 """Manage printers.""" 192 192 if not options["list"] : 193 193 self.adminOnly() 194 194 195 195 docups = options["cups"] 196 197 if not options["list"] : 196 197 if not options["list"] : 198 198 percent = Percent(self) 199 199 200 200 if not options["add"] : 201 201 if not options["list"] : … … 208 208 percent.display("\n") 209 209 raise PyKotaCommandLineError, _("There's no printer matching %s") % " ".join(names) 210 if not options["list"] : 210 if not options["list"] : 211 211 percent.setSize(len(printers)) 212 212 213 213 if options["list"] : 214 214 for printer in printers : … … 220 220 print " %s" % (_("Maximum job size : %s") % ((printer.MaxJobSize and (_("%s pages") % printer.MaxJobSize)) or _("Unlimited"))) 221 221 print " %s" % (_("Routed through PyKota : %s") % ((self.isPrinterCaptured(printer.Name) and _("YES")) or _("NO"))) 222 if parents : 222 if parents : 223 223 print " %s %s" % (_("in"), parents) 224 print 225 elif options["delete"] : 224 print 225 elif options["delete"] : 226 226 percent.display("\n%s..." % _("Deletion")) 227 227 self.storage.deleteManyPrinters(printers) … … 233 233 percent.oneMore() 234 234 else : 235 if options["groups"] : 235 if options["groups"] : 236 236 printersgroups = self.storage.getMatchingPrinters(options["groups"]) 237 237 if not printersgroups : 238 238 raise PyKotaCommandLineError, _("There's no printer matching %s") % " ".join(options["groups"].split(',')) 239 else : 239 else : 240 240 printersgroups = [] 241 241 242 242 if options["charge"] : 243 243 try : 244 244 charges = [float(part) for part in options["charge"].split(',', 1)] 245 except ValueError : 245 except ValueError : 246 246 raise PyKotaCommandLineError, _("Invalid charge amount value %s") % options["charge"] 247 else : 247 else : 248 248 if len(charges) > 2 : 249 249 charges = charges[:2] … … 251 251 charges = [charges[0], None] 252 252 (perpage, perjob) = charges 253 else : 253 else : 254 254 charges = perpage = perjob = None 255 256 if options["maxjobsize"] : 255 256 if options["maxjobsize"] : 257 257 try : 258 258 maxjobsize = int(options["maxjobsize"]) 259 259 if maxjobsize < 0 : 260 260 raise ValueError 261 except ValueError : 261 except ValueError : 262 262 raise PyKotaCommandLineError, _("Invalid maximum job size value %s") % options["maxjobsize"] 263 else : 263 else : 264 264 maxjobsize = None 265 265 266 266 description = options["description"] 267 267 if description : 268 268 description = description.strip() 269 270 nopassthrough = options["nopassthrough"] 269 270 nopassthrough = options["nopassthrough"] 271 271 passthrough = options["passthrough"] 272 272 remove = options["remove"] … … 274 274 self.storage.beginTransaction() 275 275 try : 276 if options["add"] : 276 if options["add"] : 277 277 percent.display("%s...\n" % _("Creation")) 278 278 percent.setSize(len(names)) … … 283 283 description, passthrough, \ 284 284 nopassthrough, maxjobsize) 285 oldprinter = self.storage.addPrinter(printer) 286 285 oldprinter = self.storage.addPrinter(printer) 286 287 287 if docups : 288 288 self.reroutePrinterThroughPyKota(printer) 289 289 290 290 if oldprinter is not None : 291 291 if skipexisting : 292 292 self.logdebug(_("Printer %s already exists, skipping.") % pname) 293 else : 293 else : 294 294 self.logdebug(_("Printer %s already exists, will be modified.") % pname) 295 295 self.modifyPrinter(oldprinter, charges, \ … … 297 297 passthrough, nopassthrough, \ 298 298 maxjobsize) 299 oldprinter.save() 299 oldprinter.save() 300 300 self.managePrintersGroups(printersgroups, oldprinter, remove) 301 elif printersgroups : 301 elif printersgroups : 302 302 self.managePrintersGroups(printersgroups, \ 303 303 self.storage.getPrinter(pname), \ 304 304 remove) 305 else : 305 else : 306 306 raise PyKotaCommandLineError, _("Invalid printer name %s") % pname 307 307 percent.oneMore() 308 else : 308 else : 309 309 percent.display("\n%s...\n" % _("Modification")) 310 for printer in printers : 310 for printer in printers : 311 311 self.modifyPrinter(printer, charges, perpage, perjob, \ 312 312 description, passthrough, \ 313 313 nopassthrough, maxjobsize) 314 printer.save() 314 printer.save() 315 315 self.managePrintersGroups(printersgroups, printer, remove) 316 316 if docups : 317 317 self.reroutePrinterThroughPyKota(printer) 318 318 percent.oneMore() 319 except : 319 except : 320 320 self.storage.rollbackTransaction() 321 321 raise 322 else : 322 else : 323 323 self.storage.commitTransaction() 324 324 325 325 if not options["list"] : 326 326 percent.done() 327 328 if __name__ == "__main__" : 327 328 if __name__ == "__main__" : 329 329 retcode = 0 330 330 try : … … 334 334 "skipexisting", "passthrough", "nopassthrough", \ 335 335 "maxjobsize="] 336 336 337 337 # Initializes the command line tool 338 338 manager = PKPrinters(doc=__doc__) 339 339 manager.deferredInit() 340 340 341 341 # parse and checks the command line 342 342 (options, args) = manager.parseCommandline(sys.argv[1:], short_options, long_options) 343 343 344 344 # sets long options 345 345 options["help"] = options["h"] or options["help"] … … 349 349 options["charge"] = options["c"] or options["charge"] 350 350 options["description"] = options["D"] or options["description"] 351 options["delete"] = options["d"] or options["delete"] 351 options["delete"] = options["d"] or options["delete"] 352 352 options["groups"] = options["g"] or options["groups"] 353 353 options["list"] = options["l"] or options["list"] … … 357 357 options["passthrough"] = options["p"] or options["passthrough"] 358 358 options["nopassthrough"] = options["n"] or options["nopassthrough"] 359 359 360 360 if options["help"] : 361 361 manager.display_usage_and_quit() … … 374 374 else : 375 375 retcode = manager.main(args, options) 376 except KeyboardInterrupt : 376 except KeyboardInterrupt : 377 377 logerr("\nInterrupted with Ctrl+C !\n") 378 378 retcode = -3 379 except PyKotaCommandLineError, msg : 379 except PyKotaCommandLineError, msg : 380 380 logerr("%s : %s\n" % (sys.argv[0], msg)) 381 381 retcode = -2 382 except SystemExit : 382 except SystemExit : 383 383 pass 384 384 except : 385 385 try : 386 386 manager.crashed("pkprinters failed") 387 except : 387 except : 388 388 crashed("pkprinters failed") 389 389 retcode = -1 … … 391 391 try : 392 392 manager.storage.close() 393 except (TypeError, NameError, AttributeError) : 393 except (TypeError, NameError, AttributeError) : 394 394 pass 395 396 sys.exit(retcode) 395 396 sys.exit(retcode)