| 1 |
|
| 2 |
from auto import AUTOutil |
| 3 |
import sys |
| 4 |
import os |
| 5 |
from auto import AUTOCommands |
| 6 |
from auto import runAUTO |
| 7 |
from auto import interactiveBindings |
| 8 |
try: |
| 9 |
import __builtin__ |
| 10 |
from new import function |
| 11 |
except ImportError: |
| 12 |
import builtins as __builtin__ |
| 13 |
from types import FunctionType |
| 14 |
|
| 15 |
class AUTOSimpleFunctions: |
| 16 |
def __init__(self,outputRecorder=None,errorRecorder=None): |
| 17 |
|
| 18 |
|
| 19 |
runner = runAUTO.runAUTO() |
| 20 |
if outputRecorder is not None: |
| 21 |
stdout = sys.stdout |
| 22 |
class WriteLog(object): |
| 23 |
def write(self,s): |
| 24 |
outputRecorder.write(s) |
| 25 |
stdout.write(s) |
| 26 |
def flush(self): |
| 27 |
outputRecorder.flush() |
| 28 |
stdout.flush() |
| 29 |
runner.config(log=WriteLog()) |
| 30 |
if errorRecorder is not None: |
| 31 |
stderr = sys.stderr |
| 32 |
class WriteErr(object): |
| 33 |
def write(self,s): |
| 34 |
errorRecorder.write(s) |
| 35 |
stderr.write(s) |
| 36 |
def flush(self): |
| 37 |
errorRecorder.flush() |
| 38 |
stderr.flush() |
| 39 |
runner.config(err=WriteErr()) |
| 40 |
self._runner = runner |
| 41 |
|
| 42 |
|
| 43 |
self._aliases = None |
| 44 |
|
| 45 |
parser = AUTOutil.getAUTORC() |
| 46 |
if parser.has_section("AUTO_command_aliases"): |
| 47 |
self._aliases = {} |
| 48 |
for option in parser.options("AUTO_command_aliases"): |
| 49 |
cmd = parser.get("AUTO_command_aliases",option) |
| 50 |
if cmd not in self._aliases: |
| 51 |
self._aliases[cmd] = [] |
| 52 |
self._aliases[cmd].append(option) |
| 53 |
|
| 54 |
self._addCommands([AUTOCommands]) |
| 55 |
|
| 56 |
|
| 57 |
for key, aliases in self._aliases.items(): |
| 58 |
for alias in aliases: |
| 59 |
f = self._copyfunction(getattr(AUTOCommands,key).fun, alias) |
| 60 |
setattr(self, alias, f) |
| 61 |
doc = getattr(AUTOCommands,key).__doc__ |
| 62 |
doc = self._adjustdoc(doc, alias, key) |
| 63 |
f.__doc__ = doc |
| 64 |
|
| 65 |
def _copyfunction(self, f, key): |
| 66 |
|
| 67 |
def withrunner(runner=None): |
| 68 |
return self._runner or runner |
| 69 |
|
| 70 |
if 'FunctionType' in globals(): |
| 71 |
func_globals = f.__globals__.copy() |
| 72 |
func_globals["withrunner"] = withrunner |
| 73 |
return FunctionType(f.__code__, func_globals, key, |
| 74 |
f.__defaults__, f.__closure__) |
| 75 |
else: |
| 76 |
func_globals = f.func_globals.copy() |
| 77 |
func_globals["withrunner"] = withrunner |
| 78 |
return function(f.func_code, func_globals, key, |
| 79 |
f.func_defaults or ()) |
| 80 |
|
| 81 |
def _adjustdoc(self, doc, commandname, truecommandname = None): |
| 82 |
|
| 83 |
|
| 84 |
if doc == None: |
| 85 |
return doc |
| 86 |
|
| 87 |
doc = doc.replace("\\begin{verbatim}","") |
| 88 |
doc = doc.replace("\\end{verbatim}","") |
| 89 |
doc = doc + "\n" |
| 90 |
|
| 91 |
doc = doc.replace("FUNC", commandname) |
| 92 |
|
| 93 |
if not truecommandname is None: |
| 94 |
commandname = truecommandname |
| 95 |
doc = doc + "Command name: "+commandname+"\n" |
| 96 |
doc = doc + "Aliases: " |
| 97 |
if commandname in self._aliases: |
| 98 |
doc = doc + " ".join(self._aliases[commandname]) |
| 99 |
return doc |
| 100 |
|
| 101 |
def _addCommands(self,moduleList): |
| 102 |
addaliases = self._aliases is None |
| 103 |
if addaliases: |
| 104 |
self._aliases = {} |
| 105 |
for module in [AUTOCommands]: |
| 106 |
|
| 107 |
for key in module.__dict__: |
| 108 |
cmd = getattr(module,key) |
| 109 |
|
| 110 |
if hasattr(cmd,"fun"): |
| 111 |
if addaliases and cmd.alias is not None: |
| 112 |
self._aliases[key] = [cmd.fun.__name__] + cmd.alias |
| 113 |
f = self._copyfunction(cmd.fun, key) |
| 114 |
setattr(self, key, f) |
| 115 |
doc = cmd.__doc__ |
| 116 |
doc = self._adjustdoc(doc, key) |
| 117 |
f.__doc__ = doc |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
def exportFunctions(log=None,err=None): |
| 122 |
AUTOSimpleFunctionsInstance = AUTOSimpleFunctions(log,err) |
| 123 |
dict = {} |
| 124 |
for name in AUTOSimpleFunctionsInstance.__dict__: |
| 125 |
dict[name] = getattr(AUTOSimpleFunctionsInstance, name) |
| 126 |
return dict |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
if "AUTO_DIR" not in os.environ: |
| 134 |
absfile = os.path.abspath(__file__) |
| 135 |
autodir = os.path.dirname(os.path.dirname(os.path.dirname(absfile))) |
| 136 |
os.environ["AUTO_DIR"] = autodir |
| 137 |
funcs = exportFunctions() |
| 138 |
runner = interactiveBindings.AUTOInteractiveConsole(funcs) |
| 139 |
for name,value in funcs.items(): |
| 140 |
if name not in __builtin__.__dict__: |
| 141 |
globals()[name] = value |
| 142 |
|
| 143 |
def test(): |
| 144 |
interactiveBindings._testFilename("../demos/python/fullTest.auto","test_data/fullTest.log") |
| 145 |
|
| 146 |
if __name__ == "__main__": |
| 147 |
test() |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|