32 | | __doc__ = N_("""dumpykota v%(__version__)s (c) %(__years__)s %(__author__)s |
| 35 | if __name__ == "__main__" : |
| 36 | parser = PyKotaOptionParser(description=_("Data Dumper for PyKota."), |
| 37 | usage="dumpykota [options] [filterexpr]") |
| 38 | |
| 39 | parser.add_option("-d", "--data", |
| 40 | dest="data", |
| 41 | help=_("Select the type of datas to dump. This option is mandatory. Supported data types are : history, payments, billingcodes, users, groups, printers, upquotas, gpquotas, umembers, pmembers, and all. The 'all' value forces the output format to XML.")) |
| 42 | parser.add_option("-f", "--format", |
| 43 | default="csv", |
| 44 | dest="format", |
| 45 | help=_("Select the output format, the default being comma separated values. Supported formats are : csv, ssv, tsv, xml and cups. The 'cups' output format only works when dumping the history, and produces CUPS' page_log compatible output.")) |
| 46 | parser.add_option("-o", "--output", |
| 47 | dest="output", |
| 48 | default=u"-", |
| 49 | help=_("The name of the file the data dump will be written to. The default value is '-', which tells dumpykota to write the dump to stdout.")) |
| 50 | parser.add_option("-O", "--orderby", |
| 51 | dest="orderby", |
| 52 | help=_("Change the ordering of the output based on a comma separated list of ordering statements. For example '-username,+printername' would sort the output by descending order of user names and ascending order of printer names. Not all expressions are supported, and you should not use this if you don't know the internal structure of PyKota's database." )) |
| 53 | parser.add_option("-s", "--sum", |
| 54 | dest="sum", |
| 55 | action="store_true", |
| 56 | default=False, |
| 57 | help=_("Summarize the output. Only available when dumping the printing history or the payments.")) |
| 58 | |
| 59 | parser.add_filterexpression("username", _("User's name")) |
| 60 | parser.add_filterexpression("groupname", _("Users group's name")) |
| 61 | parser.add_filterexpression("printername", _("Printer's name")) |
| 62 | parser.add_filterexpression("pgroupname", _("Printers group's name")) |
| 63 | parser.add_filterexpression("hostname", _("Host's name")) |
| 64 | parser.add_filterexpression("jobid", _("Job's id")) |
| 65 | parser.add_filterexpression("billingcode", _("Job's billing code")) |
| 66 | parser.add_filterexpression("start", _("Job's date of printing")) |
| 67 | parser.add_filterexpression("end", _("Job's date of printing")) |
| 68 | |
| 69 | parser.add_example('--unit EURO --output /tmp/invoices.pdf start=now-30', |
| 70 | _("This would generate a PDF document containing invoices for all users who have spent some credits last month. Amounts would be in EURO and not VAT information would be included.")) |
45 | | -d | --data type Dumps 'type' datas. Allowed types are : |
46 | | |
47 | | - history : dumps the jobs history. |
48 | | - users : dumps users. |
49 | | - groups : dumps user groups. |
50 | | - printers : dump printers. |
51 | | - upquotas : dump user quotas. |
52 | | - gpquotas : dump user groups quotas. |
53 | | - payments : dumps user payments. |
54 | | - pmembers : dumps printer groups members. |
55 | | - umembers : dumps user groups members. |
56 | | - billingcodes : dumps billing codes. |
57 | | - all : dumps all PyKota datas. The output format |
58 | | is always XML in this case. |
59 | | |
60 | | NB : the -d | --data command line option |
61 | | is MANDATORY. |
| 75 | parser.add_example("--data users --format xml -o users.xml", |
| 76 | _("This would dump all users into the 'users.xml' file in the XML format.")) |
63 | | -f | --format fmt Dumps datas in the 'fmt' format. When not specified, |
64 | | the format is to dump datas in the csv format (comma |
65 | | separated values). All data dumped is between double |
66 | | quotes. Allowed formats are : |
67 | | |
68 | | - csv : separate datas with commas |
69 | | - ssv : separate datas with semicolons |
70 | | - tsv : separate datas with tabs |
71 | | - xml : dump data as XML |
72 | | - cups : dump datas in CUPS' page_log format : |
73 | | ONLY AVAILABLE WITH --data history |
74 | | |
75 | | -o | --output fname All datas will be dumped to the file instead of |
76 | | to the standard output. The special '-' filename |
77 | | is the default value and means stdout. |
78 | | WARNING : existing files are truncated ! |
79 | | |
80 | | -O | --orderby exp Change the ordering or result. 'exp' is a comma |
81 | | separated list of ordering statements, for example |
82 | | '--orderby +username,-printername'. Not all expression |
83 | | values are meaningful, so using this command line |
84 | | switch is not recommanded if you don't know the |
85 | | exact layout of PyKota's database schema. |
86 | | |
87 | | -s | --sum Summarize the selected datas. |
88 | | ONLY AVAILABLE WITH --data history or payments |
89 | | |
90 | | Use the filter expressions to extract only parts of the |
91 | | datas. Allowed filters are of the form : |
92 | | |
93 | | key=value |
94 | | |
95 | | Allowed keys for now are : |
96 | | |
97 | | username User's name |
98 | | groupname Users group's name |
99 | | printername Printer's name |
100 | | pgroupname Printers group's name |
101 | | hostname Client's hostname |
102 | | jobid Job's Id |
103 | | billingcode Job's billing code |
104 | | start Job's date of printing |
105 | | end Job's date of printing |
106 | | |
107 | | Dates formatting with 'start' and 'end' filter keys : |
| 78 | parser.add_example("--data history printername=HP2100 username=jerome", |
| 79 | _("This would dump jerome's printing history on printer HP2100.")) |
109 | | YYYY : year boundaries |
110 | | YYYYMM : month boundaries |
111 | | YYYYMMDD : day boundaries |
112 | | YYYYMMDDhh : hour boundaries |
113 | | YYYYMMDDhhmm : minute boundaries |
114 | | YYYYMMDDhhmmss : second boundaries |
115 | | yesterday[+-NbDays] : yesterday more or less N days (e.g. : yesterday-15) |
116 | | today[+-NbDays] : today more or less N days (e.g. : today-15) |
117 | | tomorrow[+-NbDays] : tomorrow more or less N days (e.g. : tomorrow-15) |
118 | | now[+-NbDays] : now more or less N days (e.g. now-15) |
119 | | |
120 | | 'now' and 'today' are not exactly the same since today represents the first |
121 | | or last second of the day depending on if it's used in a start= or end= |
122 | | date expression. The utility to be able to specify dates in the future is |
123 | | a question which remains to be answered :-) |
124 | | |
125 | | Contrary to other PyKota management tools, wildcard characters are not |
126 | | expanded, so you can't use them. |
127 | | |
128 | | NB : not all keys are allowed for each data type, so the result may be |
129 | | empty if you use a key not available for a particular data type. |
130 | | |
131 | | Examples : |
132 | | |
133 | | $ dumpykota --data history --format csv >myfile.csv |
134 | | |
135 | | This dumps the history in a comma separated values file, for possible |
136 | | use in a spreadsheet. |
137 | | |
138 | | $ dumpykota --data users --format xml -o users.xml |
139 | | |
140 | | Dumps all users datas to the users.xml file. |
141 | | |
142 | | $ dumpykota --data history printername=HP2100 username=jerome |
143 | | |
144 | | Dumps the job history for user jerome on printer HP2100 only. |
145 | | |
146 | | $ dumpykota --data history start=200503 end=20050730234615 |
147 | | |
148 | | Dumps all jobs printed between March 1st 2005 at midnight and |
149 | | July 30th 2005 at 23 hours 46 minutes and 15 secondes included. |
150 | | """) |
151 | | |
152 | | if __name__ == "__main__" : |
153 | | retcode = 0 |
154 | | try : |
155 | | defaults = { \ |
156 | | "format" : "csv", \ |
157 | | "output" : "-", \ |
158 | | } |
159 | | short_options = "vhd:f:o:sO:" |
160 | | long_options = ["help", "version", "data=", "format=", "output=", "sum", "orderby="] |
161 | | |
162 | | # Initializes the command line tool |
163 | | dumper = DumPyKota(doc=__doc__) |
164 | | dumper.deferredInit() |
165 | | |
166 | | # parse and checks the command line |
167 | | (options, args) = dumper.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
168 | | |
169 | | # sets long options |
170 | | options["help"] = options["h"] or options["help"] |
171 | | options["version"] = options["v"] or options["version"] |
172 | | options["data"] = options["d"] or options["data"] |
173 | | options["format"] = options["f"] or options["format"] or defaults["format"] |
174 | | options["output"] = options["o"] or options["output"] or defaults["output"] |
175 | | options["sum"] = options["s"] or options["sum"] |
176 | | options["orderby"] = options["O"] or options["orderby"] |
177 | | |
178 | | if options["help"] : |
179 | | dumper.display_usage_and_quit() |
180 | | elif options["version"] : |
181 | | dumper.display_version_and_quit() |
182 | | elif options["data"] is None : |
183 | | raise PyKotaCommandLineError, _("The -d | --data command line option is mandatory, see help.") |
184 | | else : |
185 | | retcode = dumper.main(args, options) |
186 | | except KeyboardInterrupt : |
187 | | logerr("\nInterrupted with Ctrl+C !\n") |
188 | | retcode = -3 |
189 | | except PyKotaCommandLineError, msg : |
190 | | logerr("%s : %s\n" % (sys.argv[0], msg)) |
191 | | retcode = -2 |
192 | | except SystemExit : |
193 | | pass |
194 | | except : |
195 | | try : |
196 | | dumper.crashed("dumpykota failed") |
197 | | except : |
198 | | crashed("dumpykota failed") |
199 | | retcode = -1 |
200 | | |
201 | | try : |
202 | | dumper.storage.close() |
203 | | except (TypeError, NameError, AttributeError) : |
204 | | pass |
205 | | |
206 | | sys.exit(retcode) |
| 81 | parser.add_example("--data history start=200503 end=20050730234615", |
| 82 | _("This would dump all jobs printer between March 1st 2008 at midnight and July 30th 2008 at 23 hours 46 minutes and 15 seconds, included.")) |
| 83 | run(parser, DumPyKota) |