From a4ebd726b3a6bfab3d96a2a70005fe38296a29ca Mon Sep 17 00:00:00 2001 From: Michael Hanselmann <hansmi@google.com> Date: Mon, 23 Aug 2010 17:14:11 +0200 Subject: [PATCH] =?UTF-8?q?Use=20one=20function=20to=20parse=20=E2=80=9C--?= =?UTF-8?q?fields=E2=80=9D=20option=20values?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Michael Hanselmann <hansmi@google.com> Reviewed-by: Iustin Pop <iustin@google.com> --- lib/cli.py | 19 +++++++++++++++++++ scripts/gnt-instance | 7 +------ scripts/gnt-job | 7 +------ scripts/gnt-node | 24 +++++++----------------- test/ganeti.cli_unittest.py | 13 +++++++++++++ 5 files changed, 41 insertions(+), 29 deletions(-) diff --git a/lib/cli.py b/lib/cli.py index 20190cc81..b33c98c80 100644 --- a/lib/cli.py +++ b/lib/cli.py @@ -195,6 +195,7 @@ __all__ = [ "cli_option", "SplitNodeOption", "CalculateOSNames", + "ParseFields", ] NO_PREFIX = "no_" @@ -1200,6 +1201,24 @@ def CalculateOSNames(os_name, os_variants): return [os_name] +def ParseFields(selected, default): + """Parses the values of "--field"-like options. + + @type selected: string or None + @param selected: User-selected options + @type default: list + @param default: Default fields + + """ + if selected is None: + return default + + if selected.startswith("+"): + return default + selected[1:].split(",") + + return selected.split(",") + + UsesRPC = rpc.RunWithRPC diff --git a/scripts/gnt-instance b/scripts/gnt-instance index e64ef9349..6d9e0f9cb 100755 --- a/scripts/gnt-instance +++ b/scripts/gnt-instance @@ -244,12 +244,7 @@ def ListInstances(opts, args): @return: the desired exit code """ - if opts.output is None: - selected_fields = _LIST_DEF_FIELDS - elif opts.output.startswith("+"): - selected_fields = _LIST_DEF_FIELDS + opts.output[1:].split(",") - else: - selected_fields = opts.output.split(",") + selected_fields = ParseFields(opts.output, _LIST_DEF_FIELDS) output = GetClient().QueryInstances(args, selected_fields, opts.do_locking) diff --git a/scripts/gnt-job b/scripts/gnt-job index 3088a1427..df2ced97d 100755 --- a/scripts/gnt-job +++ b/scripts/gnt-job @@ -61,12 +61,7 @@ def ListJobs(opts, args): @return: the desired exit code """ - if opts.output is None: - selected_fields = _LIST_DEF_FIELDS - elif opts.output.startswith("+"): - selected_fields = _LIST_DEF_FIELDS + opts.output[1:].split(",") - else: - selected_fields = opts.output.split(",") + selected_fields = ParseFields(opts.output, _LIST_DEF_FIELDS) output = GetClient().QueryJobs(args, selected_fields) if not opts.no_headers: diff --git a/scripts/gnt-node b/scripts/gnt-node index 1cb7f7d98..c17e2c64c 100755 --- a/scripts/gnt-node +++ b/scripts/gnt-node @@ -46,6 +46,10 @@ _LIST_DEF_FIELDS = [ ] +#: Default field list for L{ListVolumes} +_LIST_VOL_DEF_FIELDS = ["node", "phys", "vg", "name", "size", "instance"] + + #: default list of field for L{ListStorage} _LIST_STOR_DEF_FIELDS = [ constants.SF_NODE, @@ -188,12 +192,7 @@ def ListNodes(opts, args): @return: the desired exit code """ - if opts.output is None: - selected_fields = _LIST_DEF_FIELDS - elif opts.output.startswith("+"): - selected_fields = _LIST_DEF_FIELDS + opts.output[1:].split(",") - else: - selected_fields = opts.output.split(",") + selected_fields = ParseFields(opts.output, _LIST_DEF_FIELDS) output = GetClient().QueryNodes(args, selected_fields, opts.do_locking) @@ -466,11 +465,7 @@ def ListVolumes(opts, args): @return: the desired exit code """ - if opts.output is None: - selected_fields = ["node", "phys", "vg", - "name", "size", "instance"] - else: - selected_fields = opts.output.split(",") + selected_fields = ParseFields(opts.output, _LIST_VOL_DEF_FIELDS) op = opcodes.OpQueryNodeVolumes(nodes=args, output_fields=selected_fields) output = SubmitOpCode(op, opts=opts) @@ -514,12 +509,7 @@ def ListStorage(opts, args): storage_type = ConvertStorageType(opts.user_storage_type) - if opts.output is None: - selected_fields = _LIST_STOR_DEF_FIELDS - elif opts.output.startswith("+"): - selected_fields = _LIST_STOR_DEF_FIELDS + opts.output[1:].split(",") - else: - selected_fields = opts.output.split(",") + selected_fields = ParseFields(opts.output, _LIST_STOR_DEF_FIELDS) op = opcodes.OpQueryNodeStorage(nodes=args, storage_type=storage_type, diff --git a/test/ganeti.cli_unittest.py b/test/ganeti.cli_unittest.py index a1ae1471a..77ad4c16e 100755 --- a/test/ganeti.cli_unittest.py +++ b/test/ganeti.cli_unittest.py @@ -429,5 +429,18 @@ class TestFormatLogMessage(unittest.TestCase): self.assert_(cli.FormatLogMessage("some other type", (1, 2, 3))) +class TestParseFields(unittest.TestCase): + def test(self): + self.assertEqual(cli.ParseFields(None, []), []) + self.assertEqual(cli.ParseFields("name,foo,hello", []), + ["name", "foo", "hello"]) + self.assertEqual(cli.ParseFields(None, ["def", "ault", "fields", "here"]), + ["def", "ault", "fields", "here"]) + self.assertEqual(cli.ParseFields("name,foo", ["def", "ault"]), + ["name", "foo"]) + self.assertEqual(cli.ParseFields("+name,foo", ["def", "ault"]), + ["def", "ault", "name", "foo"]) + + if __name__ == '__main__': testutils.GanetiTestProgram() -- GitLab