Bump copyright years.
[mailman.git] / src / mailman / commands / cli_qfile.py
blob986898beebc56c64f471df7e38c7d0c7a90642ee
1 # Copyright (C) 2009-2014 by the Free Software Foundation, Inc.
3 # This file is part of GNU Mailman.
5 # GNU Mailman is free software: you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option)
8 # any later version.
10 # GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 # more details.
15 # You should have received a copy of the GNU General Public License along with
16 # GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
18 """Getting information out of a qfile."""
20 from __future__ import absolute_import, print_function, unicode_literals
22 __metaclass__ = type
23 __all__ = [
24 'QFile',
28 import cPickle
30 from pprint import PrettyPrinter
31 from zope.interface import implementer
33 from mailman.core.i18n import _
34 from mailman.interfaces.command import ICLISubCommand
35 from mailman.utilities.interact import interact
38 m = []
42 @implementer(ICLISubCommand)
43 class QFile:
44 """Get information out of a queue file."""
46 name = 'qfile'
48 def add(self, parser, command_parser):
49 """See `ICLISubCommand`."""
50 self.parser = parser
51 command_parser.add_argument(
52 '-n', '--noprint',
53 dest='doprint', default=True, action='store_false',
54 help=_("""\
55 Don't attempt to pretty print the object. This is useful if there
56 is some problem with the object and you just want to get an
57 unpickled representation. Useful with 'bin/dumpdb -i <file>'. In
58 that case, the list of unpickled objects will be left in a
59 variable called 'm'."""))
60 command_parser.add_argument(
61 '-i', '--interactive',
62 default=False, action='store_true',
63 help=_("""\
64 Start an interactive Python session, with a variable called 'm'
65 containing the list of unpickled objects."""))
66 command_parser.add_argument(
67 'qfile', metavar='FILENAME', nargs=1,
68 help=_('The queue file to dump.'))
70 def process(self, args):
71 """See `ICLISubCommand`."""
72 printer = PrettyPrinter(indent=4)
73 assert len(args.qfile) == 1, 'Wrong number of positional arguments'
74 with open(args.qfile[0]) as fp:
75 while True:
76 try:
77 m.append(cPickle.load(fp))
78 except EOFError:
79 break
80 if args.doprint:
81 print(_('[----- start pickle -----]'))
82 for i, obj in enumerate(m):
83 count = i + 1
84 print(_('<----- start object $count ----->'))
85 if isinstance(obj, basestring):
86 print(obj)
87 else:
88 printer.pprint(obj)
89 print(_('[----- end pickle -----]'))
90 count = len(m)
91 banner = _("The variable 'm' contains $count objects")
92 if args.interactive:
93 interact(banner=banner)