12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- import hashlib
16
- import os
17
- import tempfile
18
15
import time
19
16
from typing import List , Optional , Tuple , TypeVar
20
17
@@ -164,19 +161,14 @@ def main(args) -> Optional[str]:
164
161
165
162
if args .interactive :
166
163
print ('Interactive mode...' )
167
- # Read last message that was sent if it exists in temp
168
- content = get_last_message_content (args .message_type )
169
164
# 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 ))
177
167
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 )
180
172
181
173
with DirectNode (args , node_name = args .node_name ) as node :
182
174
return publisher (
@@ -193,63 +185,7 @@ def main(args) -> Optional[str]:
193
185
args .keep_alive )
194
186
195
187
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 :
253
189
"""
254
190
Show a tui to edit a given message yaml.
255
191
@@ -263,17 +199,16 @@ def bottom_toolbar():
263
199
264
200
# Create key bindings for the prompt
265
201
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
271
206
272
207
# Show prompt to edit the message before sending it
273
208
return prompt (
274
209
"> " ,
275
210
multiline = True ,
276
- default = msg_str ,
211
+ default = default_msg_str ,
277
212
lexer = PygmentsLexer (YamlLexer ),
278
213
mouse_support = True ,
279
214
bottom_toolbar = bottom_toolbar ,
@@ -304,6 +239,7 @@ def parse_msg(msg_type: str, yaml_values: Optional[str] = None) -> Tuple[MsgType
304
239
raise RuntimeError ('The passed value needs to be a dictionary in YAML format' )
305
240
# Set all fields in the message to the provided values
306
241
try :
242
+ # Unfortunately, if you specifi
307
243
timestamp_fields = set_message_fields (
308
244
msg , values_dictionary , expand_header_auto = True , expand_time_now = True )
309
245
except Exception as e :
0 commit comments