added lazy command fields to history

This commit is contained in:
Anthony Scopatz 2015-08-16 21:41:02 -04:00
parent 004be145e2
commit 70e87e37cf
2 changed files with 26 additions and 5 deletions

View file

@ -5,8 +5,8 @@ import os
import nose
from nose.tools import assert_equal, assert_true
from xonsh.history import History
from xonsh.lazyjson import LazyJSON
from xonsh.history import History, CommandField
HIST_TEST_KWARGS = dict(sessionid='SESSIONID', gc=False)
@ -48,6 +48,25 @@ def test_hist_flush():
os.remove(FNAME)
def test_cmd_field():
FNAME = 'xonsh-SESSIONID.json'
FNAME += '.cmdfield'
hist = History(filename=FNAME, here='yup', **HIST_TEST_KWARGS)
# in-memory
hf = hist.append({'rtn': 1})
yield assert_true, hf is None
yield assert_equal, 1, hist.rtns[0]
yield assert_equal, 1, hist.rtns[-1]
yield assert_equal, None, hist.outs[-1]
# slice
yield assert_equal, [1], hist.rtns[:]
# on disk
hf = hist.flush()
yield assert_true, hf is not None
yield assert_equal, 1, hist.rtns[0]
yield assert_equal, 1, hist.rtns[-1]
yield assert_equal, None, hist.outs[-1]
os.remove(FNAME)
if __name__ == '__main__':
nose.runmodule()

View file

@ -98,6 +98,7 @@ class HistoryFlusher(Thread):
self.queue.popleft()
def i_am_at_the_front(self):
"""Tests if the flusher is at the front of the queue."""
return self is self.queue[0]
def dump(self):
@ -146,10 +147,10 @@ class CommandField(Sequence):
if size - bufsize <= key: # key is in buffer
return self.hist.buffer[key + bufsize - size].get(self.field, self.default)
# now we know we have to go into the file
queue = self.hist.queue
queue = self.hist._queue
queue.append(self)
with self.hist.cond as cond:
cond.wait_for(self.i_am_at_the_front)
with self.hist._cond:
self.hist._cond.wait_for(self.i_am_at_the_front)
with open(self.hist.filename, 'r', newline='\n') as f:
lj = lazyjson.LazyJSON(f, reopen=False)
rtn = lj['cmds'][key].get(self.field, self.default)
@ -159,7 +160,8 @@ class CommandField(Sequence):
return rtn
def i_am_at_the_front(self):
return self is self.hist.queue[0]
"""Tests if the command field is at the front of the queue."""
return self is self.hist._queue[0]
class History(object):