- args = ['setxkbmap', '-print']
- if r is not None:
- args.append('-rules')
- args.append('{}'.format(r))
- if m is not None:
- args.append('-model')
- args.append('{}'.format(m))
- if l is not None:
- args.append('-layout')
- args.append('{}'.format(l))
- if v is not None:
- args.append('-variant')
- args.append('{}'.format(v))
- if o is not None:
- args.append('-option')
- args.append('{}'.format(o))
-
- success = True
- out = io.StringIO()
- if verbose:
- print(':: {}'.format(' '.join(args)), file=out)
-
- try:
- xkbcomp_args = ['xkbcomp', '-xkb', '-', '-']
-
- setxkbmap = subprocess.Popen(args, stdout=subprocess.PIPE)
- xkbcomp = subprocess.Popen(xkbcomp_args, stdin=setxkbmap.stdout,
- stdout=subprocess.PIPE, stderr=subprocess.PIPE,
- universal_newlines=True)
- setxkbmap.stdout.close()
- stdout, stderr = xkbcomp.communicate()
- if xkbcomp.returncode != 0:
- print('ERROR: Failed to compile: {}'.format(' '.join(args)), file=out)
- success = False
- if xkbcomp.returncode != 0 or verbose:
- print(stdout, file=out)
- print(stderr, file=out)
-
- # This catches setxkbmap errors.
- except subprocess.CalledProcessError as err:
- print('ERROR: Failed to compile: {}'.format(' '.join(args)), file=out)
- print(err.output, file=out)
- success = False
-
- return success, out.getvalue()