14
14
15
15
from abc import ABC
16
16
from abc import abstractmethod
17
- import os
17
+ from os .path import expandvars
18
+ from pathlib import Path
18
19
import pickle
20
+ from typing import Any
19
21
20
22
from rclpy .clock import Clock
21
23
from rclpy .clock import ClockType
@@ -28,12 +30,15 @@ def __init__(self) -> None:
28
30
self ._summary = "NoData"
29
31
self ._frame = {}
30
32
33
+ @property
31
34
def success (self ) -> bool :
32
35
return self ._success
33
36
37
+ @property
34
38
def summary (self ) -> str :
35
39
return self ._summary
36
40
41
+ @property
37
42
def frame (self ) -> dict :
38
43
return self ._frame
39
44
@@ -48,33 +53,45 @@ def set_frame(self) -> None:
48
53
49
54
class ResultWriter :
50
55
def __init__ (self , result_json_path : str , ros_clock : Clock , condition : dict ) -> None :
51
- # 拡張子を書き換える
52
- result_file = os .path .splitext (os .path .expandvars (result_json_path ))[0 ] + ".jsonl"
53
- self ._result_file = open (result_file , "w" ) # noqa
56
+ self ._result_path = self .create_jsonl_path (result_json_path )
57
+ self ._result_file = self ._result_path .open ("w" )
54
58
self ._ros_clock = ros_clock
55
59
self ._system_clock = Clock (clock_type = ClockType .SYSTEM_TIME )
56
- self .write_condition (condition )
57
- self .write_header ()
60
+ self .write_line ({"Condition" : condition })
61
+ self .write_line (self .get_header ())
62
+
63
+ @property
64
+ def result_path (self ) -> Path :
65
+ return self ._result_path
66
+
67
+ def create_jsonl_path (self , result_json_path : str ) -> Path :
68
+ # For compatibility with previous versions.
69
+ # If a json file name is passed, replace it with the filename + jsonl
70
+ original_path = Path (expandvars (result_json_path ))
71
+ return original_path .parent .joinpath (original_path .stem + ".jsonl" )
58
72
59
73
def close (self ) -> None :
60
74
self ._result_file .close ()
61
75
62
- def write_condition (self , condition : dict ) -> None :
63
- dict_condition = {"Condition" : condition }
64
- str_condition = json .dumps (dict_condition , ignore_nan = True ) + "\n "
65
- self ._result_file .write (str_condition )
76
+ def delete_result_file (self ) -> None :
77
+ self ._result_path .unlink ()
78
+
79
+ def write_line (self , write_obj : Any ) -> None :
80
+ str_record = json .dumps (write_obj , ignore_nan = True ) + "\n "
81
+ self ._result_file .write (str_record )
82
+
83
+ def write_result (self , result : ResultBase ) -> None :
84
+ self .write_line (self .get_result (result ))
66
85
67
- def write_header (self ) -> None :
86
+ def get_header (self ) -> dict :
68
87
system_time = self ._system_clock .now ()
69
- dict_header = {
88
+ return {
70
89
"Result" : {"Success" : False , "Summary" : "NoData" },
71
90
"Stamp" : {"System" : system_time .nanoseconds / pow (10 , 9 )},
72
91
"Frame" : {},
73
92
}
74
- str_header = json .dumps (dict_header , ignore_nan = True ) + "\n "
75
- self ._result_file .write (str_header )
76
93
77
- def write (self , result : ResultBase ) -> None :
94
+ def get_result (self , result : ResultBase ) -> dict :
78
95
system_time = self ._system_clock .now ()
79
96
time_dict = {"System" : system_time .nanoseconds / pow (10 , 9 )}
80
97
if self ._ros_clock .ros_time_is_active :
@@ -83,16 +100,14 @@ def write(self, result: ResultBase) -> None:
83
100
else :
84
101
time_dict ["ROS" ] = 0.0
85
102
86
- dict_record = {
87
- "Result" : {"Success" : result .success () , "Summary" : result .summary () },
103
+ return {
104
+ "Result" : {"Success" : result .success , "Summary" : result .summary },
88
105
"Stamp" : time_dict ,
89
- "Frame" : result .frame () ,
106
+ "Frame" : result .frame ,
90
107
}
91
- str_record = json .dumps (dict_record , ignore_nan = True ) + "\n "
92
- self ._result_file .write (str_record )
93
108
94
109
95
110
class PickleWriter :
96
- def __init__ (self , out_pkl_path : str , write_object ) -> None : # noqa
97
- with open ( os . path . expandvars (out_pkl_path ), "wb" ) as pkl_file :
111
+ def __init__ (self , out_pkl_path : str , write_object : Any ) -> None :
112
+ with Path ( expandvars (out_pkl_path )). open ( "wb" ) as pkl_file :
98
113
pickle .dump (write_object , pkl_file )
0 commit comments