| 
						
						
							
								
							
						
						
					 | 
					 | 
					@ -263,23 +263,48 @@ class DBWriter(DBAccessor): | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      self._lock = None | 
					 | 
					 | 
					 | 
					      self._lock = None | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					def read_db(params_path, key): | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					  path = "%s/d/%s" % (params_path, key) | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					  try: | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    with open(path, "rb") as f: | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					      return f.read() | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					  except IOError: | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    return None | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					class JSDB(object): | 
					 | 
					 | 
					 | 
					def write_db(params_path, key, value): | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  def __init__(self, fn): | 
					 | 
					 | 
					 | 
					  lock = FileLock(params_path+"/.lock", True) | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    self._fn = fn | 
					 | 
					 | 
					 | 
					  lock.acquire() | 
				
			
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  def begin(self, write=False): | 
					 | 
					 | 
					 | 
					  try: | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    if write: | 
					 | 
					 | 
					 | 
					    tmp_path = tempfile.mktemp(prefix=".tmp", dir=params_path) | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      return DBWriter(self._fn) | 
					 | 
					 | 
					 | 
					    with open(tmp_path, "wb") as f: | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    else: | 
					 | 
					 | 
					 | 
					      f.write(value) | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      return DBReader(self._fn) | 
					 | 
					 | 
					 | 
					      f.flush() | 
				
			
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					      os.fsync(f.fileno()) | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    path = "%s/d/%s" % (params_path, key) | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    os.rename(tmp_path, path) | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    fsync_dir(os.path.dirname(path)) | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					  finally: | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    lock.release() | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					class Params(object): | 
					 | 
					 | 
					 | 
					class Params(object): | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  def __init__(self, db='/data/params'): | 
					 | 
					 | 
					 | 
					  def __init__(self, db='/data/params'): | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    self.env = JSDB(db) | 
					 | 
					 | 
					 | 
					    self.db = db | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    # create the database if it doesn't exist... | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    if not os.path.exists(self.db+"/d"): | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					      with self.transaction(write=True): | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					        pass | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					  def transaction(self, write=False): | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    if write: | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					      return DBWriter(self.db) | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    else: | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					      return DBReader(self.db) | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  def _clear_keys_with_type(self, tx_type): | 
					 | 
					 | 
					 | 
					  def _clear_keys_with_type(self, tx_type): | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    with self.env.begin(write=True) as txn: | 
					 | 
					 | 
					 | 
					    with self.transaction(write=True) as txn: | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      for key in keys: | 
					 | 
					 | 
					 | 
					      for key in keys: | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        if keys[key] == tx_type: | 
					 | 
					 | 
					 | 
					        if keys[key] == tx_type: | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					          txn.delete(key) | 
					 | 
					 | 
					 | 
					          txn.delete(key) | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
					 | 
					@ -291,7 +316,7 @@ class Params(object): | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    self._clear_keys_with_type(TxType.CLEAR_ON_CAR_START) | 
					 | 
					 | 
					 | 
					    self._clear_keys_with_type(TxType.CLEAR_ON_CAR_START) | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  def delete(self, key): | 
					 | 
					 | 
					 | 
					  def delete(self, key): | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    with self.env.begin(write=True) as txn: | 
					 | 
					 | 
					 | 
					    with self.transaction(write=True) as txn: | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      txn.delete(key) | 
					 | 
					 | 
					 | 
					      txn.delete(key) | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  def get(self, key, block=False): | 
					 | 
					 | 
					 | 
					  def get(self, key, block=False): | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
					 | 
					@ -299,8 +324,7 @@ class Params(object): | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      raise UnknownKeyName(key) | 
					 | 
					 | 
					 | 
					      raise UnknownKeyName(key) | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    while 1: | 
					 | 
					 | 
					 | 
					    while 1: | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      with self.env.begin() as txn: | 
					 | 
					 | 
					 | 
					      ret = read_db(self.db, key) | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        ret = txn.get(key) | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      if not block or ret is not None: | 
					 | 
					 | 
					 | 
					      if not block or ret is not None: | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        break | 
					 | 
					 | 
					 | 
					        break | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      # is polling really the best we can do? | 
					 | 
					 | 
					 | 
					      # is polling really the best we can do? | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
					 | 
					@ -311,8 +335,7 @@ class Params(object): | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    if key not in keys: | 
					 | 
					 | 
					 | 
					    if key not in keys: | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      raise UnknownKeyName(key) | 
					 | 
					 | 
					 | 
					      raise UnknownKeyName(key) | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    with self.env.begin(write=True) as txn: | 
					 | 
					 | 
					 | 
					    write_db(self.db, key, dat) | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      txn.put(key, dat) | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					if __name__ == "__main__": | 
					 | 
					 | 
					 | 
					if __name__ == "__main__": | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  params = Params() | 
					 | 
					 | 
					 | 
					  params = Params() | 
				
			
			
		
	
	
		
		
			
				
					| 
						
							
								
							
						
						
						
					 | 
					 | 
					
  |