tools: reduce code duplication

Have one implementation for all property-related tools.
This commit is contained in:
Marek Marczykowski-Górecki 2017-03-08 16:19:27 +01:00
parent bb770b4744
commit 6b8d58885b
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724
2 changed files with 52 additions and 111 deletions

View File

@ -26,90 +26,17 @@
from __future__ import print_function
import argparse
import sys
import qubesmgmt
import qubesmgmt.tools
import qubesmgmt.utils
parser = qubesmgmt.tools.QubesArgumentParser()
# keep it here for compatibility with earlier and possibly future versions
parser.add_argument('--force-root',
action='store_true', help=argparse.SUPPRESS)
# parser.add_argument('--help-properties',
# action=qubesmgmt.tools.HelpPropertiesAction)
parser.add_argument('--get', '-g',
action='store_true',
help='Ignored; for compatibility with older scripts.')
parser.add_argument('--set', '-s',
action='store_true',
help='Ignored; for compatibility with older scripts.')
parser.add_argument('property', metavar='PROPERTY',
nargs='?',
help='name of the property to show or change')
parser_value = parser.add_mutually_exclusive_group()
parser_value.add_argument('value', metavar='VALUE',
nargs='?',
help='new value of the property')
parser.add_argument('--unset', '--default', '--delete', '-D',
dest='delete',
action='store_true',
help='unset the property; if property has default value, it will be used'
' instead')
import qubesmgmt.tools.qvm_prefs
def main(args=None): # pylint: disable=missing-docstring
parser = qubesmgmt.tools.qvm_prefs.get_parser(0)
args = parser.parse_args(args)
if args.property is None:
properties = args.app.property_list()
width = max(len(prop.__name__) for prop in properties)
for prop in sorted(properties):
try:
value = getattr(args.app, prop.__name__)
except AttributeError:
print('{name:{width}s} U'.format(
name=prop.__name__, width=width))
continue
if args.app.property_is_default(prop):
print('{name:{width}s} D {value!s}'.format(
name=prop.__name__, width=width, value=value))
else:
print('{name:{width}s} - {value!s}'.format(
name=prop.__name__, width=width, value=value))
return 0
else:
args.property = args.property.replace('-', '_')
if args.value is not None:
setattr(args.app, args.property, args.value)
args.app.save()
return 0
if args.delete:
delattr(args.app, args.property)
args.app.save()
return 0
print(str(getattr(args.app, args.property)))
return 0
target = args.app
return qubesmgmt.tools.qvm_prefs.process_actions(parser, args, target)
if __name__ == '__main__':
sys.exit(main())

View File

@ -33,56 +33,63 @@ import qubesmgmt.utils
import qubesmgmt.vm
parser = qubesmgmt.tools.QubesArgumentParser(
want_force_root=True,
vmname_nargs=1)
def get_parser(vmname_nargs=1):
'''Return argument parser for generic property-related tool'''
parser = qubesmgmt.tools.QubesArgumentParser(
want_force_root=True,
vmname_nargs=vmname_nargs)
# parser.add_argument('--help-properties',
# action=qubesmgmt.tools.HelpPropertiesAction,
# klass=qubesmgmt.vm.QubesVM)
# parser.add_argument('--help-properties',
# action=qubesmgmt.tools.HelpPropertiesAction,
# klass=qubesmgmt.vm.QubesVM)
parser.add_argument('--get', '-g',
action='store_true',
help='Ignored; for compatibility with older scripts.')
parser.add_argument('--get', '-g',
action='store_true',
help='Ignored; for compatibility with older scripts.')
parser.add_argument('--set', '-s',
action='store_true',
help='Ignored; for compatibility with older scripts.')
parser.add_argument('--set', '-s',
action='store_true',
help='Ignored; for compatibility with older scripts.')
parser.add_argument('property', metavar='PROPERTY',
nargs='?',
help='name of the property to show or change')
parser.add_argument('property', metavar='PROPERTY',
nargs='?',
help='name of the property to show or change')
parser_value = parser.add_mutually_exclusive_group()
parser_value = parser.add_mutually_exclusive_group()
parser_value.add_argument('value', metavar='VALUE',
nargs='?',
help='new value of the property')
parser_value.add_argument('value', metavar='VALUE',
nargs='?',
help='new value of the property')
parser.add_argument('--unset', '--default', '--delete', '-D',
dest='delete',
action='store_true',
help='unset the property; if property has default value, it will be used'
' instead')
parser.add_argument('--unset', '--default', '--delete', '-D',
dest='delete',
action='store_true',
help='unset the property; '
'if property has default value, it will be used instead')
return parser
def main(args=None): # pylint: disable=missing-docstring
args = parser.parse_args(args)
args.domain = args.domains.pop()
def process_actions(parser, args, target):
'''Handle actions for generic property-related tool
:param parser: argument parser used to produce args
:param args: arguments to handle
:param target: object on which actions should be performed
'''
if args.property is None:
properties = args.domain.property_list()
properties = target.property_list()
width = max(len(prop.__name__) for prop in properties)
for prop in sorted(properties):
try:
value = getattr(args.domain, prop.__name__)
value = getattr(target, prop.__name__)
except AttributeError:
print('{name:{width}s} U'.format(
name=prop.__name__, width=width))
continue
if args.domain.property_is_default(prop):
if target.property_is_default(prop):
print('{name:{width}s} D {value!s}'.format(
name=prop.__name__, width=width, value=value))
else:
@ -94,23 +101,30 @@ def main(args=None): # pylint: disable=missing-docstring
args.property = args.property.replace('-', '_')
if args.property not in [prop.__name__
for prop in args.domain.property_list()]:
for prop in target.property_list()]:
parser.error('no such property: {!r}'.format(args.property))
if args.value is not None:
setattr(args.domain, args.property, args.value)
setattr(target, args.property, args.value)
args.app.save()
return 0
if args.delete:
delattr(args.domain, args.property)
delattr(target, args.property)
args.app.save()
return 0
print(str(getattr(args.domain, args.property)))
print(str(getattr(target, args.property)))
return 0
def main(args=None): # pylint: disable=missing-docstring
parser = get_parser(1)
args = parser.parse_args(args)
target = args.domains.pop()
return process_actions(parser, args, target)
if __name__ == '__main__':
sys.exit(main())