Created
May 9, 2012 08:48
-
-
Save saturngod/2643075 to your computer and use it in GitHub Desktop.
fixed convert file in sdconv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys, os, glob, commands, urllib2 | |
import PListReader, xml.sax | |
from optparse import OptionParser | |
temp_dir = "sdconv-temp" | |
script_module = { ".py": "python" } | |
def cleanup(): | |
os.system("rm -rf %s" % temp_dir) | |
def list_dictionaries(): | |
def get_dict_property(dict_path): | |
dict_plist = "%s/Contents/Info.plist" % dict_path | |
try: | |
reader = PListReader.PListReader() | |
parser = xml.sax.make_parser() | |
for key, value in reader.getRecommendedFeatures().items(): | |
parser.setFeature(key, value) | |
parser.setContentHandler(reader) | |
parser.parse(open(dict_plist, 'r')) | |
# print the Python data structure | |
result = reader.getResult() | |
dict_id = result["CFBundleIdentifier"].split('.')[-1] | |
return [ dict_id, result["CFBundleDisplayName"] ] | |
except: | |
return [ "(null)", "(null)" ] | |
def print_dictionary_info(dict_path): | |
(dict_id, dict_name) = get_dict_property(dict_path) | |
print "%20s: %s" % (dict_id, dict_name) | |
print "Installed dictionaries:\n" | |
# initialize dicts with *.dictionary files in user's home directory | |
dicts = glob.glob(os.path.expanduser("~/Library/Dictionaries/*.dictionary")) | |
# then add dictionaries in system directory | |
dicts.extend(glob.glob("/Library/Dictionaries/*.dictionary")) | |
for dict in dicts: | |
print_dictionary_info(dict) | |
self_path = sys.path[0] | |
usage = "usage: %prog [options] <stardict-*.bz2>" | |
parser = OptionParser(usage) | |
parser.add_option("-s", "--script", dest="script", | |
help="use FILE as script to process dictionary", | |
metavar="FILE") | |
parser.add_option("-n", "--name", dest="name", metavar="STR", | |
help="use STR as the dictionary name [default: guessed]") | |
parser.add_option("-i", "--id", dest="id", metavar="STR", | |
help="use STR as the dictionary id [default: guessed]") | |
parser.add_option("-d", "--debug", dest="debug", action="store_true", | |
help="enable debugging mode (only converts a few entries)") | |
parser.add_option("-l", "--list", dest="list", action="store_true", | |
help="list installed dictionaries") | |
parser.add_option("-c", "--console", dest="console", action="store_true", | |
help="output to console, do not write files") | |
(options, args) = parser.parse_args() | |
if options.list: | |
list_dictionaries() | |
sys.exit() | |
if len(args) != 1: | |
print "usage: convert [options] <stardict-*.bz2> (-h for full usage)" | |
sys.exit(1) | |
dict_file = args[0] | |
script_file = None | |
cleanup() | |
os.makedirs(temp_dir) | |
# if it's a bzipped file, extract it first | |
if dict_file.endswith(".bz2"): | |
os.system("tar -xjf '%s' -C %s" % (dict_file, temp_dir)) | |
ifos = glob.glob("%s/*/*.ifo" % temp_dir) | |
if len(ifos) == 0: | |
print "no .ifo files existed in %s, not a valid stardict format" % dict_file | |
cleanup() | |
sys.exit(1) | |
ifo_file = ifos[0] | |
elif dict_file.endswith(".ifo"): | |
if os.access(dict_file, os.R_OK): | |
ifo_file = dict_file | |
else: | |
print "%s not readable" % dict_file | |
sys.exit(1) | |
else: | |
print "%s not readable" % dict_file | |
sys.exit(1) | |
if options.script: | |
script_file = options.script | |
if os.access(script_file, os.R_OK): | |
ext = os.path.splitext(script_file)[1] | |
if ext in script_module: | |
module = script_module[ext] | |
print "script_file = %s (%s)" % (script_file, module) | |
else: | |
print "script %s not supported" % script_file | |
sys.exit(1) | |
else: | |
print "script %s not readable" % script_file | |
sys.exit(1) | |
print "ifo_file = %s" % ifo_file | |
(dict_id, ext) = os.path.splitext(os.path.basename(ifo_file)) | |
if options.id: | |
dict_id = options.id | |
print "dict_id = %s" % dict_id | |
# dyld_lib_path = os.getenv("DYLD_LIBRARY_PATH") | |
# dyld_lib_path = "%s/lib:%s" % (self_path, dyld_lib_path) | |
# os.putenv("DYLD_LIBRARY_PATH", dyld_lib_path) | |
dict_path = "%s/dict-%s" % (temp_dir, dict_id) | |
os.system("cp -r '%s/templates' '%s'" % (self_path, dict_path)) | |
if script_file: | |
script_opt = "-m %s '%s' " % (module, script_file) | |
else: | |
script_opt = "" | |
if options.debug: | |
debug_opt = "-d " | |
else: | |
debug_opt = "" | |
if options.console: | |
out_file = "" | |
else: | |
out_file = " '%s/%s/Dictionary.xml'" % (os.getcwd(),dict_path) | |
cmd = "DYLD_LIBRARY_PATH='%s/bin' '%s/bin/sdconv' %s%s'%s'%s" % (self_path, | |
self_path, | |
debug_opt, | |
script_opt, | |
os.getcwd()+"/"+ifo_file, | |
out_file) | |
print cmd | |
(status, output) = commands.getstatusoutput(cmd) | |
if status != 0: | |
print "convert dictionary %s failed, abort now." % dict_id | |
cleanup() | |
sys.exit(1) | |
if options.console: | |
print output | |
sys.exit(1) | |
if options.name: | |
dict_name = options.name | |
else: | |
dict_name = output.split()[0] | |
print "dict_name = %s" % dict_name | |
os.system("/usr/bin/sed -i '' -e 's/\\$DICT_NAME/%s/g' '%s/DictInfo.plist'" % (dict_name, dict_path)) | |
os.system("/usr/bin/sed -i '' -e 's/\\$DICT_ID/%s/g' '%s/DictInfo.plist'" % (dict_id, dict_path)) | |
os.system("cat %s/DictInfo.plist" % dict_path) | |
os.system("pushd %s; python build-dict.py %s '%s'; popd" % (dict_path, dict_id, self_path)) | |
cleanup() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment