示例#1
0
def create_entr(cursor, parsed):
    # From the dictionary of wwwjdict submission values in
    # 'parsed' we create the same kind of data that cgi/edform.py
    # creates internally to send to the edform.tal template: an
    # Entr object with some attached extra data.  This object is
    # returned to caller (who will serialize it and write it to
    # a file).

    if parsed['subtype'] == 'new':
        entr = jdb.Entr()
        entr.src = jdb.KW.SRC['jmdict'].id
    else:  # == 'amend'
        seqnum = parsed['seqnum']
        errs = []
        # FIXME: following assumes seqnum is an entry in jmdict.
        entrs = jmcgi.get_entrs(cursor,
                                None, [seqnum],
                                errs,
                                active=True,
                                corpus='jmdict')
        if errs: print('\n'.join(errs))
        if entrs: entr = entrs[0]
        else:
            raise ParseError("Unable to get entry seq# %s from database" %
                             seqnum)

    kanj = []
    rdng = []
    gloss = []
    for x in parsed.get('headw', []):
        if jdb.jstr_reb(x): rdng.append(x)
        else: kanj.append(x)
    rdng.extend(parsed.get('kana', []))
    ktxt = ';'.join(kanj)
    rtxt = ';'.join(rdng)
    stxt = ' / '.join(parsed.get('english', []))
    pos = ','.join(parsed.get('pos', []))
    misc = ','.join(parsed.get('misc', []))
    xref = ','.join(parsed.get('crossref', []))
    #FIXME: Note that including pos, xref. et.al. can break
    # a sense parse that would otherwise be ok.  Maybe if the
    # parse fails, we should try again without this stuff,
    # and if that works, append this stuff as "unparsable"-
    # tagged extra text.
    # However, senses other than the first may have this
    # information embedded in the text and it seems a bit
    # much to try pulling it out...
    stxt = (('('+pos+')') if pos else '') \
            + (('(See '+xref+')') if xref else '') \
            + (('('+misc+')') if misc else '') \
            + (' ' if pos or misc or xref else '') + stxt

    #FIXME:  What do about 'date', 'entlangnam' fields?
    # I don't think we care about 'sendNotJS'.

    ktxt, rtxt, stxt = reformat(ktxt, rtxt, stxt, entr)
    entr.ktxt, entr.rtxt, entr.stxt = ktxt, rtxt, stxt
    return entr
示例#2
0
def main(args, opts):
    jdb.reset_encoding(sys.stdout, 'utf-8')
    errs = []
    try:
        form, svc, dbg, cur, sid, sess, parms, cfg = jmcgi.parseform()
    except Exception as e:
        jmcgi.err_page([str(e)])

    entries = jmcgi.get_entrs(cur, form.getlist('e'), form.getlist('q'), errs)
    if not entries:
        jmcgi.err_page("No entries found")
        return

    ids = [e.id for e in entries]
    sql, args = "SELECT * FROM vinflxt WHERE id IN %s", (tuple(ids), )
    results = jdb.dbread(cur, sql, args)
    poses = set([p.kw for e in entries for s in e._sens for p in s._pos])
    poskws = sorted([jdb.KW.POS[p].kw for p in poses])
    if not results:
        if poskws:
            msg = "Unable to conjugate any of the following parts-of-speech: %s." % (
                ', '.join(poskws))
        else:
            msg = "Word does not have a part-of-speech tag."
        jmcgi.err_page(msg)
        return

    sql, args = "SELECT DISTINCT id,txt FROM vconotes WHERE pos IN %s ORDER BY id", (
        tuple(poses), )
    notes = jdb.dbread(cur, sql, args)

    cur.close()
    # Make notes links, replace '\n's with <br/>s.
    htmlify_conjs(results)
    # Divide the conjugations table up into sections, one for each word (by id).
    sections = partition_conjs(results)
    # Make each note a link target.
    htmlify_notes(notes)

    if errs: jmcgi.err_page(errs)

    jmcgi.jinja_page('conj.jinja',
                     sections=sections,
                     notes=notes,
                     svc=svc,
                     dbg=dbg,
                     sid=sid,
                     session=sess,
                     cfg=cfg,
                     parms=parms,
                     this_page='conj.py')
