Example #1
0
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)
Example #2
0
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)
Example #3
0
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())
Example #4
0
 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)
Example #5
0
 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)
Example #6
0
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())
Example #7
0
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())
Example #8
0
 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)
Example #9
0
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())
Example #10
0
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())
Example #11
0
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())
Example #12
0
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())
Example #13
0
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())
Example #14
0
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())
Example #15
0
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())
Example #16
0
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())
Example #17
0
    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)
Example #18
0
    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)
Example #19
0
def test_lazy_str():
    f = StringIO()
    dump('wakka', f)
    f.seek(0)
    lj = LazyJSON(f)
    assert_equal('wakka', lj.load())
Example #20
0
def test_lazy_int():
    f = StringIO()
    dump(42, f)
    f.seek(0)
    lj = LazyJSON(f)
    assert_equal(42, lj.load())
Example #21
0
def test_lazy_int():
    f = StringIO()
    dump(42, f)
    f.seek(0)
    lj = LazyJSON(f)
    assert_equal(42, lj.load())
Example #22
0
def test_lazy_str():
    f = StringIO()
    dump('wakka', f)
    f.seek(0)
    lj = LazyJSON(f)
    assert_equal('wakka', lj.load())
Example #23
0
File: hist.py Project: aig787/xonsh
 def close_history(self):
     with open(self.hf, 'w+') as fp:
         lazyjson.dump(self.ordered_history, fp)