qubes_prefs.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/python2
  2. # -*- encoding: utf8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2010-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. # Copyright (C) 2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. #
  23. # TODO merge printing with qvm-prefs
  24. # TODO list only non-default properties
  25. from __future__ import print_function
  26. import argparse
  27. import sys
  28. import textwrap
  29. import qubes
  30. import qubes.tools
  31. import qubes.utils
  32. parser = qubes.tools.QubesArgumentParser(
  33. want_force_root=True)
  34. parser.add_argument('--help-properties',
  35. action=qubes.tools.HelpPropertiesAction)
  36. parser.add_argument('property', metavar='PROPERTY',
  37. nargs='?',
  38. help='name of the property to show or change')
  39. parser_value = parser.add_mutually_exclusive_group()
  40. parser_value.add_argument('value', metavar='VALUE',
  41. nargs='?',
  42. help='new value of the property')
  43. parser.add_argument('--unset', '--default', '--delete', '-D',
  44. dest='delete',
  45. action='store_true',
  46. help='unset the property; if property has default value, it will be used'
  47. ' instead')
  48. def main(args=None):
  49. args = parser.parse_args(args)
  50. if args.property is None:
  51. properties = args.app.property_list()
  52. width = max(len(prop.__name__) for prop in properties)
  53. for prop in sorted(properties):
  54. try:
  55. value = getattr(args.app, prop.__name__)
  56. except AttributeError:
  57. print('{name:{width}s} U'.format(
  58. name=prop.__name__, width=width))
  59. continue
  60. if args.app.property_is_default(prop):
  61. print('{name:{width}s} D {value!r}'.format(
  62. name=prop.__name__, width=width, value=value))
  63. else:
  64. print('{name:{width}s} - {value!r}'.format(
  65. name=prop.__name__, width=width, value=value))
  66. return 0
  67. if args.value is not None:
  68. setattr(args.app, args.property, args.value)
  69. args.app.save()
  70. return 0
  71. if args.delete:
  72. delattr(args.app, args.property)
  73. args.app.save()
  74. return 0
  75. print(str(getattr(args.app, args.property)))
  76. return 0
  77. if __name__ == '__main__':
  78. sys.exit(main())