From 5e9688be00ed02e0fba6f938a3540c00d0903d14 Mon Sep 17 00:00:00 2001 From: Alsor Zhou Date: Fri, 18 Sep 2009 16:47:30 +0800 Subject: [PATCH] Dictionary converter parameter parse --- util/converter.py | 133 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 132 insertions(+), 1 deletion(-) mode change 100644 => 100755 util/converter.py diff --git a/util/converter.py b/util/converter.py old mode 100644 new mode 100755 index 2b75be3..aa27662 --- a/util/converter.py +++ b/util/converter.py @@ -1,10 +1,11 @@ +#!/usr/bin/python ######################################################################## ## ## Copyright (C) 2009 MiM ## ## Contact: Handspring ## -## AUTHOR: +## AUTHOR: Alsor Zhou ## ## This file is part of MiM Pinyin. ## @@ -22,3 +23,133 @@ ## along with Sigil. If not, see . ## ######################################################################## + +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 + + 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 . + ''' + 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 ''' + 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:]) -- 1.7.9.5