Advertisement
Guest User

Untitled

a guest
May 19th, 2012
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.57 KB | None | 0 0
  1. #!/usr/bin/python
  2. #
  3. # This script does 2 things:
  4. # When given the "show" command, it will output the current keyboard layout.
  5. # When given the "cycle" command, it will cycle the keyboard from the AVAILABLE_LAYOUTS
  6. # list.
  7.  
  8. import subprocess
  9. import re
  10.  
  11. AVAILABLE_LAYOUTS = ['us', 'ca(multix)']
  12. KBRE = re.compile('^layout: *[()\w]*$', flags=re.MULTILINE)
  13.  
  14. def check_output(command):
  15.     process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
  16.     output = process.communicate()
  17.     retcode = process.poll()
  18.     if retcode:
  19.             raise subprocess.CalledProcessError(retcode, command, output=output[0])
  20.     return output
  21.  
  22.  
  23. def get_current_layout():
  24.     r1, _ = check_output('setxkbmap -query')
  25.     return KBRE.findall(r1)[0].split(' ')[-1:][0]
  26.  
  27. def cycle_layout():
  28.     next_index = AVAILABLE_LAYOUTS.index(get_current_layout())+1
  29.     if len(AVAILABLE_LAYOUTS) - 1 < next_index:
  30.         next_index = 0
  31.     check_output('setxkbmap \'%s\'' % AVAILABLE_LAYOUTS[next_index])
  32.  
  33. import argparse
  34. if __name__ == '__main__':
  35.     parser = argparse.ArgumentParser(description='Prints out current keyboard layout, or cycles through available layouts.')
  36.     parser.add_argument('command', choices=['cycle', 'show'], help='cycle: Cycles through available layouts, show: Shows current layout')
  37.     args = parser.parse_args()
  38.     if args:
  39.         if args.command == 'cycle':
  40.             cycle_layout()
  41.             print get_current_layout()
  42.         if args.command == 'show':
  43.             print get_current_layout()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement