Make it possible to supply different watcher
Used in tests
This commit is contained in:
parent
5a50acf16b
commit
e35b1541e8
|
@ -22,10 +22,19 @@ class MultiClientWatcher(object):
|
||||||
subscribers = set()
|
subscribers = set()
|
||||||
received_events = {}
|
received_events = {}
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
global watcher
|
||||||
|
self.subscribers.add(self)
|
||||||
|
if not watcher:
|
||||||
|
watcher = create_file_watcher()
|
||||||
|
|
||||||
def watch(self, file):
|
def watch(self, file):
|
||||||
watcher.watch(file)
|
watcher.watch(file)
|
||||||
|
|
||||||
def __call__(self, file):
|
def __call__(self, file):
|
||||||
|
if self not in self.subscribers:
|
||||||
|
return False
|
||||||
|
|
||||||
if file in self.received_events and self not in self.received_events[file]:
|
if file in self.received_events and self not in self.received_events[file]:
|
||||||
self.received_events[file].add(self)
|
self.received_events[file].add(self)
|
||||||
if self.received_events[file] >= self.subscribers:
|
if self.received_events[file] >= self.subscribers:
|
||||||
|
@ -38,12 +47,14 @@ class MultiClientWatcher(object):
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def __del__(self):
|
def unsubscribe(self):
|
||||||
try:
|
try:
|
||||||
self.subscribers.remove(self)
|
self.subscribers.remove(self)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
__del__ = unsubscribe
|
||||||
|
|
||||||
|
|
||||||
def open_file(path):
|
def open_file(path):
|
||||||
return open(path, 'r')
|
return open(path, 'r')
|
||||||
|
@ -132,8 +143,8 @@ class Powerline(object):
|
||||||
run_once=False,
|
run_once=False,
|
||||||
logger=None,
|
logger=None,
|
||||||
use_daemon_threads=True,
|
use_daemon_threads=True,
|
||||||
interval=10):
|
interval=10,
|
||||||
global watcher
|
watcher=None):
|
||||||
self.ext = ext
|
self.ext = ext
|
||||||
self.renderer_module = renderer_module or ext
|
self.renderer_module = renderer_module or ext
|
||||||
self.run_once = run_once
|
self.run_once = run_once
|
||||||
|
@ -156,9 +167,7 @@ class Powerline(object):
|
||||||
|
|
||||||
self.renderer_options = {}
|
self.renderer_options = {}
|
||||||
|
|
||||||
if not watcher:
|
self.watcher = watcher or MultiClientWatcher()
|
||||||
watcher = create_file_watcher()
|
|
||||||
self.watcher = MultiClientWatcher()
|
|
||||||
|
|
||||||
self.prev_common_config = None
|
self.prev_common_config = None
|
||||||
self.prev_ext_config = None
|
self.prev_ext_config = None
|
||||||
|
@ -245,8 +254,9 @@ class Powerline(object):
|
||||||
|
|
||||||
if load_colorscheme or load_colors:
|
if load_colorscheme or load_colors:
|
||||||
self._purge_configs('colorscheme')
|
self._purge_configs('colorscheme')
|
||||||
colorscheme_config = self.load_colorscheme_config(self.ext_config['colorscheme'])
|
if load_colorscheme:
|
||||||
self.renderer_options['colorscheme'] = Colorscheme(colorscheme_config, self.colors_config)
|
self.colorscheme_config = self.load_colorscheme_config(self.ext_config['colorscheme'])
|
||||||
|
self.renderer_options['colorscheme'] = Colorscheme(self.colorscheme_config, self.colors_config)
|
||||||
|
|
||||||
if load_theme:
|
if load_theme:
|
||||||
self._purge_configs('theme')
|
self._purge_configs('theme')
|
||||||
|
@ -311,7 +321,6 @@ class Powerline(object):
|
||||||
|
|
||||||
def _load_config(self, cfg_path, type):
|
def _load_config(self, cfg_path, type):
|
||||||
'''Load configuration and setup watcher.'''
|
'''Load configuration and setup watcher.'''
|
||||||
global watcher
|
|
||||||
path = find_config_file(self.config_paths, cfg_path)
|
path = find_config_file(self.config_paths, cfg_path)
|
||||||
with self.configs_lock:
|
with self.configs_lock:
|
||||||
self.configs[type].add(path)
|
self.configs[type].add(path)
|
||||||
|
@ -386,10 +395,11 @@ class Powerline(object):
|
||||||
'''Lock renderer from modifications and run its ``.shutdown()`` method.
|
'''Lock renderer from modifications and run its ``.shutdown()`` method.
|
||||||
'''
|
'''
|
||||||
self.shutdown_event.set()
|
self.shutdown_event.set()
|
||||||
with self.renderer_lock:
|
|
||||||
self.renderer.shutdown()
|
|
||||||
if self.use_daemon_threads and self.is_alive():
|
if self.use_daemon_threads and self.is_alive():
|
||||||
self.thread.join()
|
self.thread.join()
|
||||||
|
with self.renderer_lock:
|
||||||
|
self.renderer.shutdown()
|
||||||
|
self.watcher.unsubscribe()
|
||||||
|
|
||||||
def is_alive(self):
|
def is_alive(self):
|
||||||
return self.thread and self.thread.is_alive()
|
return self.thread and self.thread.is_alive()
|
||||||
|
@ -401,7 +411,6 @@ class Powerline(object):
|
||||||
self.thread.start()
|
self.thread.start()
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
global watcher
|
|
||||||
while not self.shutdown_event.is_set():
|
while not self.shutdown_event.is_set():
|
||||||
kwargs = {}
|
kwargs = {}
|
||||||
with self.configs_lock:
|
with self.configs_lock:
|
||||||
|
|
Loading…
Reference in New Issue