def test_lazy_load_index(): f = StringIO() dump({'wakka': 42}, f) f.seek(0) lj = LazyJSON(f) assert_equal({'wakka': 10, '__total__': 0}, lj.offsets) assert_equal({'wakka': 2, '__total__': 14}, lj.sizes)
def test_lazy_list_empty(): x = [] f = StringIO() dump(x, f) f.seek(0) lj = LazyJSON(f) assert_equal(0, len(lj)) assert_equal(x, lj.load())
def dump(self): with open(self.filename, "r", newline="\n") as f: hist = lazyjson.LazyJSON(f).load() hist["cmds"].extend(self.buffer) if self.at_exit: hist["ts"][1] = time.time() # apply end time hist["locked"] = False with open(self.filename, "w", newline="\n") as f: lazyjson.dump(hist, f, sort_keys=True)
def dump(self): with open(self.filename, 'r', newline='\n') as f: hist = lazyjson.LazyJSON(f).load() hist['cmds'].extend(self.buffer) if self.at_exit: hist['ts'][1] = time.time() # apply end time hist['locked'] = False with open(self.filename, 'w', newline='\n') as f: lazyjson.dump(hist, f, sort_keys=True)
def test_lazy_dict(): f = StringIO() dump({'wakka': 42}, f) f.seek(0) lj = LazyJSON(f) assert_equal(['wakka'], list(lj.keys())) assert_equal(42, lj['wakka']) assert_equal(1, len(lj)) assert_equal({'wakka': 42}, lj.load())
def test_lazy_list_list_ints(): x = [[0, 1], [6, 28], [496, 8128]] f = StringIO() dump(x, f) f.seek(0) lj = LazyJSON(f) assert_is_instance(lj[1], Node) assert_equal(28, lj[1][1]) assert_equal([6, 28], lj[1].load()) assert_equal(x, lj.load())
def test_lazy_list_str(): x = ['I', 'have', 'seen', 'the', 'wind', 'blow'] f = StringIO() dump(x, f) f.seek(0) lj = LazyJSON(f) assert_equal('the', lj[3]) assert_equal(x[:2:-2], lj[:2:-2]) assert_equal(x, [_ for _ in lj]) assert_equal(x, lj.load())
def test_lazy_list_ints(): x = [0, 1, 6, 28, 496, 8128] f = StringIO() dump(x, f) f.seek(0) lj = LazyJSON(f) assert_equal(28, lj[3]) assert_equal(x[:2:-2], lj[:2:-2]) assert_equal(x, [_ for _ in lj]) assert_equal(x, lj.load())
def test_lazy_dict_dict_int(): x = {'wakka': {'jawaka': 42}} f = StringIO() dump(x, f) f.seek(0) lj = LazyJSON(f) assert_equal(['wakka'], list(lj.keys())) assert_is_instance(lj['wakka'], Node) assert_equal(42, lj['wakka']['jawaka']) assert_equal(1, len(lj)) assert_equal(x, lj.load())
def __init__(self, filename=None, sessionid=None, buffersize=100, gc=True, **meta): """Represents a xonsh session's history as an in-memory buffer that is periodically flushed to disk. Parameters ---------- filename : str, optional Location of history file, defaults to ``$XONSH_DATA_DIR/xonsh-{sessionid}.json``. sessionid : int, uuid, str, optional Current session identifier, will generate a new sessionid if not set. buffersize : int, optional Maximum buffersize in memory. meta : optional Top-level metadata to store along with the history. The kwargs 'cmds' and 'sessionid' are not allowed and will be overwritten. gc : bool, optional Run garbage collector flag. """ self.sessionid = sid = uuid.uuid4() if sessionid is None else sessionid if filename is None: # pylint: disable=no-member data_dir = builtins.__xonsh_env__.get('XONSH_DATA_DIR') self.filename = os.path.join(data_dir, 'xonsh-{0}.json'.format(sid)) else: self.filename = filename self.buffer = [] self.buffersize = buffersize self._queue = deque() self._cond = Condition() self._len = 0 self.last_cmd_out = None self.last_cmd_rtn = None meta['cmds'] = [] meta['sessionid'] = str(sid) with open(self.filename, 'w', newline='\n') as f: lazyjson.dump(meta, f, sort_keys=True) self.gc = HistoryGC() if gc else None # command fields that are known self.tss = CommandField('ts', self) self.inps = CommandField('inp', self) self.outs = CommandField('out', self) self.rtns = CommandField('rtn', self)
def __init__(self, filename=None, sessionid=None, buffersize=100, gc=True, **meta): """Represents a xonsh session's history as an in-memory buffer that is periodically flushed to disk. Parameters ---------- filename : str, optional Location of history file, defaults to ``$XONSH_DATA_DIR/xonsh-{sessionid}.json``. sessionid : int, uuid, str, optional Current session identifier, will generate a new sessionid if not set. buffersize : int, optional Maximum buffersize in memory. meta : optional Top-level metadata to store along with the history. The kwargs 'cmds' and 'sessionid' are not allowed and will be overwritten. gc : bool, optional Run garbage collector flag. """ self.sessionid = sid = uuid.uuid4() if sessionid is None else sessionid if filename is None: # pylint: disable=no-member data_dir = builtins.__xonsh_env__.get('XONSH_DATA_DIR') data_dir = os.path.expanduser(data_dir) self.filename = os.path.join(data_dir, 'xonsh-{0}.json'.format(sid)) else: self.filename = filename self.buffer = [] self.buffersize = buffersize self._queue = deque() self._cond = Condition() self._len = 0 self.last_cmd_out = None self.last_cmd_rtn = None meta['cmds'] = [] meta['sessionid'] = str(sid) with open(self.filename, 'w', newline='\n') as f: lazyjson.dump(meta, f, sort_keys=True) self.gc = HistoryGC() if gc else None # command fields that are known self.tss = CommandField('ts', self) self.inps = CommandField('inp', self) self.outs = CommandField('out', self) self.rtns = CommandField('rtn', self)
def test_lazy_str(): f = StringIO() dump('wakka', f) f.seek(0) lj = LazyJSON(f) assert_equal('wakka', lj.load())
def test_lazy_int(): f = StringIO() dump(42, f) f.seek(0) lj = LazyJSON(f) assert_equal(42, lj.load())
def close_history(self): with open(self.hf, 'w+') as fp: lazyjson.dump(self.ordered_history, fp)