Debian Codegraph @

07p/python/auto/AUTOclui.py

raw symbols SCIP
1 #! /usr/bin/env python
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__ # Python 3
13 from types import FunctionType
14
15 class AUTOSimpleFunctions:
16 def __init__(self,outputRecorder=None,errorRecorder=None):
17
18 # Initialize the global AUTO runner
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 # Read in the aliases.
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 # Now I resolve the aliases
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 # If we were created with the nonempty string return a formatted
83 # reference for the given command as the string
84 if doc == None:
85 return doc
86 # Get rid of the LaTeX stuff from the string that gets returned.
87 doc = doc.replace("\\begin{verbatim}","")
88 doc = doc.replace("\\end{verbatim}","")
89 doc = doc + "\n"
90
91 doc = doc.replace("FUNC", commandname)
92 # This means help was asked for an alias
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 # Now we copy the commands from the module
107 for key in module.__dict__:
108 cmd = getattr(module,key)
109 # Check to see if it is a command
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 # Export the functions inside AUTOSimpleFunctions in a dictionary
120 # This also allows the setting of the log
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 # Export the functions inside AUTOSimpleFunctions in this modules namespace
129 # This is to make "from AUTOclui import *" work
130 # ALMOST like the AUTOInteractiveConsole. Things that
131 # don't work are help, shell, !, ls, cd, and any changes
132 # to the aliases
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