Skip to content

Commit

Permalink
Add configuration file support to the chewing module.
Browse files Browse the repository at this point in the history
Enable user specific phrase database of libchewing.
  • Loading branch information
PCMan committed Feb 6, 2016
1 parent 291f863 commit ec0a653
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 14 deletions.
66 changes: 66 additions & 0 deletions server/input_methods/chewing/chewing_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#! python3
import json
import os

DEF_FONT_SIZE = 16

selKeys=(
"1234567890",
"asdfghjkl;",
"asdfzxcv89",
"asdfjkl789",
"aoeuhtn789",
"1234qweras"
)

class ChewingConfig:

def __init__(self):
self.keyboardLayout = 0
self.candPerRow = 3
self.defaultEnglish = False
self.defaultFullSpace = False
self.showCandWithSpaceKey = False
self.switchLangWithShift = True
self.outputSimpChinese = False
self.addPhraseForward = True
self.colorCandWnd = True
self.advanceAfterSelection = True
self.fontSize = DEF_FONT_SIZE
self.selKeyType = 0
self.candPerPage = 9
self.cursorCandList = 1
self.enableCapsLock = 1
self.fullShapeSymbols = 1
self.phraseMark = 1
self.escCleanAllBuf = 0
self.easySymbolsWithShift = 1
self.easySymbolsWithCtrl = 0
self.upperCaseWithShift = 0

self.load() # try to load from the config file

def getConfigDir(self):
config_dir = os.path.join(os.path.expanduser("~"), "PIME", "chewing")
os.makedirs(config_dir, mode=0o700, exist_ok=True)
return config_dir

def getConfigFile(self):
return os.path.join(self.getConfigDir(), "config.json")

def getSelKeys(self):
return selKeys[self.selKeyType]

def load(self):
try:
with open(self.getConfigFile(), "r") as f:
self.__dict__.update(json.load(f))
except Exception:
pass # FIXME: handle I/O errors?

def save(self):
try:
with open(self.getConfigFile(), "w") as f:
js = json.dump(self.__dict__, f, indent=4)
except Exception:
pass # FIXME: handle I/O errors?
42 changes: 28 additions & 14 deletions server/input_methods/chewing/chewing_ime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
from keycodes import * # for VK_XXX constants
from textService import *
import os.path
import string
from . import libchewing
from .libchewing import ChewingContext
from .chewing_config import ChewingConfig

# from libchewing/include/global.h
CHINESE_MODE = 1
ENGLISH_MODE = 0
FULLSHAPE_MODE = 1
HALFSHAPE_MODE = 0

# from libchewing/include/internal/userphrase-private.h
DB_NAME = "chewing.sqlite3"

keyNames = {
VK_ESCAPE: "Esc",
VK_RETURN: "Enter",
Expand Down Expand Up @@ -40,41 +43,52 @@ def __init__(self, client):
self.outputSimpChinese_ = False
self.lastKeyDownCode_ = 0

# load configurations from a user-specific config file
# FIXME: should we share this among all ChewingTextService instances?
# FIXME: how to reload the configurations properly when they are changed?
self.config = ChewingConfig()

def onActivate(self):
cfg = self.config
TextService.onActivate(self)
# load libchewing context
datadir = self.datadir.encode("UTF-8")
ctx = libchewing.ChewingContext(syspath = datadir, userpath = None)
user_phrase = os.path.join(cfg.getConfigDir(), DB_NAME).encode("UTF-8")
ctx = ChewingContext(syspath = datadir, userpath = user_phrase)
self.ctx = ctx

ctx.set_maxChiSymbolLen(50)

# add user phrase before or after the cursor
# cxt.set_addPhraseDirection(cfg.addPhraseForward);
ctx.set_addPhraseDirection(cfg.addPhraseForward);

# automatically shift cursor to the next char after choosing a candidate
# ctx.set_autoShiftCur(cfg.advanceAfterSelection);
ctx.set_autoShiftCur(cfg.advanceAfterSelection);

# candiate strings per page
# ctx.set_candPerPage(cfg.candPerPage);
ctx.set_candPerPage(cfg.candPerPage);

# clean the composition buffer by Esc key
# ctx.set_escCleanAllBuf(cfg.escCleanAllBuf);
ctx.set_escCleanAllBuf(cfg.escCleanAllBuf);

# keyboard type
# ctx.set_KBType(cfg.keyboardLayout);
ctx.set_KBType(cfg.keyboardLayout);

# Use space key to open candidate window.
# ctx.set_spaceAsSelection(cfg.showCandWithSpaceKey);
ctx.set_spaceAsSelection(cfg.showCandWithSpaceKey);

self.customizeUI(candFontSize = cfg.fontSize, candPerRow = cfg.candPerRow)
self.setSelKeys(cfg.getSelKeys())

self.customizeUI(candFontSize = 20, candPerRow = 1)
self.selKeys = "1234567890"
ctx.set_selKey(self.selKeys)
self.setSelKeys(self.selKeys)

self.langMode_ = CHINESE_MODE
ctx.set_ChiEngMode(CHINESE_MODE)

def setSelKeys(self, selKeys):
TextService.setSelKeys(self, selKeys)
self.selKeys = selKeys
if self.ctx:
self.ctx.set_selKey(selKeys)

def onDeactivate(self):
TextService.onDeactivate(self)
# unload libchewing context
Expand Down

0 comments on commit ec0a653

Please sign in to comment.