mirror of
https://github.com/powerline/powerline.git
synced 2025-07-27 07:44:36 +02:00
Automatically reload configuration
Needs testing
This commit is contained in:
parent
5d1089f252
commit
7646c949e2
@ -7,8 +7,9 @@ import sys
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from powerline.colorscheme import Colorscheme
|
from powerline.colorscheme import Colorscheme
|
||||||
|
from powerline.lib.file_watcher import create_file_watcher
|
||||||
|
|
||||||
from threading import Lock
|
from threading import Lock, Thread, Event
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_SYSTEM_CONFIG_DIR = None
|
DEFAULT_SYSTEM_CONFIG_DIR = None
|
||||||
@ -18,14 +19,18 @@ def open_file(path):
|
|||||||
return open(path, 'r')
|
return open(path, 'r')
|
||||||
|
|
||||||
|
|
||||||
def load_json_config(search_paths, config_file, load=json.load, open_file=open_file):
|
def find_config_file(search_paths, config_file):
|
||||||
config_file += '.json'
|
config_file += '.json'
|
||||||
for path in search_paths:
|
for path in search_paths:
|
||||||
config_file_path = os.path.join(path, config_file)
|
config_file_path = os.path.join(path, config_file)
|
||||||
if os.path.isfile(config_file_path):
|
if os.path.isfile(config_file_path):
|
||||||
|
return config_file_path
|
||||||
|
raise IOError('Config file not found in search path: {0}'.format(config_file))
|
||||||
|
|
||||||
|
|
||||||
|
def load_json_config(config_file_path, load=json.load, open_file=open_file):
|
||||||
with open_file(config_file_path) as config_file_fp:
|
with open_file(config_file_path) as config_file_fp:
|
||||||
return load(config_file_fp)
|
return load(config_file_fp)
|
||||||
raise IOError('Config file not found in search path: {0}'.format(config_file))
|
|
||||||
|
|
||||||
|
|
||||||
class PowerlineState(object):
|
class PowerlineState(object):
|
||||||
@ -110,6 +115,11 @@ class Powerline(object):
|
|||||||
self.config_paths = self.get_config_paths()
|
self.config_paths = self.get_config_paths()
|
||||||
|
|
||||||
self.renderer_lock = Lock()
|
self.renderer_lock = Lock()
|
||||||
|
self.configs_lock = Lock()
|
||||||
|
self.shutdown_event = Event()
|
||||||
|
self.watcher = create_file_watcher()
|
||||||
|
self.configs = {}
|
||||||
|
self.thread = None
|
||||||
|
|
||||||
self.prev_common_config = None
|
self.prev_common_config = None
|
||||||
self.prev_ext_config = None
|
self.prev_ext_config = None
|
||||||
@ -134,8 +144,6 @@ class Powerline(object):
|
|||||||
Determines whether colorscheme configuration should be (re)loaded.
|
Determines whether colorscheme configuration should be (re)loaded.
|
||||||
:param bool load_theme:
|
:param bool load_theme:
|
||||||
Determines whether theme configuration should be reloaded.
|
Determines whether theme configuration should be reloaded.
|
||||||
|
|
||||||
Note: reloading of local themes should be taken care of in renderer.
|
|
||||||
'''
|
'''
|
||||||
common_config_differs = False
|
common_config_differs = False
|
||||||
ext_config_differs = False
|
ext_config_differs = False
|
||||||
@ -208,13 +216,24 @@ class Powerline(object):
|
|||||||
self.pl.exception('Failed to import renderer module: {0}', str(e))
|
self.pl.exception('Failed to import renderer module: {0}', str(e))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
# Renderer updates configuration file via segments’ .startup thus it
|
||||||
|
# should be locked to prevent state when configuration was updated,
|
||||||
|
# but .render still uses old renderer.
|
||||||
with self.renderer_lock:
|
with self.renderer_lock:
|
||||||
self.renderer = Renderer(self.theme_config,
|
try:
|
||||||
|
renderer = Renderer(self.theme_config,
|
||||||
self.local_themes,
|
self.local_themes,
|
||||||
self.theme_kwargs,
|
self.theme_kwargs,
|
||||||
self.colorscheme,
|
self.colorscheme,
|
||||||
self.pl,
|
self.pl,
|
||||||
**self.renderer_options)
|
**self.renderer_options)
|
||||||
|
except Exception as e:
|
||||||
|
self.pl.exception('Failed to construct renderer object: {0}', str(e))
|
||||||
|
else:
|
||||||
|
self.renderer = renderer
|
||||||
|
|
||||||
|
if not self.run_once and not self.is_alive():
|
||||||
|
self.start()
|
||||||
|
|
||||||
def get_log_handler(self):
|
def get_log_handler(self):
|
||||||
'''Get log handler.
|
'''Get log handler.
|
||||||
@ -250,6 +269,14 @@ class Powerline(object):
|
|||||||
config_paths.append(plugin_path)
|
config_paths.append(plugin_path)
|
||||||
return config_paths
|
return config_paths
|
||||||
|
|
||||||
|
def _load_config(self, cfg_path, type):
|
||||||
|
'''Load configuration and setup watcher.'''
|
||||||
|
path = find_config_file(self.config_paths, cfg_path)
|
||||||
|
with self.configs_lock:
|
||||||
|
self.configs[path] = type
|
||||||
|
self.watcher.watch(path)
|
||||||
|
return load_json_config(path)
|
||||||
|
|
||||||
def load_theme_config(self, name):
|
def load_theme_config(self, name):
|
||||||
'''Get theme configuration.
|
'''Get theme configuration.
|
||||||
|
|
||||||
@ -258,14 +285,14 @@ class Powerline(object):
|
|||||||
|
|
||||||
:return: dictionary with :ref:`theme configuration <config-themes>`
|
:return: dictionary with :ref:`theme configuration <config-themes>`
|
||||||
'''
|
'''
|
||||||
return load_json_config(self.config_paths, os.path.join('themes', self.ext, name))
|
return self._load_config(os.path.join('themes', self.ext, name), 'theme')
|
||||||
|
|
||||||
def load_main_config(self):
|
def load_main_config(self):
|
||||||
'''Get top-level configuration.
|
'''Get top-level configuration.
|
||||||
|
|
||||||
:return: dictionary with :ref:`top-level configuration <config-main>`.
|
:return: dictionary with :ref:`top-level configuration <config-main>`.
|
||||||
'''
|
'''
|
||||||
return load_json_config(self.config_paths, 'config')
|
return self._load_config('config', 'main_config')
|
||||||
|
|
||||||
def load_colorscheme_config(self, name):
|
def load_colorscheme_config(self, name):
|
||||||
'''Get colorscheme.
|
'''Get colorscheme.
|
||||||
@ -275,14 +302,14 @@ class Powerline(object):
|
|||||||
|
|
||||||
:return: dictionary with :ref:`colorscheme configuration <config-colorschemes>`.
|
:return: dictionary with :ref:`colorscheme configuration <config-colorschemes>`.
|
||||||
'''
|
'''
|
||||||
return load_json_config(self.config_paths, os.path.join('colorschemes', self.ext, name))
|
return self._load_config(os.path.join('colorschemes', self.ext, name), 'colorscheme')
|
||||||
|
|
||||||
def load_colors_config(self):
|
def load_colors_config(self):
|
||||||
'''Get colorscheme.
|
'''Get colorscheme.
|
||||||
|
|
||||||
:return: dictionary with :ref:`colors configuration <config-colors>`.
|
:return: dictionary with :ref:`colors configuration <config-colors>`.
|
||||||
'''
|
'''
|
||||||
return load_json_config(self.config_paths, 'colors')
|
return self._load_config('colors', 'colors')
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_local_themes(local_themes):
|
def get_local_themes(local_themes):
|
||||||
@ -310,5 +337,33 @@ class Powerline(object):
|
|||||||
def shutdown(self):
|
def shutdown(self):
|
||||||
'''Lock renderer from modifications and run its ``.shutdown()`` method.
|
'''Lock renderer from modifications and run its ``.shutdown()`` method.
|
||||||
'''
|
'''
|
||||||
|
self.shutdown_event.set()
|
||||||
with self.renderer_lock:
|
with self.renderer_lock:
|
||||||
self.renderer.shutdown()
|
self.renderer.shutdown()
|
||||||
|
|
||||||
|
def is_alive(self):
|
||||||
|
return self.thread and self.thread.is_alive()
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
self.thread = Thread(target=self.run)
|
||||||
|
self.thread.start()
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
while not self.shutdown_event.is_set():
|
||||||
|
kwargs = {}
|
||||||
|
with self.configs_lock:
|
||||||
|
for path, type in self.configs.items():
|
||||||
|
if self.watcher(path):
|
||||||
|
kwargs['load_' + type] = True
|
||||||
|
if kwargs:
|
||||||
|
try:
|
||||||
|
self.create_renderer(**kwargs)
|
||||||
|
except Exception as e:
|
||||||
|
self.pl.exception('Failed to create renderer: {0}', str(e))
|
||||||
|
self.shutdown_event.wait(10)
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, *args):
|
||||||
|
self.shutdown()
|
||||||
|
@ -17,7 +17,6 @@ class ThreadedSegment(object):
|
|||||||
self.shutdown_event = Event()
|
self.shutdown_event = Event()
|
||||||
self.write_lock = Lock()
|
self.write_lock = Lock()
|
||||||
self.run_once = True
|
self.run_once = True
|
||||||
self.did_set_interval = False
|
|
||||||
self.thread = None
|
self.thread = None
|
||||||
self.skip = False
|
self.skip = False
|
||||||
self.crashed_value = None
|
self.crashed_value = None
|
||||||
@ -48,17 +47,12 @@ class ThreadedSegment(object):
|
|||||||
return self.thread and self.thread.is_alive()
|
return self.thread and self.thread.is_alive()
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
self.keep_going = True
|
self.shutdown_event.clear()
|
||||||
self.thread = Thread(target=self.run)
|
self.thread = Thread(target=self.run)
|
||||||
self.thread.start()
|
self.thread.start()
|
||||||
|
|
||||||
def sleep(self, adjust_time):
|
|
||||||
self.shutdown_event.wait(max(self.interval - adjust_time, self.min_sleep_time))
|
|
||||||
if self.shutdown_event.is_set():
|
|
||||||
self.keep_going = False
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
while self.keep_going:
|
while not self.shutdown_event.is_set():
|
||||||
start_time = monotonic()
|
start_time = monotonic()
|
||||||
try:
|
try:
|
||||||
self.update()
|
self.update()
|
||||||
@ -67,7 +61,7 @@ class ThreadedSegment(object):
|
|||||||
self.skip = True
|
self.skip = True
|
||||||
else:
|
else:
|
||||||
self.skip = False
|
self.skip = False
|
||||||
self.sleep(monotonic() - start_time)
|
self.shutdown_event.wait(max(self.interval - (monotonic() - start_time), self.min_sleep_time))
|
||||||
|
|
||||||
def shutdown(self):
|
def shutdown(self):
|
||||||
self.shutdown_event.set()
|
self.shutdown_event.set()
|
||||||
@ -79,10 +73,8 @@ class ThreadedSegment(object):
|
|||||||
# .set_interval().
|
# .set_interval().
|
||||||
interval = interval or getattr(self, 'interval')
|
interval = interval or getattr(self, 'interval')
|
||||||
self.interval = interval
|
self.interval = interval
|
||||||
self.has_set_interval = True
|
|
||||||
|
|
||||||
def set_state(self, interval=None, update_first=True, **kwargs):
|
def set_state(self, interval=None, update_first=True, **kwargs):
|
||||||
if not self.did_set_interval or interval:
|
|
||||||
self.set_interval(interval)
|
self.set_interval(interval)
|
||||||
self.updated = not (update_first and self.update_first)
|
self.updated = not (update_first and self.update_first)
|
||||||
|
|
||||||
@ -90,8 +82,9 @@ class ThreadedSegment(object):
|
|||||||
self.run_once = False
|
self.run_once = False
|
||||||
self.pl = pl
|
self.pl = pl
|
||||||
|
|
||||||
if not self.is_alive():
|
|
||||||
self.set_state(**kwargs)
|
self.set_state(**kwargs)
|
||||||
|
|
||||||
|
if not self.is_alive():
|
||||||
self.start()
|
self.start()
|
||||||
|
|
||||||
def error(self, *args, **kwargs):
|
def error(self, *args, **kwargs):
|
||||||
@ -161,12 +154,14 @@ class KwThreadedSegment(ThreadedSegment):
|
|||||||
self.queries.pop(key)
|
self.queries.pop(key)
|
||||||
|
|
||||||
def set_state(self, interval=None, update_first=True, **kwargs):
|
def set_state(self, interval=None, update_first=True, **kwargs):
|
||||||
if not self.did_set_interval or (interval < self.interval):
|
|
||||||
self.set_interval(interval)
|
self.set_interval(interval)
|
||||||
|
|
||||||
if self.update_first:
|
if self.update_first:
|
||||||
self.update_first = update_first
|
self.update_first = update_first
|
||||||
|
|
||||||
|
with self.write_lock:
|
||||||
|
self.queries.clear()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def render_one(update_state, **kwargs):
|
def render_one(update_state, **kwargs):
|
||||||
return update_state
|
return update_state
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
from powerline.lint.markedjson import load
|
from powerline.lint.markedjson import load
|
||||||
from powerline import load_json_config, Powerline
|
from powerline import load_json_config, find_config_file, Powerline
|
||||||
from powerline.lint.markedjson.error import echoerr, MarkedError
|
from powerline.lint.markedjson.error import echoerr, MarkedError
|
||||||
from powerline.segments.vim import vim_modes
|
from powerline.segments.vim import vim_modes
|
||||||
import itertools
|
import itertools
|
||||||
@ -21,14 +21,6 @@ def open_file(path):
|
|||||||
return open(path, 'rb')
|
return open(path, 'rb')
|
||||||
|
|
||||||
|
|
||||||
def find_config(search_paths, config_file):
|
|
||||||
config_file += '.json'
|
|
||||||
for path in search_paths:
|
|
||||||
if os.path.isfile(os.path.join(path, config_file)):
|
|
||||||
return path
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
EMPTYTUPLE = tuple()
|
EMPTYTUPLE = tuple()
|
||||||
|
|
||||||
|
|
||||||
@ -893,7 +885,7 @@ def check(path=None):
|
|||||||
|
|
||||||
hadproblem = False
|
hadproblem = False
|
||||||
try:
|
try:
|
||||||
main_config = load_json_config(search_paths, 'config', load=load_config, open_file=open_file)
|
main_config = load_json_config(find_config_file(search_paths, 'config'), load=load_config, open_file=open_file)
|
||||||
except IOError:
|
except IOError:
|
||||||
main_config = {}
|
main_config = {}
|
||||||
sys.stderr.write('\nConfiguration file not found: config.json\n')
|
sys.stderr.write('\nConfiguration file not found: config.json\n')
|
||||||
@ -909,7 +901,7 @@ def check(path=None):
|
|||||||
import_paths = [os.path.expanduser(path) for path in main_config.get('common', {}).get('paths', [])]
|
import_paths = [os.path.expanduser(path) for path in main_config.get('common', {}).get('paths', [])]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
colors_config = load_json_config(search_paths, 'colors', load=load_config, open_file=open_file)
|
colors_config = load_json_config(find_config_file(search_paths, 'colors'), load=load_config, open_file=open_file)
|
||||||
except IOError:
|
except IOError:
|
||||||
colors_config = {}
|
colors_config = {}
|
||||||
sys.stderr.write('\nConfiguration file not found: colors.json\n')
|
sys.stderr.write('\nConfiguration file not found: colors.json\n')
|
||||||
|
@ -15,17 +15,6 @@ VBLOCK = chr(ord('V') - 0x40)
|
|||||||
SBLOCK = chr(ord('S') - 0x40)
|
SBLOCK = chr(ord('S') - 0x40)
|
||||||
|
|
||||||
|
|
||||||
def shutdown(powerline):
|
|
||||||
from powerline.segments import common, vim
|
|
||||||
try:
|
|
||||||
powerline.shutdown()
|
|
||||||
finally:
|
|
||||||
# After shutdown threads are useless, it is needed to recreate them.
|
|
||||||
from imp import reload
|
|
||||||
reload(common)
|
|
||||||
reload(vim)
|
|
||||||
|
|
||||||
|
|
||||||
class TestConfig(TestCase):
|
class TestConfig(TestCase):
|
||||||
def test_vim(self):
|
def test_vim(self):
|
||||||
from powerline.vim import VimPowerline
|
from powerline.vim import VimPowerline
|
||||||
@ -36,8 +25,8 @@ class TestConfig(TestCase):
|
|||||||
outputs = {}
|
outputs = {}
|
||||||
i = 0
|
i = 0
|
||||||
mode = None
|
mode = None
|
||||||
powerline = VimPowerline()
|
|
||||||
|
|
||||||
|
with VimPowerline() as powerline:
|
||||||
def check_output(*args):
|
def check_output(*args):
|
||||||
out = powerline.render(*args + (0 if mode == 'nc' else 1,))
|
out = powerline.render(*args + (0 if mode == 'nc' else 1,))
|
||||||
if out in outputs:
|
if out in outputs:
|
||||||
@ -60,7 +49,6 @@ class TestConfig(TestCase):
|
|||||||
check_output(1, 0)
|
check_output(1, 0)
|
||||||
finally:
|
finally:
|
||||||
vim_module._start_mode('n')
|
vim_module._start_mode('n')
|
||||||
shutdown(powerline)
|
|
||||||
|
|
||||||
def test_tmux(self):
|
def test_tmux(self):
|
||||||
from powerline.segments import common
|
from powerline.segments import common
|
||||||
@ -68,29 +56,26 @@ class TestConfig(TestCase):
|
|||||||
reload(common)
|
reload(common)
|
||||||
from powerline.shell import ShellPowerline
|
from powerline.shell import ShellPowerline
|
||||||
with replace_attr(common, 'urllib_read', urllib_read):
|
with replace_attr(common, 'urllib_read', urllib_read):
|
||||||
powerline = ShellPowerline(Args(ext=['tmux']), run_once=False)
|
with ShellPowerline(Args(ext=['tmux']), run_once=False) as powerline:
|
||||||
powerline.render()
|
powerline.render()
|
||||||
powerline = ShellPowerline(Args(ext=['tmux']), run_once=False)
|
with ShellPowerline(Args(ext=['tmux']), run_once=False) as powerline:
|
||||||
powerline.render()
|
powerline.render()
|
||||||
shutdown(powerline)
|
|
||||||
|
|
||||||
def test_zsh(self):
|
def test_zsh(self):
|
||||||
from powerline.shell import ShellPowerline
|
from powerline.shell import ShellPowerline
|
||||||
args = Args(last_pipe_status=[1, 0], ext=['shell'], renderer_module='zsh_prompt')
|
args = Args(last_pipe_status=[1, 0], ext=['shell'], renderer_module='zsh_prompt')
|
||||||
powerline = ShellPowerline(args, run_once=False)
|
with ShellPowerline(args, run_once=False) as powerline:
|
||||||
powerline.render(segment_info=args)
|
powerline.render(segment_info=args)
|
||||||
powerline = ShellPowerline(args, run_once=False)
|
with ShellPowerline(args, run_once=False) as powerline:
|
||||||
powerline.render(segment_info=args)
|
powerline.render(segment_info=args)
|
||||||
shutdown(powerline)
|
|
||||||
|
|
||||||
def test_bash(self):
|
def test_bash(self):
|
||||||
from powerline.shell import ShellPowerline
|
from powerline.shell import ShellPowerline
|
||||||
args = Args(last_exit_code=1, ext=['shell'], renderer_module='bash_prompt', config=[('ext', {'shell': {'theme': 'default_leftonly'}})])
|
args = Args(last_exit_code=1, ext=['shell'], renderer_module='bash_prompt', config=[('ext', {'shell': {'theme': 'default_leftonly'}})])
|
||||||
powerline = ShellPowerline(args, run_once=False)
|
with ShellPowerline(args, run_once=False) as powerline:
|
||||||
powerline.render(segment_info=args)
|
powerline.render(segment_info=args)
|
||||||
powerline = ShellPowerline(args, run_once=False)
|
with ShellPowerline(args, run_once=False) as powerline:
|
||||||
powerline.render(segment_info=args)
|
powerline.render(segment_info=args)
|
||||||
shutdown(powerline)
|
|
||||||
|
|
||||||
def test_ipython(self):
|
def test_ipython(self):
|
||||||
from powerline.ipython import IpythonPowerline
|
from powerline.ipython import IpythonPowerline
|
||||||
@ -100,12 +85,11 @@ class TestConfig(TestCase):
|
|||||||
config_overrides = None
|
config_overrides = None
|
||||||
theme_overrides = {}
|
theme_overrides = {}
|
||||||
|
|
||||||
powerline = IpyPowerline()
|
with IpyPowerline() as powerline:
|
||||||
segment_info = Args(prompt_count=1)
|
segment_info = Args(prompt_count=1)
|
||||||
for prompt_type in ['in', 'in2', 'out', 'rewrite']:
|
for prompt_type in ['in', 'in2', 'out', 'rewrite']:
|
||||||
powerline.render(matcher_info=prompt_type, segment_info=segment_info)
|
powerline.render(matcher_info=prompt_type, segment_info=segment_info)
|
||||||
powerline.render(matcher_info=prompt_type, segment_info=segment_info)
|
powerline.render(matcher_info=prompt_type, segment_info=segment_info)
|
||||||
shutdown(powerline)
|
|
||||||
|
|
||||||
def test_wm(self):
|
def test_wm(self):
|
||||||
from powerline.segments import common
|
from powerline.segments import common
|
||||||
|
Loading…
x
Reference in New Issue
Block a user