Skip to content

Commit

Permalink
Added handling for more file fields in GetAppmenus
Browse files Browse the repository at this point in the history
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
  • Loading branch information
marmarta committed Jun 5, 2019
1 parent 06da168 commit 31214ab
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions qubesappmenus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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())

0 comments on commit 31214ab

Please sign in to comment.