11#!/usr/bin/env python
22
3- """@package dynamic_graph_bridge
3+ """@package dynamic_graph_bridge.
44
55@file
66@license License BSD-3-Clause
1111
1212"""
1313
14+ import atexit
15+ import code
16+
1417# Standard import.
1518import optparse
16- import os .path
17- import code
1819import os
19- import sys
20+ import os . path
2021import readline
21- import atexit
2222import signal
23+ import sys
24+ from pathlib import Path
25+
2326import rclpy
2427
2528# Used to connect to ROS services
2831
2932
3033def signal_handler (sig , frame ):
31- """
32- Catch Ctrl+C and quit.
33- """
34+ """Catch Ctrl+C and quit."""
3435 print ("" )
3536 print ("You pressed Ctrl+C! Closing ros client and shell." )
3637 rclpy .try_shutdown ()
3738 sys .exit (0 )
3839
3940
4041# Command history, auto-completetion and keyboard management
41- python_history = os . path .join (os .environ ["HOME" ], ".dg_python_history" )
42+ python_history = Path .join (os .environ ["HOME" ], ".dg_python_history" )
4243readline .parse_and_bind ("tab: complete" )
4344readline .set_history_length (100000 )
4445
4546
4647def save_history (histfile ):
47- """ Write the history of the user command in a file """
48+ """Write the history of the user command in a file. """
4849 readline .write_history_file (histfile )
4950
5051
5152"""
52- Read the current history if it exists and program its save upon the program end.
53+ Read the current history if it exists and program
54+ its save upon the program end.
5355"""
5456if hasattr (readline , "read_history_file" ):
5557 try :
5658 readline .read_history_file (python_history )
57- except IOError :
59+ except OSError :
5860 pass
5961 atexit .register (save_history , python_history )
6062
6163
6264class DynamicGraphInteractiveConsole (code .InteractiveConsole ):
63- """
64- For the subtilities please read https://docs.python.org/3/library/code.html
65- """
65+ """For the subtilities please read https://docs.python.org/3/library/code.html."""
6666
6767 def __init__ (self ):
68-
68+ """Create interpreter in ROS for DG interactive console."""
6969 # Create the python terminal
7070 code .InteractiveConsole .__init__ (self )
7171
@@ -78,19 +78,19 @@ def __init__(self):
7878 readline .set_completer (self .dg_completer .complete )
7979
8080 def runcode (self , code ):
81- """
82- Inherited from code.InteractiveConsole
81+ """Inherited from code.InteractiveConsole.
8382
84- We execute the code pushed in the cache `self.lines_pushed`. The code is
85- pushed whenever the user press enter during the interactive session.
83+ We execute the code pushed in the cache `self.lines_pushed`.
84+ The code is pushed whenever the user press enter during the
85+ interactive session.
8686 see https://docs.python.org/3/library/code.html
8787 """
8888 try :
8989 # we copy the line in a tmp var
9090 code_string = self .lines_pushed [:- 1 ]
91- result = self .ros_python_interpreter . run_python_command (
92- code_string
93- )
91+ rpi = self .ros_python_interpreter
92+ result = rpi . run_python_command ( code_string )
93+
9494 self .write (result )
9595 if not result .endswith ("\n " ):
9696 self .write ("\n " )
@@ -101,17 +101,15 @@ def runcode(self, code):
101101 return False
102102
103103 def runsource (self , source , filename = "<input>" , symbol = "single" ):
104- """
105- Inherited from code.InteractiveConsole
104+ """Inherited from code.InteractiveConsole.
106105
107106 see https://docs.python.org/3/library/code.html
108107 """
109108 try :
110109 c = code .compile_command (source , filename , symbol )
111110 if c :
112111 return self .runcode (c )
113- else :
114- return True
112+ return True
115113 except SyntaxError :
116114 self .showsyntaxerror ()
117115 self .lines_pushed = ""
@@ -123,11 +121,9 @@ def runsource(self, source, filename="<input>", symbol="single"):
123121 return False
124122
125123 def push (self , line ):
126- """
127- Upon pressing enter in the interactive shell the user "push" a string.
128- This method is then called with the string pushed.
129- We catch the string to send it via the rosservice.
130- """
124+ """Upon pressing enter in the interactive shell the user "push" a string."""
125+ """This method is then called with the string pushed.
126+ We catch the string to send it via the rosservice."""
131127 self .lines_pushed += line + "\n "
132128 return code .InteractiveConsole .push (self , line )
133129
@@ -142,14 +138,9 @@ def push(self, line):
142138
143139 if args [:]:
144140 infile = args [0 ]
145- response = dg_console .ros_python_interpreter .run_python_script (
146- os .path .abspath (infile )
147- )
148- print (
149- dg_console .ros_python_interpreter .run_python_command (
150- "print('File parsed')"
151- )
152- )
141+ rpi = dg_console .ros_python_interpreter
142+ response = rpi .run_python_script (Path .abspath (infile ))
143+ print (rpi .run_python_command ("print('File parsed')" ))
153144
154145 signal .signal (signal .SIGINT , signal_handler )
155146 dg_console .interact ("Interacting with remote python server." )
0 commit comments