params: code cleanup (#22744)

* cleanup params

* apply review

* continue

* use c_str

* cleanup filelock

* don't check return code of close()

* remove call_once

* cleanup params_pyx

* cleanup comment
old-commit-hash: 2773ff5aceb8874a9319c819ec3707705a00604d
This commit is contained in:
Dean Lee
2021-11-01 18:22:34 +08:00
committed by GitHub
parent e2e7e68978
commit 40b2bb9f65
4 changed files with 54 additions and 150 deletions
+7 -23
View File
@@ -14,7 +14,6 @@ cdef extern from "selfdrive/common/params.h":
ALL
cdef cppclass c_Params "Params":
c_Params() nogil
c_Params(string) nogil
string get(string, bool) nogil
bool getBool(string) nogil
@@ -34,36 +33,25 @@ class UnknownKeyName(Exception):
cdef class Params:
cdef c_Params* p
def __cinit__(self, d=None):
cdef string path
if d is None:
with nogil:
self.p = new c_Params()
else:
path = <string>d.encode()
with nogil:
self.p = new c_Params(path)
def __cinit__(self, d=""):
cdef string path = <string>d.encode()
with nogil:
self.p = new c_Params(path)
def __dealloc__(self):
del self.p
def clear_all(self, tx_type=None):
if tx_type is None:
tx_type = ParamKeyType.ALL
def clear_all(self, tx_type=ParamKeyType.ALL):
self.p.clearAll(tx_type)
def check_key(self, key):
key = ensure_bytes(key)
if not self.p.checkKey(key):
raise UnknownKeyName(key)
return key
def get(self, key, bool block=False, encoding=None):
cdef string k = self.check_key(key)
cdef string val
with nogil:
val = self.p.get(k, block)
@@ -76,10 +64,7 @@ cdef class Params:
else:
return None
if encoding is not None:
return val.decode(encoding)
else:
return val
return val if encoding is None else val.decode(encoding)
def get_bool(self, key):
cdef string k = self.check_key(key)
@@ -110,8 +95,7 @@ cdef class Params:
with nogil:
self.p.remove(k)
def put_nonblocking(key, val, d=None):
def put_nonblocking(key, val, d=""):
def f(key, val):
params = Params(d)
cdef string k = ensure_bytes(key)