2013-04-03 04:33:56 +02:00
|
|
|
# vim:fileencoding=utf-8:noet
|
|
|
|
from threading import Lock
|
|
|
|
from powerline.renderer import Renderer
|
2013-04-04 20:57:14 +02:00
|
|
|
from powerline.lib.config import ConfigLoader
|
2013-04-03 04:33:56 +02:00
|
|
|
from powerline import Powerline
|
|
|
|
from copy import deepcopy
|
2014-06-24 21:36:50 +02:00
|
|
|
from time import sleep
|
2013-11-20 21:18:34 +01:00
|
|
|
from functools import wraps
|
2013-04-03 04:33:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
access_log = []
|
|
|
|
access_lock = Lock()
|
|
|
|
|
|
|
|
|
2013-04-04 20:57:14 +02:00
|
|
|
def load_json_config(config_file_path, *args, **kwargs):
|
2013-04-03 04:33:56 +02:00
|
|
|
global access_log
|
|
|
|
with access_lock:
|
|
|
|
access_log.append(config_file_path)
|
|
|
|
try:
|
2013-04-04 20:57:14 +02:00
|
|
|
return deepcopy(config_container['config'][config_file_path])
|
2013-04-03 04:33:56 +02:00
|
|
|
except KeyError:
|
|
|
|
raise IOError(config_file_path)
|
|
|
|
|
|
|
|
|
2014-07-10 18:27:24 +02:00
|
|
|
def _find_config_file(config, search_paths, config_file):
|
2013-04-03 04:33:56 +02:00
|
|
|
if config_file.endswith('raise') and config_file not in config:
|
|
|
|
raise IOError('fcf:' + config_file)
|
|
|
|
return config_file
|
|
|
|
|
|
|
|
|
|
|
|
def pop_events():
|
|
|
|
global access_log
|
|
|
|
with access_lock:
|
|
|
|
r = access_log[:]
|
|
|
|
access_log = []
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
2013-11-20 21:18:34 +01:00
|
|
|
def log_call(func):
|
|
|
|
@wraps(func)
|
|
|
|
def ret(self, *args, **kwargs):
|
|
|
|
self._calls.append((func.__name__, args, kwargs))
|
|
|
|
return func(self, *args, **kwargs)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2013-04-03 04:33:56 +02:00
|
|
|
class Watcher(object):
|
|
|
|
events = set()
|
|
|
|
lock = Lock()
|
|
|
|
|
2013-11-20 21:18:34 +01:00
|
|
|
def __init__(self):
|
|
|
|
self._calls = []
|
|
|
|
|
|
|
|
@log_call
|
2013-04-03 04:33:56 +02:00
|
|
|
def watch(self, file):
|
|
|
|
pass
|
|
|
|
|
2013-11-20 21:18:34 +01:00
|
|
|
@log_call
|
2013-04-03 04:33:56 +02:00
|
|
|
def __call__(self, file):
|
2013-04-04 20:57:14 +02:00
|
|
|
with self.lock:
|
|
|
|
if file in self.events:
|
2013-04-03 04:33:56 +02:00
|
|
|
self.events.remove(file)
|
2013-04-04 20:57:14 +02:00
|
|
|
return True
|
2013-04-03 04:33:56 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
def _reset(self, files):
|
|
|
|
with self.lock:
|
|
|
|
self.events.clear()
|
|
|
|
self.events.update(files)
|
|
|
|
|
2013-11-20 21:18:34 +01:00
|
|
|
@log_call
|
2013-04-03 04:33:56 +02:00
|
|
|
def unsubscribe(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class Logger(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.messages = []
|
|
|
|
self.lock = Lock()
|
|
|
|
|
|
|
|
def _add_msg(self, attr, msg):
|
|
|
|
with self.lock:
|
|
|
|
self.messages.append(attr + ':' + msg)
|
|
|
|
|
|
|
|
def _pop_msgs(self):
|
|
|
|
with self.lock:
|
|
|
|
r = self.messages
|
|
|
|
self.messages = []
|
|
|
|
return r
|
|
|
|
|
|
|
|
def __getattr__(self, attr):
|
|
|
|
return lambda *args, **kwargs: self._add_msg(attr, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
class SimpleRenderer(Renderer):
|
|
|
|
def hlstyle(self, fg=None, bg=None, attr=None):
|
|
|
|
return '<{fg} {bg} {attr}>'.format(fg=fg and fg[0], bg=bg and bg[0], attr=attr)
|
|
|
|
|
|
|
|
|
2014-06-24 21:36:50 +02:00
|
|
|
class EvenSimplerRenderer(Renderer):
|
|
|
|
def hlstyle(self, fg=None, bg=None, attr=None):
|
|
|
|
return '{{{fg}{bg}{attr}}}'.format(
|
|
|
|
fg=fg and fg[0] or '-',
|
|
|
|
bg=bg and bg[0] or '-',
|
|
|
|
attr=attr if attr else '',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2013-04-03 04:33:56 +02:00
|
|
|
class TestPowerline(Powerline):
|
|
|
|
_created = False
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_local_themes(local_themes):
|
|
|
|
return local_themes
|
|
|
|
|
|
|
|
def _will_create_renderer(self):
|
|
|
|
return self.create_renderer_kwargs
|
|
|
|
|
|
|
|
|
|
|
|
renderer = SimpleRenderer
|
|
|
|
|
|
|
|
|
|
|
|
def get_powerline(**kwargs):
|
2014-06-25 17:55:20 +02:00
|
|
|
return get_powerline_raw(
|
|
|
|
TestPowerline,
|
|
|
|
ext='test',
|
|
|
|
renderer_module='tests.lib.config_mock',
|
|
|
|
logger=Logger(),
|
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def get_powerline_raw(PowerlineClass, **kwargs):
|
2014-06-24 21:36:50 +02:00
|
|
|
global renderer
|
2013-11-20 21:23:25 +01:00
|
|
|
watcher = Watcher()
|
2014-06-24 21:36:50 +02:00
|
|
|
if kwargs.pop('simpler_renderer', False):
|
|
|
|
renderer = EvenSimplerRenderer
|
|
|
|
else:
|
|
|
|
renderer = SimpleRenderer
|
2014-06-25 17:55:20 +02:00
|
|
|
pl = PowerlineClass(
|
2014-06-28 17:01:12 +02:00
|
|
|
config_loader=ConfigLoader(
|
|
|
|
load=load_json_config,
|
|
|
|
watcher=watcher,
|
|
|
|
watcher_type='test',
|
|
|
|
run_once=kwargs.get('run_once')
|
|
|
|
),
|
2013-04-03 04:33:56 +02:00
|
|
|
**kwargs
|
|
|
|
)
|
2013-11-20 21:23:25 +01:00
|
|
|
pl._watcher = watcher
|
|
|
|
return pl
|
2013-04-03 04:33:56 +02:00
|
|
|
|
|
|
|
|
2013-04-04 20:57:14 +02:00
|
|
|
config_container = None
|
|
|
|
|
|
|
|
|
|
|
|
def swap_attributes(cfg_container, powerline_module, replaces):
|
|
|
|
global config_container
|
|
|
|
config_container = cfg_container
|
2013-04-03 04:33:56 +02:00
|
|
|
if not replaces:
|
|
|
|
replaces = {
|
2014-07-10 18:27:24 +02:00
|
|
|
'_find_config_file': lambda *args: _find_config_file(config_container['config'], *args),
|
2013-04-03 04:33:56 +02:00
|
|
|
}
|
|
|
|
for attr, val in replaces.items():
|
|
|
|
old_val = getattr(powerline_module, attr)
|
|
|
|
setattr(powerline_module, attr, val)
|
|
|
|
replaces[attr] = old_val
|
|
|
|
return replaces
|
2014-06-24 21:36:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
def add_watcher_events(p, *args, **kwargs):
|
|
|
|
p._watcher._reset(args)
|
|
|
|
while not p._will_create_renderer():
|
|
|
|
sleep(kwargs.get('interval', 0.1))
|
|
|
|
if not kwargs.get('wait', True):
|
|
|
|
return
|