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 from __future__ import print_function
import argparse
import sys import sys
import qubesmgmt import qubesmgmt
import qubesmgmt.tools import qubesmgmt.tools.qvm_prefs
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')
def main(args=None): # pylint: disable=missing-docstring def main(args=None): # pylint: disable=missing-docstring
parser = qubesmgmt.tools.qvm_prefs.get_parser(0)
args = parser.parse_args(args) args = parser.parse_args(args)
target = args.app
if args.property is None: return qubesmgmt.tools.qvm_prefs.process_actions(parser, args, target)
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
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(main()) sys.exit(main())

View File

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