forked from sopel-irc/sopel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This module allows you to open an IPython console in module context. If you want to debug willie in real time, or experiment with the API given to modules, this module is for you.
- Loading branch information
Elad Alfassa
committed
Apr 26, 2014
1 parent
709e1dc
commit 0af4492
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#coding: utf8 | ||
""" | ||
ipython.py - willie ipython console! | ||
Copyright © 2014, Elad Alfassa <elad@fedoraproject.org> | ||
Licensed under the Eiffel Forum License 2. | ||
Willie: http://willie.dftba.net/ | ||
""" | ||
from __future__ import unicode_literals | ||
import willie | ||
import sys | ||
from IPython.frontend.terminal.embed import InteractiveShellEmbed | ||
console = None | ||
|
||
|
||
@willie.module.commands('console') | ||
def interactive_shell(bot, trigger): | ||
""" | ||
Starts an interactive IPython console | ||
""" | ||
global console | ||
if not trigger.admin: | ||
bot.say('Only admins can start the interactive console') | ||
return | ||
if 'iconsole_running' in bot.memory and bot.memory['iconsole_running']: | ||
bot.say('Console already running') | ||
return | ||
|
||
# Backup stderr/stdout wrappers | ||
old_stdout = sys.stdout | ||
old_stderr = sys.stderr | ||
|
||
# IPython wants actual stderr and stdout | ||
sys.stdout = sys.__stdout__ | ||
sys.stderr = sys.__stderr__ | ||
|
||
banner1 = 'Willie interactive shell (embedded IPython)' | ||
banner2 = '`bot` and `trigger` are available. To exit, type exit' | ||
exitmsg = 'Interactive shell closed' | ||
|
||
console = InteractiveShellEmbed(banner1=banner1, banner2=banner2, | ||
exit_msg=exitmsg) | ||
|
||
bot.memory['iconsole_running'] = True | ||
bot.say('console started') | ||
console() | ||
bot.memory['iconsole_running'] = False | ||
|
||
# Restore stderr/stdout wrappers | ||
sys.stdout = old_stdout | ||
sys.stderr = old_stderr |