Dictionary converter parameter parse
authorAlsor Zhou <alsor.zhou@gmail.com>
Fri, 18 Sep 2009 08:47:30 +0000 (16:47 +0800)
committerAlsor Zhou <alsor.zhou@gmail.com>
Fri, 18 Sep 2009 08:47:30 +0000 (16:47 +0800)
util/converter.py [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index 2b75be3..aa27662
@@ -1,10 +1,11 @@
+#!/usr/bin/python
 ########################################################################
 ##
 ##  Copyright (C) 2009  MiM
 ##
 ##          Contact: Handspring <xhealer@gmail.com>
 ##
 ########################################################################
 ##
 ##  Copyright (C) 2009  MiM
 ##
 ##          Contact: Handspring <xhealer@gmail.com>
 ##
-##          AUTHOR:
+##          AUTHOR: Alsor Zhou <alsor.zhou@gmail.com>
 ##
 ##  This file is part of MiM Pinyin.
 ##
 ##
 ##  This file is part of MiM Pinyin.
 ##
 ##  along with Sigil.  If not, see <http://www.gnu.org/licenses/>.
 ##
 ########################################################################
 ##  along with Sigil.  If not, see <http://www.gnu.org/licenses/>.
 ##
 ########################################################################
+
+import getopt
+import sys
+import os.path
+
+DEBUG           = True
+
+# Global ERROR DEFINATION
+ERR_PARAMS      = 2
+ERR_UNKOWN      = 255
+
+def PRINT(v):
+    '''Print wrapper with debug function supported
+    
+    Never use this function in production (always output) code '''
+    if DEBUG == True:
+        print v
+        
+def license():
+    '''
+Copyright (C) 2009  MiM
+
+      Contact: Handspring <xhealer@gmail.com>
+
+      AUTHOR:
+
+This file is part of MiM Pinyin.
+
+This is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This 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 General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Sigil.  If not, see <http://www.gnu.org/licenses/>.
+    '''
+    print license.__doc__
+
+def usage():
+    '''converter.py [options] SRC [options...DEST]
+    -s SRC  :   specify dictionary source
+    -t DEST :   save converted binary map into DEST
+    -c SRC  :   syntax check SRC, without converstion
+
+    GNU long option style
+    --source SRC : same with '-s SRC'
+    --target DEST: same with '-t DEST'    
+    --check SRC  : same with '-c SRC'
+    '''
+    print usage.__doc__
+
+def version():
+    '''MiM pinyin dictionary converter version 0.0.1 Handspring <xhealer@gmail.com>'''
+    print version.__doc__
+    
+def convert(src, dest):
+    '''Convertion from original text format dictionary to binary map.
+    
+    @param  src : text format dictionary
+    @param  dest: binary map dictionary
+    
+    @return None
+    '''
+    PRINT(convert.__doc__)
+
+def check(src):
+    '''Check syntax format of orignal text format dictionary
+    
+    @param  src : text format dictionary
+    
+    @return True without syntax error, False else.
+    '''
+    PRINT(check.__doc__)
+    
+def main(argv):
+    '''Main business logic
+    
+    @param  argv : sys.argv[1:]
+    @return error code if any
+    '''
+
+    # handle parameter parse
+    valid_args = "hvVt:c:s:d"
+    valid_long_args = ["help", "version", "source", "target", "check"]
+    src = None
+    dest = None
+    
+    try:
+        opts, args = getopt.getopt(argv, valid_args, valid_long_args)
+    except getopt.GetoptError, err:
+        print str(err)
+        license()
+        usage()
+        sys.exit(ERR_PARAMS)
+    output = None
+    verbose = False
+    for o, a in opts:
+        if o in ("-s", "--source"):
+            if a == None:
+                assert False, "No dictionary source specified"
+                usage()
+                sys.exit(ERR_PARAMS)
+            # no dest specified, use same filename as src to store file
+            if dest == None:
+                basename = os.path.basename(src)
+                dest = os.path.splitext(basename)[0]
+                dest = os.path.join(dest, ".bin")
+            src = a
+            convert(src, dest)
+        elif o in ("-t", "--target"):
+            dest = a
+        elif o in ("-c", "--check"):
+            check(a)
+        elif o == "-v":
+            verbose = True
+        elif o in ("-h", "--help"):
+            usage()
+            sys.exit()
+        elif o in ("-V", "--version"):
+            version()
+        else:
+            usage()
+
+if __name__ == "__main__":
+    main(sys.argv[1:])