tools/qvm-device: make PEP8 happy
This commit is contained in:
parent
4d0545405f
commit
174f155d31
@ -21,7 +21,7 @@
|
|||||||
# with this program; if not, write to the Free Software Foundation, Inc.,
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
|
||||||
'''Qubes volume and block device managment'''
|
"""Qubes volume and block device managment"""
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import os
|
import os
|
||||||
@ -34,7 +34,7 @@ import qubesadmin.devices
|
|||||||
|
|
||||||
|
|
||||||
def prepare_table(dev_list):
|
def prepare_table(dev_list):
|
||||||
''' Converts a list of :py:class:`qubes.devices.DeviceInfo` objects to a
|
""" Converts a list of :py:class:`qubes.devices.DeviceInfo` objects to a
|
||||||
list of tupples for the :py:func:`qubes.tools.print_table`.
|
list of tupples for the :py:func:`qubes.tools.print_table`.
|
||||||
|
|
||||||
If :program:`qvm-devices` is running in a TTY, it will ommit duplicate
|
If :program:`qvm-devices` is running in a TTY, it will ommit duplicate
|
||||||
@ -43,7 +43,7 @@ def prepare_table(dev_list):
|
|||||||
:param iterable dev_list: List of :py:class:`qubes.devices.DeviceInfo`
|
:param iterable dev_list: List of :py:class:`qubes.devices.DeviceInfo`
|
||||||
objects.
|
objects.
|
||||||
:returns: list of tupples
|
:returns: list of tupples
|
||||||
'''
|
"""
|
||||||
output = []
|
output = []
|
||||||
header = []
|
header = []
|
||||||
if sys.stdout.isatty():
|
if sys.stdout.isatty():
|
||||||
@ -60,7 +60,8 @@ def prepare_table(dev_list):
|
|||||||
|
|
||||||
|
|
||||||
class Line(object):
|
class Line(object):
|
||||||
'''Helper class to hold single device info for listing'''
|
"""Helper class to hold single device info for listing"""
|
||||||
|
|
||||||
# pylint: disable=too-few-public-methods
|
# pylint: disable=too-few-public-methods
|
||||||
def __init__(self, device: qubesadmin.devices.DeviceInfo, attached_to=None):
|
def __init__(self, device: qubesadmin.devices.DeviceInfo, attached_to=None):
|
||||||
self.ident = "{!s}:{!s}".format(device.backend_domain, device.ident)
|
self.ident = "{!s}:{!s}".format(device.backend_domain, device.ident)
|
||||||
@ -70,13 +71,13 @@ class Line(object):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def assignments(self):
|
def assignments(self):
|
||||||
'''list of frontends the device is assigned to'''
|
"""list of frontends the device is assigned to"""
|
||||||
return ', '.join(self.frontends)
|
return ', '.join(self.frontends)
|
||||||
|
|
||||||
|
|
||||||
def list_devices(args):
|
def list_devices(args):
|
||||||
''' Called by the parser to execute the qubes-devices list
|
""" Called by the parser to execute the qubes-devices list
|
||||||
subcommand. '''
|
subcommand. """
|
||||||
app = args.app
|
app = args.app
|
||||||
|
|
||||||
devices = set()
|
devices = set()
|
||||||
@ -105,7 +106,8 @@ def list_devices(args):
|
|||||||
if assignment.options:
|
if assignment.options:
|
||||||
result[dev].frontends.append('{!s} ({})'.format(
|
result[dev].frontends.append('{!s} ({})'.format(
|
||||||
domain, ', '.join('{}={}'.format(key, value)
|
domain, ', '.join('{}={}'.format(key, value)
|
||||||
for key, value in assignment.options.items())))
|
for key, value in
|
||||||
|
assignment.options.items())))
|
||||||
else:
|
else:
|
||||||
result[dev].frontends.append(str(domain))
|
result[dev].frontends.append(str(domain))
|
||||||
|
|
||||||
@ -113,9 +115,9 @@ def list_devices(args):
|
|||||||
|
|
||||||
|
|
||||||
def attach_device(args):
|
def attach_device(args):
|
||||||
''' Called by the parser to execute the :program:`qvm-devices attach`
|
""" Called by the parser to execute the :program:`qvm-devices attach`
|
||||||
subcommand.
|
subcommand.
|
||||||
'''
|
"""
|
||||||
device_assignment = args.device_assignment
|
device_assignment = args.device_assignment
|
||||||
vm = args.domains[0]
|
vm = args.domains[0]
|
||||||
options = dict(opt.split('=', 1) for opt in args.option or [])
|
options = dict(opt.split('=', 1) for opt in args.option or [])
|
||||||
@ -127,9 +129,9 @@ def attach_device(args):
|
|||||||
|
|
||||||
|
|
||||||
def detach_device(args):
|
def detach_device(args):
|
||||||
''' Called by the parser to execute the :program:`qvm-devices detach`
|
""" Called by the parser to execute the :program:`qvm-devices detach`
|
||||||
subcommand.
|
subcommand.
|
||||||
'''
|
"""
|
||||||
vm = args.domains[0]
|
vm = args.domains[0]
|
||||||
if args.device_assignment:
|
if args.device_assignment:
|
||||||
vm.devices[args.devclass].detach(args.device_assignment)
|
vm.devices[args.devclass].detach(args.device_assignment)
|
||||||
@ -139,7 +141,7 @@ def detach_device(args):
|
|||||||
|
|
||||||
|
|
||||||
def init_list_parser(sub_parsers):
|
def init_list_parser(sub_parsers):
|
||||||
''' Configures the parser for the :program:`qvm-devices list` subcommand '''
|
""" Configures the parser for the :program:`qvm-devices list` subcommand """
|
||||||
# pylint: disable=protected-access
|
# pylint: disable=protected-access
|
||||||
list_parser = sub_parsers.add_parser('list', aliases=('ls', 'l'),
|
list_parser = sub_parsers.add_parser('list', aliases=('ls', 'l'),
|
||||||
help='list devices')
|
help='list devices')
|
||||||
@ -152,10 +154,10 @@ def init_list_parser(sub_parsers):
|
|||||||
|
|
||||||
|
|
||||||
class DeviceAction(qubesadmin.tools.QubesAction):
|
class DeviceAction(qubesadmin.tools.QubesAction):
|
||||||
''' Action for argument parser that gets the
|
""" Action for argument parser that gets the
|
||||||
:py:class:``qubesadmin.device.DeviceAssignment`` from a
|
:py:class:``qubesadmin.device.DeviceAssignment`` from a
|
||||||
BACKEND:DEVICE_ID string.
|
BACKEND:DEVICE_ID string.
|
||||||
''' # pylint: disable=too-few-public-methods
|
""" # pylint: disable=too-few-public-methods
|
||||||
|
|
||||||
def __init__(self, help='A backend & device id combination',
|
def __init__(self, help='A backend & device id combination',
|
||||||
required=True, allow_unknown=False, **kwargs):
|
required=True, allow_unknown=False, **kwargs):
|
||||||
@ -165,7 +167,7 @@ class DeviceAction(qubesadmin.tools.QubesAction):
|
|||||||
**kwargs)
|
**kwargs)
|
||||||
|
|
||||||
def __call__(self, parser, namespace, values, option_string=None):
|
def __call__(self, parser, namespace, values, option_string=None):
|
||||||
''' Set ``namespace.device_assignment`` to ``values`` '''
|
""" Set ``namespace.device_assignment`` to ``values`` """
|
||||||
setattr(namespace, self.dest, values)
|
setattr(namespace, self.dest, values)
|
||||||
|
|
||||||
def parse_qubes_app(self, parser, namespace):
|
def parse_qubes_app(self, parser, namespace):
|
||||||
@ -177,6 +179,7 @@ class DeviceAction(qubesadmin.tools.QubesAction):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
vmname, device_id = backend_device_id.split(':', 1)
|
vmname, device_id = backend_device_id.split(':', 1)
|
||||||
|
vm = None
|
||||||
try:
|
try:
|
||||||
vm = app.domains[vmname]
|
vm = app.domains[vmname]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
@ -184,36 +187,37 @@ class DeviceAction(qubesadmin.tools.QubesAction):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
dev = vm.devices[devclass][device_id]
|
dev = vm.devices[devclass][device_id]
|
||||||
if not self.allow_unknown and isinstance(dev,
|
if not self.allow_unknown and \
|
||||||
qubesadmin.devices.UnknownDevice):
|
isinstance(dev, qubesadmin.devices.UnknownDevice):
|
||||||
raise KeyError(device_id)
|
raise KeyError(device_id)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
parser.error_runtime(
|
parser.error_runtime(
|
||||||
"backend vm {!r} doesn't expose device {!r}"
|
"backend vm {!r} doesn't expose device {!r}".format(
|
||||||
.format(vmname, device_id))
|
vmname, device_id))
|
||||||
device_assignment = qubesadmin.devices.DeviceAssignment(vm,
|
device_assignment = qubesadmin.devices.DeviceAssignment(
|
||||||
device_id)
|
vm, device_id)
|
||||||
setattr(namespace, self.dest, device_assignment)
|
setattr(namespace, self.dest, device_assignment)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
parser.error('expected a backend vm & device id combination ' \
|
parser.error(
|
||||||
'like foo:bar got %s' % backend_device_id)
|
'expected a backend vm & device id combination like foo:bar '
|
||||||
|
'got %s' % backend_device_id)
|
||||||
|
|
||||||
|
|
||||||
def get_parser(device_class=None):
|
def get_parser(device_class=None):
|
||||||
'''Create :py:class:`argparse.ArgumentParser` suitable for
|
"""Create :py:class:`argparse.ArgumentParser` suitable for
|
||||||
:program:`qvm-block`.
|
:program:`qvm-block`.
|
||||||
'''
|
"""
|
||||||
parser = qubesadmin.tools.QubesArgumentParser(description=__doc__,
|
parser = qubesadmin.tools.QubesArgumentParser(description=__doc__,
|
||||||
want_app=True)
|
want_app=True)
|
||||||
parser.register('action', 'parsers',
|
parser.register('action', 'parsers',
|
||||||
qubesadmin.tools.AliasedSubParsersAction)
|
qubesadmin.tools.AliasedSubParsersAction)
|
||||||
if device_class:
|
if device_class:
|
||||||
parser.add_argument('devclass', const=device_class,
|
parser.add_argument('devclass', const=device_class,
|
||||||
action='store_const',
|
action='store_const',
|
||||||
help=argparse.SUPPRESS)
|
help=argparse.SUPPRESS)
|
||||||
else:
|
else:
|
||||||
parser.add_argument('devclass', metavar='DEVICE_CLASS', action='store',
|
parser.add_argument('devclass', metavar='DEVICE_CLASS', action='store',
|
||||||
help="Device class to manage ('pci', 'usb', etc)")
|
help="Device class to manage ('pci', 'usb', etc)")
|
||||||
|
|
||||||
# default action
|
# default action
|
||||||
parser.set_defaults(func=list_devices)
|
parser.set_defaults(func=list_devices)
|
||||||
@ -229,27 +233,32 @@ def get_parser(device_class=None):
|
|||||||
"detach", help="Detach device from domain", aliases=('d', 'dt'))
|
"detach", help="Detach device from domain", aliases=('d', 'dt'))
|
||||||
|
|
||||||
attach_parser.add_argument('VMNAME', nargs=1,
|
attach_parser.add_argument('VMNAME', nargs=1,
|
||||||
action=qubesadmin.tools.VmNameAction)
|
action=qubesadmin.tools.VmNameAction)
|
||||||
detach_parser.add_argument('VMNAME', nargs=1,
|
detach_parser.add_argument('VMNAME', nargs=1,
|
||||||
action=qubesadmin.tools.VmNameAction)
|
action=qubesadmin.tools.VmNameAction)
|
||||||
|
|
||||||
attach_parser.add_argument(metavar='BACKEND:DEVICE_ID',
|
attach_parser.add_argument(metavar='BACKEND:DEVICE_ID',
|
||||||
dest='device_assignment',
|
dest='device_assignment',
|
||||||
action=DeviceAction)
|
action=DeviceAction)
|
||||||
detach_parser.add_argument(metavar='BACKEND:DEVICE_ID',
|
detach_parser.add_argument(metavar='BACKEND:DEVICE_ID',
|
||||||
dest='device_assignment', nargs=argparse.OPTIONAL,
|
dest='device_assignment',
|
||||||
action=DeviceAction, allow_unknown=True)
|
nargs=argparse.OPTIONAL,
|
||||||
|
action=DeviceAction, allow_unknown=True)
|
||||||
|
|
||||||
attach_parser.add_argument('--option', '-o', action='append',
|
attach_parser.add_argument('--option', '-o', action='append',
|
||||||
help="Set option for the device in opt=value form (can be specified "
|
help="Set option for the device in opt=value "
|
||||||
"multiple times), see man qvm-device for details")
|
"form (can be specified "
|
||||||
|
"multiple times), see man qvm-device for "
|
||||||
|
"details")
|
||||||
attach_parser.add_argument('--ro', action='store_true', default=False,
|
attach_parser.add_argument('--ro', action='store_true', default=False,
|
||||||
help="Attach device read-only (alias for read-only=yes option, "
|
help="Attach device read-only (alias for "
|
||||||
"takes precedence)")
|
"read-only=yes option, "
|
||||||
|
"takes precedence)")
|
||||||
attach_parser.add_argument('--persistent', '-p', action='store_true',
|
attach_parser.add_argument('--persistent', '-p', action='store_true',
|
||||||
default=False,
|
default=False,
|
||||||
help="Attach device persistently (so it will be automatically "
|
help="Attach device persistently (so it will "
|
||||||
"attached at qube startup)")
|
"be automatically "
|
||||||
|
"attached at qube startup)")
|
||||||
|
|
||||||
attach_parser.set_defaults(func=attach_device)
|
attach_parser.set_defaults(func=attach_device)
|
||||||
detach_parser.set_defaults(func=detach_device)
|
detach_parser.set_defaults(func=detach_device)
|
||||||
@ -258,7 +267,7 @@ def get_parser(device_class=None):
|
|||||||
|
|
||||||
|
|
||||||
def main(args=None, app=None):
|
def main(args=None, app=None):
|
||||||
'''Main routine of :program:`qvm-block`.'''
|
"""Main routine of :program:`qvm-block`."""
|
||||||
basename = os.path.basename(sys.argv[0])
|
basename = os.path.basename(sys.argv[0])
|
||||||
devclass = None
|
devclass = None
|
||||||
if basename.startswith('qvm-') and basename != 'qvm-device':
|
if basename.startswith('qvm-') and basename != 'qvm-device':
|
||||||
|
Loading…
Reference in New Issue
Block a user