rtinject.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- Python -*-
00003 # -*- coding: utf-8 -*-
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 rtinject_comp
00038 import rtshell
00039 
00040 
00041 def write_to_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 
00050     if options.const:
00051         val = mm.evaluate(options.const)
00052         if options.verbose:
00053             print >>sys.stderr, 'Evaluated value to {0}'.format(val)
00054     else:
00055         if options.verbose:
00056             print >>sys.stderr, 'Reading values from stdin.'
00057 
00058     if options.timeout == -1:
00059         max = options.max
00060         if options.verbose:
00061             print >>sys.stderr, 'Will run {0} times.'.format(max)
00062     else:
00063         max = -1
00064         if options.verbose:
00065             print >>sys.stderr, 'Will stop after {0}s'.format(options.timeout)
00066 
00067     targets = port_types.parse_targets(raw_paths)
00068     if not tree:
00069         paths = [t[0] for t in targets]
00070         tree = rtctree.tree.RTCTree(paths=paths, filter=paths)
00071     port_specs = port_types.make_port_specs(targets, mm, tree)
00072     port_types.require_all_output(port_specs)
00073     if options.verbose:
00074         print >>sys.stderr, \
00075                 'Port specifications: {0}'.format([str(p) for p in port_specs])
00076 
00077     if options.const:
00078         comp_name, mgr = comp_mgmt.make_comp('rtinject_writer', tree,
00079                 rtinject_comp.Writer, port_specs, event=event, rate=options.rate,
00080                 max=max, val=val)
00081     else:
00082         buffer = []
00083         mutex = threading.RLock()
00084         comp_name, mgr = comp_mgmt.make_comp('rtinject_writer', tree,
00085                 rtinject_comp.StdinWriter, port_specs, event=event,
00086                 rate=options.rate, max=max, buf=buffer, mutex=mutex)
00087     if options.verbose:
00088         print >>sys.stderr, 'Created component {0}'.format(comp_name)
00089     comp = comp_mgmt.find_comp_in_mgr(comp_name, mgr)
00090     comp_mgmt.connect(comp, port_specs, tree)
00091     comp_mgmt.activate(comp)
00092     if options.const:
00093         try:
00094             if options.timeout != -1:
00095                 event.wait(options.timeout)
00096             elif options.max > -1:
00097                 event.wait()
00098             else:
00099                 raw_input()
00100         except KeyboardInterrupt:
00101             pass
00102         except EOFError:
00103             pass
00104     else:
00105         # Read stdin until we receive max number of values or Ctrl-C is hit
00106         val_cnt = 0
00107         try:
00108             while val_cnt < max or max < 0:
00109                 l = sys.stdin.readline()
00110                 if not l:
00111                     break
00112                 if l[0] == '#':
00113                     continue
00114                 val = mm.evaluate(l)
00115                 with mutex:
00116                     buffer.append(val)
00117                 val_cnt += 1
00118         except KeyboardInterrupt:
00119             pass
00120         # Wait until the buffer has been cleared
00121         while True:
00122             with mutex:
00123                 if not buffer:
00124                     break
00125     comp_mgmt.disconnect(comp)
00126     comp_mgmt.deactivate(comp)
00127     tree.give_away_orb()
00128     del tree
00129     comp_mgmt.shutdown(mgr)
00130 
00131 
00132 def main(argv=None, tree=None):
00133     usage = '''Usage: %prog [options] <path1>:<port1> [<path2>:<port2>...]
00134 Write a constant value to one or more ports.'''
00135     version = rtshell.RTSH_VERSION
00136     parser = optparse.OptionParser(usage=usage, version=version)
00137     parser.add_option('-c', '--const', dest='const', action='store',
00138             type='string', default='',
00139             help='The constant value to send, as a Python expression. If '
00140             'not specified, values will be read from standard in.')
00141     parser.add_option('-m', '--mod', dest='modules', action='append',
00142             type='string', default=[],
00143             help='Extra modules to import. If automatic module loading '
00144             'struggles with your data types, try listing the modules here. '
00145             'The module and its __POA partner will be imported.')
00146     parser.add_option('-n', '--number', dest='max', action='store',
00147             type='int', default='1',
00148             help='Specify the number of times to write to the port. '
00149             '[Default: %default]')
00150     parser.add_option('-p', '--path', dest='paths', action='append',
00151             type='string', default=[],
00152             help='Extra module search paths to add to the PYTHONPATH.')
00153     parser.add_option('-r', '--rate', dest='rate', action='store',
00154             type='float', default=1.0,
00155             help='Specify the rate in Hertz at which to emit data. '
00156             '[Default: %default]')
00157     parser.add_option('-t', '--timeout', dest='timeout', action='store',
00158             type='float', default=-1, help='Write data for this many seconds, '
00159             'then stop. This option overrides --number. [Default: %default]')
00160     parser.add_option('-v', '--verbose', dest='verbose', action='store_true',
00161             default=False,
00162             help='Output verbose information. [Default: %default]')
00163 
00164     if argv:
00165         sys.argv = [sys.argv[0]] + argv
00166     try:
00167         options, args = parser.parse_args()
00168     except optparse.OptionError, e:
00169         print >>sys.stderr, 'OptionError:', e
00170         return 1
00171 
00172     if len(args) < 1:
00173         print >>sys.stderr, usage
00174         return 1
00175 
00176     try:
00177         write_to_ports([path.cmd_path_to_full_path(p) \
00178                 for p in args], options, tree=tree)
00179     except Exception, e:
00180         if options.verbose:
00181             traceback.print_exc()
00182         print >>sys.stderr, '{0}: {1}'.format(os.path.basename(sys.argv[0]), e)
00183         return 1
00184     return 0
00185 


rtshell
Author(s): Geoffrey Biggs
autogenerated on Fri Aug 28 2015 12:55:12