Add %powerline reload IPython magic function

This commit is contained in:
ZyX 2014-08-19 21:29:32 +04:00
parent 06175dcd74
commit 137fffc9f7
3 changed files with 75 additions and 34 deletions

View File

@ -5,7 +5,21 @@ from weakref import ref
from powerline.ipython import IPythonPowerline, RewriteResult
from IPython.core.prompts import PromptManager
from IPython.core.hooks import TryNext
from IPython.core.magic import Magics, magics_class, line_magic
@magics_class
class PowerlineMagics(Magics):
def __init__(self, ip, powerline):
super(PowerlineMagics, self).__init__(ip)
self._powerline = powerline
@line_magic
def powerline(self, line):
if line == 'reload':
self._powerline.reload()
else:
raise ValueError('Expected `reload`, but got {0}'.format(line))
class IPythonInfo(object):
@ -19,6 +33,7 @@ class IPythonInfo(object):
class PowerlinePromptManager(PromptManager):
def __init__(self, powerline, shell):
self.powerline = powerline
self.powerline_segment_info = IPythonInfo(shell)
self.shell = shell
@ -40,6 +55,17 @@ class PowerlinePromptManager(PromptManager):
return ret
class ShutdownHook(object):
powerline = lambda: None
def __call__(self):
from IPython.core.hooks import TryNext
powerline = self.powerline()
if powerline is not None:
powerline.shutdown()
raise TryNext()
class ConfigurableIPythonPowerline(IPythonPowerline):
def init(self, ip):
config = ip.config.Powerline
@ -48,25 +74,29 @@ class ConfigurableIPythonPowerline(IPythonPowerline):
self.paths = config.get('paths')
super(ConfigurableIPythonPowerline, self).init()
def do_setup(self, ip, shutdown_hook):
prompt_manager = PowerlinePromptManager(
powerline=self,
shell=ip.prompt_manager.shell,
)
magics = PowerlineMagics(ip, self)
shutdown_hook.powerline = ref(self)
ip.prompt_manager = prompt_manager
ip.register_magics(magics)
old_prompt_manager = None
def load_ipython_extension(ip):
global old_prompt_manager
old_prompt_manager = ip.prompt_manager
powerline = ConfigurableIPythonPowerline(ip)
shutdown_hook = ShutdownHook()
ip.prompt_manager = PowerlinePromptManager(
powerline=powerline,
shell=ip.prompt_manager.shell,
)
powerline.setup(ref(ip.prompt_manager))
def shutdown_hook():
powerline.shutdown()
raise TryNext()
powerline.setup(ip, shutdown_hook)
ip.hooks.shutdown_hook.add(shutdown_hook)

View File

@ -23,6 +23,7 @@ class IPythonInfo(object):
class PowerlinePrompt(BasePrompt):
def __init__(self, powerline, powerline_last_in, old_prompt):
self.powerline = powerline
self.powerline_last_in = powerline_last_in
self.powerline_segment_info = IPythonInfo(old_prompt.cache)
self.cache = old_prompt.cache
@ -99,36 +100,45 @@ class ConfigurableIPythonPowerline(IPythonPowerline):
self.paths = paths
super(ConfigurableIPythonPowerline, self).init()
def do_setup(self, wrefs):
for wref in wrefs:
obj = wref()
if obj is not None:
setattr(obj, 'powerline', self)
def ipython_magic(self, ip, parameter_s=''):
if parameter_s == 'reload':
self.reload()
else:
raise ValueError('Expected `reload`, but got {0}'.format(parameter_s))
def setup(**kwargs):
ip = get_ipython()
old_widths = {}
powerline = ConfigurableIPythonPowerline(**kwargs)
def late_startup_hook():
def do_setup(self, ip, shutdown_hook):
last_in = {'nrspaces': 0}
prompts = []
for attr, prompt_class in (
('prompt1', PowerlinePrompt1),
('prompt2', PowerlinePrompt2),
('prompt_out', PowerlinePromptOut)
):
old_prompt = getattr(ip.IP.outputcache, attr)
prompt = prompt_class(powerline, last_in, old_prompt)
prompt = prompt_class(self, last_in, old_prompt)
setattr(ip.IP.outputcache, attr, prompt)
prompts.append(ref(prompt))
powerline.setup(prompts)
ip.expose_magic('powerline', self.ipython_magic)
shutdown_hook.powerline = ref(self)
class ShutdownHook(object):
powerline = lambda: None
def __call__(self):
from IPython.ipapi import TryNext
powerline = self.powerline()
if powerline is not None:
powerline.shutdown()
raise TryNext()
def shutdown_hook():
powerline.shutdown()
def setup(**kwargs):
ip = get_ipython()
powerline = ConfigurableIPythonPowerline(**kwargs)
shutdown_hook = ShutdownHook()
def late_startup_hook():
powerline.setup(ip, shutdown_hook)
raise TryNext()
ip.IP.hooks.late_startup_hook.add(late_startup_hook)

View File

@ -50,7 +50,8 @@ class IPythonPowerline(Powerline):
mergedicts(r, self.theme_overrides[name])
return r
def do_setup(self, wref):
def do_setup(self, wrefs):
for wref in wrefs:
obj = wref()
if obj:
if obj is not None:
setattr(obj, 'powerline', self)