Don't use `flake8: noqa`.
[mailman.git] / src / mailman / commands / cli_qfile.py
blob0ed078ff3f261e22f553f729884b2b566197b3db
1 # Copyright (C) 2009-2016 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 import pickle
22 from mailman import public
23 from mailman.core.i18n import _
24 from mailman.interfaces.command import ICLISubCommand
25 from mailman.utilities.interact import interact
26 from pprint import PrettyPrinter
27 from zope.interface import implementer
30 # This is deliberately called 'm' for use with --interactive.
31 m = []
34 @public
35 @implementer(ICLISubCommand)
36 class QFile:
37 """Get information out of a queue file."""
39 name = 'qfile'
41 def add(self, parser, command_parser):
42 """See `ICLISubCommand`."""
43 self.parser = parser
44 command_parser.add_argument(
45 '-n', '--noprint',
46 dest='doprint', default=True, action='store_false',
47 help=_("""\
48 Don't attempt to pretty print the object. This is useful if there
49 is some problem with the object and you just want to get an
50 unpickled representation. Useful with 'bin/dumpdb -i <file>'. In
51 that case, the list of unpickled objects will be left in a
52 variable called 'm'."""))
53 command_parser.add_argument(
54 '-i', '--interactive',
55 default=False, action='store_true',
56 help=_("""\
57 Start an interactive Python session, with a variable called 'm'
58 containing the list of unpickled objects."""))
59 command_parser.add_argument(
60 'qfile', metavar='FILENAME', nargs=1,
61 help=_('The queue file to dump.'))
63 def process(self, args):
64 """See `ICLISubCommand`."""
65 printer = PrettyPrinter(indent=4)
66 assert len(args.qfile) == 1, 'Wrong number of positional arguments'
67 with open(args.qfile[0], 'rb') as fp:
68 while True:
69 try:
70 m.append(pickle.load(fp))
71 except EOFError:
72 break
73 if args.doprint:
74 print(_('[----- start pickle -----]'))
75 for i, obj in enumerate(m):
76 count = i + 1
77 print(_('<----- start object $count ----->'))
78 if isinstance(obj, (bytes, str)):
79 print(obj)
80 else:
81 printer.pprint(obj)
82 print(_('[----- end pickle -----]'))
83 count = len(m) # noqa
84 banner = _("The variable 'm' contains $count objects")
85 if args.interactive:
86 interact(banner=banner)