1# vim:fileencoding=utf-8:noet 2from __future__ import (unicode_literals, division, absolute_import, print_function) 3 4import itertools 5 6from powerline.lib.unicode import unicode 7from powerline.lint.markedjson.markedvalue import MarkedUnicode 8from powerline.lint.selfcheck import havemarks 9 10 11class JStr(unicode): 12 def join(self, iterable): 13 return super(JStr, self).join((unicode(item) for item in iterable)) 14 15 16key_sep = JStr('/') 17 18 19def list_themes(data, context): 20 theme_type = data['theme_type'] 21 ext = data['ext'] 22 main_theme_name = data['main_config'].get('ext', {}).get(ext, {}).get('theme', None) 23 is_main_theme = (data['theme'] == main_theme_name) 24 if theme_type == 'top': 25 return list(itertools.chain(*[ 26 [(theme_ext, theme) for theme in theme_configs.values()] 27 for theme_ext, theme_configs in data['theme_configs'].items() 28 ])) 29 elif theme_type == 'main' or is_main_theme: 30 return [(ext, theme) for theme in data['ext_theme_configs'].values()] 31 else: 32 return [(ext, context[0][1])] 33 34 35class Context(tuple): 36 for func in dir(tuple): 37 if func in ('__getitem__', '__init__', '__getattribute__', '__len__', '__iter__'): 38 continue 39 exec(( 40 'def {0}(self, *args, **kwargs):\n' 41 ' raise TypeError("{0} is not allowed for Context")' 42 ).format(func)) 43 del func 44 45 __slots__ = () 46 47 def __new__(cls, base, context_key=None, context_value=None): 48 if context_key is not None: 49 assert(context_value is not None) 50 assert(type(base) is Context) 51 havemarks(context_key, context_value) 52 return tuple.__new__(cls, tuple.__add__(base, ((context_key, context_value),))) 53 else: 54 havemarks(base) 55 return tuple.__new__(cls, ((MarkedUnicode('', base.mark), base),)) 56 57 @property 58 def key(self): 59 return key_sep.join((c[0] for c in self)) 60 61 def enter_key(self, value, key): 62 return self.enter(value.keydict[key], value[key]) 63 64 def enter_item(self, name, item): 65 return self.enter(MarkedUnicode(name, item.mark), item) 66 67 def enter(self, context_key, context_value): 68 return Context.__new__(Context, self, context_key, context_value) 69