From 780d4addbafe120b2072d8df9231f5292af2cf4d Mon Sep 17 00:00:00 2001 From: x4base Date: Tue, 14 Oct 2014 15:14:41 +0800 Subject: [PATCH 1/2] Add a method for generating the tabkeys for sysdb. The design of database has been changed in commit 4fa966bc0651500bec38061ce4b33e22a15407dd. This method helps update the database by altering the db and generating the tabkeys from m0,m1,m2,m3,m4 in the old schema. --- engine/tabsqlitedb.py | 73 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 65 insertions(+), 8 deletions(-) diff --git a/engine/tabsqlitedb.py b/engine/tabsqlitedb.py index 258c838e..38fecbbb 100644 --- a/engine/tabsqlitedb.py +++ b/engine/tabsqlitedb.py @@ -867,6 +867,52 @@ def generate_userdb_desc (self): import traceback traceback.print_exc () + def generate_sysdb_tabkeys(self, db): + if 'tabkeys' in self.get_columns_of_phrase_table(db): + return + + tab_dict = { + 'a':1, 'b':2, 'c':3, 'd':4, 'e':5, + 'f':6, 'g':7, 'h':8, 'i':9, 'j':10, + 'k':11, 'l':12, 'm':13, 'n':14, 'o':15, + 'p':16, 'q':17, 'r':18, 's':19, 't':20, + 'u':21, 'v':22, 'w':23, 'x':24, 'y':25, + 'z':26, "'":27, ';':28, '`':29, '~':30, + '!':31, '@':32, '#':33, '$':34, '%':35, + '^':36, '&':37, '*':38, '(':39, ')':40, + '-':41, '_':42, '=':43, '+':44, '[':45, + ']':46, '{':47, '}':48, '|':49, '/':50, + ':':51, '"':52, '<':53, '>':54, ',':55, + '.':56, '?':57, '\\':58, 'A':59, 'B':60, + 'C':61, 'D':62, 'E':63, 'F':64, 'G':65, + 'H':66, 'I':67, 'J':68, 'K':69, 'L':70, + 'M':71, 'N':72, 'O':73, 'P':74, 'Q':75, + 'R':76, 'S':77, 'T':78, 'U':79, 'V':80, + 'W':81, 'X':82, 'Y':83, 'Z':84, '0':85, + '1':86, '2':87, '3':88, '4':89, '5':90, + '6':91, '7':92, '8':93, '9':94, + # for translit + u'ä':95, + u'ö':96, + u'ü':97, + u'Ä':98, + u'Ö':99, + u'Ü':100 + } + id_tab_dict = {} + for key, id in tab_dict.items(): + id_tab_dict[id] = key + + def concat_chars(*args): + chars = [id_tab_dict.get(id, '') for id in args] + return ''.join(chars) + + db.execute('ALTER TABLE phrases ADD COLUMN tabkeys;') + db.commit() + db.create_function('concat_chars', 5, concat_chars) + db.execute('UPDATE phrases set tabkeys = concat_chars(m0,m1,m2,m3,m4);') + db.commit() + def init_user_db(self,db_file): if not path.exists(db_file): db = sqlite3.connect(db_file) @@ -894,12 +940,12 @@ def get_database_desc (self, db_file): except: return None - def get_number_of_columns_of_phrase_table(self, db_file): + def get_columns_of_phrase_table(self, db): ''' - Get the number of columns in the 'phrases' table in + Get the column names of the 'phrases' table in the database in db_file. - Determines the number of columns by parsing this: + Determined by parsing this: sqlite> select sql from sqlite_master where name='phrases'; CREATE TABLE phrases @@ -910,10 +956,7 @@ def get_number_of_columns_of_phrase_table(self, db_file): This result could be on a single line, as above, or on multiple lines. ''' - if not path.exists (db_file): - return 0 try: - db = sqlite3.connect (db_file) tp_res = db.execute( "select sql from sqlite_master where name='phrases';" ).fetchall() @@ -923,9 +966,23 @@ def get_number_of_columns_of_phrase_table(self, db_file): res = re.match(r'.*\((.*)\)', str) if res: tp = res.group(1).split(',') - return len(tp) + names = [column.strip().split(' ')[0] for column in tp] + return names else: - return 0 + return [] + except: + return [] + + def get_number_of_columns_of_phrase_table(self, db_file): + ''' + Get the number of columns in the 'phrases' table in + the database in db_file. + ''' + if not path.exists(db_file): + return 0 + try: + db = sqlite3.connect(db_file) + return len(self.get_columns_of_phrase_table(db)) except: return 0 From 1fc621946e110ac67e7cf637d78ed43fc9cf13db Mon Sep 17 00:00:00 2001 From: x4base Date: Fri, 17 Oct 2014 23:13:42 +0800 Subject: [PATCH 2/2] Add ibus-table-updatedb to help update the database --- configure.ac | 1 + engine/.gitignore | 1 + engine/Makefile.am | 4 ++- engine/ibus-table-updatedb.in | 29 +++++++++++++++ engine/tabsqlitedb.py | 12 ++++--- engine/tabupdatedb.py | 66 +++++++++++++++++++++++++++++++++++ 6 files changed, 108 insertions(+), 5 deletions(-) create mode 100644 engine/ibus-table-updatedb.in create mode 100644 engine/tabupdatedb.py diff --git a/configure.ac b/configure.ac index 75056a62..e01a3793 100644 --- a/configure.ac +++ b/configure.ac @@ -60,6 +60,7 @@ AC_CONFIG_FILES([po/Makefile.in engine/Makefile engine/ibus-engine-table engine/ibus-table-createdb + engine/ibus-table-updatedb engine/table.xml.in data/Makefile icons/Makefile diff --git a/engine/.gitignore b/engine/.gitignore index e348dd3a..3f411d40 100644 --- a/engine/.gitignore +++ b/engine/.gitignore @@ -3,4 +3,5 @@ table.xml.in ibus-engine-table ibus-table-createdb ibus-table-createdb.1 +ibus-table-updatedb *.pyc diff --git a/engine/Makefile.am b/engine/Makefile.am index 13348769..d20a8f45 100644 --- a/engine/Makefile.am +++ b/engine/Makefile.am @@ -36,7 +36,7 @@ engine_table_DATA = \ $(NULL) engine_tabledir = $(datadir)/ibus-table/engine -bin_SCRIPTS = ibus-table-createdb +bin_SCRIPTS = ibus-table-createdb ibus-table-updatedb libexec_SCRIPTS = ibus-engine-table @@ -45,6 +45,7 @@ enginedir = $(datadir)/ibus/component EXTRA_DIST = \ ibus-table-createdb.in \ + ibus-table-updatedb.in \ ibus-engine-table.in \ table.xml.in \ $(SGML) \ @@ -56,6 +57,7 @@ CLEANFILES = \ *.pyo \ ibus-engine-table \ ibus-table-createdb \ + ibus-table-updatedb \ table.xml \ $(NULL) diff --git a/engine/ibus-table-updatedb.in b/engine/ibus-table-updatedb.in new file mode 100644 index 00000000..f9c272a5 --- /dev/null +++ b/engine/ibus-table-updatedb.in @@ -0,0 +1,29 @@ +#!/bin/sh +# vim:set et sts=4 sw=4 +# +# ibus-table - The Tables engine for IBus +# +# Copyright (c) 2008-2009 Yu Yuwei +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +# +prefix=@prefix@ +exec_prefix=@exec_prefix@ +bindir=@bindir@ +datarootdir=@datarootdir@ +datadir=@datadir@ +export IBUS_TABLE_DATA_DIR=@datarootdir@ +export IBUS_TABLE_BIN_PATH=@bindir@ +exec @PYTHON@ @datarootdir@/ibus-table/engine/tabupdatedb.py $@ diff --git a/engine/tabsqlitedb.py b/engine/tabsqlitedb.py index 38fecbbb..37ea137c 100644 --- a/engine/tabsqlitedb.py +++ b/engine/tabsqlitedb.py @@ -867,8 +867,14 @@ def generate_userdb_desc (self): import traceback traceback.print_exc () - def generate_sysdb_tabkeys(self, db): - if 'tabkeys' in self.get_columns_of_phrase_table(db): + def generate_sysdb_tabkeys(self): + db = self.db + if 'tabkeys' not in self.get_columns_of_phrase_table(db): + db.execute('ALTER TABLE phrases ADD COLUMN tabkeys TEXT;') + db.commit() + + result = db.execute('SELECT count(*) FROM phrases WHERE ifnull(tabkeys, "") = ""').fetchall() + if result and result[0][0] == 0: return tab_dict = { @@ -907,8 +913,6 @@ def concat_chars(*args): chars = [id_tab_dict.get(id, '') for id in args] return ''.join(chars) - db.execute('ALTER TABLE phrases ADD COLUMN tabkeys;') - db.commit() db.create_function('concat_chars', 5, concat_chars) db.execute('UPDATE phrases set tabkeys = concat_chars(m0,m1,m2,m3,m4);') db.commit() diff --git a/engine/tabupdatedb.py b/engine/tabupdatedb.py new file mode 100644 index 00000000..2165083f --- /dev/null +++ b/engine/tabupdatedb.py @@ -0,0 +1,66 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- +# vim:et sts=4 sw=4 +# +# ibus-table - The Tables engine for IBus +# +# Copyright (c) 2008-2009 Yu Yuwei +# Copyright (c) 2009-2014 Caius "kaio" CHANCE +# Copyright (c) 2012-2014 Mike FABIAN +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +# + +import os +import sys +sys.path.append( os.path.dirname(os.path.abspath(__file__)) ) +import tabsqlitedb + +from optparse import OptionParser + +# we use OptionParser to parse the cmd arguments :) +usage = "usage: %prog table.db" +opt_parser = OptionParser(usage=usage) + +opt_parser.add_option( '-d', '--debug', + action = 'store_true', dest='debug', default = False, + help = 'Print extra debug messages.') + +opts,args = opt_parser.parse_args() + + +def main(): + def debug_print(message): + if opts.debug: + print(message) + + db_path = args[0] if len(args) > 0 else None + if not db_path: + opt_parser.print_help() + print('\nYou need to specify the database file of the IME!') + sys.exit(2) + + debug_print("Processing Database") + db = tabsqlitedb.tabsqlitedb(filename=db_path, + user_db=None, + create_database=False) + + db.generate_sysdb_tabkeys() + + debug_print('Done! :D') + + +if __name__ == "__main__": + main()