00001
00002
00003
00004
00005 '''rtshell
00006
00007 Copyright (C) 2009-2014
00008 Geoffrey Biggs
00009 RT-Synthesis Research Group
00010 Intelligent Systems Research Institute,
00011 National Institute of Advanced Industrial Science and Technology (AIST),
00012 Japan
00013 All rights reserved.
00014 Licensed under the Eclipse Public License -v 1.0 (EPL)
00015 http://www.opensource.org/licenses/eclipse-1.0.txt
00016
00017 Implementation of the command to print data sent by ports to the console.
00018
00019 '''
00020
00021
00022 import optparse
00023 import os.path
00024 import rtctree.tree
00025 import rtctree.utils
00026 import sys
00027 import threading
00028 import time
00029 import traceback
00030 import OpenRTM_aist
00031 import RTC
00032
00033 import comp_mgmt
00034 import modmgr
00035 import path
00036 import port_types
00037 import rtprint_comp
00038 import rtshell
00039
00040
00041 def read_from_ports(raw_paths, options, tree=None):
00042 event = threading.Event()
00043
00044 mm = modmgr.ModuleMgr(verbose=options.verbose, paths=options.paths)
00045 mm.load_mods_and_poas(options.modules)
00046 if options.verbose:
00047 print >>sys.stderr, \
00048 'Pre-loaded modules: {0}'.format(mm.loaded_mod_names)
00049 if options.timeout == -1:
00050 max = options.max
00051 if options.verbose:
00052 print >>sys.stderr, 'Will run {0} times.'.format(max)
00053 else:
00054 max = -1
00055 if options.verbose:
00056 print >>sys.stderr, 'Will stop after {0}s'.format(options.timeout)
00057
00058 targets = port_types.parse_targets(raw_paths)
00059 if not tree:
00060 paths = [t[0] for t in targets]
00061 tree = rtctree.tree.RTCTree(paths=paths, filter=paths)
00062 port_specs = port_types.make_port_specs(targets, mm, tree)
00063 port_types.require_all_input(port_specs)
00064 if options.verbose:
00065 print >>sys.stderr, \
00066 'Port specifications: {0}'.format([str(p) for p in port_specs])
00067
00068 comp_name, mgr = comp_mgmt.make_comp('rtprint_reader', tree,
00069 rtprint_comp.Reader, port_specs, event=event, rate=options.rate,
00070 max=max)
00071 if options.verbose:
00072 print >>sys.stderr, 'Created component {0}'.format(comp_name)
00073 comp = comp_mgmt.find_comp_in_mgr(comp_name, mgr)
00074 comp_mgmt.connect(comp, port_specs, tree)
00075 comp_mgmt.activate(comp)
00076 try:
00077 if options.timeout != -1:
00078 event.wait(options.timeout)
00079 comp_mgmt.disconnect(comp)
00080 comp_mgmt.deactivate(comp)
00081 elif options.max > -1:
00082 event.wait()
00083 comp_mgmt.disconnect(comp)
00084 comp_mgmt.deactivate(comp)
00085 else:
00086 while True:
00087 raw_input()
00088
00089
00090 except KeyboardInterrupt:
00091 pass
00092 except EOFError:
00093 pass
00094 tree.give_away_orb()
00095 del tree
00096 comp_mgmt.shutdown(mgr)
00097
00098
00099 def main(argv=None, tree=None):
00100 usage = '''Usage: %prog [options] <path1>:<port1> [<path2>:<port2>...]
00101 Print the data being sent by one or more output ports.'''
00102 version = rtshell.RTSH_VERSION
00103 parser = optparse.OptionParser(usage=usage, version=version)
00104 parser.add_option('-m', '--mod', dest='modules', action='append',
00105 type='string', default=[],
00106 help='Extra modules to import. If automatic module loading '\
00107 'struggles with your data types, try listing the modules here. '\
00108 'The module and its __POA partner will be imported.')
00109 parser.add_option('-n', '--number', dest='max', action='store',
00110 type='int', default='-1', help='Specify the number of times to '\
00111 'read from any ports. [Default: infinity]')
00112 parser.add_option('-p', '--path', dest='paths', action='append',
00113 type='string', default=[],
00114 help='Extra module search paths to add to the PYTHONPATH.')
00115 parser.add_option('-r', '--rate', dest='rate', action='store',
00116 type='float', default=100.0, help='Specify the rate in Hertz at '\
00117 'which to read and print. [Default: %default]')
00118 parser.add_option('-t', '--timeout', dest='timeout', action='store',
00119 type='float', default=-1, help='Read data for this many seconds, '\
00120 'then stop. Specify -1 for no timeout. This option overrides '\
00121 '--number. [Default: %default]')
00122 parser.add_option('-v', '--verbose', dest='verbose', action='store_true',
00123 default=False,
00124 help='Output verbose information. [Default: %default]')
00125
00126 if argv:
00127 sys.argv = [sys.argv[0]] + argv
00128 try:
00129 options, args = parser.parse_args()
00130 except optparse.OptionError, e:
00131 print >>sys.stderr, 'OptionError:', e
00132 return 1
00133
00134 if len(args) < 1:
00135 print >>sys.stderr, usage
00136 return 1
00137
00138 try:
00139 read_from_ports(
00140 [path.cmd_path_to_full_path(p) for p in args], options, tree)
00141 except Exception, e:
00142 if options.verbose:
00143 traceback.print_exc()
00144 print >>sys.stderr, '{0}: {1}'.format(os.path.basename(sys.argv[0]), e)
00145 return 1
00146 return 0
00147