Dictionary converter parameter parse
[mim] / util / converter.py
1 #!/usr/bin/python
2 ########################################################################
3 ##
4 ##  Copyright (C) 2009  MiM
5 ##
6 ##          Contact: Handspring <xhealer@gmail.com>
7 ##
8 ##          AUTHOR: Alsor Zhou <alsor.zhou@gmail.com>
9 ##
10 ##  This file is part of MiM Pinyin.
11 ##
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.
16 ##
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.
21 ##
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/>.
24 ##
25 ########################################################################
26
27 import getopt
28 import sys
29 import os.path
30
31 DEBUG           = True
32
33 # Global ERROR DEFINATION
34 ERR_PARAMS      = 2
35 ERR_UNKOWN      = 255
36
37 def PRINT(v):
38     '''Print wrapper with debug function supported
39     
40     Never use this function in production (always output) code '''
41     if DEBUG == True:
42         print v
43         
44 def license():
45     '''
46 Copyright (C) 2009  MiM
47
48       Contact: Handspring <xhealer@gmail.com>
49
50       AUTHOR:
51
52 This file is part of MiM Pinyin.
53
54 This is free software: you can redistribute it and/or modify
55 it under the terms of the GNU General Public License as published by
56 the Free Software Foundation, either version 3 of the License, or
57 (at your option) any later version.
58
59 This is distributed in the hope that it will be useful,
60 but WITHOUT ANY WARRANTY; without even the implied warranty of
61 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
62 GNU General Public License for more details.
63
64 You should have received a copy of the GNU General Public License
65 along with Sigil.  If not, see <http://www.gnu.org/licenses/>.
66     '''
67     print license.__doc__
68
69 def usage():
70     '''converter.py [options] SRC [options...DEST]
71     -s SRC  :   specify dictionary source
72     -t DEST :   save converted binary map into DEST
73     -c SRC  :   syntax check SRC, without converstion
74
75     GNU long option style
76     --source SRC : same with '-s SRC'
77     --target DEST: same with '-t DEST'    
78     --check SRC  : same with '-c SRC'
79     '''
80     print usage.__doc__
81
82 def version():
83     '''MiM pinyin dictionary converter version 0.0.1 Handspring <xhealer@gmail.com>'''
84     print version.__doc__
85     
86 def convert(src, dest):
87     '''Convertion from original text format dictionary to binary map.
88     
89     @param  src : text format dictionary
90     @param  dest: binary map dictionary
91     
92     @return None
93     '''
94     PRINT(convert.__doc__)
95
96 def check(src):
97     '''Check syntax format of orignal text format dictionary
98     
99     @param  src : text format dictionary
100     
101     @return True without syntax error, False else.
102     '''
103     PRINT(check.__doc__)
104     
105 def main(argv):
106     '''Main business logic
107     
108     @param  argv : sys.argv[1:]
109     @return error code if any
110     '''
111
112     # handle parameter parse
113     valid_args = "hvVt:c:s:d"
114     valid_long_args = ["help", "version", "source", "target", "check"]
115     src = None
116     dest = None
117     
118     try:
119         opts, args = getopt.getopt(argv, valid_args, valid_long_args)
120     except getopt.GetoptError, err:
121         print str(err)
122         license()
123         usage()
124         sys.exit(ERR_PARAMS)
125     output = None
126     verbose = False
127     for o, a in opts:
128         if o in ("-s", "--source"):
129             if a == None:
130                 assert False, "No dictionary source specified"
131                 usage()
132                 sys.exit(ERR_PARAMS)
133             # no dest specified, use same filename as src to store file
134             if dest == None:
135                 basename = os.path.basename(src)
136                 dest = os.path.splitext(basename)[0]
137                 dest = os.path.join(dest, ".bin")
138             src = a
139             convert(src, dest)
140         elif o in ("-t", "--target"):
141             dest = a
142         elif o in ("-c", "--check"):
143             check(a)
144         elif o == "-v":
145             verbose = True
146         elif o in ("-h", "--help"):
147             usage()
148             sys.exit()
149         elif o in ("-V", "--version"):
150             version()
151         else:
152             usage()
153
154 if __name__ == "__main__":
155     main(sys.argv[1:])