Ejemplo n.º 1
0
def write(glos, filename, sort=True, dictZip=True, install=True):  ## FIXME
    if sort:
        glos = glos.copy()
        glos.data.sort()
    (filename_nox, ext) = splitext(filename)
    if ext.lower() == '.index':
        filename = filename_nox
    indexFd = open(filename + '.index', 'wb')
    dictFd = open(filename + '.dict', 'wb')
    dictMark = 0
    for item in glos.data:
        (word, defi) = item[:2]
        lm = len(defi)
        indexFd.write(word + '\t' + intToIndexStr(dictMark) + '\t' +
                      intToIndexStr(lm) + '\n')  ## FIXME
        dictFd.write(defi)
        dictMark += lm
    indexFd.close()
    dictFd.close()
    #for key in glos.infoKeys():
    #  value = glos.getInfo(key)
    #  if value!='':
    #    pass ## FIXME
    if dictZip:
        runDictzip(filename)
    if install:
        installToDictd(filename, glos.getInfo('name').replace(' ', '_'))
Ejemplo n.º 2
0
def write_ext(glos, filename, sort=True, dictZip=True):
    if sort:
        g = glos.copy()
        g.data.sort()
    else:
        g = glos
    try:
        import _stardictbuilder
    except ImportError:
        printAsError('Binary module "_stardictbuilder" can not be imported! '+\
          'Using internal StarDict builder')
        return g.writeStardict(filename, sort=False)
    db = _stardictbuilder.new_StarDictBuilder(filename)
    _stardictbuilder.StarDictBuilder_swigregister(db)
    for item in g.data:
        _stardictbuilder.StarDictBuilder_addHeadword(db, item[0], item[1], '')
    _stardictbuilder.StarDictBuilder_setTitle(db, g.getInfo('name'))
    _stardictbuilder.StarDictBuilder_setAuthor(db, g.getInfo('author'))
    _stardictbuilder.StarDictBuilder_setLicense(db, g.getInfo('license'))
    _stardictbuilder.StarDictBuilder_setOrigLang(db, g.getInfo('origLang'))
    _stardictbuilder.StarDictBuilder_setDestLang(db, g.getInfo('destLang'))
    _stardictbuilder.StarDictBuilder_setDescription(db,
                                                    g.getInfo('description'))
    _stardictbuilder.StarDictBuilder_setComments(db, g.getInfo('comments'))
    _stardictbuilder.StarDictBuilder_setEmail(db, g.getInfo('email'))
    _stardictbuilder.StarDictBuilder_setWebsite(db, g.getInfo('website'))
    _stardictbuilder.StarDictBuilder_setVersion(db, g.getInfo('version'))
    _stardictbuilder.StarDictBuilder_setcreationTime(db, '')
    _stardictbuilder.StarDictBuilder_setLastUpdate(db, '')
    _stardictbuilder.StarDictBuilder_finish(db)
    if dictZip:
        if filename[-4:] == '.ifo':
            filename = filename[:-4]
            runDictzip(filename)
Ejemplo n.º 3
0
def write(glos, filename, sort=True, dictZip=True, install=True):## FIXME
  if sort:
    glos = glos.copy()
    glos.data.sort()
  (filename_nox, ext) = splitext(filename)
  if ext.lower()=='.index':
    filename = filename_nox
  indexFd = open(filename+'.index', 'wb')
  dictFd = open(filename+'.dict', 'wb')
  dictMark = 0
  for item in glos.data:
    (word, defi) = item[:2]
    lm = len(defi)
    indexFd.write(word + '\t' + intToIndexStr(dictMark) + '\t' + intToIndexStr(lm) + '\n')## FIXME
    dictFd.write(defi)
    dictMark += lm
  indexFd.close()
  dictFd.close()
  #for key in glos.infoKeys():
  #  value = glos.getInfo(key)
  #  if value!='':
  #    pass ## FIXME
  if dictZip:
    runDictzip(filename)
  if install:
    installToDictd(filename, glos.getInfo('name').replace(' ', '_'))
Ejemplo n.º 4
0
def write_ext(glos, filename, sort=True, dictZip=True):
  if sort:
    g = glos.copy()
    g.data.sort()
  else:
    g = glos
  try:
    import _stardictbuilder
  except ImportError:
    printAsError('Binary module "_stardictbuilder" can not be imported! '+\
      'Using internal StarDict builder')
    return g.writeStardict(filename, sort=False)
  db = _stardictbuilder.new_StarDictBuilder(filename)
  _stardictbuilder.StarDictBuilder_swigregister(db)
  for item in g.data:
    _stardictbuilder.StarDictBuilder_addHeadword(db,item[0],item[1], '')
  _stardictbuilder.StarDictBuilder_setTitle(db, g.getInfo('name'))
  _stardictbuilder.StarDictBuilder_setAuthor(db, g.getInfo('author'))
  _stardictbuilder.StarDictBuilder_setLicense(db, g.getInfo('license'))
  _stardictbuilder.StarDictBuilder_setOrigLang(db, g.getInfo('origLang'))
  _stardictbuilder.StarDictBuilder_setDestLang(db, g.getInfo('destLang'))
  _stardictbuilder.StarDictBuilder_setDescription(db, g.getInfo('description'))
  _stardictbuilder.StarDictBuilder_setComments(db, g.getInfo('comments'))
  _stardictbuilder.StarDictBuilder_setEmail(db, g.getInfo('email'))
  _stardictbuilder.StarDictBuilder_setWebsite(db, g.getInfo('website'))
  _stardictbuilder.StarDictBuilder_setVersion(db, g.getInfo('version'))
  _stardictbuilder.StarDictBuilder_setcreationTime(db, '')
  _stardictbuilder.StarDictBuilder_setLastUpdate(db, '')
  _stardictbuilder.StarDictBuilder_finish(db)
  if dictZip:
    if filename[-4:]=='.ifo':
      filename = filename[:-4]
      runDictzip(filename)
