forked from maxpowa/inumuta-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipython.py
78 lines (67 loc) · 2.29 KB
/
ipython.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# coding=utf-8
"""
ipython.py - sopel ipython console!
Copyright © 2014, Elad Alfassa <elad@fedoraproject.org>
Licensed under the Eiffel Forum License 2.
Sopel: http://sopel.dftba.net/
"""
from __future__ import unicode_literals
import sopel
import sys
if sys.version_info.major >= 3:
# Backup stderr/stdout wrappers
old_stdout = sys.stdout
old_stderr = sys.stderr
# IPython wants actual stderr and stdout. In Python 2, it only needed that
# when actually starting the console, but in Python 3 it seems to need that
# on import as well
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
try:
import IPython
if hasattr(IPython, 'terminal'):
from IPython.terminal.embed import InteractiveShellEmbed
else:
from IPython.frontend.terminal.embed import InteractiveShellEmbed
finally:
if sys.version_info.major >= 3:
# Restore stderr/stdout wrappers
sys.stdout = old_stdout
sys.stderr = old_stderr
console = None
@sopel.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
if not sys.__stdout__.isatty():
bot.say('A tty is required to start the console')
return
if bot.config._is_deamonized:
bot.say('Can\'t start console when running as a deamon')
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 = 'Sopel 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