Skip to content

Commit

Permalink
Offer additional engines which do simple ibus-m17n emulation by default
Browse files Browse the repository at this point in the history
Resolves: #570
([ENHANCEMENT] Offer additional engines which do simple ibus-m17n emulation by default)

Resolves: #569
([BUG] setup tool may crash when changing the sound file)
  • Loading branch information
mike-fabian committed Dec 16, 2024
1 parent 28ca6f5 commit 3a27622
Show file tree
Hide file tree
Showing 11 changed files with 1,184 additions and 596 deletions.
26 changes: 20 additions & 6 deletions engine/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
# pylint: enable=wrong-import-position
import hunspell_table
import tabsqlitedb
import itb_util

LOGGER = logging.getLogger('ibus-typing-booster')

Expand Down Expand Up @@ -67,24 +68,37 @@ def do_create_engine( # pylint: disable=arguments-differ
LOGGER.debug(
'EngineFactory.do_create_engine(engine_name=%s)\n',
engine_name)
engine_base_path = "/com/redhat/IBus/engines/table/%s/engine/"
engine_path = engine_base_path % re.sub(
r'[^a-zA-Z0-9_/]', '_', engine_name)
engine_path = ('/com/redhat/IBus/engines/typing_booster/'
f"{re.sub(r'[^a-zA-Z0-9_/]', '_', engine_name)}"
'/engine/')
try:
if engine_name in self.database_dict:
self.database = self.database_dict[engine_name]
else:
self.database = tabsqlitedb.TabSqliteDb()
user_db_file = 'user.db'
if engine_name != 'typing-booster':
match = itb_util.M17N_ENGINE_NAME_PATTERN.search(
engine_name)
if not match:
raise ValueError('Invalid engine name.')
m17n_ime_lang = match.group('lang')
m17n_ime_name = match.group('name')
user_db_file = f'user-{m17n_ime_lang}-{m17n_ime_name}.db'
self.database = tabsqlitedb.TabSqliteDb(
user_db_file=user_db_file)
self.database_dict[engine_name] = self.database
if engine_name in self.enginedict:
engine = self.enginedict[engine_name]
else:
engine = hunspell_table.TypingBoosterEngine(
self.bus, engine_path + str(self.engine_id), self.database)
self.bus,
engine_path + str(self.engine_id),
self.database,
engine_name=engine_name)
self.enginedict[engine_name] = engine
self.engine_id += 1
return engine
except Exception as error:
except Exception as error: # pylint: disable=broad-except
LOGGER.exception(
'Failed to create engine %s: %s: %s',
engine_name, error.__class__.__name__, error)
Expand Down
406 changes: 260 additions & 146 deletions engine/hunspell_table.py

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions engine/itb_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@
_: Callable[[str], str] = lambda a: gettext.dgettext(DOMAINNAME, a)
N_: Callable[[str], str] = lambda a: a

M17N_ENGINE_NAME_PATTERN = re.compile(
r'^tb:(?P<lang>[a-z]{1,3}):(?P<name>[^\s:]+)$')

