py/gadict_c5.py
author Oleksandr Gavenko <gavenkoa@gmail.com>
Thu, 15 Sep 2016 20:13:18 +0300
changeset 558 53fd793e345d
parent 554 59714b9033bc
child 560 dad049a0f585
permissions -rw-r--r--
Add shortcut to deploy to HG repos.

# -*- coding: utf-8 -*-
"""dictd C5 format writer"""

import io
import sys
import codecs

import gadict


FINAME = None
FONAME = None
if len(sys.argv) >= 2:
    FINAME = sys.argv[1]
if len(sys.argv) >= 3:
    FONAME = sys.argv[2]
LANGS = None
if len(sys.argv) >= 4:
    LANGS = set(sys.argv[3].split(","))

FIN = io.open(FINAME, mode='r', buffering=1, encoding="utf-8")

PARSER = gadict.Parser()
try:
    DOM = PARSER.parse(FIN)
except gadict.ParseException as ex:
    sys.stdout.write("{:s}{:s}\n".format(FINAME, repr(ex)))
    if __debug__:
        import traceback
        traceback.print_exc()
    exit(1)
finally:
    FIN.close()


PRELUDE = DOM[0]

with open(FONAME+".name", "w") as f:        # for dictfmt -s
    if PRELUDE.name is not None:
        f.write(PRELUDE.name)
    f.write("\n")

with open(FONAME+".url", "w") as f:         # for dictfmt -u
    if len(PRELUDE.urls) > 0:
        f.write(PRELUDE.urls[0])
    f.write("\n")


if FONAME is None:
    FOUT = sys.stdout
else:
    FOUT = codecs.open(FONAME, "w", "utf-8")

if PRELUDE.name is not None:
    FOUT.write("Dictionary name: ")
    FOUT.write(PRELUDE.name)
    FOUT.write("\n\n")
FOUT.write("Project URLs: ")
FOUT.write(" , ".join(PRELUDE.urls))
FOUT.write("\n\n")
FOUT.write("Project licenses: ")
FOUT.write(", ".join(PRELUDE.licences))
FOUT.write("\n\n")
FOUT.write(PRELUDE.about)
FOUT.write("\n")


for (headwords, translations) in DOM[1:]:
    FOUT.write("_____\n\n")
    title = "; ".join([h.headword for h in headwords])
    FOUT.write(title)
    FOUT.write("\n\n")
    for hw in headwords:
        FOUT.write("  ")
        FOUT.write(hw.headword)
        if hw.pron is not None:
            FOUT.write(" [")
            FOUT.write(hw.pron)
            FOUT.write("]")
        if len(hw.attrs) > 0:
            FOUT.write(" ")
            l = ["«"+x+"»" for x in hw.attrs]
            l.sort()
            FOUT.write(", ".join(l))
        FOUT.write("\n")
    FOUT.write("\n")
    for sense in translations:
        if not sense:
            raise Exception("""Empty sense for article: """ + headwords.__iter__().__next__())
        FOUT.write("  ")
        if sense.pos:
            FOUT.write("«")
            FOUT.write(sense.pos)
            FOUT.write("» ")
            if sense.ant_list and len(sense.ant_list) > 0:
                FOUT.write(" ant: ")
                FOUT.write("; ".join(["{"+s+"}" for s in sense.ant_list]))
            if sense.syn_list and len(sense.syn_list) > 0:
                FOUT.write(" syn: ")
                FOUT.write("; ".join(["{"+s+"}" for s in sense.syn_list]))
            FOUT.write("\n")
        for (lang, tr) in sense.tr_list:
            FOUT.write("  ")
            if LANGS is None:
                FOUT.write(lang)
                FOUT.write("→ ")
                FOUT.write(tr)
            elif lang in LANGS:
                if len(LANGS) == 1:
                    FOUT.write(tr)
                else:
                    FOUT.write(lang)
                    FOUT.write("→ ")
                    FOUT.write(tr)
            FOUT.write("\n")