35 | | __doc__ = N_("""pkbcodes v%(__version__)s (c) %(__years__)s %(__author__)s |
36 | | |
37 | | A billing codes Manager for PyKota. |
38 | | |
39 | | command line usage : |
40 | | |
41 | | pkbcodes [options] code1 code2 code3 ... codeN |
42 | | |
43 | | options : |
44 | | |
45 | | -v | --version Prints pkbcodes version number then exits. |
46 | | -h | --help Prints this message then exits. |
47 | | |
48 | | -a | --add Adds billing codes if they don't exist in PyKota's |
49 | | database. If they exist, they are modified |
50 | | unless -s|--skipexisting is also used. |
51 | | |
52 | | -d | --delete Deletes billing codes from PyKota's database. |
53 | | NB : the history entries with this billing code |
54 | | are not deleted, voluntarily. |
55 | | |
56 | | -D | --description d Adds a textual description to billing codes. |
57 | | |
58 | | -l | --list List informations about the billing codes. |
59 | | |
60 | | -r | --reset Resets the billing codes' balance and page counters |
61 | | to 0. |
62 | | |
63 | | -s | --skipexisting In combination with the --add option above, tells |
64 | | pkbcodes to not modify existing billing codes. |
65 | | |
66 | | code1 through codeN can contain wildcards if the --add option |
67 | | is not set. |
68 | | |
69 | | examples : |
70 | | |
71 | | $ pkbcodes --add -D "My project" myproj |
72 | | |
73 | | Will create the myproj billing code with "My project" |
74 | | as the description. |
75 | | |
76 | | $ pkbcodes --delete "*" |
77 | | |
78 | | This will completely delete all the billing codes, but without |
79 | | removing any matching job from the history. USE WITH CARE ANYWAY ! |
80 | | |
81 | | $ pkbcodes --list "my*" |
82 | | |
83 | | This will list all billing codes which name begins with 'my'. |
84 | | """) |
85 | | |
169 | | retcode = 0 |
170 | | try : |
171 | | short_options = "hvaD:dlrs" |
172 | | long_options = ["help", "version", "add", "description=", "delete", "list", "reset", "skipexisting"] |
173 | | |
174 | | # Initializes the command line tool |
175 | | manager = PKBcodes(doc=__doc__) |
176 | | manager.deferredInit() |
177 | | |
178 | | # parse and checks the command line |
179 | | (options, args) = manager.parseCommandline(sys.argv[1:], short_options, long_options) |
180 | | |
181 | | # sets long options |
182 | | options["help"] = options["h"] or options["help"] |
183 | | options["version"] = options["v"] or options["version"] |
184 | | options["add"] = options["a"] or options["add"] |
185 | | options["description"] = options["D"] or options["description"] |
186 | | options["delete"] = options["d"] or options["delete"] |
187 | | options["list"] = options["l"] or options["list"] |
188 | | options["reset"] = options["r"] or options["reset"] |
189 | | options["skipexisting"] = options["s"] or options["skipexisting"] |
190 | | |
191 | | if options["help"] : |
192 | | manager.display_usage_and_quit() |
193 | | elif options["version"] : |
194 | | manager.display_version_and_quit() |
195 | | elif (options["delete"] and (options["add"] or options["reset"] or options["description"])) \ |
196 | | or (options["skipexisting"] and not options["add"]) \ |
197 | | or (options["list"] and (options["add"] or options["delete"] or options["reset"] or options["description"])) : |
198 | | raise PyKotaCommandLineError, _("incompatible options, see help.") |
199 | | elif (not args) and (options["add"] or options["delete"]) : |
200 | | raise PyKotaCommandLineError, _("You have to pass billing codes on the command line") |
201 | | else : |
202 | | retcode = manager.main(args, options) |
203 | | except KeyboardInterrupt : |
204 | | logerr("\nInterrupted with Ctrl+C !\n") |
205 | | retcode = -3 |
206 | | except PyKotaCommandLineError, msg : |
207 | | logerr("%s : %s\n" % (sys.argv[0], msg)) |
208 | | retcode = -2 |
209 | | except SystemExit : |
210 | | pass |
211 | | except : |
212 | | try : |
213 | | manager.crashed("pkbcodes failed") |
214 | | except : |
215 | | crashed("pkbcodes failed") |
216 | | retcode = -1 |
217 | | |
218 | | try : |
219 | | manager.storage.close() |
220 | | except (TypeError, NameError, AttributeError) : |
221 | | pass |
222 | | |
223 | | sys.exit(retcode) |
| 133 | parser = PyKotaOptionParser(description=_("A billing codes manager for PyKota."), |
| 134 | usage="pkbcodes [options] code1 code2 ... codeN") |
| 135 | parser.add_option("-a", "--add", |
| 136 | action="store_const", |
| 137 | const="add", |
| 138 | dest="action", |
| 139 | help=_("Add new, or modify existing, billing codes.")) |
| 140 | parser.add_option("-d", "--delete", |
| 141 | action="store_const", |
| 142 | const="delete", |
| 143 | dest="action", |
| 144 | help=_("Deletes billing codes. Matching entries in the printing history are not deleted, on purpose.")) |
| 145 | parser.add_option("-D", "--description", |
| 146 | dest="description", |
| 147 | help=_("Set a textual description for the specified billing codes.")) |
| 148 | parser.add_option("-l", "--list", |
| 149 | action="store_const", |
| 150 | const="list", |
| 151 | dest="action", |
| 152 | help=_("Display information about the specified billing codes.")) |
| 153 | parser.add_option("-r", "--reset", |
| 154 | action="store_true", |
| 155 | dest="reset", |
| 156 | help=_("Reset the page count and amount spent for the specified billing codes.")) |
| 157 | parser.add_option("-s", "--skipexisting", |
| 158 | action="store_true", |
| 159 | dest="skipexisting", |
| 160 | help=_("If --add is used, ensure that existing billing codes won't be modified.")) |
| 161 | |
| 162 | parser.add_example('-D "Financial Department" financial', |
| 163 | _("Would create a billing code labelled 'financial' with the specified textual description.")) |
| 164 | parser.add_example('--delete "fin*"', |
| 165 | _("Would delete all billing codes which label begins with 'fin'. Matching jobs in the printing history wouldn't be deleted though.")) |
| 166 | parser.add_example("--list", |
| 167 | _("Would display details about all existing billing codes.")) |
| 168 | |
| 169 | (options, arguments) = parser.parse_args() |
| 170 | run(parser, PKBcodes) |