Skip to content

Commit

Permalink
tools: adjust for qubesmgmt module
Browse files Browse the repository at this point in the history
For now comment out HelpPropertiesAction, as we don't have VM classes
here and accessing properties help require VM instances.
This needs some better idea...
  • Loading branch information
marmarek committed Mar 8, 2017
1 parent 2e2ce84 commit a813d50
Showing 1 changed file with 52 additions and 49 deletions.
101 changes: 52 additions & 49 deletions qubesmgmt/tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# encoding=utf-8
#
# The Qubes OS Project, https://www.qubes-os.org/
#
Expand Down Expand Up @@ -32,7 +33,8 @@
import sys
import textwrap

import qubes.log
import qubesmgmt.log
import qubesmgmt.exc

#: constant returned when some action should be performed on all qubes
VM_ALL = object()
Expand Down Expand Up @@ -105,46 +107,47 @@ def __call__(self, parser, namespace, values, option_string=None):
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=qubes.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 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 '''
Expand Down Expand Up @@ -290,7 +293,7 @@ def parse_qubes_app(self, parser, namespace):
try:
pools = [app.get_pool(name) for name in pool_names]
setattr(namespace, self.dest, pools)
except qubes.exc.QubesException as e:
except qubesmgmt.exc.QubesException as e:
parser.error(str(e))
sys.exit(2)

Expand Down Expand Up @@ -357,8 +360,7 @@ def parse_args(self, *args, **kwargs):

if self._want_app and not self._want_app_no_instance:
self.set_qubes_verbosity(namespace)
namespace.app = qubes.Qubes(namespace.app,
offline_mode=namespace.offline_mode)
namespace.app = qubesmgmt.Qubes()

if self._want_force_root:
self.dont_run_as_root(namespace)
Expand Down Expand Up @@ -419,9 +421,9 @@ def set_qubes_verbosity(namespace):
verbose = namespace.verbose - namespace.quiet

if verbose >= 2:
qubes.log.enable_debug()
qubesmgmt.log.enable_debug()
elif verbose >= 1:
qubes.log.enable()
qubesmgmt.log.enable()

# pylint: disable=no-self-use
def print_error(self, *args, **kwargs):
Expand All @@ -430,16 +432,17 @@ def print_error(self, *args, **kwargs):


class AliasedSubParsersAction(argparse._SubParsersAction):
'''SubParser with support for action aliases'''
# source https://gist.github.com/sampsyo/471779
# pylint: disable=protected-access,too-few-public-methods
# pylint: disable=protected-access,too-few-public-methods,missing-docstring
class _AliasedPseudoAction(argparse.Action):
# pylint: disable=redefined-builtin
def __init__(self, name, aliases, help):
dest = name
if aliases:
dest += ' (%s)' % ','.join(aliases)
sup = super(AliasedSubParsersAction._AliasedPseudoAction, self)
sup.__init__(option_strings=[], dest=dest, help=help)
super(AliasedSubParsersAction._AliasedPseudoAction, self).\
__init__(option_strings=[], dest=dest, help=help)

def __call__(self, **kwargs):
super(AliasedSubParsersAction._AliasedPseudoAction, self).__call__(
Expand Down

0 comments on commit a813d50

Please sign in to comment.