def doedit (entr, hist, cmd): # entr -- A jdb.Entr() instance to be edited. # hist -- A jdb.Hist instance that will be edited (if the edit # is to add a comment of refs.) # cmd -- A Cmd instance that describes changes to be made to entry. # # Apply the change described by <cmd> to <entr> and /or <hist>. # # Should return True if <entr> or <hist> were actually changed, # False if not, but currently always retuns True. new = None if cmd.operand in ('kanj', 'rdng'): tlist = getattr (entr, '_'+cmd.operand) if cmd.new: if cmd.operand == 'kanj': new = jdb.Kanj (txt=cmd.new) else: new = jdb.Rdng (txt=cmd.new) edit (tlist, 'txt', cmd.old, new or cmd.new, cmd.operand, cmd.old, cmd.new) elif cmd.operand == 'gloss': tlist = getattr (getattr (entr, '_sens')[cmd.sens-1], '_'+cmd.operand) if cmd.new: new = jdb.Gloss (txt=cmd.new, lang=jdb.KW.LANG['eng'].id, ginf=jdb.KW.GINF['equ'].id) edit (tlist, 'txt', cmd.old, new or cmd.new, cmd.operand, cmd.old, cmd.new) elif cmd.operand in ('pos','misc','fld','dial'): tlist = getattr (getattr (entr, '_sens')[cmd.sens-1], '_'+cmd.operand) new, old = kw2id (cmd.operand, cmd.new, cmd.old) edit (tlist, 'kw', old, new, cmd.operand, cmd.old, cmd.new) elif cmd.operand == 'entr': if cmd.cmd == 'del': entr.stat = jdb.KW.STAT['D'].id elif cmd.operand == 'comment': hist.notes = cmd.new elif cmd.operand == 'refs': hist.refs = cmd.new else: raise ValueError (cmd.operand) return True #FIXME: how to determine if no change was made to entry?
def mkentr(jtxt, etxt): global Lnnum # Create an entry object to represent the "A" line text of the # example sentence. e = jdb.Entr(stat=KW.STAT_A, unap=False) e.srcnote = str(Lnnum) if jdb.jstr_reb(jtxt): e._rdng = [jdb.Rdng(txt=jtxt)] else: e._kanj = [jdb.Kanj(txt=jtxt)] e._sens = [ jdb.Sens( _gloss=[jdb.Gloss(txt=etxt, ginf=KW.GINF_equ, lang=KW.LANG_eng)]) ] return e
def mkentr (jtxt, etxt, kwds): global Lnnum # Create an entry object to represent the "A" line text of the # example sentence. e = jdb.Entr (stat=KW.STAT_A, unap=False) e.srcnote = str (Lnnum) # Each @$kwds item is a 2-array consisting of the kw # id number and optionally a note string. kws = [x[0] for x in kwds] sens_note = "; ".join ([x[1] for x in kwds if len(x)>1]) or None if jdb.jstr_reb (jtxt): e._rdng = [jdb.Rdng (txt=jtxt)] else: e._kanj = [jdb.Kanj (txt=jtxt)] e._sens = [jdb.Sens (notes=sens_note, _gloss=[jdb.Gloss (lang=KW.LANG_eng, ginf=KW.GINF_equ, txt=etxt)], _misc=[jdb.Misc (kw=x) for x in kws])] return e
def do_kanjs (self, elems, entr, fmap): if elems is None: return kanjs = []; dupchk = {} for ord, elem in enumerate (elems): txt = elem.find('keb').text if not jdb.unique (txt, dupchk): self.warn ("Duplicate keb text: '%s'" % txt); continue if not (jdb.jstr_keb (txt)): self.warn ("keb text '%s' not kanji." % txt) kanj = jdb.Kanj (kanj=ord+1, txt=txt) self.do_kws (elem.findall('ke_inf'), kanj, '_inf', 'KINF') for x in elem.findall ('ke_pri'): freqtuple = self.parse_freq (x.text, "ke_pri") if not freqtuple: continue klist = fmap[freqtuple][1] if not jdb.isin (kanj, klist): klist.append (kanj) else: self.freq_warn ("Duplicate", None, kanj, x.text) kanjs.append (kanj) if kanjs: entr._kanj = kanjs
def p_kanjitem_2(p): '''kanjitem : krtext taglists''' kanj = jdb.Kanj(txt=p[1]) err = bld_kanj (kanj, p[2]) if err: perror (p, err) p[0] = kanj
def p_kanjitem_1(p): '''kanjitem : krtext''' p[0] = jdb.Kanj(txt=p[1])
def do_chr(elem, srcid, langs): global Char # Process a <character> element. The element has been # parsed by the xml ElementTree parse and is in "elem". # "lineno" is the source file line number. chtxt = elem.find('literal').text Char = chtxt # For warning messages created by warn(). c = jdb.Chr(chr=chtxt, _cinf=[]) e = jdb.Entr(src=srcid, stat=KW.STAT_A, seq=jdb.uord(chtxt), unap=False, chr=c, _kanj=[jdb.Kanj(txt=chtxt)], _rdng=[], _sens=[], _krslv=[]) for x in elem.findall('codepoint/cp_value'): codepoint(x, c, chtxt) for x in elem.findall('radical/rad_value'): radical(x, c) x = None try: x = (elem.find('misc/freq')).text except: pass if x: if c.freq is not None: warn('Duplicate "freq" element ignored: %s' % x) else: c.freq = int(x) x = None try: x = (elem.find('misc/grade')).text except: pass if x: if c.grade is not None: warn('Duplicate "grade" element ignored: %s' % x) else: c.grade = int(x) for n, x in enumerate(elem.findall('misc/stroke_count')): strokes(x, n, c) rn = '\u3001'.join([x.text for x in elem.findall('misc/rad_name')]) if rn: c.radname = rn for x in elem.findall('reading_meaning'): reading_meaning(x, e._rdng, e._sens, c._cinf, langs) x = elem.find('dic_number') if x is not None: dicnum(x, c._cinf) x = elem.find('query_code') if x is not None: qcode(x, c._cinf) for x in elem.findall('misc/variant'): e._krslv.append(variant(x)) x = elem.find('misc/jlpt') if x is not None: jlptnum(x, c) return e