py/gadict_c5.py
author Oleksandr Gavenko <gavenkoa@gmail.com>
Mon, 28 Mar 2016 00:31:52 +0300
changeset 404 cdfa6dfa9072
parent 403 241ecfe2ca3c
child 405 6208d07b30f0
permissions -rw-r--r--
Integrate dictionary metainfo line name and home page url into dictd build.


import gadict
import io
import sys


finame = None
foname = None
if len(sys.argv) >= 2:
    finame = sys.argv[1]
if len(sys.argv) >= 3:
    foname = sys.argv[2]

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 = open(foname, "w")

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 idx in range(1, len(dom)):
    article = dom[idx]
    fout.write("_____\n\n")
    title = "; ".join(article[0].keys())
    fout.write(title)
    fout.write("\n\n")
    for (word, (pron, attrs)) in article[0].items():
        if word == "approach":
            fout.write(str(article[0]))
        fout.write("  ")
        fout.write(word)
        fout.write("\n")
        if pron is not None:
            fout.write("    [")
            fout.write(pron)
            fout.write("]\n")
        if len(attrs) > 0:
            fout.write("    ")
            l = list(attrs)
            l.sort()
            fout.write(", ".join(l))
            fout.write("\n")
    fout.write("\n")
    for (pos, trs, exs) in article[1]:
        fout.write("  ")
        if pos is not None:
            fout.write("⟨")
            fout.write(pos)
            fout.write("⟩ ")
        for (lang, tr) in trs:
            if lang == "ru":
                fout.write(tr)
                break
        fout.write("\n")