Package osh :: Module api
[frames] | no frames]

Source Code for Module osh.api

  1  # osh 
  2  # Copyright (C) Jack Orenstein <jao@geophile.com> 
  3  # 
  4  # This program is free software; you can redistribute it and/or modify 
  5  # it under the terms of the GNU General Public License as published by 
  6  # the Free Software Foundation; either version 2 of the License, or 
  7  # (at your option) any later version. 
  8  # 
  9  # This program is distributed in the hope that it will be useful, 
 10  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 12  # GNU General Public License for more details. 
 13  # 
 14  # You should have received a copy of the GNU General Public License 
 15  # along with this program; if not, write to the Free Software 
 16  # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 17   
 18  """The osh API (Application Programming Interface) supports the use of osh from Python code. 
 19  The recommended form of import is C{from osh.api import *}. This imports 
 20  the functions documented here as well as 
 21  the symbols from the module C{osh.builtins}, 
 22  the C{Process} class, and C{processes} 
 23  function. 
 24   
 25  In general, the function C{F()} can be found in the module 
 26  C{osh.command.F}. See documentation for the package osh.command for 
 27  information on each function. 
 28  """ 
 29   
 30  import sys 
 31  import types 
 32   
 33  from args import Option 
 34  import apiparser 
 35  import core 
 36  import config 
 37  import error 
 38  import command 
 39  import command.f 
 40  from builtins import * 
 41   
 42  # Get symbols defined in .oshrc 
 43  globals().update(core.namespace()) 
 44   
45 -def _import_package(package_name):
46 package = globals()[package_name] 47 for module_name in package.__all__: 48 exec('import %s.%s' % (package_name, module_name)) 49 mod = getattr(package, module_name) 50 for element_name in dir(mod): 51 if not element_name.startswith('_'): 52 element = getattr(mod, element_name) 53 if type(element) is types.FunctionType: 54 globals()[element_name] = element
55 56 57 # Top-level api invocation 58
59 -def osh(*ops):
60 """Invoke osh interpreter. Each argument is a function invocation identifying a command. 61 The command sequence corresponds to the sequence of arguments. 62 """ 63 # Construct the pipeline 64 pipeline = None 65 try: 66 pipeline = apiparser._sequence_op(ops) 67 except Exception, e: 68 print >>sys.stderr, e 69 sys.exit(1) 70 # Run 71 command = core.Command(pipeline) 72 command.execute() 73 last_op = ops[-1] 74 if type(last_op) is _ReturnList: 75 return last_op.list 76 else: 77 return None
78
79 -class _ReturnList(core.Op):
80 81 _unwrap_singleton = None 82 _list = None 83
84 - def __init__(self, unwrap_singleton):
85 core.Op.__init__(self, '', (0, 0)) 86 self._unwrap_singleton = unwrap_singleton 87 self._list = []
88
89 - def setup(self):
90 pass
91
92 - def receive(self, object):
93 if self._unwrap_singleton and len(object) == 1: 94 self._list.append(object[0]) 95 else: 96 self._list.append(object)
97
98 - def receive_complete(self):
99 self.send_complete()
100 101 list = property(lambda self: self._list)
102 103
104 -def return_list(unwrap_singleton = True):
105 """Input tuples are accumulated in a list which is returned as the value of the 106 C{osh()} invocation. If unwrap_singleton is True, then items in the list that 107 are 1-object sequences are unwrapped, e.g. (419,) -> 419. 108 """ 109 return _ReturnList(unwrap_singleton)
110 111 112 # Error and exception handling 113
114 -def set_error_handler(handler):
115 """Replaces the standard error handler (which prints to stderr). An error handler 116 takes these arguments: 117 - C{line}: A line printed to stderr by the failing operation. 118 - C{op}: The failing operation. 119 - C{input}: The input to the failing operation. 120 - C{host}: The host on which the error occurred (in case of remote execution). 121 """ 122 error.set_error_handler(handler)
123
124 -def set_exception_handler(handler):
125 """Replaces the standard exception handler (which prints to stderr). An exception handler 126 takes these arguments: 127 - C{exception}: The exception being handled. 128 - C{op}: The operation that caused the exception. 129 - C{input}: The input to the operation that caused the exception. 130 - C{host}: The host on which the exception occurred (in case of remote execution). 131 """ 132 error.set_exception_handler(handler)
133 134 135 # Debugging 136
137 -def debug(verbosity):
138 """Control osh debugging: 0 = off, 1 = parse tree, 2 = command execution. 139 """ 140 core.verbosity = verbosity
141 142 143 # Setup 144 145 _import_package('command') 146