# When matching keybindings, only the bits in the following mask are
# considered for key.state:
KEYBINDING_STATE_MASK = (
Expand Down Expand Up @@ -3351,6 +3354,8 @@ def variant_to_value(variant: GLib.Variant) -> Any:
'''
Convert a GLib variant to a value
'''
if variant is None:
return None
if not isinstance(variant, GLib.Variant):
LOGGER.info('not a GLib.Variant')
return variant
Expand Down
94 changes: 71 additions & 23 deletions engine/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from gi.repository import GLib
# pylint: enable=wrong-import-position

import itb_util
import itb_version

LOGGER = logging.getLogger('ibus-typing-booster')
Expand Down Expand Up @@ -250,46 +251,93 @@ def write_xml() -> None:
'''
Writes the XML to describe the engine(s) to standard output.
'''
m17n_db_info = itb_util.M17nDbInfo()
m17n_input_methods = set(m17n_db_info.get_imes())
m17n_input_methods_skip = set([
'NoIME',
])
# Input methods offering multiple candidates are not yet supported
# by typing-booster:
m17n_input_methods_multiple_candidates = set([
'ja-anthy',
't-lsymbol',
'ml-swanalekha',
'vi-han',
'vi-nomtelex',
'vi-nomvni',
'vi-tcvn',
'vi-telex',
'vi-viqr',
'vi-vni',
'zh-cangjie',
'zh-pinyin-vi',
'zh-pinyin',
'zh-py',
'zh-py-b5',
'zh-py-gb',
'zh-quick',
'zh-tonepy',
'zh-tonepy-gb',
'zh-tonepy-b5',
'zh-zhuyin',
])
supported_input_methods = sorted(
(m17n_input_methods - m17n_input_methods_skip)
- m17n_input_methods_multiple_candidates
) + ['typing-booster']
# supported_input_methods = ['typing-booster']
egs = Element('engines') # pylint: disable=possibly-used-before-assignment

for language in ('t',):
for ime in supported_input_methods:
_engine = SubElement( # pylint: disable=possibly-used-before-assignment
egs, 'engine')

_name = SubElement(_engine, 'name')
_name.text = 'typing-booster'
name = 'typing-booster'
longname = 'Typing Booster'
language = 't'
license = 'GPL'
author = ('Mike FABIAN <[email protected]>'
', Anish Patil <[email protected]>')
layout = 'default'
icon = os.path.join(ICON_DIR, 'ibus-typing-booster.svg')
description = 'A completion input method to speedup typing.'
symbol = '🚀'
setup = SETUP_TOOL
icon_prop_key = 'InputMode'
if ime != 'typing-booster':
m17n_lang, m17n_name = ime.split('-', maxsplit=1)
name = f'tb:{m17n_lang}:{m17n_name}'
longname = f'{ime} (Typing Booster)'
language = m17n_lang
icon = m17n_db_info.get_icon(ime)
description = m17n_db_info.get_description(ime)
symbol = m17n_lang
if symbol == 't':
symbol = '⌨️'
setup = SETUP_TOOL + f' --engine-name {name}'

_name = SubElement(_engine, 'name')
_name.text = name
_longname = SubElement(_engine, 'longname')
_longname.text = 'Typing Booster'

_longname.text = longname
_language = SubElement(_engine, 'language')
_language.text = language

_license = SubElement(_engine, 'license')
_license.text = 'GPL'

_license.text = license
_author = SubElement(_engine, 'author')
_author.text = (
'Mike FABIAN <[email protected]>'
+ ', Anish Patil <[email protected]>')

_author.text = author
_icon = SubElement(_engine, 'icon')
_icon.text = os.path.join(ICON_DIR, 'ibus-typing-booster.svg')

_icon.text = icon
_layout = SubElement(_engine, 'layout')
_layout.text = 'default'

_layout.text = layout
_desc = SubElement(_engine, 'description')
_desc.text = 'A completion input method to speedup typing.'

_desc.text = description
_symbol = SubElement(_engine, 'symbol')
_symbol.text = '🚀'

_symbol.text = symbol
_setup = SubElement(_engine, 'setup')
_setup.text = SETUP_TOOL

_setup.text = setup
_icon_prop_key = SubElement(_engine, 'icon_prop_key')
_icon_prop_key.text = 'InputMode'
_icon_prop_key.text = icon_prop_key

# now format the xmlout pretty
indent(egs)
Expand Down
25 changes: 7 additions & 18 deletions engine/tabsqlitedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,11 @@ class TabSqliteDb:
“id”, “input_phrase”, “phrase”, “p_phrase”, “pp_phrase”, “user_freq”, “timestamp”
There are 2 databases, sysdb, userdb.
sysdb: “Database” with the suggestions from the hunspell dictionaries
user_freq = 0 always.
Actually there is no Sqlite3 database called “sysdb”, these
are the suggestions coming from hunspell_suggest, i.e. from
grepping the hunspell dictionaries and from pyhunspell.
(Historic note: ibus-typing-booster started as a fork of
ibus-table, in ibus-table “sysdb” is a Sqlite3 database
which is installed systemwide and readonly for the user)
user_db: Database on disk where the phrases learned from the user are stored
user_freq >= 1: The number of times the user has used this phrase
It is a database where the phrases learned from the user are stored.
user_freq >= 1: The number of times the user has used this phrase
'''
# pylint: enable=line-too-long
def __init__(self, user_db_file: str = '') -> None:
def __init__(self, user_db_file: str = 'user.db') -> None:
global DEBUG_LEVEL # pylint: disable=global-statement
try:
DEBUG_LEVEL = int(str(os.getenv('IBUS_TYPING_BOOSTER_DEBUG_LEVEL')))
Expand All @@ -75,9 +63,10 @@ def __init__(self, user_db_file: str = '') -> None:
LOGGER.debug(
'TabSqliteDb.__init__(user_db_file = %s)', user_db_file)
self.user_db_file = user_db_file
if not self.user_db_file and os.getenv('HOME'):
self.user_db_file = os.path.join(
str(os.getenv('HOME')), '.local/share/ibus-typing-booster/user.db')
if (self.user_db_file != ':memory:'
and os.path.basename(self.user_db_file) == self.user_db_file):
self.user_db_file = os.path.join(os.path.expanduser(
'~/.local/share/ibus-typing-booster'), self.user_db_file)
if not self.user_db_file:
LOGGER.debug('Falling back to ":memory:" for user.db')
self.user_db_file = ':memory:'
Expand Down
3 changes: 1 addition & 2 deletions org.freedesktop.ibus.engine.typing-booster.gschema.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<schemalist>
<schema path="/org/freedesktop/ibus/engine/typing-booster/"
id="org.freedesktop.ibus.engine.typing-booster">
<schema id="org.freedesktop.ibus.engine.typing-booster">
<key name="colorcomposepreview" type="b">
<default>true</default>
<summary>Use color for compose preview</summary>
Expand Down
Loading

0 comments on commit 3a27622

Please sign in to comment.