示例#3
0
def main (args, opts):
        jdb.reset_encoding (sys.stdout, 'utf-8')
        errs = []
        try: form, svc, dbg, cur, sid, sess, parms, cfg = jmcgi.parseform()
        except Exception as e: jmcgi.err_page ([str (e)])
        entries = jmcgi.get_entrs (cur, form.getlist ('e'),
                                        form.getlist ('q'), errs)
        if errs: jmcgi.err_page (errs)

          # Add a .SEQKR attribute to each entry in 'entries' that
          # gives the kanji and reading of the newest (most recently 
          # edited) entry that has the same sequence number. 
        seqkr_decorate (entries)

          # Sort the entries.  The sorting order will group entries 
          # with the same sequence number (.src,.seq) together and 
          # each of those groups will be ordered by the kanji/reading
          # of the newest (most recently edited) entry in the group.
          # (The kanji and/or readings of an entry are sometimes changed
          # and this order will keep the changed entries together with
          # their pre-changed versions, while maintaining an overall
          # ordering by kanji/reading.)  Within each group having the 
          # same sequence number, entries are sorted in descending order
          # by the timestamp of the most recent history; that is, from
          # the most recently edited entry to the least recently edited
          # one. 
        entries.sort (key=lambda e: (
                e.SEQKR[0], e.SEQKR[1], 
                e.src, e.seq,  # In case different seqs have same SEQKR.
                  # e._hist[*].dt is a datatime.datetime instance.
                -(e._hist[-1].dt.timestamp() if e._hist else 0), 
                -e.id))
        for e in entries:
            for s in e._sens:
                if hasattr (s, '_xref'): jdb.augment_xrefs (cur, s._xref)
                if hasattr (s, '_xrer'): jdb.augment_xrefs (cur, s._xrer, 1)
            if hasattr (e, '_snd'): jdb.augment_snds (cur, e._snd)
        cur.close()
        disp = form.getfirst ('disp')
        if disp == 'xml':
            etxts = [fmtxml.entr (e) for e in entries]
        elif disp == 'jm':
            etxts = [fmtxml.entr (e, compat='jmdict') for e in entries]
        elif disp == 'jmne':
            etxts = [fmtxml.entr (e, compat='jmnedict') for e in entries]
        elif disp == 'jel':
            etxts = [fmtjel.entr (e) for e in entries]
        elif disp == 'ed':
            etxts = [xslfmt.entr (e) for e in entries]
        else:
            etxts = ['' for e in entries]
        jmcgi.htmlprep (entries)
        jmcgi.add_encodings (entries)    # For kanjidic entries.
        if disp == 'ed': etxts = [jmcgi.txt2html (x) for x in etxts]
        jmcgi.add_filtered_xrefs (entries, rem_unap=True)

        if errs: jmcgi.err_page (errs)

        jmcgi.jinja_page ('entr.jinja',
                        entries=list(zip(entries, etxts)), disp=disp,
                        svc=svc, dbg=dbg, sid=sid, session=sess, cfg=cfg,
                        parms=parms, this_page='entr.py')
示例#4
0
def main(args, opts):
    jdb.reset_encoding(sys.stdout, 'utf-8')
    errs = []
    entrs = []
    try:
        form, svc, dbg, cur, sid, sess, parms, cfg = jmcgi.parseform()
    except Exception as e:
        jmcgi.err_page([str(e)])

    fv = form.getfirst
    fl = form.getlist
    is_editor = jmcgi.is_editor(sess)
    dbg = fv('dbg')
    meth = fv('meth')
    def_corp = fv('c')  # Default corpus for new entries.
    defcorpid = None
    if def_corp:
        try:
            def_corp = int(def_corp)
        except ValueError:
            pass
        try:
            defcorpid = jdb.KW.SRC[def_corp].id
        except KeyError:
            errs.append("Bad url parameter: c=%s" % def_corp)
    force_corp = fv('f')  # Force default corpus for new entries.

    sentrs = fl("entr")
    for sentr in sentrs:
        try:
            entrs = serialize.unserialize(sentr)
        except Exception as e:
            errs.append("Bad 'entr' value, unable to unserialize: %s" % str(e))
        else:
            entrs.append(entr)

    jentrs = fl('j')
    for jentr in jentrs:
        try:
            entr = edparse.entr(jentr)
        except Exception as e:
            errs.append("Bad 'j' value, unable to parse: %s" % str(e))
        else:
            entr.src = None
            entrs.append(entr)

    elist, qlist, active = fl('e'), fl('q'), fv('a')
    if elist or qlist:
        entrs.extend(
            jmcgi.get_entrs(cur,
                            elist or [],
                            qlist or [],
                            errs,
                            active=active,
                            corpus=def_corp) or [])
    cur.close()

    if (elist or qlist or jentrs or sentrs) and not entrs:
        # The caller explictly specified and entry to edit but we
        # didn't find it (or them).  Rather than treating this as
        # though no entries were given and displaying a blank edit
        # form, show an error message.
        errs.append("No matching entries were found")
    if errs: jmcgi.err_page(errs)

    srcs = sorted(jdb.KW.recs('SRC'), key=lambda x: x.kw.lower())
    #srcs.insert (0, jdb.Obj (id=0, kw='', descr=''))
    if not entrs:
        # This is a blank new entry.
        # The following dummy entry will produce the default
        # text for new entries: no kanji, no reading, and sense
        # text "[1][n]".
        entr = jdb.Entr(
            _sens=[jdb.Sens(_pos=[jdb.Pos(kw=jdb.KW.POS['n'].id)])], src=None)
        entrs = [entr]
    for e in entrs:
        if not is_editor: remove_freqs(e)
        e.ISDELETE = (e.stat == jdb.KW.STAT['D'].id) or None
        # Provide a default corpus.
        if not e.src: e.src = defcorpid
        e.NOCORPOPT = force_corp

    if errs: jmcgi.err_page(errs)

    for e in entrs:
        e.ktxt = fmtjel.kanjs(e._kanj)
        e.rtxt = fmtjel.rdngs(e._rdng, e._kanj)
        e.stxt = fmtjel.senss(e._sens, e._kanj, e._rdng)

    if errs: jmcgi.err_page(errs)

    jmcgi.jinja_page('edform.jinja',
                     parms=parms,
                     extra={},
                     entrs=entrs,
                     srcs=srcs,
                     is_editor=is_editor,
                     svc=svc,
                     dbg=dbg,
                     sid=sid,
                     session=sess,
                     cfg=cfg,
                     this_page='edform.py')