File: generate-keycodes.py

package info (click to toggle)
python-blessed 1.25-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,812 kB
  • sloc: python: 14,645; makefile: 13; sh: 7
file content (46 lines) | stat: -rw-r--r-- 1,619 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# generate keycodes for the tables in docs/keyboard.rst
# std imports
import os

# local
from blessed.keyboard import DEFAULT_SEQUENCE_MIXIN, CURSES_KEYCODE_OVERRIDE_MIXIN


def is_override(key_attr_name, code):
    return (code in [val for name, val in CURSES_KEYCODE_OVERRIDE_MIXIN] and
            key_attr_name not in [name for name, val in CURSES_KEYCODE_OVERRIDE_MIXIN])


def main():
    from blessed import Terminal
    term = Terminal()
    csv_header = """
.. csv-table:: All Terminal class attribute Keyboard codes, by name
   :delim: |
   :header: "Name"| "Value"| "Example Sequence(s)"

"""
    fname = os.path.abspath(
        os.path.join(os.path.dirname(__file__), os.pardir, 'docs', 'all_the_keys.txt'))
    with open(fname, 'w') as fout:
        print(f"write: {fout.name}")
        fout.write(csv_header)
        for key_attr_name in sorted([
                attr for attr in dir(term) if attr.startswith('KEY_')
        ]):
            # filter away F23-F63 (lol)
            if key_attr_name.startswith('KEY_F'):
                maybe_digit = key_attr_name[len('KEY_F'):]
                if maybe_digit.isdigit() and int(maybe_digit) > 23:
                    continue
            code = getattr(term, key_attr_name)
            repr_sequences = [repr(seq) for (seq, value) in DEFAULT_SEQUENCE_MIXIN if value == code]
            txt_sequences = ', '.join(repr_sequences).replace('\\', '\\\\')
            fout.write(f'    {key_attr_name} | {code}')
            if txt_sequences:
                fout.write(f'| {txt_sequences}')
            fout.write('\n')


if __name__ == '__main__':
    main()