51 | | |
52 | | __doc__ = N_("""pkrefund v%(__version__)s (c) %(__years__)s %(__author__)s |
53 | | |
54 | | Refunds jobs. |
55 | | |
56 | | command line usage : |
57 | | |
58 | | pkrefund [options] [filterexpr] |
59 | | |
60 | | options : |
61 | | |
62 | | -v | --version Prints pkrefund's version number then exits. |
63 | | -h | --help Prints this message then exits. |
64 | | |
65 | | -f | --force Doesn't ask for confirmation before refunding jobs. |
66 | | -r | --reason txt Sets textual information to explain the refunding. |
67 | | |
68 | | -l | --logo img Use the image as the receipt's logo. The logo will |
69 | | be drawn at the center top of the page. The default |
70 | | logo is /usr/share/pykota/logos/pykota.jpeg |
71 | | |
72 | | -p | --pagesize sz Sets sz as the page size. Most well known |
73 | | page sizes are recognized, like 'A4' or 'Letter' |
74 | | to name a few. The default size is A4. |
75 | | |
76 | | -n | --number N Sets the number of the first receipt. This number |
77 | | will automatically be incremented for each receipt. |
78 | | |
79 | | -o | --output f.pdf Defines the name of the PDF file which will contain |
80 | | the receipts. If not set, then no PDF file will |
81 | | be created. If set to '-', then --force is assumed, |
82 | | and the PDF document is sent to standard output. |
83 | | |
84 | | -u | --unit u Defines the name of the unit to use on the receipts. |
85 | | The default unit is 'Credits', optionally translated |
86 | | to your native language if it is supported by PyKota. |
87 | | |
88 | | |
89 | | Use the filter expressions to extract only parts of the |
90 | | datas. Allowed filters are of the form : |
91 | | |
92 | | key=value |
93 | | |
94 | | Allowed keys for now are : |
95 | | |
96 | | username User's name |
97 | | printername Printer's name |
98 | | hostname Client's hostname |
99 | | jobid Job's Id |
100 | | billingcode Job's billing code |
101 | | start Job's date of printing |
102 | | end Job's date of printing |
103 | | |
104 | | Dates formatting with 'start' and 'end' filter keys : |
105 | | |
106 | | YYYY : year boundaries |
107 | | YYYYMM : month boundaries |
108 | | YYYYMMDD : day boundaries |
109 | | YYYYMMDDhh : hour boundaries |
110 | | YYYYMMDDhhmm : minute boundaries |
111 | | YYYYMMDDhhmmss : second boundaries |
112 | | yesterday[+-NbDays] : yesterday more or less N days (e.g. : yesterday-15) |
113 | | today[+-NbDays] : today more or less N days (e.g. : today-15) |
114 | | tomorrow[+-NbDays] : tomorrow more or less N days (e.g. : tomorrow-15) |
115 | | now[+-NbDays] : now more or less N days (e.g. now-15) |
116 | | |
117 | | 'now' and 'today' are not exactly the same since today represents the first |
118 | | or last second of the day depending on if it's used in a start= or end= |
119 | | date expression. The utility to be able to specify dates in the future is |
120 | | a question which remains to be answered :-) |
121 | | |
122 | | Contrary to other PyKota management tools, wildcard characters are not |
123 | | expanded, so you can't use them. |
124 | | |
125 | | Examples : |
126 | | |
127 | | $ pkrefund --output /tmp/receipts.pdf jobid=503 |
128 | | |
129 | | This will refund all jobs which Id is 503. BEWARE : installing CUPS |
130 | | afresh will reset the first job id at 1, so you probably want to use |
131 | | a more precise filter as explained below. A confirmation will |
132 | | be asked for each job to refund, and a PDF file named /tmp/receipts.pdf |
133 | | will be created which will contain printable receipts. |
134 | | |
135 | | $ pkrefund --reason "Hardware problem" jobid=503 start=today-7 |
136 | | |
137 | | Refunds all jobs which id is 503 but which were printed during the |
138 | | past week. The reason will be marked as being an hardware problem. |
139 | | |
140 | | $ pkrefund --force username=jerome printername=HP2100 |
141 | | |
142 | | Refunds all jobs printed by user jerome on printer HP2100. No |
143 | | confirmation will be asked. |
144 | | |
145 | | $ pkrefund --force printername=HP2100 start=200602 end=yesterday |
146 | | |
147 | | Refunds all jobs printed on printer HP2100 between February 1st 2006 |
148 | | and yesterday. No confirmation will be asked. |
149 | | """) |
150 | | |
151 | | class PkRefund(PyKotaTool) : |
| 56 | |
| 57 | class PKRefund(PyKotaTool) : |
408 | | retcode = 0 |
409 | | try : |
410 | | defaults = { "unit" : N_("Credits"), |
411 | | "pagesize" : "a4", \ |
412 | | "logo" : "/usr/share/pykota/logos/pykota.jpeg", |
413 | | "number" : "1", |
414 | | } |
415 | | short_options = "vhfru:o:p:l:n:" |
416 | | long_options = ["help", "version", "force", "reason=", "unit=", "output=", "pagesize=", "logo=", "number="] |
417 | | |
418 | | # Initializes the command line tool |
419 | | refundmanager = PkRefund(doc=__doc__) |
420 | | refundmanager.deferredInit() |
421 | | |
422 | | # parse and checks the command line |
423 | | (options, args) = refundmanager.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
424 | | |
425 | | # sets long options |
426 | | options["help"] = options["h"] or options["help"] |
427 | | options["version"] = options["v"] or options["version"] |
428 | | options["force"] = options["f"] or options["force"] |
429 | | options["reason"] = options["r"] or options["reason"] |
430 | | options["unit"] = options["u"] or options["unit"] or defaults["unit"] |
431 | | options["output"] = options["o"] or options["output"] |
432 | | options["pagesize"] = options["p"] or options["pagesize"] or defaults["pagesize"] |
433 | | options["number"] = options["n"] or options["number"] or defaults["number"] |
434 | | options["logo"] = options["l"] or options["logo"] |
435 | | if options["logo"] is None : # Allows --logo="" to disable the logo entirely |
436 | | options["logo"] = defaults["logo"] |
437 | | |
438 | | if options["help"] : |
439 | | refundmanager.display_usage_and_quit() |
440 | | elif options["version"] : |
441 | | refundmanager.display_version_and_quit() |
442 | | else : |
443 | | retcode = refundmanager.main(args, options) |
444 | | except KeyboardInterrupt : |
445 | | logerr("\nInterrupted with Ctrl+C !\n") |
446 | | retcode = -3 |
447 | | except PyKotaCommandLineError, msg : |
448 | | logerr("%s : %s\n" % (sys.argv[0], msg)) |
449 | | retcode = -2 |
450 | | except SystemExit : |
451 | | pass |
452 | | except : |
453 | | try : |
454 | | refundmanager.crashed("pkrefund failed") |
455 | | except : |
456 | | crashed("pkrefund failed") |
457 | | retcode = -1 |
458 | | |
459 | | try : |
460 | | refundmanager.storage.close() |
461 | | except (TypeError, NameError, AttributeError) : |
462 | | pass |
463 | | |
464 | | sys.exit(retcode) |
| 299 | parser = PyKotaOptionParser(description=_("Refunding tool for PyKota."), |
| 300 | usage="pkrefund [options] [filterexpr]") |
| 301 | parser.add_option("-f", "--force", |
| 302 | dest="force", |
| 303 | action="store_true", |
| 304 | help=_("Doesn't ask for confirmation before refunding.")) |
| 305 | parser.add_option("-l", "--logo", |
| 306 | dest="logo", |
| 307 | default=u"/usr/share/pykota/logos/pykota.jpeg", |
| 308 | help=_("The image to use as a logo. The logo will be drawn at the center top of the page. The default logo is %default")) |
| 309 | parser.add_option("-p", "--pagesize", |
| 310 | type="string", |
| 311 | action="callback", |
| 312 | callback=checkandset_pagesize, |
| 313 | dest="pagesize", |
| 314 | default=u"A4", |
| 315 | help=_("Set the size of the page. Most well known page sizes are recognized, like 'A4' or 'Letter' to name a few. The default page size is %default")) |
| 316 | parser.add_option("-n", "--number", |
| 317 | dest="number", |
| 318 | type="int", |
| 319 | action="callback", |
| 320 | callback=checkandset_positiveint, |
| 321 | default=1, |
| 322 | help=_("Sets the number of the first receipt. This number will automatically be incremented for each receipt. The default value is %default")) |
| 323 | parser.add_option("-o", "--output", |
| 324 | dest="output", |
| 325 | type="string", |
| 326 | default=u"-", |
| 327 | help=_("The name of the file to which the PDF receipts will be written. If not set or set to '%default', the PDF document will be sent to the standard output.")) |
| 328 | parser.add_option("-r", "--reason", |
| 329 | dest="reason", |
| 330 | type="string", |
| 331 | help=_("The reason why there was a refund.")) |
| 332 | |
| 333 | # TODO : due to Python's optparse.py bug #1498146 fixed in rev 46861 |
| 334 | # TODO : we can't use 'default=_("Credits")' for this option |
| 335 | parser.add_option("-u", "--unit", |
| 336 | dest="unit", |
| 337 | type="string", |
| 338 | help=_("The name of the unit to use on the receipts. The default value is 'Credits' or its locale translation.")) |
| 339 | |
| 340 | parser.add_filterexpression("username", _("User's name")) |
| 341 | parser.add_filterexpression("printername", _("Printer's name")) |
| 342 | parser.add_filterexpression("hostname", _("Host's name")) |
| 343 | parser.add_filterexpression("jobid", _("Job's id")) |
| 344 | parser.add_filterexpression("billingcode", _("Job's billing code")) |
| 345 | parser.add_filterexpression("start", _("Job's date of printing")) |
| 346 | parser.add_filterexpression("end", _("Job's date of printing")) |
| 347 | |
| 348 | parser.add_example('--output /tmp/receipts.pdf jobid=503', |
| 349 | _("This would refund all jobs which Id is 503. A confirmation would be asked for each job to refund, and a PDF file named /tmp/receipts.pdf would be created containing printable receipts. BEWARE of job ids rolling over if you reset CUPS' history.")) |
| 350 | |
| 351 | parser.add_example('--reason "Hardware problem" jobid=503 start=today-7', |
| 352 | _("This would refund all jobs which id is 503 but which would have been printed during the past week. The reason would be marked as being an hardware problem.")) |
| 353 | |
| 354 | parser.add_example('--force username=jerome printername=HP2100', |
| 355 | _("This would refund all jobs printed by user jerome on printer HP2100. No confirmation would be asked.")) |
| 356 | |
| 357 | parser.add_example('--force printername=HP2100 start=200602 end=yesterday', |
| 358 | _("This would refund all jobs printed on printer HP2100 between February 1st 2006 and yesterday. No confirmation would be asked.")) |
| 359 | |
| 360 | (options, arguments) = parser.parse_args() |
| 361 | run(parser, PKRefund) |