Package rospy :: Module rosconsole

Source Code for Module rospy.rosconsole

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2015, Chris Mansley, Open Source Robotics Foundation, Inc. 
  4  # All rights reserved. 
  5  # 
  6  # Redistribution and use in source and binary forms, with or without 
  7  # modification, are permitted provided that the following conditions 
  8  # are met: 
  9  # 
 10  #  * Redistributions of source code must retain the above copyright 
 11  #    notice, this list of conditions and the following disclaimer. 
 12  #  * Redistributions in binary form must reproduce the above 
 13  #    copyright notice, this list of conditions and the following 
 14  #    disclaimer in the documentation and/or other materials provided 
 15  #    with the distribution. 
 16  #  * Neither the name of Willow Garage, Inc. nor the names of its 
 17  #    contributors may be used to endorse or promote products derived 
 18  #    from this software without specific prior written permission. 
 19  # 
 20  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 21  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 22  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 23  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 24  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 25  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 26  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 27  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 28  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 29  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 30  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  # POSSIBILITY OF SUCH DAMAGE. 
 32   
 33  from __future__ import print_function 
 34   
 35  import os 
 36  import socket 
 37  import sys 
 38   
 39  import rosgraph 
 40  import rospy 
 41   
 42  from .logger_level_service_caller import LoggerLevelServiceCaller 
 43  from .logger_level_service_caller import ROSConsoleException 
 44   
 45  NAME = 'rosconsole' 
 46   
 47   
48 -def error(status, msg):
49 print("%s: error: %s" % (NAME, msg), file=sys.stderr) 50 sys.exit(status)
51 52
53 -def _get_cmd_list_optparse():
54 from optparse import OptionParser 55 56 usage = "usage: %prog list <node>" 57 parser = OptionParser(usage=usage, prog=NAME) 58 59 return parser
60 61
62 -def _rosconsole_cmd_list(argv):
63 args = argv[2:] 64 parser = _get_cmd_list_optparse() 65 (options, args) = parser.parse_args(args) 66 67 if not args: 68 parser.error("you must specify a node to list loggers") 69 elif len(args) > 1: 70 parser.error("you may only specify one node to list") 71 72 logger_level = LoggerLevelServiceCaller() 73 74 loggers = logger_level.get_loggers(args[0]) 75 76 output = '\n'.join(loggers) 77 print(output)
78 79
80 -def _get_cmd_set_optparse():
81 from optparse import OptionParser 82 83 usage = "usage: %prog set <node> <logger> <level>" 84 levels = ', '.join(LoggerLevelServiceCaller().get_levels()) 85 usage += "\n\n <level> must be one of [" + levels + "]" 86 parser = OptionParser(usage=usage, prog=NAME) 87 88 return parser
89 90
91 -def _rosconsole_cmd_set(argv):
92 args = argv[2:] 93 parser = _get_cmd_set_optparse() 94 (options, args) = parser.parse_args(args) 95 96 if len(args) < 3: 97 parser.error("you must specify a node, a logger and a level") 98 99 logger_level = LoggerLevelServiceCaller() 100 logger_level.get_loggers(args[0]) 101 102 if args[1] not in logger_level._current_levels: 103 error(2, "node " + args[0] + " does not contain logger " + args[1]) 104 105 level = args[2].lower() 106 if level not in logger_level.get_levels(): 107 parser.error("invalid level") 108 109 logger_level.send_logger_change_message(args[0], args[1], args[2])
110 111
112 -def _get_cmd_get_optparse():
113 from optparse import OptionParser 114 115 usage = "usage: %prog get <node> <logger>" 116 parser = OptionParser(usage=usage, prog=NAME) 117 118 return parser
119 120
121 -def _rosconsole_cmd_get(argv):
122 args = argv[2:] 123 parser = _get_cmd_get_optparse() 124 (options, args) = parser.parse_args(args) 125 126 if len(args) < 2: 127 parser.error("you must specify a node and a logger") 128 129 logger_level = LoggerLevelServiceCaller() 130 logger_level.get_loggers(args[0]) 131 132 if args[1] not in logger_level._current_levels: 133 error(2, "node " + args[0] + " does not contain logger " + args[1]) 134 135 print(logger_level._current_levels[args[1]])
136 137
138 -def _fullusage():
139 print("""rosconsole is a command-line tool for configuring the logger level of ROS nodes. 140 141 Commands: 142 \trosconsole get\tdisplay level for a logger 143 \trosconsole list\tlist loggers for a node 144 \trosconsole set\tset level for a logger 145 146 Type rosconsole <command> -h for more detailed usage, e.g. 'rosconsole list -h' 147 """) 148 sys.exit(getattr(os, 'EX_USAGE', 1))
149 150
151 -def main(argv=None):
152 if argv is None: 153 argv = sys.argv 154 155 # Initialize ourselves as a node, to ensure handling of namespace and 156 # remapping arguments 157 rospy.init_node('rosconsole', anonymous=True) 158 argv = rospy.myargv(argv) 159 160 # process argv 161 if len(argv) == 1: 162 _fullusage() 163 164 try: 165 command = argv[1] 166 if command == 'get': 167 _rosconsole_cmd_get(argv) 168 elif command == 'list': 169 _rosconsole_cmd_list(argv) 170 elif command == 'set': 171 _rosconsole_cmd_set(argv) 172 else: 173 _fullusage() 174 except socket.error as e: 175 error(1, 176 "Network communication failed; most likely failed to communicate with master: %s" % e) 177 except rosgraph.MasterException as e: 178 # mainly for invalid master URI/rosgraph.masterapi 179 error(1, str(e)) 180 except ROSConsoleException as e: 181 error(1, str(e)) 182 except KeyboardInterrupt: 183 pass 184 except rospy.ROSInterruptException: 185 pass
186