Linter fixes

This commit is contained in:
deajan 2024-09-15 20:44:14 +02:00
parent 542da5b4c4
commit 09b669e9a8
4 changed files with 16 additions and 13 deletions

View file

@ -58,7 +58,7 @@ from npbackup.path_helper import CURRENT_DIR
from npbackup.__version__ import version_string
from npbackup.__debug__ import _DEBUG
from npbackup.restic_wrapper import ResticRunner
from npbackup.restic_wrapper import schema, MSGSPEC
from npbackup.restic_wrapper import schema, HAVE_MSGSPEC
logger = getLogger()
@ -193,13 +193,13 @@ def _make_treedata_from_json(ls_result: List[dict]) -> sg.TreeData:
"""
treedata = sg.TreeData()
count = 0
if not MSGSPEC:
if not HAVE_MSGSPEC:
logger.info(
"Using basic json representation for data which is slow and memory hungry. Consider using a newer OS that supports Python 3.8+"
)
for entry in ls_result:
# Make sure we drop the prefix '/' so sg.TreeData does not get an empty root
if MSGSPEC:
if HAVE_MSGSPEC:
entry.path = entry.path.lstrip("/")
if os.name == "nt":
# On windows, we need to make sure tree keys don't get duplicate because of lower/uppercase

View file

@ -30,12 +30,12 @@ from npbackup.restic_wrapper import schema
try:
import msgspec
MSGSPEC = True
HAVE_MSGSPEC = True
except ImportError:
# We may not have msgspec on Python 3.7
import json
MSGSPEC = False
HAVE_MSGSPEC = False
logger = getLogger()
@ -682,7 +682,7 @@ class ResticRunner:
}
if result:
if output:
if MSGSPEC:
if HAVE_MSGSPEC:
decoder = msgspec.json.Decoder()
ls_decoder = msgspec.json.Decoder(schema.LsNode)
is_first_line = True
@ -690,7 +690,7 @@ class ResticRunner:
for line in output.split("\n"):
if not line:
continue
if MSGSPEC:
if HAVE_MSGSPEC:
try:
if (
not is_first_line
@ -710,6 +710,7 @@ class ResticRunner:
js["result"] = False
else:
try:
# pylint: disable=E0601 (used-before-assignment)
js["output"].append(json.loads(line))
except json.JSONDecodeError as exc:
msg = f"JSON decode error: {exc} on content '{line}'"
@ -728,7 +729,7 @@ class ResticRunner:
js["reason"] = msg
self.write_logs(msg, level="error")
if output:
if MSGSPEC:
if HAVE_MSGSPEC:
try:
js["output"] = msgspec.json.decode(output)
except msgspec.DecodeError as exc:
@ -738,6 +739,7 @@ class ResticRunner:
js["output"] = {"data": output}
else:
try:
# pylint: disable=E0601 (used-before-assignment)
js["output"] = json.loads(output)
except json.JSONDecodeError as exc:
msg = f"JSON decode error: {exc} on output '{output}'"

View file

@ -17,7 +17,7 @@ try:
from msgspec import Struct
from enum import StrEnum
MSGSPEC = True
HAVE_MSGSPEC = True
except ImportError:
class Struct:
@ -29,7 +29,7 @@ except ImportError:
class StrEnum:
pass
MSGSPEC = False
HAVE_MSGSPEC = False
class LsNodeType(StrEnum):

View file

@ -18,11 +18,11 @@ from logging import getLogger
try:
import msgspec.json
MSGSPEC = True
HAVE_MSGSPEC = True
except ImportError:
import json
MSGSPEC = False
HAVE_MSGSPEC = False
import datetime
from npbackup.core.runner import NPBackupRunner
@ -78,9 +78,10 @@ def entrypoint(*args, **kwargs):
else:
logger.error(f"Operation finished")
else:
if MSGSPEC:
if HAVE_MSGSPEC:
print(msgspec.json.encode(result))
else:
# pylint: disable=E0601 (used-before-assignment)
print(json.dumps(result, default=serialize_datetime))
sys.exit(0)