Skip to content

Commit 65a6ebe

Browse files
committed
addressed reviews
1 parent e765db8 commit 65a6ebe

File tree

3 files changed

+17
-23
lines changed

3 files changed

+17
-23
lines changed

nodescraper/connection/inband/inband.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,35 +46,36 @@ class FileArtifact(BaseModel):
4646
filename: str
4747
contents: str | bytes = Field(exclude=True)
4848

49-
@staticmethod
50-
def coerce_contents(value: io.BytesIO | str | bytes) -> bytes:
49+
@field_validator("contents", mode="before")
50+
@classmethod
51+
def validate_contents(cls, value: io.BytesIO | str | bytes):
5152
if isinstance(value, io.BytesIO):
5253
return value.getvalue()
5354
if isinstance(value, str):
5455
return value.encode("utf-8")
5556
return value
5657

57-
@field_validator("contents", mode="before")
58-
@classmethod
59-
def contents_conformer(cls, value):
60-
return cls.coerce_contents(value)
61-
6258
def log_model(self, log_path: str, encoding: Optional[str] = None) -> None:
6359
"""Log the file contents to disk.
6460
6561
Args:
6662
log_path (str): path to write the file
67-
encoding (str | None): if None, writes as binary
63+
encoding (str | None): if None, auto-detect binary or not
6864
"""
6965
log_name = os.path.join(log_path, self.filename)
70-
contents = self.coerce_contents(self.contents)
66+
contents = self.contents
7167

72-
if encoding is None:
73-
with open(log_name, "wb") as f:
74-
f.write(contents)
75-
else:
68+
if encoding:
7669
with open(log_name, "w", encoding=encoding) as f:
7770
f.write(contents.decode(encoding))
71+
else:
72+
try:
73+
decoded = contents.decode("utf-8")
74+
with open(log_name, "w", encoding="utf-8") as f:
75+
f.write(decoded)
76+
except UnicodeDecodeError:
77+
with open(log_name, "wb") as f:
78+
f.write(contents)
7879

7980
def contents_str(self) -> str:
8081
"""Safe string representation of contents (for logs)."""

nodescraper/plugins/inband/nvme/nvme_collector.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def collect_data(
7171
return self.result, None
7272

7373
all_device_data = {}
74-
f_name = "telemetry_log"
74+
f_name = "telemetry_log.bin"
7575

7676
for dev in nvme_devices:
7777
device_data = {}
@@ -89,7 +89,7 @@ def collect_data(
8989
for key, cmd in commands.items():
9090
res = self._run_sut_cmd(cmd, sudo=True)
9191
if "--output-file" in cmd:
92-
_ = self._read_sut_file(filename=f_name, encoding=None)
92+
_ = self._read_sut_file(filename=f"{dev}_{f_name}", encoding=None)
9393
if res.exit_code == 0:
9494
device_data[key] = res.stdout
9595
else:

nodescraper/taskresulthooks/filesystemloghook.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,8 @@ def process_result(self, task_result: TaskResult, data: Optional[DataModel] = No
6262
for artifact in task_result.artifacts:
6363
if isinstance(artifact, FileArtifact):
6464
log_name = get_unique_filename(log_path, artifact.filename)
65-
file_path = os.path.join(log_path, log_name)
65+
artifact.log_model(log_path)
6666

67-
contents = artifact.contents
68-
if isinstance(contents, bytes):
69-
with open(file_path, "wb") as log_file:
70-
log_file.write(contents)
71-
else:
72-
with open(file_path, "w", encoding="utf-8") as log_file:
73-
log_file.write(contents)
7467
else:
7568
name = f"{pascal_to_snake(artifact.__class__.__name__)}s"
7669
if name in artifact_map:

0 commit comments

Comments
 (0)