#! /usr/bin/env python # -*- coding: ISO-8859-15 -*- # PyKota Print Quota Data Dumper # # PyKota - Print Quotas for CUPS and LPRng # # (c) 2003-2004 Jerome Alet # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. # # $Id$ # # $Log$ # Revision 1.2 2004/09/14 22:29:12 jalet # First version of dumpykota. Works fine but only with PostgreSQL backend # for now. # # Revision 1.1 2004/07/01 19:22:37 jalet # First draft of dumpykota # # # import sys import os import pwd from pykota import version from pykota.tool import PyKotaTool, PyKotaToolError from pykota.config import PyKotaConfigError from pykota.storage import PyKotaStorageError __doc__ = """dumpykota v%s (c) 2003-2004 C@LL - Conseil Internet & Logiciels Libres Dumps PyKota database's content. command line usage : dumpykota [options] options : -v | --version Prints repykota's version number then exits. -h | --help Prints this message then exits. -d | --data type Dumps 'type' datas. Allowed types are : - history : dumps the jobs history. - users : dumps users. - groups : dumps user groups. - printers : dump printers. - uquotas : dump user quotas. - gquotas : dump user groups quotas. - payments : dumps user payments. NB : the -d | --data command line option is MANDATORY. -f | --format fmt Dumps datas in the 'fmt' format. When not specified, the format is to dump datas in the csv format (comma separated values). All data dumped is between double quotes. Allowed formats are : - csv : separate datas with commas - ssv : separate datas with semicolons - tsv : separate datas with tabs This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. Please e-mail bugs to: %s""" % (version.__version__, version.__author__) class DumPyKota(PyKotaTool) : """A class for dumpykota.""" def main(self, arguments, options) : """Print Quota Data Dumper.""" datatype = options["data"] if datatype not in [ "history", "users", "groups", "printers", "upquotas", "gpquotas", "payments", ] : raise PyKotaToolError, _("Invalid modifier [%s] for --data command line option, see help.") % datatype format = options["format"] if format not in [ "csv", "ssv", "tsv", ] : raise PyKotaToolError, _("Invalid modifier [%s] for --format command line option, see help.") % datatype entries = getattr(self.storage, "extract%s" % datatype.title())() getattr(self, "dump%s" % format.title())(entries) return 0 def dumpWithSeparator(self, separator, entries) : """Dumps datas with a separator.""" for entry in entries : line = separator.join([ '"%s"' % field for field in entry ]) print line def dumpCsv(self, entries) : """Dumps datas with a comma as the separator.""" self.dumpWithSeparator(",", entries) def dumpSsv(self, entries) : """Dumps datas with a comma as the separator.""" self.dumpWithSeparator(";", entries) def dumpTsv(self, entries) : """Dumps datas with a comma as the separator.""" self.dumpWithSeparator("\t", entries) if __name__ == "__main__" : try : defaults = { \ "format" : "csv", \ } short_options = "vhd:f:" long_options = ["help", "version", "data=", "format="] # Initializes the command line tool dumper = DumPyKota(doc=__doc__) # parse and checks the command line (options, args) = dumper.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) # sets long options options["help"] = options["h"] or options["help"] options["version"] = options["v"] or options["version"] options["data"] = options["d"] or options["data"] options["format"] = options["f"] or options["format"] or defaults["format"] if options["help"] : dumper.display_usage_and_quit() elif options["version"] : dumper.display_version_and_quit() elif options["data"] is None : raise PyKotaToolError, _("The -d | --data command line option is mandatory, see help.") else : if args : raise PyKotaToolError, _("Too many arguments, see help.") retcode = dumper.main(args, options) except SystemExit : pass except : try : dumper.crashed("dumpykota failed") except : pass retcode = -1 try : dumper.storage.close() except (TypeError, NameError, AttributeError) : pass sys.exit(retcode)