Ejemplo n.º 5
0
def write(glos, filename, dictZip=True, richText=True, resOverwrite=False):
    g = glos.copy()
    g.data.sort(stardict_strcmp, lambda x: x[0])
    if filename[-4:].lower() == '.ifo':
        filename = filename[:-4]
    elif filename[-1] == os.sep:
        if not os.path.isdir(filename):
            os.makedirs(filename)
        filename = os.path.join(filename, os.path.split(filename[:-1])[-1])
    elif os.path.isdir(filename):
        filename = os.path.join(filename, os.path.split(filename)[-1])
    # filename now contains full file path without extension
    dictMark = 0
    idxStr = ''
    dictStr = ''
    alternates = []  # contains tuples ('alternate', index-of-word)
    for i in xrange(len(g.data)):
        item = g.data[i]
        (word, defi) = item[:2]
        if len(item) > 2 and 'alts' in item[2]:
            alternates += [(x, i) for x in item[2]['alts']]
        lm = len(defi)
        idxStr += (word + '\x00' + intToBinStr(dictMark, 4) +
                   intToBinStr(lm, 4))
        dictMark += lm
        dictStr += defi
    with open(filename + '.dict', 'wb') as f:
        f.write(dictStr)
    with open(filename + '.idx', 'wb') as f:
        f.write(idxStr)
    #from pprint import pformat; open(filename+'-alternates', 'w').write(pformat(alternates))
    if alternates:
        alternates.sort(stardict_strcmp, lambda x: x[0])
        synStr = ''
        for item in alternates:
            synStr += (item[0] + '\x00' + intToBinStr(item[1], 4))
        with open(filename + '.syn', 'wb') as f:
            f.write(synStr)
    ifoStr = '''StarDict\'s dict ifo file\nversion=3.0.0
bookname=%s
wordcount=%d
idxfilesize=%d
sametypesequence=%s
''' % (new_lines_2_space(g.getInfo('name')), len(g.data), len(idxStr),
       ('h' if richText else 'm'))
    if alternates:
        ifoStr += 'synwordcount={0}\n'.format(len(alternates))
    for key in infoKeys:
        if key in ('bookname', 'wordcount', 'idxfilesize', 'sametypesequence'):
            continue
        value = g.getInfo(key)
        if value == '':
            continue
        if key == 'description':
            ifoStr += '{0}={1}\n'.format(key, new_line_2_br(value))
        else:
            ifoStr += '{0}={1}\n'.format(key, new_lines_2_space(value))
    with open(filename + '.ifo', 'wb') as f:
        f.write(ifoStr)
    if dictZip:
        runDictzip(filename)
    copy_resources(glos.resPath, os.path.join(os.path.dirname(filename),
                                              'res'), resOverwrite)
Ejemplo n.º 6
0
def write(glos, filename, dictZip=True, richText=True, resOverwrite=False):
  g = glos.copy()
  g.data.sort(stardict_strcmp, lambda x: x[0])
  if filename[-4:].lower()=='.ifo':
    filename=filename[:-4]
  elif filename[-1]==os.sep:
    if not os.path.isdir(filename):
      os.makedirs(filename)
    filename = os.path.join(filename, os.path.split(filename[:-1])[-1])
  elif os.path.isdir(filename):
    filename = os.path.join(filename, os.path.split(filename)[-1])
  # filename now contains full file path without extension
  dictMark = 0
  idxStr = ''
  dictStr = ''
  alternates = [] # contains tuples ('alternate', index-of-word)
  for i in xrange(len(g.data)):
    item = g.data[i]
    (word, defi) = item[:2]
    if len(item) > 2 and 'alts' in item[2]:
      alternates += [(x, i) for x in item[2]['alts']]
    lm = len(defi)
    idxStr += (word + '\x00' + intToBinStr(dictMark, 4) + intToBinStr(lm, 4))
    dictMark += lm
    dictStr += defi
  with open(filename+'.dict', 'wb') as f:
    f.write(dictStr)
  with open(filename+'.idx', 'wb') as f:
    f.write(idxStr)
  #from pprint import pformat; open(filename+'-alternates', 'w').write(pformat(alternates))
  if alternates:
    alternates.sort(stardict_strcmp, lambda x: x[0])
    synStr = ''
    for item in alternates:
      synStr += (item[0] + '\x00' + intToBinStr(item[1], 4))
    with open(filename+'.syn', 'wb') as f:
      f.write(synStr)
  ifoStr = '''StarDict\'s dict ifo file\nversion=3.0.0
bookname=%s
wordcount=%d
idxfilesize=%d
sametypesequence=%s
'''%(new_lines_2_space(g.getInfo('name')), len(g.data), len(idxStr), ('h' if richText else 'm'))
  if alternates:
    ifoStr += 'synwordcount={0}\n'.format(len(alternates))
  for key in infoKeys:
    if key in ('bookname', 'wordcount', 'idxfilesize', 'sametypesequence'):
      continue
    value = g.getInfo(key)
    if value == '':
      continue
    if key == 'description':
      ifoStr += '{0}={1}\n'.format(key, new_line_2_br(value))
    else:
      ifoStr += '{0}={1}\n'.format(key, new_lines_2_space(value))
  with open(filename+'.ifo', 'wb') as f:
    f.write(ifoStr)
  if dictZip:
    runDictzip(filename)
  copy_resources(
    glos.resPath,
    os.path.join(os.path.dirname(filename), 'res'),
    resOverwrite
  )