예제 #1
0
파일: mdict_query.py 프로젝트: z1lc/anki
    def _make_mdx_index(self, db_name):
        mdx = MDX(self._mdx_file)
        self._mdx_db = db_name
        index_list = (mdx.get_index())['index_dict_list']
        conn = sqlite3.connect(db_name)
        c = conn.cursor()
        c.execute(''' CREATE TABLE MDX_INDEX
               (key_text text,
                file_pos integer,
                compressed_size integer,
                record_block_type integer,
                record_start integer,
                record_end integer,
                offset integer
                )''')

        tuple_list = []
        for item in index_list:
            tuple = (item['key_text'], item['file_pos'],
                     item['compressed_size'], item['record_block_type'],
                     item['record_start'], item['record_end'], item['offset'])
            tuple_list.append(tuple)
        c.executemany('INSERT INTO MDX_INDEX VALUES (?,?,?,?,?,?,?)',
                      tuple_list)
        # build the metadata table
        meta = (mdx.get_index())['meta']
        c.execute('''CREATE TABLE META
               (key text,
                value text
                )''')

        #for k,v in meta:
        #    c.execute(
        #    'INSERT INTO META VALUES (?,?)',
        #    (k, v)
        #    )

        c.executemany('INSERT INTO META VALUES (?,?)',
                      [('encoding', meta['encoding']),
                       ('stylesheet', meta['stylesheet']),
                       ('title', meta['title']),
                       ('description', meta['description'])])

        conn.commit()
        conn.close()
        #set class member
        self._encoding = meta['encoding']
        self._stylesheet = json.loads(meta['stylesheet'])
        self._title = meta['title']
        self._description = meta['description']
예제 #2
0
    def _make_mdx_index(self, db_name):
        if os.path.exists(db_name):
            os.remove(db_name)
        mdx = MDX(self._mdx_file)
        self._mdx_db = db_name
        returned_index = mdx.get_index(check_block=self._check)
        index_list = returned_index['index_dict_list']
        conn = sqlite3.connect(db_name)
        c = conn.cursor()
        c.execute(''' CREATE TABLE MDX_INDEX
               (key_text text not null,
                file_pos integer,
                compressed_size integer,
                decompressed_size integer,
                record_block_type integer,
                record_start integer,
                record_end integer,
                offset integer
                )''')

        tuple_list = [
            (item['key_text'], item['file_pos'], item['compressed_size'],
             item['decompressed_size'], item['record_block_type'],
             item['record_start'], item['record_end'], item['offset'])
            for item in index_list
        ]
        c.executemany('INSERT INTO MDX_INDEX VALUES (?,?,?,?,?,?,?,?)',
                      tuple_list)
        # build the metadata table
        meta = returned_index['meta']
        c.execute('''CREATE TABLE META
               (key text,
                value text
                )''')

        #for k,v in meta:
        #    c.execute(
        #    'INSERT INTO META VALUES (?,?)',
        #    (k, v)
        #    )

        c.executemany('INSERT INTO META VALUES (?,?)',
                      [('encoding', meta['encoding']),
                       ('stylesheet', meta['stylesheet']),
                       ('title', meta['title']),
                       ('description', meta['description']),
                       ('version', version)])

        if self._sql_index:
            c.execute('''
                CREATE INDEX key_index ON MDX_INDEX (key_text)
                ''')

        conn.commit()
        conn.close()
        #set class member
        self._encoding = meta['encoding']
        self._stylesheet = json.loads(meta['stylesheet'])
        self._title = meta['title']
        self._description = meta['description']
예제 #3
0
    def _make_mdx_index(self):
        if os.path.exists(self._mdx_db):
            os.remove(self._mdx_db)
        mdx = MDX(self._mdx_file, only_header=False)
        index_list = mdx.get_index(check_block=self._check)
        conn = sqlite3.connect(self._mdx_db)
        c = conn.cursor()
        c.execute(''' CREATE TABLE MDX_INDEX
               (key_text text not null,
                file_pos integer,
                compressed_size integer,
                decompressed_size integer,
                record_block_type integer,
                record_start integer,
                record_end integer,
                offset integer
                )''')

        tuple_list = [
            (item['key_text'], item['file_pos'], item['compressed_size'],
             item['decompressed_size'], item['record_block_type'],
             item['record_start'], item['record_end'], item['offset'])
            for item in index_list
        ]
        c.executemany('INSERT INTO MDX_INDEX VALUES (?,?,?,?,?,?,?,?)',
                      tuple_list)
        # build the metadata table
        c.execute('''CREATE TABLE META
               (key text,
                value text
                )''')
        c.executemany('INSERT INTO META VALUES (?,?)',
                      [('encoding', self.meta['encoding']),
                       ('stylesheet', json.dumps(self.meta['stylesheet'])),
                       ('title', self.meta['title']),
                       ('description', self.meta['description']),
                       ('version', version)])

        if self._sql_index:
            c.execute('''
                CREATE INDEX key_index ON MDX_INDEX (key_text)
                ''')

        conn.commit()
        conn.close()
예제 #4
0
    def make_sqlite(self):
        sqlite_file = self._mdx_file + '.sqlite.db'
        if os.path.exists(sqlite_file):
            os.remove(sqlite_file)
        mdx = MDX(self._mdx_file)
        conn = sqlite3.connect(sqlite_file)
        cursor = conn.cursor()
        cursor.execute(''' CREATE TABLE MDX_DICT
                (key text not null,
                value text
                )''')

        # remove '(pīnyīn)', remove `1`:
        aeiou = 'āáǎàĀÁǍÀēéěèêềếĒÉĚÈÊỀẾīíǐìÍǏÌōóǒòŌÓǑÒūúǔùŪÚǓÙǖǘǚǜǕǗǙǛḾǹňŃŇ'
        pattern = r"`\d+`|[(\(]?['a-z%s]*[%s]['a-z%s]*[\))]?" % (aeiou, aeiou,
                                                                 aeiou)
        tuple_list = [(key.decode(), re.sub(pattern, '', value.decode()))
                      for key, value in mdx.items()]

        cursor.executemany('INSERT INTO MDX_DICT VALUES (?,?)', tuple_list)

        returned_index = mdx.get_index(check_block=self._check)
        meta = returned_index['meta']
        cursor.execute('''CREATE TABLE META (key text, value text)''')

        cursor.executemany('INSERT INTO META VALUES (?,?)',
                           [('encoding', meta['encoding']),
                            ('stylesheet', meta['stylesheet']),
                            ('title', meta['title']),
                            ('description', meta['description']),
                            ('version', version)])

        if self._sql_index:
            cursor.execute('''
                CREATE INDEX key_index ON MDX_DICT (key)
                ''')
        conn.commit()
        conn.close()