Add skeleton for target dictionary bin map generation
authorAlsor Zhou <alsor.zhou@gmail.com>
Sun, 27 Sep 2009 09:57:30 +0000 (17:57 +0800)
committerAlsor Zhou <alsor.zhou@gmail.com>
Sun, 27 Sep 2009 09:57:30 +0000 (17:57 +0800)
util/converter.py

index aa27662..f42ad5b 100755 (executable)
@@ -27,6 +27,7 @@
 import getopt
 import sys
 import os.path
+import zlib
 
 DEBUG           = True
 
@@ -71,11 +72,13 @@ def usage():
     -s SRC  :   specify dictionary source
     -t DEST :   save converted binary map into DEST
     -c SRC  :   syntax check SRC, without converstion
+    -d DEST :   generate dummy dictionary bin map
 
     GNU long option style
     --source SRC : same with '-s SRC'
     --target DEST: same with '-t DEST'    
-    --check SRC  : same with '-c SRC'
+    --check  SRC : same with '-c SRC'
+    --dummy  DEST: same with '-d DEST'
     '''
     print usage.__doc__
 
@@ -83,6 +86,114 @@ def version():
     '''MiM pinyin dictionary converter version 0.0.1 Handspring <xhealer@gmail.com>'''
     print version.__doc__
     
+# Target file segmentation layout
+tgt_delimitor           = "\n"
+tgt_file_start_index    = 0
+tgt_header_start_offset = 0
+tgt_st1_offset          = 100
+tgt_st2_offset          = 200
+tgt_table_a_offset      = 300
+tgt_table_b_offset      = 400
+
+tgt_global_position_ind = 0
+# Header definition
+#  Example header fileds:
+#    Fn:dictionary.bin\n
+#    Ver:0.2\n
+#    Authors:Jackson\n
+#    ActiveChunkTableFlag:A\n
+#    ChunkTableAOffset:300
+#    ChunkTableBOffset:400
+#    ChunkSize:65535\n
+#    CRC32:\n
+tgt_header_delemitor_str = ":"
+tgt_header_fn_str        = "Fn"
+tgt_header_version_str   = "Ver"
+tgt_header_author_str    = "Authors"
+tgt_header_actf_str      = "ActiveChunkTableFlag"
+tgt_header_ctao_str      = "ChunkTableAOffset"
+tgt_header_ctbo_str      = "ChunkTableBOffset"
+tgt_header_chunk_size_str= "ChunkSize"
+tgt_header_crc_str       = "CRC32"
+
+# Chunk Table offset
+tgt_ctable_flag_offset           = 0
+tgt_ctable_flag_fld_siz          = 2   # bytes
+tgt_ctable_chk_base_offset       = 2
+tgt_ctable_chk_base_fld_size     = 2   # bytes, 65535 maximize
+tgt_ctable_chk_acroyn_fld_size   = 2
+tgt_ctable_chk_offset_fld_size   = 2
+tgt_ctable_chk_size_fld_size     = 2
+
+# Internal function definition
+def generate_header(fn, ver, authors, actf, ctao, ctbo, csize):
+    '''Generate target file header.
+    @param fn: file name
+    @param ver: dictionary version
+    @param authors: dictionary authors
+    @param actf: active chunk table flag (A/B) 
+    @param ctao: chunk table A offset
+    @param ctbo: chunk table B offset
+    @param csize: chunk size (fixed)
+    
+    @return header: header string with crc32 computed
+    '''
+    crc32  = None
+    header = None
+    header += tgt_header_fn_str + tgt_header_delemitor_str + fn + tgt_delimitor
+    header += tgt_header_version_str + tgt_header_delemitor_str + ver + tgt_delimitor
+    header += tgt_header_version_str + tgt_header_delemitor_str + authors + tgt_delimitor
+    crc32  = crc32(header); # FIXME: should we crc the timestamp?
+    header += tgt_header_version_str + tgt_header_delemitor_str + actf + tgt_delimitor
+    header += tgt_header_version_str + tgt_header_delemitor_str + ctao + tgt_delimitor
+    header += tgt_header_version_str + tgt_header_delemitor_str + ctbo + tgt_delimitor
+    header += tgt_header_version_str + tgt_header_delemitor_str + csize + tgt_delimitor
+    header += tgt_header_version_str + tgt_header_delemitor_str + crc32
+    
+    PRINT(generate_header.__doc__)
+    return header
+
+def generate_st1():
+    '''Generate static table 1.
+    '''
+    PRINT(generate_st1.__doc__)
+
+def generate_st2():
+    '''Generate static table 2.
+    '''
+    PRINT(generate_st2.__doc__)
+
+def generate_ctable_a():
+    '''Chunk Table A generation.
+    Example chunk table:
+    0------------2------------4--------6--------8------10-------12-------14----16
+    | Table flag | Chunk Base | Acroyn | Offset | Size | Acroyn | Offset | Size |  
+    '''
+    PRINT(generate_ctable_a.__doc__)
+
+def generate_ctable_b():
+    '''Chunk Table B generation.
+    Example chunk table:
+    0------------2------------4--------6--------8------10-------12-------14----16
+    | Table flag | Chunk Base | Acroyn | Offset | Size | Acroyn | Offset | Size |  
+    '''
+    PRINT(generate_ctable_b.__doc__)
+
+def generate_dictionary():
+    '''
+    Normally, target data file have 2 dictionary map for data integrity. The 
+    active map is used for memory holding, and reflash the inactivie map under 
+    some mechanism.
+    '''
+    PRINT(generate_dictionary.__doc__)
+
+def gen_dummy_dict_binmap():
+    '''Generate dummy dictionary bin map.
+    
+    '''
+    generate_header("dictionary.bin", "0.2", "Jackson", "A", 300, 400, 65535);
+    PRINT(gen_dummy_dict_binmap.__doc__)
+
 def convert(src, dest):
     '''Convertion from original text format dictionary to binary map.
     
@@ -111,7 +222,7 @@ def main(argv):
 
     # handle parameter parse
     valid_args = "hvVt:c:s:d"
-    valid_long_args = ["help", "version", "source", "target", "check"]
+    valid_long_args = ["help", "version", "source", "target", "check", "dummy"]
     src = None
     dest = None
     
@@ -139,6 +250,8 @@ def main(argv):
             convert(src, dest)
         elif o in ("-t", "--target"):
             dest = a
+        elif o in ("-d", "--dummy"):
+            gen_dummy_dict_binmap()
         elif o in ("-c", "--check"):
             check(a)
         elif o == "-v":