From 03298ebe95f393fc77f8acce62e5aa0b95a2bdd0 Mon Sep 17 00:00:00 2001 From: Michael Hanselmann <hansmi@google.com> Date: Mon, 17 Aug 2009 15:42:51 +0200 Subject: [PATCH] cli: Use ToStdout/ToStderr instead of print Signed-off-by: Michael Hanselmann <hansmi@google.com> Reviewed-by: Iustin Pop <iustin@google.com> --- lib/cli.py | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/lib/cli.py b/lib/cli.py index fd329695f..cf51a19a1 100644 --- a/lib/cli.py +++ b/lib/cli.py @@ -41,6 +41,7 @@ from ganeti import rpc from optparse import (OptionParser, make_option, TitledHelpFormatter, Option, OptionValueError) + __all__ = ["DEBUG_OPT", "NOHDR_OPT", "SEP_OPT", "GenericMain", "SubmitOpCode", "GetClient", "cli_option", "ikv_option", "keyval_option", @@ -57,6 +58,7 @@ __all__ = ["DEBUG_OPT", "NOHDR_OPT", "SEP_OPT", "GenericMain", NO_PREFIX = "no_" UN_PREFIX = "-" + def _ExtractTagsObject(opts, args): """Extract the tag type object. @@ -122,7 +124,7 @@ def ListTags(opts, args): result = list(result) result.sort() for tag in result: - print tag + ToStdout(tag) def AddTags(opts, args): @@ -356,7 +358,7 @@ def _ParseArgs(argv, commands, aliases): binary = argv[0].split("/")[-1] if len(argv) > 1 and argv[1] == "--version": - print "%s (ganeti) %s" % (binary, constants.RELEASE_VERSION) + ToStdout("%s (ganeti) %s", binary, constants.RELEASE_VERSION) # Quit right away. That way we don't have to care about this special # argument. optparse.py does it the same. sys.exit(0) @@ -366,22 +368,27 @@ def _ParseArgs(argv, commands, aliases): # let's do a nice thing sortedcmds = commands.keys() sortedcmds.sort() - print ("Usage: %(bin)s {command} [options...] [argument...]" - "\n%(bin)s <command> --help to see details, or" - " man %(bin)s\n" % {"bin": binary}) + + ToStdout("Usage: %s {command} [options...] [argument...]", binary) + ToStdout("%s <command> --help to see details, or man %s", binary, binary) + ToStdout("") + # compute the max line length for cmd + usage mlen = max([len(" %s" % cmd) for cmd in commands]) mlen = min(60, mlen) # should not get here... + # and format a nice command list - print "Commands:" + ToStdout("Commands:") for cmd in sortedcmds: cmdstr = " %s" % (cmd,) help_text = commands[cmd][4] - help_lines = textwrap.wrap(help_text, 79-3-mlen) - print "%-*s - %s" % (mlen, cmdstr, help_lines.pop(0)) + help_lines = textwrap.wrap(help_text, 79 - 3 - mlen) + ToStdout("%-*s - %s", mlen, cmdstr, help_lines.pop(0)) for line in help_lines: - print "%-*s %s" % (mlen, "", line) - print + ToStdout("%-*s %s", mlen, "", line) + + ToStdout("") + return None, None, None # get command, unalias it, and look it up in commands @@ -406,15 +413,13 @@ def _ParseArgs(argv, commands, aliases): options, args = parser.parse_args() if nargs is None: if len(args) != 0: - print >> sys.stderr, ("Error: Command %s expects no arguments" % cmd) + ToStderr("Error: Command %s expects no arguments", cmd) return None, None, None elif nargs < 0 and len(args) != -nargs: - print >> sys.stderr, ("Error: Command %s expects %d argument(s)" % - (cmd, -nargs)) + ToStderr("Error: Command %s expects %d argument(s)", cmd, -nargs) return None, None, None elif nargs >= 0 and len(args) < nargs: - print >> sys.stderr, ("Error: Command %s expects at least %d argument(s)" % - (cmd, nargs)) + ToStderr("Error: Command %s expects at least %d argument(s)", cmd, nargs) return None, None, None return func, options, args @@ -560,7 +565,7 @@ def PollJob(job_id, cl=None, feedback_fn=None): feedback_fn(log_entry[1:]) else: encoded = utils.SafeEncode(message) - print "%s %s" % (time.ctime(utils.MergeTime(timestamp)), encoded) + ToStdout("%s %s", time.ctime(utils.MergeTime(timestamp)), encoded) prev_logmsg_serial = max(prev_logmsg_serial, serial) # TODO: Handle canceled and archived jobs -- GitLab