Package rosh :: Package impl :: Module roshlets
[frames] | no frames]

Source Code for Module rosh.impl.roshlets

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2010, Willow Garage, 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  # Revision $Id: roshlets.py 11292 2010-09-28 01:11:36Z kwc $ 
 34   
 35  from __future__ import with_statement 
 36   
 37  import sys 
 38   
 39  import roslib.packages 
 40   
 41  import rosh 
 42  from rosh.impl.exceptions import ROSHException 
 43  import rosh.impl.ros_graph 
 44   
45 -def create_globals(plugin_context, dependencies):
46 """ 47 Generate global symbol table for roshlet 48 49 @param plugin_context: context for loading plugins that roshlet depends on 50 @type plugin_context: [str] 51 @param dependencies: names of plugins that roshlet depends on 52 @type dependencies: [str] 53 """ 54 55 rosh = sys.modules['rosh'] 56 # TODO: need to have an __all__ table instead so as to not load in extra dependencies 57 globals_ = rosh.__dict__.copy() 58 globals_['rosh'] = rosh 59 60 # load in default symbols 61 # TODO: need common init routine for rosh so that this does not diverge 62 rosh.impl.ros_graph.load_rosh_plugin('rosh.impl.ros_graph', plugin_context, globals_) 63 64 for d in dependencies: 65 rosh.plugin.load_plugin(d, plugin_context, globals_) 66 return globals_
67
68 -def load_roshlet(filename, plugin_context, dependencies):
69 """ 70 @param filename: name of file with roshlet code 71 @type filename: str 72 @param plugin_context: context for loading plugins that roshlet depends on 73 @type plugin_context: [str] 74 @param dependencies: names of plugins that roshlet depends on 75 @type dependencies: [str] 76 """ 77 with open(filename) as f: 78 str = f.read() 79 return load_roshlet_str(str, plugin_context, dependencies, filename=filename)
80
81 -def load_roshlet_str(str_, plugin_context, dependencies, filename="input"):
82 """ 83 @param str_: roshlet code 84 @type str_: str 85 @param plugin_context: context for loading plugins that roshlet depends on 86 @type plugin_context: [str] 87 @param dependencies: names of plugins that roshlet depends on 88 @type dependencies: [str] 89 """ 90 globals_ = create_globals(plugin_context, dependencies) 91 try: 92 c = compile(str_, filename, 'exec') 93 except SyntaxError, e: 94 raise ROSHException("roshlet [%s] has syntax errors: \n%s"%(filename, e)) 95 except TypeError, e: 96 raise ROSHException("roshlet [%s] source contains null bytes: \n%s"%(filename, e)) 97 98 return c, globals_
99
100 -def exec_roshlet(compiled, globals_):
101 exec compiled in globals_
102
103 -def find_roshlet(package, type_):
104 """ 105 @raise roslib.packages.InvalidROSPkgException: If package does not exist 106 @raise IOError: If roshlet cannot be located 107 """ 108 script_candidates = roslib.packages.find_resource(package, type_) 109 if not script_candidates: 110 raise IOError("cannot find roshlet [%s] in package [%s]"%(type_, package)) 111 # - for now, assume it's the first match 112 return script_candidates[0]
113 114 # TODO: possibly consider executing roshlets at a particular rate
115 -def standalone(name, script, plugins):
116 """ 117 Execute roshlet standalone. This method blocks until roshlet exits. 118 119 @param name: roshlet name 120 @type name: str 121 @param package: roshlet package 122 @type package: str 123 @param type_: roshlet type 124 @type type_: str 125 @param plugins: list of roshlet plugins to load for roshlet 126 @type plugins: [str] 127 @raise roslib.packages.InvalidROSPkgException: If package does not exist 128 @raise IOError: If roshlet cannot be located 129 """ 130 # clean plugin list 131 plugins = [p for p in plugins if p] 132 133 # load rosh symbols 134 rosh.rosh_init() 135 136 # Load roshlet and execute blocking in this thread 137 pc = rosh.get_default_plugin_context() 138 args = rosh.impl.roshlets.load_roshlet(script, pc, plugins) 139 rosh.impl.roshlets.exec_roshlet(*args)
140