summaryrefslogtreecommitdiffstats
path: root/tests/modules/lib/config_mock.py
blob: 900b60fad31ede26dadbc07c0e864d4e47179b48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# vim:fileencoding=utf-8:noet
from __future__ import (unicode_literals, division, absolute_import, print_function)

import os

from threading import Lock
from copy import deepcopy
from time import sleep
from functools import wraps

from powerline.renderer import Renderer
from powerline.lib.config import ConfigLoader
from powerline import Powerline, get_default_theme

from tests.modules.lib import Args, replace_attr


UT = get_default_theme(is_unicode=True)
AT = get_default_theme(is_unicode=False)


class TestHelpers(object):
	def __init__(self, config):
		self.config = config
		self.access_log = []
		self.access_lock = Lock()

	def loader_condition(self, path):
		return (path in self.config) and path

	def find_config_files(self, cfg_path, config_loader, loader_callback):
		if cfg_path.endswith('.json'):
			cfg_path = cfg_path[:-5]
		if cfg_path.startswith('/'):
			cfg_path = cfg_path.lstrip('/')
		with self.access_lock:
			self.access_log.append('check:' + cfg_path)
		if cfg_path in self.config:
			yield cfg_path
		else:
			if config_loader:
				config_loader.register_missing(self.loader_condition, loader_callback, cfg_path)
			raise IOError(('fcf:' if cfg_path.endswith('raise') else '') + cfg_path)

	def load_json_config(self, config_file_path, *args, **kwargs):
		if config_file_path.endswith('.json'):
			config_file_path = config_file_path[:-5]
		if config_file_path.startswith('/'):
			config_file_path = config_file_path.lstrip('/')
		with self.access_lock:
			self.access_log.append('load:' + config_file_path)
		try:
			return deepcopy(self.config[config_file_path])
		except KeyError:
			raise IOError(config_file_path)

	def pop_events(self):
		with self.access_lock:
			r = self.access_log[:]
			self.access_log = []
		return r


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


class TestWatcher(object):
	events = set()
	lock = Lock()

	def __init__(self):
		self._calls = []

	@log_call
	def watch(self, file):
		pass

	@log_call
	def __call__(self, file):
		with self.lock:
			if file in self.events:
				self.events.remove(file)
				return True
		return False

	def _reset(self, files):
		with self.lock:
			self.events.clear()
			self.events.update(files)

	@log_call
	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, attrs=None):
		return '<{fg} {bg} {attrs}>'.format(fg=fg and fg[0], bg=bg and bg[0], attrs=attrs)


class EvenSimplerRenderer(Renderer):
	def hlstyle(self, fg=None, bg=None, attrs=None):
		return '{{{fg}{bg}{attrs}}}'.format(
			fg=fg and fg[0] or '-',
			bg=bg and bg[0] or '-',
			attrs=attrs if attrs else '',
		)


class TestPowerline(Powerline):
	_created = False

	def __init__(self, _helpers, **kwargs):
		super(TestPowerline, self).__init__(**kwargs)
		self._helpers = _helpers
		self.find_config_files = _helpers.find_config_files

	@staticmethod
	def get_local_themes(local_themes):
		return local_themes

	@staticmethod
	def get_config_paths():
		return ['']

	def _will_create_renderer(self):
		return self.cr_kwargs

	def _pop_events(self):
		return self._helpers.pop_events()


renderer = EvenSimplerRenderer


class TestConfigLoader(ConfigLoader):
	def __init__(self, _helpers, **kwargs):
		watcher = TestWatcher()
		super(TestConfigLoader, self).__init__(
			load=_helpers.load_json_config,
			watcher=watcher,
			watcher_type='test',
			**kwargs
		)


def get_powerline(config, **kwargs):
	helpers = TestHelpers(config)
	return get_powerline_raw(
		helpers,
		TestPowerline,
		_helpers=helpers,
		ext='test',
		renderer_module='tests.modules.lib.config_mock',
		logger=Logger(),
		**kwargs
	)


def select_renderer(simpler_renderer=False):
	global renderer
	renderer = EvenSimplerRenderer if simpler_renderer else SimpleRenderer


def get_powerline_raw(helpers, PowerlineClass, replace_gcp=False, **kwargs):
	if not isinstance(helpers, TestHelpers):
		helpers = TestHelpers(helpers)
	select_renderer(kwargs.pop('simpler_renderer', False))

	if replace_gcp:
		class PowerlineClass(PowerlineClass):
			@staticmethod
			def get_config_paths():
				return ['/']

	pl = PowerlineClass(
		config_loader=TestConfigLoader(
			_helpers=helpers,
			run_once=kwargs.get('run_once')
		),
		**kwargs
	)
	pl._watcher = pl.config_loader.watcher
	return pl


def swap_attributes(config, powerline_module):
	return replace_attr(powerline_module, 'os', Args(
		path=Args(
			isfile=lambda path: path.lstrip('/').replace('.json', '') in config,
			join=os.path.join,
			expanduser=lambda path: path,
			realpath=lambda path: path,
			dirname=os.path.dirname,
		),
		environ={},
	))


def add_watcher_events(p, *args, **kwargs):
	if isinstance(p._watcher, TestWatcher):
		p._watcher._reset(args)
	while not p._will_create_renderer():
		sleep(kwargs.get('interval', 0.1))
		if not kwargs.get('wait', True):
			return