lint: app.cli.db passes pylint
This commit is contained in:
parent
9674cb4293
commit
3610707495
1 changed files with 13 additions and 16 deletions
|
@ -45,18 +45,16 @@ models = [
|
||||||
class ExportEncoder(json.JSONEncoder):
|
class ExportEncoder(json.JSONEncoder):
|
||||||
"""Encoder to serialise all types used in the database."""
|
"""Encoder to serialise all types used in the database."""
|
||||||
|
|
||||||
def default(self, obj: Any) -> Any:
|
def default(self, o: Any) -> Any:
|
||||||
if isinstance(obj, AlarmState):
|
if isinstance(o, AlarmState):
|
||||||
return obj.name
|
return o.name
|
||||||
elif isinstance(obj, AutomationState):
|
if isinstance(o, AutomationState):
|
||||||
return obj.name
|
return o.name
|
||||||
elif isinstance(obj, bytes):
|
if isinstance(o, bytes):
|
||||||
return base64.encodebytes(obj).decode('utf-8')
|
return base64.encodebytes(o).decode('utf-8')
|
||||||
elif isinstance(obj, (datetime.datetime, datetime.date, datetime.time)):
|
if isinstance(o, (datetime.datetime, datetime.date, datetime.time)):
|
||||||
return obj.isoformat()
|
return o.isoformat()
|
||||||
elif isinstance(obj, datetime.timedelta):
|
return super().default(o)
|
||||||
return (datetime.datetime.min + obj).time().isoformat()
|
|
||||||
return super().default(obj)
|
|
||||||
|
|
||||||
|
|
||||||
def model_to_dict(model: db.Model) -> Dict[str, Any]: # type: ignore[name-defined]
|
def model_to_dict(model: db.Model) -> Dict[str, Any]: # type: ignore[name-defined]
|
||||||
|
@ -80,10 +78,9 @@ decoder = {
|
||||||
"AlarmState": lambda x: AlarmState.__getattribute__(AlarmState, x),
|
"AlarmState": lambda x: AlarmState.__getattribute__(AlarmState, x),
|
||||||
"AutomationState": lambda x: AutomationState.__getattribute__(AutomationState, x),
|
"AutomationState": lambda x: AutomationState.__getattribute__(AutomationState, x),
|
||||||
"bytes": lambda x: base64.decodebytes(x.encode('utf-8')),
|
"bytes": lambda x: base64.decodebytes(x.encode('utf-8')),
|
||||||
"datetime": lambda x: datetime.datetime.fromisoformat(x),
|
"datetime": datetime.datetime.fromisoformat,
|
||||||
"int": lambda x: int(x),
|
"int": int,
|
||||||
"str": lambda x: x,
|
"str": lambda x: x,
|
||||||
# TODO: timedelta (not currently used but could be in the future)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -92,7 +89,7 @@ def db_import_model(model: db.Model, data: List[Dict[str, Any]]) -> None: # typ
|
||||||
new = model()
|
new = model()
|
||||||
for col in row:
|
for col in row:
|
||||||
type_name, col_name = col.split("_", 1)
|
type_name, col_name = col.split("_", 1)
|
||||||
new.__setattr__(col_name, decoder.get(type_name, lambda x: x)(row[col])) # type: ignore[no-untyped-call]
|
setattr(new, col_name, decoder.get(type_name, lambda x: x)(row[col])) # type: ignore[no-untyped-call]
|
||||||
db.session.add(new)
|
db.session.add(new)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue