9a09343
from ctypes import *
9a09343
from functools import partial
f04c780
import sys
f04c780
f04c780
_libchewing = None
f04c780
if sys.platform == "win32": # Windows
f04c780
    import os.path
f04c780
    # find in current dir first
f04c780
    dll_path = os.path.join(os.path.dirname(__file__), "chewing.dll")
f04c780
    if not os.path.exists(dll_path):
f04c780
        dll_path = "chewing.dll" # search in system path
f04c780
    _libchewing = CDLL(dll_path)
f04c780
else: # UNIX-like systems
f04c780
    _libchewing = CDLL('libchewing.so.3')
9a09343
9a09343
_libchewing.chewing_commit_String.restype = c_char_p
9a09343
_libchewing.chewing_buffer_String.restype = c_char_p
9a09343
_libchewing.chewing_cand_String.restype = c_char_p
9a09343
_libchewing.chewing_zuin_String.restype = c_char_p
9a09343
_libchewing.chewing_aux_String.restype = c_char_p
f04c780
_libchewing.chewing_get_KBString.restype = c_char_p
f04c780
9a09343
9a09343
def Init(datadir, userdir):
9a09343
    return _libchewing.chewing_Init(datadir, userdir)
9a09343
f04c780
9a09343
class ChewingContext:
f04c780
    def __init__(self, **kwargs):
f04c780
        if not kwargs:
f04c780
            self.ctx = _libchewing.chewing_new()
f04c780
        else:
f04c780
            syspath = kwargs.get("syspath", None)
f04c780
            userpath = kwargs.get("userpath", None)
f04c780
            self.ctx = _libchewing.chewing_new2(
f04c780
                syspath,
f04c780
                userpath,
f04c780
                None,
f04c780
                None)
f04c780
9a09343
    def __del__(self):
f04c780
        _libchewing.chewing_delete(self.ctx)
f04c780
9a09343
    def __getattr__(self, name):
9a09343
        func = 'chewing_' + name
f04c780
        if hasattr(_libchewing, func):
f04c780
            wrap = partial(getattr(_libchewing, func), self.ctx)
9a09343
            setattr(self, name, wrap)
9a09343
            return wrap
9a09343
        else:
f04c780
            raise AttributeError(name)
f04c780
9a09343
    def Configure(self, cpp, maxlen, direction, space, kbtype):
9a09343
        self.set_candPerPage(cpp)
9a09343
        self.set_maxChiSymbolLen(maxlen)
9a09343
        self.set_addPhraseDirection(direction)
9a09343
        self.set_spaceAsSelection(space)
9a09343
        self.set_KBType(kbtype)