示例#1
0
    def initChewingContext(self):
        # load libchewing context
        if not self.chewingContext:
            cfg = chewingConfig  # 所有 ChewingTextService 共享一份設定物件
            # syspath 參數可包含多個路徑,用 ; 分隔
            # 此處把 user 設定檔目錄插入到 system-wide 資料檔路徑前
            # 如此使用者變更設定後,可以比系統預設值有優先權
            search_paths = ";".join(
                (cfg.getConfigDir(), CHEWING_DATA_DIR)).encode("UTF-8")
            user_phrase = cfg.getUserPhrase().encode("UTF-8")

            # 建立 ChewingContext,此處路徑需要 UTF-8 編碼
            chewingContext = ChewingContext(syspath=search_paths,
                                            userpath=user_phrase)
            self.chewingContext = chewingContext
            chewingContext.set_maxChiSymbolLen(50)  # 編輯區長度: 50 bytes

            # 預設英數 or 中文模式
            if self.lastLangMode is not None:
                self.langMode = self.lastLangMode  # 恢復上次鍵盤暫時關閉時儲存的狀態
            else:
                self.langMode = ENGLISH_MODE if cfg.defaultEnglish else CHINESE_MODE
            chewingContext.set_ChiEngMode(self.langMode)

            # 預設全形 or 半形
            if self.lastShapeMode is not None:
                self.shapeMode = self.lastShapeMode  # 恢復上次鍵盤暫時關閉時儲存的狀態
            else:
                self.shapeMode = FULLSHAPE_MODE if cfg.defaultFullSpace else HALFSHAPE_MODE
            chewingContext.set_ShapeMode(self.shapeMode)

        self.applyConfig()  # 套用其餘的使用者設定

        if self.lastOutputSimpChinese is not None:  # 恢復上次鍵盤暫時關閉時儲存的狀態
            self.setOutputSimplifiedChinese(self.lastOutputSimpChinese)
示例#2
0
    def initChewingContext(self):
        # load libchewing context
        if not self.chewingContext:
            cfg = chewingConfig # 所有 ChewingTextService 共享一份設定物件
            # syspath 參數可包含多個路徑,用 ; 分隔
            # 此處把 user 設定檔目錄插入到 system-wide 資料檔路徑前
            # 如此使用者變更設定後,可以比系統預設值有優先權
            search_paths = ";".join((cfg.getConfigDir(), CHEWING_DATA_DIR)).encode("UTF-8")
            user_phrase = cfg.getUserPhrase().encode("UTF-8")

            # 建立 ChewingContext,此處路徑需要 UTF-8 編碼
            chewingContext = ChewingContext(syspath = search_paths, userpath = user_phrase)
            self.chewingContext = chewingContext
            chewingContext.set_maxChiSymbolLen(50) # 編輯區長度: 50 bytes

            # 預設英數 or 中文模式
            if self.lastLangMode is not None:
                self.langMode = self.lastLangMode  # 恢復上次鍵盤暫時關閉時儲存的狀態
            else:
                self.langMode = ENGLISH_MODE if cfg.defaultEnglish else CHINESE_MODE
            chewingContext.set_ChiEngMode(self.langMode)

            # 預設全形 or 半形
            if self.lastShapeMode is not None:
                self.shapeMode = self.lastShapeMode  # 恢復上次鍵盤暫時關閉時儲存的狀態
            else:
                self.shapeMode = FULLSHAPE_MODE if cfg.defaultFullSpace else HALFSHAPE_MODE
            chewingContext.set_ShapeMode(self.shapeMode)

        self.applyConfig() # 套用其餘的使用者設定

        if self.lastOutputSimpChinese is not None:  # 恢復上次鍵盤暫時關閉時儲存的狀態
            self.setOutputSimplifiedChinese(self.lastOutputSimpChinese)
config_dir = os.path.join(os.path.expandvars("%APPDATA%"), "PIME", "chewing")
localdata_dir = os.path.join(os.path.expandvars("%LOCALAPPDATA%"), "PIME",
                             "chewing")

COOKIE_ID = "chewing_config_token"

SERVER_TIMEOUT = 120

# syspath 參數可包含多個路徑,用 ; 分隔
# 此處把 user 設定檔目錄插入到 system-wide 資料檔路徑前
# 如此使用者變更設定後,可以比系統預設值有優先權
search_paths = ";".join(
    (chewingConfig.getConfigDir(), CHEWING_DATA_DIR)).encode("UTF-8")
user_phrase = chewingConfig.getUserPhrase().encode("UTF-8")
# print(search_paths, user_phrase)
chewing_ctx = ChewingContext(syspath=search_paths,
                             userpath=user_phrase)  # new libchewing context


class BaseHandler(tornado.web.RequestHandler):
    def get_current_user(self):  # override the login check
        return self.get_cookie(COOKIE_ID)

    def prepare(self):  # called before every request
        self.application.reset_timeout()  # reset the quit server timeout


class KeepAliveHandler(BaseHandler):
    @tornado.web.authenticated
    def get(self):
        # the actual keep-alive is done inside BaseHandler.prepare()
        self.write('{"return":true}')