root / pykota / trunk / bin / dumpykota @ 1717

Revision 1717, 6.7 kB (checked in by jalet, 20 years ago)

First version of dumpykota. Works fine but only with PostgreSQL backend
for now.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1#! /usr/bin/env python
2# -*- coding: ISO-8859-15 -*-
3
4# PyKota Print Quota Data Dumper
5#
6# PyKota - Print Quotas for CUPS and LPRng
7#
8# (c) 2003-2004 Jerome Alet <alet@librelogiciel.com>
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22#
23# $Id$
24#
25# $Log$
26# Revision 1.2  2004/09/14 22:29:12  jalet
27# First version of dumpykota. Works fine but only with PostgreSQL backend
28# for now.
29#
30# Revision 1.1  2004/07/01 19:22:37  jalet
31# First draft of dumpykota
32#
33#
34#
35
36import sys
37import os
38import pwd
39
40from pykota import version
41from pykota.tool import PyKotaTool, PyKotaToolError
42from pykota.config import PyKotaConfigError
43from pykota.storage import PyKotaStorageError
44
45__doc__ = """dumpykota v%s (c) 2003-2004 C@LL - Conseil Internet & Logiciels Libres
46
47Dumps PyKota database's content.
48
49command line usage :
50
51  dumpykota [options]
52
53options :
54
55  -v | --version       Prints repykota's version number then exits.
56  -h | --help          Prints this message then exits.
57 
58  -d | --data type     Dumps 'type' datas. Allowed types are :
59                       
60                         - history : dumps the jobs history.
61                         - users : dumps users.
62                         - groups : dumps user groups.
63                         - printers : dump printers.
64                         - uquotas : dump user quotas.
65                         - gquotas : dump user groups quotas.
66                         - payments : dumps user payments.
67                         
68                       NB : the -d | --data command line option   
69                       is MANDATORY.
70 
71  -f | --format fmt    Dumps datas in the 'fmt' format. When not specified,
72                       the format is to dump datas in the csv format (comma
73                       separated values). All data dumped is between double
74                       quotes. Allowed formats are :
75                       
76                         - csv : separate datas with commas
77                         - ssv : separate datas with semicolons
78                         - tsv : separate datas with tabs
79 
80This program is free software; you can redistribute it and/or modify
81it under the terms of the GNU General Public License as published by
82the Free Software Foundation; either version 2 of the License, or
83(at your option) any later version.
84
85This program is distributed in the hope that it will be useful,
86but WITHOUT ANY WARRANTY; without even the implied warranty of
87MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
88GNU General Public License for more details.
89
90You should have received a copy of the GNU General Public License
91along with this program; if not, write to the Free Software
92Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
93
94Please e-mail bugs to: %s""" % (version.__version__, version.__author__)
95       
96class DumPyKota(PyKotaTool) :       
97    """A class for dumpykota."""
98    def main(self, arguments, options) :
99        """Print Quota Data Dumper."""
100        datatype = options["data"]
101        if datatype not in [ "history",
102                             "users",
103                             "groups",
104                             "printers",
105                             "upquotas",
106                             "gpquotas",
107                             "payments",
108                           ] :
109            raise PyKotaToolError, _("Invalid modifier [%s] for --data command line option, see help.") % datatype
110                   
111        format = options["format"]
112        if format not in [ "csv",
113                           "ssv",
114                           "tsv",
115                         ] :
116            raise PyKotaToolError, _("Invalid modifier [%s] for --format command line option, see help.") % datatype
117           
118        entries = getattr(self.storage, "extract%s" % datatype.title())()   
119        getattr(self, "dump%s" % format.title())(entries)
120        return 0
121       
122    def dumpWithSeparator(self, separator, entries) :   
123        """Dumps datas with a separator."""
124        for entry in entries :
125            line = separator.join([ '"%s"' % field for field in entry ])
126            print line
127       
128    def dumpCsv(self, entries) :   
129        """Dumps datas with a comma as the separator."""
130        self.dumpWithSeparator(",", entries)
131                           
132    def dumpSsv(self, entries) :   
133        """Dumps datas with a comma as the separator."""
134        self.dumpWithSeparator(";", entries)
135                           
136    def dumpTsv(self, entries) :   
137        """Dumps datas with a comma as the separator."""
138        self.dumpWithSeparator("\t", entries)
139                           
140if __name__ == "__main__" : 
141    try :
142        defaults = { \
143                     "format" : "csv", \
144                   }
145        short_options = "vhd:f:"
146        long_options = ["help", "version", "data=", "format="]
147       
148        # Initializes the command line tool
149        dumper = DumPyKota(doc=__doc__)
150       
151        # parse and checks the command line
152        (options, args) = dumper.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1)
153       
154        # sets long options
155        options["help"] = options["h"] or options["help"]
156        options["version"] = options["v"] or options["version"]
157        options["data"] = options["d"] or options["data"]
158        options["format"] = options["f"] or options["format"] or defaults["format"]
159       
160        if options["help"] :
161            dumper.display_usage_and_quit()
162        elif options["version"] :
163            dumper.display_version_and_quit()
164        elif options["data"] is None :   
165            raise PyKotaToolError, _("The -d | --data command line option is mandatory, see help.")
166        else :
167            if args :
168                raise PyKotaToolError, _("Too many arguments, see help.")
169            retcode = dumper.main(args, options)
170    except SystemExit :       
171        pass
172    except :
173        try :
174            dumper.crashed("dumpykota failed")
175        except :   
176            pass
177        retcode = -1
178
179    try :
180        dumper.storage.close()
181    except (TypeError, NameError, AttributeError) :   
182        pass
183       
184    sys.exit(retcode)   
Note: See TracBrowser for help on using the browser.