|
| 1 | +#!/usr/bin/env python3 |
| 2 | +'Command line program to exercise/test/debug the _internal command.' |
| 3 | +# Mark Blakeney, Oct 2019 |
| 4 | +import sys, importlib, argparse |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +CMD = '_internal' |
| 8 | +GESTURE = 'swipe' |
| 9 | +PROG = Path(sys.argv[0]).resolve() |
| 10 | +NAME = Path.cwd().name |
| 11 | +CACHE = Path.home() / '.cache' / PROG.name |
| 12 | + |
| 13 | +def import_path(path, add_to_path=False): |
| 14 | + 'Import given module path' |
| 15 | + modname = Path(path).stem.replace('-', '_') |
| 16 | + spec = importlib.util.spec_from_loader(modname, |
| 17 | + importlib.machinery.SourceFileLoader(modname, path)) |
| 18 | + module = importlib.util.module_from_spec(spec) |
| 19 | + spec.loader.exec_module(module) |
| 20 | + if add_to_path: |
| 21 | + sys.modules[modname] = module |
| 22 | + return module |
| 23 | + |
| 24 | +opt = argparse.ArgumentParser(description=__doc__) |
| 25 | +opt.add_argument('-c', '--conffile', |
| 26 | + help='alternative configuration file') |
| 27 | +opt.add_argument('-i', '--initial', type=int, |
| 28 | + help='initial desktop') |
| 29 | +opt.add_argument('-C', '--cols', type=int, default=1, |
| 30 | + help='number of columns') |
| 31 | +opt.add_argument('-t', '--text', action='store_true', |
| 32 | + help='output desktop change in text') |
| 33 | +opt.add_argument('-n', '--nocache', action='store_true', |
| 34 | + help='do not use cache') |
| 35 | +opt.add_argument('num', type=int, |
| 36 | + help='number of desktops') |
| 37 | +opt.add_argument('action', nargs='?', |
| 38 | + help='action to perform') |
| 39 | +opt.add_argument('-d', '--display', type=int, |
| 40 | + help=argparse.SUPPRESS) |
| 41 | +opt.add_argument('-s', '--set', type=int, help=argparse.SUPPRESS) |
| 42 | +args = opt.parse_args() |
| 43 | + |
| 44 | +def showgrid(pos, num, cols): |
| 45 | + print() |
| 46 | + for i in range(num): |
| 47 | + end = '\n' if (i % cols) == (cols - 1) else '' |
| 48 | + if i == pos: |
| 49 | + print(' {:02} '.format(i), end=end) |
| 50 | + else: |
| 51 | + print(' ** ', end=end) |
| 52 | + if end != '\n': |
| 53 | + print() |
| 54 | + |
| 55 | +if args.set is not None: |
| 56 | + print(args.set) |
| 57 | + sys.exit(0) |
| 58 | + |
| 59 | +if args.display is not None: |
| 60 | + for i in range(args.num): |
| 61 | + print('{} {} -'.format(i, '*' if i == args.display else '-')) |
| 62 | + sys.exit(0) |
| 63 | + |
| 64 | +if args.initial is None: |
| 65 | + if args.nocache: |
| 66 | + opt.error('Initial value must be specified') |
| 67 | + if not CACHE.exists(): |
| 68 | + opt.error('Need initial desktop specified') |
| 69 | + start = int(CACHE.read_text().strip()) |
| 70 | +else: |
| 71 | + start = args.initial |
| 72 | + |
| 73 | +lg = import_path(NAME) |
| 74 | +icmd = lg.internal_commands[CMD] |
| 75 | + |
| 76 | +icmd.CMDLIST = '{} -d {} {}'.format(PROG, start, args.num).split() |
| 77 | +icmd.CMDSET = '{} {} -s'.format(PROG, args.num).split() |
| 78 | + |
| 79 | +lg.read_conf(args.conffile, NAME + '.conf') |
| 80 | +motions = lg.handlers[GESTURE.upper()].motions |
| 81 | +actions = {k: v for k, v in motions.items() if isinstance(v, icmd)} |
| 82 | + |
| 83 | +if not args.action or args.action not in actions: |
| 84 | + opt.error('action must be one of {}'.format(list(actions.keys()))) |
| 85 | + |
| 86 | +cmd = motions[args.action] |
| 87 | +print('Command "{} {} is "{}"'.format(GESTURE, args.action, cmd)) |
| 88 | +res = cmd.run(block=True) |
| 89 | + |
| 90 | +if res: |
| 91 | + end = int(res.strip()) |
| 92 | + if not args.nocache: |
| 93 | + CACHE.write_text(str(end)) |
| 94 | +else: |
| 95 | + end = start |
| 96 | + |
| 97 | +if end < 0 or end >= args.num: |
| 98 | + sys.exit('Desktop change from {} to {}, out of range 0 to <{}!'.format( |
| 99 | + start, end, args.num)) |
| 100 | + |
| 101 | +if args.text: |
| 102 | + if start != end: |
| 103 | + print('Desktop change from {} to {}'.format(start, end)) |
| 104 | + else: |
| 105 | + print('No change') |
| 106 | +else: |
| 107 | + showgrid(start, args.num, args.cols) |
| 108 | + showgrid(end, args.num, args.cols) |
0 commit comments