From 795909bc055bc0f08955488803735e54968cc1b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Sat, 11 Mar 2017 01:49:04 +0100 Subject: [PATCH] tools/qvm-prefs: re-add --help-properties action Specific VM object is required to get list of properties and help on them, so convert HelpPropertiesAction into normal action (like --get or --set). --- qubesmgmt/tools/__init__.py | 42 ------------------------------------ qubesmgmt/tools/qvm_ls.py | 3 +++ qubesmgmt/tools/qvm_prefs.py | 22 +++++++++++++++---- 3 files changed, 21 insertions(+), 46 deletions(-) diff --git a/qubesmgmt/tools/__init__.py b/qubesmgmt/tools/__init__.py index 92f62a1..eaed808 100644 --- a/qubesmgmt/tools/__init__.py +++ b/qubesmgmt/tools/__init__.py @@ -105,48 +105,6 @@ class SinglePropertyAction(argparse.Action): if self.const is None else self.const -# class HelpPropertiesAction(argparse.Action): -# '''Action for argument parser that displays all properties and exits.''' -# # pylint: disable=redefined-builtin,too-few-public-methods -# def __init__(self, -# option_strings, -# klass=None, -# dest=argparse.SUPPRESS, -# default=argparse.SUPPRESS, -# help='list all available properties with short descriptions' -# ' and exit'): -# super(HelpPropertiesAction, self).__init__( -# option_strings=option_strings, -# dest=dest, -# default=default, -# nargs=0, -# help=help) -# -# # late import because of circular dependency -# import qubes # pylint: disable=redefined-outer-name -# self._klass = klass if klass is not None else qubes.Qubes -# -# -# def __call__(self, parser, namespace, values, option_string=None): -# # pylint: disable=redefined-outer-name -# properties = self._klass.property_list() -# width = max(len(prop.__name__) for prop in properties) -# wrapper = textwrap.TextWrapper(width=80, -# initial_indent=' ', subsequent_indent=' ' * (width + 6)) -# -# text = 'Common properties:\n' + '\n'.join( -# wrapper.fill('{name:{width}s} {doc}'.format( -# name=prop.__name__, -# doc=qubesmgmt.utils.format_doc(prop.__doc__) if prop.__doc__ -# else'', -# width=width)) -# for prop in sorted(properties)) -# if self._klass is not qubes.Qubes: -# text += '\n\n' \ -# 'There may be more properties in specific domain classes.\n' -# parser.exit(message=text) -# - class VmNameAction(QubesAction): ''' Action for parsing one ore multiple domains from provided VMNAMEs ''' diff --git a/qubesmgmt/tools/qvm_ls.py b/qubesmgmt/tools/qvm_ls.py index 0bc7af3..1c19cdc 100644 --- a/qubesmgmt/tools/qvm_ls.py +++ b/qubesmgmt/tools/qvm_ls.py @@ -451,8 +451,11 @@ class _HelpColumnsAction(argparse.Action): doc=column.__doc__ or '', width=width)) for column in sorted(Column.columns.values())) + text += '\n\nAdditionally any VM property may be used as a column, ' \ + 'see qvm-prefs --help-properties for available values' parser.exit(message=text + '\n') + class _HelpFormatsAction(argparse.Action): '''Action for argument parser that displays all formats and exits.''' # pylint: disable=redefined-builtin diff --git a/qubesmgmt/tools/qvm_prefs.py b/qubesmgmt/tools/qvm_prefs.py index 1b3a1bb..13df85b 100644 --- a/qubesmgmt/tools/qvm_prefs.py +++ b/qubesmgmt/tools/qvm_prefs.py @@ -19,12 +19,12 @@ # with this program; if not, see . ''' Manipulate VM properties.''' -# TODO list properties for all classes # TODO list only non-default properties from __future__ import print_function import sys +import textwrap import qubesmgmt import qubesmgmt.tools @@ -37,9 +37,9 @@ def get_parser(vmname_nargs=1): parser = qubesmgmt.tools.QubesArgumentParser( vmname_nargs=vmname_nargs) - # parser.add_argument('--help-properties', - # action=qubesmgmt.tools.HelpPropertiesAction, - # klass=qubesmgmt.vm.QubesVM) + parser.add_argument('--help-properties', + action='store_true', + help='list all available properties with short descriptions and exit') parser.add_argument('--get', '-g', action='store_true', @@ -75,6 +75,20 @@ def process_actions(parser, args, target): :param args: arguments to handle :param target: object on which actions should be performed ''' + if args.help_properties: + properties = target.property_list() + width = max(len(prop) for prop in properties) + wrapper = textwrap.TextWrapper(width=80, + initial_indent=' ', subsequent_indent=' ' * (width + 6)) + + for prop in sorted(properties): + help_text = target.property_help(prop) + + print(wrapper.fill('{name:{width}s} {help_text!s}'.format( + name=prop, width=width, help_text=help_text))) + + return 0 + if args.property is None: properties = target.property_list() width = max(len(prop) for prop in properties)