def roundtrip(cur, intxt): jellex.lexreset(Lexer, intxt) entr = Parser.parse(intxt, lexer=Lexer) entr.src = 1 jelparse.resolv_xrefs(cur, entr) for s in entr._sens: jdb.augment_xrefs(cur, getattr(s, '_xref', [])) for s in entr._sens: jdb.add_xsens_lists(getattr(s, '_xref', [])) for s in entr._sens: jdb.mark_seq_xrefs(cur, getattr(s, '_xref', [])) outtxt = fmtjel.entr(entr, nohdr=True) return outtxt
def roundtrip(cur, intxt): # Since hg-180523-6b1a12 we use '\f' to separate the kanji, reading # and senses sections in JEL text used as input to jelparse() # rather than '\n' which was previously used. To avoid changing # all the test data that still uses '\n', we call secsepfix() to # replace the first two '\n's in the test data with '\f's to make # suitable for parsing. intxt = secsepfix(intxt) jellex.lexreset(Lexer, intxt) entr = Parser.parse(intxt, lexer=Lexer) entr.src = 1 jelparse.resolv_xrefs(cur, entr) for s in entr._sens: jdb.augment_xrefs(cur, getattr(s, '_xref', [])) for s in entr._sens: jdb.add_xsens_lists(getattr(s, '_xref', [])) for s in entr._sens: jdb.mark_seq_xrefs(cur, getattr(s, '_xref', [])) outtxt = fmtjel.entr(entr, nohdr=True) return outtxt
def _interactive(cur, lexer, parser): cnt = 0 instr = '' while 1: instr = _getinptext() if not instr: break jellex.lexreset(lexer, instr) try: result = parser.parse(instr, lexer=lexer, debug=opts.debug) except jelparse.ParseError as e: if not e.loc: msg = e.args[0] else: msg = "%s\n%s" % (e.args[0], e.loc) print(msg) continue try: jelparse.resolv_xrefs(cur, result) except ValueError: print(e) s = fmtjel.entr(result) print(s)
def main(args, opts): jdb.reset_encoding(sys.stdout, 'utf-8') errs = [] chklist = {} 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 KW = jdb.KW # 'eid' will be an integer if we are editing an existing # entry, or undefined if this is a new entry. pentr = None eid = url_int('id', form, errs) if eid: # Get the parent entry of the edited entry. This is what the # edited entry will be diff'd against for the history record. # It is also the entry that will be pointed to by the edited # entry's 'dfrm' field. pentr = jdb.entrList(cur, None, [eid]) #FIXME: Need a better message with more explanation. if not pentr: errs.append("The entry you are editing has been deleted.") else: pentr = pentr[0] # Desired disposition: 'a':approve, 'r':reject, undef:submit. disp = url_str('disp', form) if disp != 'a' and disp != 'r' and disp != '' and disp is not None: errs.append("Invalid 'disp' parameter: '%s'" % disp) # New status is A for edit of existing or new entry, D for # deletion of existing entry. delete = fv('delete') makecopy = fv('makecopy') if delete and makecopy: errs.append( "The 'delete' and 'treat as new'" " checkboxes are mutually exclusive; please select only one.") if makecopy: eid = None # FIXME: we need to disallow new entries with corp.seq # that matches an existing A, A*, R*, D*, D? entry. # Do same check in submit.py. seq = url_int('seq', form, errs) src = url_int('src', form, errs) notes = url_str('notes', form) srcnote = url_str('srcnote', form) # These are the JEL (JMdict Edit Language) texts which # we will concatenate into a string that is fed to the # JEL parser which will create an Entr object. kanj = (stripws(url_str('kanj', form))).strip() rdng = (stripws(url_str('rdng', form))).strip() sens = (url_str('sens', form)).strip() intxt = "\f".join((kanj, rdng, sens)) grpstxt = url_str('grp', form) # Get the meta-edit info which will go into the history # record for this change. comment = url_str('comment', form) refs = url_str('reference', form) name = url_str('name', form) email = url_str('email', form) if errs: jmcgi.err_page(errs) # Parse the entry data. Problems will be reported # by messages in 'perrs'. We do the parse even if # the request is to delete the entry (is this right # thing to do???) since on the edconf page we want # to display what the entry was. The edsubmit page # will do the actual deletion. entr, errs = parse(intxt) # 'errs' is a list which if not empty has a single item # which is a 2-seq of str's: (error-type, error-message). if errs or not entr: if not entr and not errs: errs = ([], "Unable to create an entry from your input.") jmcgi.err_page([errs[0][1]], prolog=errs[0][0], cssclass="errormsg") entr.dfrm = eid entr.unap = not disp # To display the xrefs and reverse xrefs in html, they # need to be augmented with additional info about their # targets. collect_refs() simply returns a list Xref # objects that are on the entr argument's .xref list # (forward xrefs) if rev not true, or the Xref objects # on the entr argument's ._xrer list (reverse xrefs) if # rev is true). This does not remove them from the entry # and is done simply for convenience so we can have # augment_xrefs() process them all in one shot. # augment_xrefs add an attribute, .TARG, to each Xref # object whose value is an Entr object for the entry the # xref points to if rev is not true, or the entry the xref # is from, if rev is true. These Entr objects can be used # to display info about the xref target or source such as # seq#, reading or kanji. See jdb.augment_xrefs() for details. # Note that <xrefs> and <xrers> below contain references # to the xrefs on the entries; thus the augmentation done # by jdb.augment_xrefs() alters the xref objects on those # entries. if pentr: x = jdb.collect_xrefs([pentr]) if x: jdb.augment_xrefs(cur, x) # Although we don't allow editing of an entry's reverse # xref, we still augment them (on the parent entry) # because we will display them. x = jdb.collect_xrefs([pentr], rev=True) if x: jdb.augment_xrefs(cur, x, rev=True) x = jdb.collect_xrefs([entr]) if x: jdb.augment_xrefs(cur, x) if delete: # Ignore any content changes made by the submitter by # restoring original values to the new entry. entr.seq = pentr.seq entr.src = pentr.src entr.stat = KW.STAT['D'].id entr.notes = pentr.notes entr.srcnote = pentr.srcnote entr._kanj = getattr(pentr, '_kanj', []) entr._rdng = getattr(pentr, '_rdng', []) entr._sens = getattr(pentr, '_sens', []) entr._snd = getattr(pentr, '_snd', []) entr._grp = getattr(pentr, '_grp', []) entr._cinf = getattr(pentr, '_cinf', []) else: # Migrate the entr details to the new entr object # which to this point has only the kanj/rdng/sens # info provided by jbparser. entr.seq = seq entr.src = src entr.stat = KW.STAT['A'].id entr.notes = notes entr.srcnote = srcnote entr._grp = jelparse.parse_grp(grpstxt) # This form and the JEL parser provide no way to change # some entry attributes such _cinf, _snd, reverse xrefs # and for non-editors, _freq. We need to copy these items # from the original entry to the new, edited entry to avoid # loosing them. The copy can be shallow since we won't be # changing the copied content. if pentr: if not jmcgi.is_editor(sess): jdb.copy_freqs(pentr, entr) if hasattr(pentr, '_cinf'): entr._cinf = pentr._cinf copy_snd(pentr, entr) # Copy the reverse xrefs that are on pentr to entr, # removing any that are no longer valid because they # refer to senses , readings or kanji no longer present # on the edited entry. Note that these have already # been augmented above. nuked_xrers = realign_xrers(entr, pentr) if nuked_xrers: chklist['xrers'] = format_for_warnings(nuked_xrers, pentr) # Add sound details so confirm page will look the same as the # original entry page. Otherwise, the confirm page will display # only the sound clip id(s). #FIXME? Should the following snd augmentation stuff be outdented # one level so that it is done in both the delete and non-delete # paths? snds = [] for s in getattr(entr, '_snd', []): snds.append(s) for r in getattr(entr, '_rdng', []): for s in getattr(r, '_snd', []): snds.append(s) if snds: jdb.augment_snds(cur, snds) # If any xrefs were given, resolve them to actual entries # here since that is the form used to store them in the # database. If any are unresolvable, an approriate error # is saved and will reported later. rslv_errs = jelparse.resolv_xrefs(cur, entr) if rslv_errs: chklist['xrslv'] = rslv_errs if errs: jmcgi.err_page(errs) # Append a new hist record details this edit. if not hasattr(entr, '_hist'): entr._hist = [] entr = jdb.add_hist(entr, pentr, sess.userid if sess else None, name, email, comment, refs, entr.stat == KW.STAT['D'].id) if not delete: check_for_errors(entr, errs) if errs: jmcgi.err_page(errs) pseq = pentr.seq if pentr else None check_for_warnings(cur, entr, pseq, chklist) # The following all expect a list of entries. jmcgi.add_filtered_xrefs([entr], rem_unap=False) serialized = serialize.serialize([entr]) jmcgi.htmlprep([entr]) entrs = [[entr, None]] # Package 'entr' as expected by entr.jinja. jmcgi.jinja_page("edconf.jinja", entries=entrs, serialized=serialized, chklist=chklist, disp=disp, parms=parms, svc=svc, dbg=dbg, sid=sid, session=sess, cfg=cfg, this_page='edconf.py')