1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- import hashlib
16- import os
17- import tempfile
1815import time
1916from typing import List , Optional , Tuple , TypeVar
2017
@@ -164,19 +161,14 @@ def main(args) -> Optional[str]:
164161
165162 if args .interactive :
166163 print ('Interactive mode...' )
167- # Read last message that was sent if it exists in temp
168- content = get_last_message_content (args .message_type )
169164 # Show the tui
170- orig_msg , orig_timestamp_fields = parse_msg (args .message_type , content )
171- default_msg , default_timestamp_fields = parse_msg (args .message_type )
172- content = show_interactive_tui (message_to_yaml (orig_msg ), message_to_yaml (default_msg ))
173- # Load msg YAML now to be sure it does not fail and we store a broken message
174- msg , timestamp_fields = parse_msg (args .message_type , content )
175- # Store the user input so we are able to load it the next time
176- store_message_content (args .message_type , content )
165+ default_msg , default_timestamp_fields = parse_msg (args .message_type , args .values )
166+ content = show_interactive_tui (message_to_yaml (default_msg ))
177167 else :
178- # Parse the yaml string and get a message object of the desired type
179- msg , timestamp_fields = parse_msg (args .message_type , content )
168+ content = args .values
169+
170+ # Parse the yaml string and get a message object of the desired type
171+ msg , timestamp_fields = parse_msg (args .message_type , content )
180172
181173 with DirectNode (args , node_name = args .node_name ) as node :
182174 return publisher (
@@ -193,63 +185,7 @@ def main(args) -> Optional[str]:
193185 args .keep_alive )
194186
195187
196- def get_history_file (msg_type : str ) -> str :
197- """
198- Get paths for semi persistent history based on message name.
199-
200- :param msg_type: Name of the message type
201- :returns: The path where a history file would be located if it exists
202- """
203- # Get temporary directory name
204- msg_history_cache_folder_path = os .path .join (
205- tempfile .gettempdir (), "ros_interactive_msg_cache" )
206- # Create temporary history dir if needed
207- os .makedirs (msg_history_cache_folder_path , exist_ok = True )
208- # Create a file based on the message name
209- return os .path .join (
210- msg_history_cache_folder_path ,
211- f'{ hashlib .sha224 (msg_type .encode ()).hexdigest ()[:20 ]} .yaml' )
212-
213-
214- def get_last_message_content (msg_type : str ) -> str :
215- """
216- Retrieve the last message of the given type that was sent using the tui if it exists.
217-
218- :param msg_type: Name of the message type
219- :returns: The YAML representation containing the last message or an empty dict
220- """
221- content = "{}"
222- try :
223- history_path = get_history_file (msg_type )
224- # Load previous values for that message type
225- if os .path .exists (history_path ):
226- with open (history_path , 'r' ) as f :
227- content = f .read ()
228- except OSError :
229- print ('Unable load history...' )
230- return content
231-
232-
233- def store_message_content (msg_type : str , content : str ) -> None :
234- """
235- Store the YAML for the current message in a semi persistent file.
236-
237- :param msg_type: Name of the message type
238- :param content: The YAML entered by the user
239- """
240- try :
241- history_path = get_history_file (msg_type )
242- # Clear cache
243- if os .path .exists (history_path ):
244- os .remove (history_path )
245- # Store last message in cache
246- with open (history_path , 'w' ) as f :
247- f .write (content )
248- except OSError :
249- print ('Unable to store history' )
250-
251-
252- def show_interactive_tui (msg_str : str , default_msg_str : Optional [str ] = None ) -> str :
188+ def show_interactive_tui (default_msg_str : str ) -> str :
253189 """
254190 Show a tui to edit a given message yaml.
255191
@@ -263,17 +199,16 @@ def bottom_toolbar():
263199
264200 # Create key bindings for the prompt
265201 bindings = KeyBindings ()
266- if default_msg_str is not None :
267- @bindings .add ('c-r' )
268- def _ (event ):
269- """Reset the promt to the default message."""
270- event .app .current_buffer .text = default_msg_str
202+ @bindings .add ('c-r' )
203+ def _ (event ):
204+ """Reset the promt to the default message."""
205+ event .app .current_buffer .text = default_msg_str
271206
272207 # Show prompt to edit the message before sending it
273208 return prompt (
274209 "> " ,
275210 multiline = True ,
276- default = msg_str ,
211+ default = default_msg_str ,
277212 lexer = PygmentsLexer (YamlLexer ),
278213 mouse_support = True ,
279214 bottom_toolbar = bottom_toolbar ,
@@ -304,6 +239,7 @@ def parse_msg(msg_type: str, yaml_values: Optional[str] = None) -> Tuple[MsgType
304239 raise RuntimeError ('The passed value needs to be a dictionary in YAML format' )
305240 # Set all fields in the message to the provided values
306241 try :
242+ # Unfortunately, if you specifi
307243 timestamp_fields = set_message_fields (
308244 msg , values_dictionary , expand_header_auto = True , expand_time_now = True )
309245 except Exception as e :
0 commit comments