Package osh :: Package command :: Module py
[frames] | no frames]

Source Code for Module osh.command.py

 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  """C{py PYTHON_CODE} 
19   
20  C{PYTHON_CODE} is executed, and any variables defined are available to 
21  subsequent commands in the same command line. 
22   
23  B{Example}:: 
24   
25      osh py 'a = 123; b = 456' ^ f '(a, b)' $ 
26   
27  This command generates the output C{'(123, 456)'}. 
28   
29  In addition to executing the specified code, input objects are passed 
30  to the output stream. 
31  """ 
32   
33  import sys 
34   
35  import osh.args 
36  import osh.core 
37   
38  # CLI 
39 -def _py():
40 return _Py()
41 42 # API
43 -def py(python_code):
44 """C{python_code} is executed. Any symbols defined by the execution of this 45 code are available to subsequent osh commands. Input objects are passed to 46 the output stream. 47 """ 48 return _Py().process_args(python_code)
49
50 -class _Py(osh.core.Op):
51 52 # object interface 53
54 - def __init__(self):
55 osh.core.Op.__init__(self, '', (1, 1))
56 57 58 # BaseOp interface 59
60 - def doc(self):
61 return __doc__
62
63 - def setup(self):
64 code = self.args().next_string() 65 before = globals().copy() 66 exec code in globals() 67 after = globals() 68 # Put new symbols in function.defined_by_osh 69 for key, value in after.iteritems(): 70 if key not in before: 71 osh.core.add_to_namespace(key, value)
72
73 - def receive(self, object):
74 self.send(object)
75 76 77 # Generator interface 78
79 - def execute(self):
80 self.send(tuple()) # Have to send something
81