def test014(_): e1 = Sens(notes='abcd', sens=2, entr=555) e2 = serialize.unserialize(serialize.serialize(e1)) _.assertEqual(type(e1), type(e2)) _.assertEqual(e1.entr, e2.entr) _.assertEqual(e1.sens, e2.sens) _.assertEqual(e1.notes, e2.notes)
def test012(_): e1 = Rdng(txt='あいうえお', rdng=2, entr=555) e2 = serialize.unserialize(serialize.serialize(e1)) _.assertEqual(type(e1), type(e2)) _.assertEqual(e1.entr, e2.entr) _.assertEqual(e1.rdng, e2.rdng) _.assertEqual(e1.txt, e2.txt)
def test013(_): e1 = Kanj(txt='田中さん', kanj=2, entr=555) e2 = serialize.unserialize(serialize.serialize(e1)) _.assertEqual(type(e1), type(e2)) _.assertEqual(e1.entr, e2.entr) _.assertEqual(e1.kanj, e2.kanj) _.assertEqual(e1.txt, e2.txt)
def test002(_): e1 = DbRow([555, 222, 2], ['id', 'seq', 'stat']) e2 = serialize.unserialize(serialize.serialize(e1)) _.assertEqual(type(e1), type(e2)) _.assertEqual(e1.id, e2.id) _.assertEqual(e1.seq, e2.seq) _.assertEqual(e1.stat, e2.stat)
def test001(_): e1 = Obj(id=555, seq=222, stat=2) e2 = serialize.unserialize(serialize.serialize(e1)) _.assertEqual(type(e1), type(e2)) _.assertEqual(e1.id, e2.id) _.assertEqual(e1.seq, e2.seq) _.assertEqual(e1.stat, e2.stat)
def test001(_): a = [3, 4, 5] b = jdb.Obj(x=a, y=a) b2 = serialize.unserialize(serialize.serialize(b)) _.assertEqual(a, b2.x) _.assertEqual(b2.x, b2.y) _.assertEqual(id(b2.x), id(b2.y))
def test015(_): e1 = Gloss(txt='abcd', sens=2, gloss=3, entr=555, lang=33) e2 = serialize.unserialize(serialize.serialize(e1)) _.assertEqual(type(e1), type(e2)) _.assertEqual(e1.entr, e2.entr) _.assertEqual(e1.sens, e2.sens) _.assertEqual(e1.gloss, e2.gloss) _.assertEqual(e1.lang, e2.lang)
def test002(_): a1 = [3, 4, 5] a2 = [3, 4, 5] b = jdb.Obj(x=a1, y=a2) b2 = serialize.unserialize(serialize.serialize(b)) _.assertEqual(a1, b2.x) _.assertEqual(b2.x, b2.y) _.assertNotEqual(id(b2.x), id(b2.y))
def test011(_): e1 = Entr(id=555, seq=222, stat=2) e2 = serialize.unserialize(serialize.serialize(e1)) _.assertEqual(type(e1), type(e2)) _.assertEqual(e1.id, e2.id) _.assertEqual(e1.seq, e2.seq) _.assertEqual(e1.stat, e2.stat) _.assertEqual(e1.unap, e2.unap) _.assertEqual(e1.notes, e2.notes)
def _rpc_handler(self, ev_id, fd): msg = RPC.read_one_msg(fd) if not msg: return True cmd, args = serialize.unserialize(msg) cmd = int(cmd) if cmd == commands.STOP: return False host_id = self.get_host_id(fd) return EV_REMOTE, host_id, cmd, args
def test101(_): e1 = Entr(id=555, seq=222, stat=2, _rdng=[Rdng(txt='あいうえお'), Rdng(txt='たちつてと')], _kanj=[Kanj(txt='田中さん')], _sens=[ Sens(_gloss=[Gloss(txt='abcd')]), Sens(_gloss=[Gloss( txt='abcd'), Gloss(txt='efg')]) ]) e2 = serialize.unserialize(serialize.serialize(e1)) _.assertEqual(e1, e2) _.assertEqual(e1._rdng[1].txt, e2._rdng[1].txt) _.assertEqual(e1._sens[1]._gloss[1].txt, e2._sens[1]._gloss[1].txt)
def __init__(self, self_addr, others_addrs): self.addrs = [self_addr] + others_addrs self.n_hosts = len(self.addrs) # host_id (fd) -> one-sided write connection to host self._hosts = defaultdict(client.Connection) self._server = server.Listener(*self_addr) # incoming fd -> host_id (fd) self._clients = defaultdict(int) self._poll = Poll() self._handlers = dict() for (ip, port) in self.addrs: host = client.Connection(ip, port) self._hosts[host.connect()] = host self.hosts_ids = self._hosts.keys() for cl in self._server.accept_clients(self.n_hosts): self.reg_for_poll(cl, self._rpc_handler, ev_id=EV_REMOTE) for host in self.hosts_ids: self.send_to(host, commands.ID, *self_addr) self.host_by_addr = { (conn.ip, conn.port): host_id for (host_id, conn) in self._hosts.items() } while len(self._clients) != self.n_hosts: fd = self._poll.wait_one() msg = RPC.read_one_msg(fd) cmd, addr = serialize.unserialize(msg) cmd = int(cmd) if cmd != commands.ID: raise RuntimeError("Initial message from host missed.") addr = tuple([addr[0], int(addr[1])]) self._clients[fd] = self.host_by_addr[addr]
def rt(_, seq): # Test round trip from entry object through # serialize.serialize, serialize.unserialize, back to # object. Compare input and output objects # by converting both to xml and comparing # text. (Watch out for order problems). # FIXME: reading database to slow, too volatile. # read from a test xml file instead. if not Cursor: globalSetup() # FIXME: don't hardwire corpus (aka src). sql = "SELECT id FROM entr WHERE seq=%s AND src=1" elist, r = jdb.entrList(Cursor, sql, [seq], ret_tuple=1) e1 = elist[0] jdb.augment_xrefs(Cursor, r['xref']) s = serialize.serialize(e1) e2 = serialize.unserialize(s) f1 = fmtxml.entr(e1) _.assert_(len(f1) > 40) # Sanity check to detect empty entry. f2 = fmtxml.entr(e2) _.assertEqual(f1, f2)
def main(args, opts): global Svc, Sid jdb.reset_encoding(sys.stdout, 'utf-8') errs = [] dbh = svc = None try: form, svc, dbg, dbh, sid, sess, parms, cfg = jmcgi.parseform() except ValueError as e: jmcgi.err_page([str(e)]) # Svc and Sid are used in function url() and are global in # in order to avoid having to pass them through several layers # of function calls. Svc, Sid = svc, sid L('cgi.edsubmit').debug("started: userid=%s, sid=%s" % (sess and sess.userid, sess and sess.id)) fv = form.getfirst # disp values: '': User submission, 'a': Approve. 'r': Reject; disp = fv('disp') or '' if not sess and disp: errs.append("Only registered editors can approve or reject entries") if errs: jmcgi.err_page(errs) try: entrs = serialize.unserialize(fv("entr")) except Exception: jmcgi.err_page(["Bad 'entr' parameter, unable to unserialize."]) added = [] # Clear any possible transactions begun elsewhere (e.g. by the # keyword table read in jdb.dbOpen()). Failure to do this will # cause the following START TRANSACTON command to fail with: # InternalError: SET TRANSACTION ISOLATION LEVEL must be # called before any query L('cgi.edsubmit.main').debug("starting transaction") dbh.connection.rollback() dbh.execute("START TRANSACTION ISOLATION LEVEL SERIALIZABLE") # FIXME: we unserialize the entr's xref's as they were resolved # by the edconf.py page. Should we check them again here? # If target entry was deleted in meantime, attempt to add # our entr to db will fail with obscure foreign key error. # Alternatively an edited version of target may have been # created which wont have our xref pointing to it as it should. for entr in entrs: # FIXME: submission() can raise a psycopg2 # TransactionRollbackError if there is a serialization # error resulting from a concurrent update. Detecting # such a condition is why run with serializable isolation # level. We need to trap it and present some sensible # error message. e = submission(dbh, entr, disp, errs, jmcgi.is_editor(sess), sess.userid if sess else None) # The value returned by submission() is a 3-tuple consisting # of (id, seq, src) for the added entry. if e: added.append(e) if errs: L('cgi.edsubmit.main').info("rolling back transaction due to errors") dbh.connection.rollback() jmcgi.err_page(errs) else: L('cgi.edsubmit.main').info("doing commit") dbh.connection.commit() jmcgi.jinja_page("submitted.jinja", added=added, parms=parms, svc=svc, dbg=dbg, sid=sid, session=sess, cfg=cfg, this_page='edsubmit.py') L('cgi.edsubmit.main').debug("thank you page sent, exiting normally")
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')