2 ########################################################################
4 ## Copyright (C) 2009 MiM
6 ## Contact: Handspring <xhealer@gmail.com>
8 ## AUTHOR: Alsor Zhou <alsor.zhou@gmail.com>
10 ## This file is part of MiM Pinyin.
12 ## This is free software: you can redistribute it and/or modify
13 ## it under the terms of the GNU General Public License as published by
14 ## the Free Software Foundation, either version 3 of the License, or
15 ## (at your option) any later version.
17 ## This is distributed in the hope that it will be useful,
18 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ## GNU General Public License for more details.
22 ## You should have received a copy of the GNU General Public License
23 ## along with Sigil. If not, see <http://www.gnu.org/licenses/>.
25 ########################################################################
34 # Global ERROR DEFINATION
39 '''Print wrapper with debug function supported
41 Never use this function in production (always output) code '''
47 Copyright (C) 2009 MiM
49 Contact: Handspring <xhealer@gmail.com>
53 This file is part of MiM Pinyin.
55 This is free software: you can redistribute it and/or modify
56 it under the terms of the GNU General Public License as published by
57 the Free Software Foundation, either version 3 of the License, or
58 (at your option) any later version.
60 This is distributed in the hope that it will be useful,
61 but WITHOUT ANY WARRANTY; without even the implied warranty of
62 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
63 GNU General Public License for more details.
65 You should have received a copy of the GNU General Public License
66 along with Sigil. If not, see <http://www.gnu.org/licenses/>.
71 '''converter.py [options] SRC [options...DEST]
72 -s SRC : specify dictionary source
73 -t DEST : save converted binary map into DEST
74 -c SRC : syntax check SRC, without converstion
75 -d DEST : generate dummy dictionary bin map
78 --source SRC : same with '-s SRC'
79 --target DEST: same with '-t DEST'
80 --check SRC : same with '-c SRC'
81 --dummy DEST: same with '-d DEST'
86 '''MiM pinyin dictionary converter version 0.0.1 Handspring <xhealer@gmail.com>'''
89 # Target file segmentation layout
91 tgt_file_start_index = 0
92 tgt_header_start_offset = 0
95 tgt_table_a_offset = 300
96 tgt_table_b_offset = 400
98 tgt_global_position_ind = 0
100 # Example header fileds:
101 # Fn:dictionary.bin\n
104 # ActiveChunkTableFlag:A\n
105 # ChunkTableAOffset:300
106 # ChunkTableBOffset:400
109 tgt_header_delemitor_str = ":"
110 tgt_header_fn_str = "Fn"
111 tgt_header_version_str = "Ver"
112 tgt_header_author_str = "Authors"
113 tgt_header_actf_str = "ActiveChunkTableFlag"
114 tgt_header_ctao_str = "ChunkTableAOffset"
115 tgt_header_ctbo_str = "ChunkTableBOffset"
116 tgt_header_chunk_size_str= "ChunkSize"
117 tgt_header_crc_str = "CRC32"
120 tgt_ctable_flag_offset = 0
121 tgt_ctable_flag_fld_siz = 2 # bytes
122 tgt_ctable_chk_base_offset = 2
123 tgt_ctable_chk_base_fld_size = 2 # bytes, 65535 maximize
124 tgt_ctable_chk_acroyn_fld_size = 2
125 tgt_ctable_chk_offset_fld_size = 2
126 tgt_ctable_chk_size_fld_size = 2
128 # Internal function definition
129 def generate_header(fn, ver, authors, actf, ctao, ctbo, csize):
130 '''Generate target file header.
132 @param ver: dictionary version
133 @param authors: dictionary authors
134 @param actf: active chunk table flag (A/B)
135 @param ctao: chunk table A offset
136 @param ctbo: chunk table B offset
137 @param csize: chunk size (fixed)
139 @return header: header string with crc32 computed
143 header += tgt_header_fn_str + tgt_header_delemitor_str + fn + tgt_delimitor
144 header += tgt_header_version_str + tgt_header_delemitor_str + ver + tgt_delimitor
145 header += tgt_header_version_str + tgt_header_delemitor_str + authors + tgt_delimitor
146 crc32 = crc32(header); # FIXME: should we crc the timestamp?
147 header += tgt_header_version_str + tgt_header_delemitor_str + actf + tgt_delimitor
148 header += tgt_header_version_str + tgt_header_delemitor_str + ctao + tgt_delimitor
149 header += tgt_header_version_str + tgt_header_delemitor_str + ctbo + tgt_delimitor
150 header += tgt_header_version_str + tgt_header_delemitor_str + csize + tgt_delimitor
151 header += tgt_header_version_str + tgt_header_delemitor_str + crc32
153 PRINT(generate_header.__doc__)
157 '''Generate static table 1.
159 PRINT(generate_st1.__doc__)
162 '''Generate static table 2.
164 PRINT(generate_st2.__doc__)
166 def generate_ctable_a():
167 '''Chunk Table A generation.
169 0------------2------------4--------6--------8------10-------12-------14----16
170 | Table flag | Chunk Base | Acroyn | Offset | Size | Acroyn | Offset | Size |
172 PRINT(generate_ctable_a.__doc__)
174 def generate_ctable_b():
175 '''Chunk Table B generation.
177 0------------2------------4--------6--------8------10-------12-------14----16
178 | Table flag | Chunk Base | Acroyn | Offset | Size | Acroyn | Offset | Size |
180 PRINT(generate_ctable_b.__doc__)
182 def generate_dictionary():
184 Normally, target data file have 2 dictionary map for data integrity. The
185 active map is used for memory holding, and reflash the inactivie map under
188 PRINT(generate_dictionary.__doc__)
190 def gen_dummy_dict_binmap():
191 '''Generate dummy dictionary bin map.
194 generate_header("dictionary.bin", "0.2", "Jackson", "A", 300, 400, 65535);
195 PRINT(gen_dummy_dict_binmap.__doc__)
197 def convert(src, dest):
198 '''Convertion from original text format dictionary to binary map.
200 @param src : text format dictionary
201 @param dest: binary map dictionary
205 PRINT(convert.__doc__)
208 '''Check syntax format of orignal text format dictionary
210 @param src : text format dictionary
212 @return True without syntax error, False else.
217 '''Main business logic
219 @param argv : sys.argv[1:]
220 @return error code if any
223 # handle parameter parse
224 valid_args = "hvVt:c:s:d"
225 valid_long_args = ["help", "version", "source", "target", "check", "dummy"]
230 opts, args = getopt.getopt(argv, valid_args, valid_long_args)
231 except getopt.GetoptError, err:
239 if o in ("-s", "--source"):
241 assert False, "No dictionary source specified"
244 # no dest specified, use same filename as src to store file
246 basename = os.path.basename(src)
247 dest = os.path.splitext(basename)[0]
248 dest = os.path.join(dest, ".bin")
251 elif o in ("-t", "--target"):
253 elif o in ("-d", "--dummy"):
254 gen_dummy_dict_binmap()
255 elif o in ("-c", "--check"):
259 elif o in ("-h", "--help"):
262 elif o in ("-V", "--version"):
267 if __name__ == "__main__":