From 31214aba53d93617f1f9514deb9abc9f0953d49e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marta=20Marczykowska-G=C3=B3recka?= Date: Wed, 5 Jun 2019 16:17:43 +0200 Subject: [PATCH] Added handling for more file fields in GetAppmenus Adding the optional --file-field parameter. It changes to output format to '|'-separated one, and outputs not only file name and app name, but also whatever fields from the file the user has requested. Used by GUI VM settings. references QubesOS/qubes-issues#5076 --- qubesappmenus/__init__.py | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/qubesappmenus/__init__.py b/qubesappmenus/__init__.py index a86f9c4..03e97c1 100644 --- a/qubesappmenus/__init__.py +++ b/qubesappmenus/__init__.py @@ -179,21 +179,31 @@ def get_available_filenames(self, vm): listed.add(filename) yield os.path.join(template_dir, filename) - def get_available(self, vm): + def get_available(self, vm, fields=None): '''Get available menu entries for given VM - Returns a generator of (filename, display_name) tuples''' + Returns a generator of lists that contain fields to be outputted''' # TODO icon path (#2885) for filename in self.get_available_filenames(vm): + field_values = {} with open(filename) as file: name = None for line in file: if line.startswith('Name=%VMNAME%: '): name = line.partition('Name=%VMNAME%: ')[2].strip() - break + if fields: + for f in fields: + if line.startswith(f + '=') or\ + line.startswith(f + ' ='): + field_values[f] = \ + line.partition(f + '=')[2].strip() assert name is not None, \ 'template {!r} does not contain name'.format(filename) - yield (os.path.basename(filename), name) + result = [os.path.basename(filename), name] + if fields: + for f in fields: + result.append(field_values.get(f, '')) + yield result def appmenus_create(self, vm, force=False, refresh_cache=True): """Create/update .desktop files @@ -575,6 +585,9 @@ def appmenus_update(self, vm, force=False): help='required pledge for --get-available') parser.add_argument('domains', metavar='VMNAME', nargs='+', help='VMs on which perform requested actions') +parser.add_argument('--file-field', action='append', dest='fields', + help='File field to append to output for --get-available; can be used' + ' multiple times for multiple fields') def retrieve_list(path): @@ -631,8 +644,15 @@ def main(args=None, app=None): parser.error( 'this requires --i-understand-format-is-unstable ' 'and a sacrifice of one cute kitten') - sys.stdout.write(''.join('{} - {}\n'.format(*available) - for available in appmenus.get_available(vm))) + if not args.fields: + sys.stdout.write(''.join('{} - {}\n'.format(*available) + for available in + appmenus.get_available(vm))) + else: + for result in appmenus.get_available( + vm, fields=args.fields): + print(' | '.join(result)) + if __name__ == '__main__': sys.exit(main())