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 powerline.ipython import IPythonPowerline, RewriteResult
from IPython.core.prompts import PromptManager 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): class IPythonInfo(object):
@ -19,6 +33,7 @@ class IPythonInfo(object):
class PowerlinePromptManager(PromptManager): class PowerlinePromptManager(PromptManager):
def __init__(self, powerline, shell): def __init__(self, powerline, shell):
self.powerline = powerline
self.powerline_segment_info = IPythonInfo(shell) self.powerline_segment_info = IPythonInfo(shell)
self.shell = shell self.shell = shell
@ -40,6 +55,17 @@ class PowerlinePromptManager(PromptManager):
return ret 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): class ConfigurableIPythonPowerline(IPythonPowerline):
def init(self, ip): def init(self, ip):
config = ip.config.Powerline config = ip.config.Powerline
@ -48,25 +74,29 @@ class ConfigurableIPythonPowerline(IPythonPowerline):
self.paths = config.get('paths') self.paths = config.get('paths')
super(ConfigurableIPythonPowerline, self).init() 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 old_prompt_manager = None
def load_ipython_extension(ip): def load_ipython_extension(ip):
global old_prompt_manager global old_prompt_manager
old_prompt_manager = ip.prompt_manager old_prompt_manager = ip.prompt_manager
powerline = ConfigurableIPythonPowerline(ip) powerline = ConfigurableIPythonPowerline(ip)
shutdown_hook = ShutdownHook()
ip.prompt_manager = PowerlinePromptManager( powerline.setup(ip, shutdown_hook)
powerline=powerline,
shell=ip.prompt_manager.shell,
)
powerline.setup(ref(ip.prompt_manager))
def shutdown_hook():
powerline.shutdown()
raise TryNext()
ip.hooks.shutdown_hook.add(shutdown_hook) ip.hooks.shutdown_hook.add(shutdown_hook)

View File

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