Created
July 12, 2013 20:35
-
-
Save rpmuller/5987615 to your computer and use it in GitHub Desktop.
Create an interactive shell in a GUI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FileCacher: | |
"Cache the stdout text so we can analyze it before returning it" | |
def __init__(self): self.reset() | |
def reset(self): self.out = [] | |
def write(self,line): self.out.append(line) | |
def flush(self): | |
output = '\n'.join(self.out) | |
self.reset() | |
return output | |
class Shell(InteractiveConsole): | |
"Wrapper around Python that can filter input/output to the shell" | |
def __init__(self): | |
self.stdout = sys.stdout | |
self.cache = FileCacher() | |
InteractiveConsole.__init__(self) | |
return | |
def get_output(self): sys.stdout = self.cache | |
def return_output(self): sys.stdout = self.stdout | |
def push(self,line): | |
self.get_output() | |
# you can filter input here by doing something like | |
# line = filter(line) | |
InteractiveConsole.push(self,line) | |
self.return_output() | |
output = self.cache.flush() | |
# you can filter the output here by doing something like | |
# output = filter(output) | |
print output # or do something else with it | |
return | |
if __name__ == '__main__': | |
sh = Shell() | |
sh.interact() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment