add langset from file

This commit is contained in:
pegasko 2024-05-19 22:09:04 +03:00
parent 187e8e679a
commit 50c536ebf1
3 changed files with 46 additions and 9 deletions

View file

@ -16,6 +16,21 @@ Sample usage for KDE:
* Fill something like `fleepit --mode x11 --langset qwerty-en-ru` * Fill something like `fleepit --mode x11 --langset qwerty-en-ru`
* Set some shortcut, for example `CTRL+` ` * Set some shortcut, for example `CTRL+` `
# Langset file
Sample langset file:
```json
{
"langset": [
"`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,.~!#%&*()_+QWERTYUIOP{}|ASDFGHJKL:\"ZXCVBNM<>",
"ё1234567890-=йцукенгшщзхъ\\фывапролджэячсмитьбюЁ!№%?*()_+ЙЦУКЕНГШЩЗХЪ/ФЫВАПРОЛДЖЭЯЧСМИТЬБЮ"
]
}
```
# Support # Support
Current list of language convertations: Current list of language convertations:

View file

@ -17,20 +17,30 @@
import subprocess import subprocess
import argparse import argparse
import typing import typing
import json
# Built-in langsets
LANGSETS = { LANGSETS = {
'qwerty-en-ru': ( 'qwerty-en-ru': (
r"""`1234567890-=qwertyuiop[]\asdfghjkl;'zxcvbnm,.~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:"ZXCVBNM<>""", r"""`1234567890-=qwertyuiop[]\asdfghjkl;'zxcvbnm,.~!#%&*()_+QWERTYUIOP{}|ASDFGHJKL:"ZXCVBNM<>""",
r"""ё1234567890-=йцукенгшщзхъ\фывапролджэячсмитьбюЁ!"№;%:?*()_+ЙЦУКЕНГШЩЗХЪ/ФЫВАПРОЛДЖЭЯЧСМИТЬБЮ""", r"""ё1234567890-=йцукенгшщзхъ\фывапролджэячсмитьбюЁ!%?*()_+ЙЦУКЕНГШЩЗХЪ/ФЫВАПРОЛДЖЭЯЧСМИТЬБЮ""",
), ),
} }
def make_langset_dict(langset: str) -> typing.Dict[str, str]: def make_langset_dict(langset: typing.Tuple[str, str]) -> typing.Dict[str, str]:
ls = LANGSETS[langset]
flipper = {} flipper = {}
for (a, b) in zip(*ls):
if len(langset) != 2 or len(langset[0]) != len(langset[1]):
raise ValueError("langset alternatives not matching")
for (a, b) in zip(*langset):
if a in flipper:
raise ValueError(f'alternative for "{ a }" already exists')
if b in flipper:
raise ValueError(f'alternative for "{ b }" already exists')
flipper[a] = b flipper[a] = b
flipper[b] = a flipper[b] = a
return flipper return flipper
@ -58,7 +68,11 @@ def do_x11(args: argparse.Namespace):
result.check_returncode() result.check_returncode()
# Do convert # Do convert
conv = make_langset_dict(args.langset) if args.langset:
conv = make_langset_dict(LANGSETS[args.langset])
else:
with open(args.langset_file, 'r') as f:
conv = make_langset_dict(json.load(f)['langset'])
converted = flip_langset(clip, conv) converted = flip_langset(clip, conv)
if args.verbose: if args.verbose:
@ -82,17 +96,25 @@ def main():
'x11', 'x11',
'wayland', 'wayland',
], ],
help='compositor compatibility mode',
required=True, required=True,
) )
parser.add_argument( parser.add_argument(
'-v', '--verbose', '-v', '--verbose',
help='verbose logging ',
action='store_true', action='store_true',
) )
parser.add_argument( langset_group = parser.add_mutually_exclusive_group(required=True)
langset_group.add_argument(
'-l', '--langset', '-l', '--langset',
help='use one of embedded langsets',
type=str, type=str,
choices=list(LANGSETS.keys()), choices=list(LANGSETS.keys()),
required=True, )
langset_group.add_argument(
'-f', '--langset-file',
help='use langset from json file containing',
type=str,
) )
args = parser.parse_args() args = parser.parse_args()

View file

@ -11,7 +11,7 @@ except:
setup( setup(
name='fleepit', name='fleepit',
version='4', version='5',
py_modules=['fleepit'], py_modules=['fleepit'],
description='Text seleciton keyboard layout switcher based on xclip / etc for X11 & wayland', description='Text seleciton keyboard layout switcher based on xclip / etc for X11 & wayland',
url='https://git.pegasko.art/bitrate16/fleepit', url='https://git.pegasko.art/bitrate16/fleepit',