qubes_prefs.py 3.3 KB

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