added BufferedJSON
This commit is contained in:
35
dap/utils/bufjson.py
Normal file
35
dap/utils/bufjson.py
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import os
|
||||||
|
from time import sleep
|
||||||
|
|
||||||
|
from .utils import json_load
|
||||||
|
|
||||||
|
|
||||||
|
class BufferedJSON:
|
||||||
|
|
||||||
|
def __init__(self, fname):
|
||||||
|
self.fname = fname
|
||||||
|
self.last_time = self.get_time()
|
||||||
|
self.last_data = self.get_data()
|
||||||
|
|
||||||
|
|
||||||
|
def load(self):
|
||||||
|
current_time = self.get_time()
|
||||||
|
time_delta = current_time - self.last_time
|
||||||
|
if time_delta <= 2: #TODO: is that a good time?
|
||||||
|
return self.last_data
|
||||||
|
|
||||||
|
sleep(0.5) #TODO: why?
|
||||||
|
current_data = self.get_data()
|
||||||
|
self.last_time = current_time
|
||||||
|
self.last_data = current_data
|
||||||
|
return current_data
|
||||||
|
|
||||||
|
|
||||||
|
def get_time(self):
|
||||||
|
return os.path.getmtime(self.fname)
|
||||||
|
|
||||||
|
def get_data(self, *args, **kwargs):
|
||||||
|
return json_load(self.fname, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user