def register_common_name(name, cmodule, cname): s = cmodule + '.' + cname cmodule_mark = Mark('<common name definition>', 1, 1, s, 1) cname_mark = Mark('<common name definition>', 1, len(cmodule) + 1, s, len(cmodule) + 1) common_names[name].add( (MarkedUnicode(cmodule, cmodule_mark), MarkedUnicode(cname, cname_mark)))
def get_all_possible_functions(data, context, echoerr): name = context[-2][0] module, name = name.rpartition('.')[::2] if module: func = import_segment(name, data, context, echoerr, module=module) if func: yield func else: if name in common_names: for cmodule, cname in common_names[name]: cfunc = import_segment(cname, data, context, echoerr, module=MarkedUnicode(cmodule, None)) if cfunc: yield cfunc for ext, theme_config in list_themes(data, context): for segments in theme_config.get('segments', {}).values(): for segment in segments: if segment.get('type', 'function') == 'function': function_name = segment.get('function') current_name = segment.get('name') if function_name: module, function_name = get_function_strings(function_name, ((None, theme_config),), ext) if current_name == name or function_name == name: func = import_segment(function_name, data, context, echoerr, module=module) if func: yield func
def get_function_strings(function_name, context, ext): if '.' in function_name: module, function_name = function_name.rpartition('.')[::2] else: module = context[0][1].get( 'default_module', MarkedUnicode('powerline.segments.' + ext, None)) return module, function_name
def check_exinclude_function(name, data, context, echoerr): ext = data['ext'] module, name = name.rpartition('.')[::2] if not module: module = MarkedUnicode('powerline.selectors.' + ext, None) func = import_function('selector', name, data, context, echoerr, module=module) if not func: return True, False, True return True, False, False
def __new__(cls, base, context_key=None, context_value=None): if context_key is not None: assert(context_value is not None) assert(type(base) is Context) havemarks(context_key, context_value) return tuple.__new__(cls, tuple.__add__(base, ((context_key, context_value),))) else: havemarks(base) return tuple.__new__(cls, ((MarkedUnicode('', base.mark), base),))
def check_key_compatibility(segment, data, context, echoerr): havemarks(segment) segment_type = segment.get('type', MarkedUnicode('function', None)) havemarks(segment_type) if segment_type not in type_keys: echoerr(context='Error while checking segments (key {key})'.format(key=context.key), problem='found segment with unknown type {0}'.format(segment_type), problem_mark=segment_type.mark) return False, False, True hadproblem = False keys = set(segment) if not ((keys - generic_keys) < type_keys[segment_type]): unknown_keys = keys - generic_keys - type_keys[segment_type] echoerr( context='Error while checking segments (key {key})'.format(key=context.key), context_mark=context[-1][1].mark, problem='found keys not used with the current segment type: {0}'.format( list_sep.join(unknown_keys)), problem_mark=list(unknown_keys)[0].mark ) hadproblem = True if not (keys >= required_keys[segment_type]): missing_keys = required_keys[segment_type] - keys echoerr( context='Error while checking segments (key {key})'.format(key=context.key), context_mark=context[-1][1].mark, problem='found missing required keys: {0}'.format( list_sep.join(missing_keys)) ) hadproblem = True if not (segment_type == 'function' or (keys & highlight_keys)): echoerr( context='Error while checking segments (key {key})'.format(key=context.key), context_mark=context[-1][1].mark, problem=( 'found missing keys required to determine highlight group. ' 'Either highlight_groups or name key must be present' ) ) hadproblem = True return True, False, hadproblem
def check_segment_function(function_name, data, context, echoerr): havemarks(function_name) ext = data['ext'] module, function_name = get_function_strings(function_name, context, ext) if context[-2][1].get('type', 'function') == 'function': func = import_segment(function_name, data, context, echoerr, module=module) if not func: return True, False, True hl_groups = [] divider_hl_group = None if func.__doc__: H_G_USED_STR = 'Highlight groups used: ' LHGUS = len(H_G_USED_STR) D_H_G_USED_STR = 'Divider highlight group used: ' LDHGUS = len(D_H_G_USED_STR) pointer = 0 mark_name = '<{0} docstring>'.format(function_name) for i, line in enumerate(func.__doc__.split('\n')): if H_G_USED_STR in line: idx = line.index(H_G_USED_STR) + LHGUS hl_groups.append(( line[idx:], (mark_name, i + 1, idx + 1, func.__doc__), pointer + idx )) elif D_H_G_USED_STR in line: idx = line.index(D_H_G_USED_STR) + LDHGUS + 2 mark = Mark(mark_name, i + 1, idx + 1, func.__doc__, pointer + idx) divider_hl_group = MarkedUnicode(line[idx:-3], mark) pointer += len(line) + len('\n') hadproblem = False if divider_hl_group: r = hl_exists(divider_hl_group, data, context, echoerr, allow_gradients=True) if r: echoerr( context='Error while checking theme (key {key})'.format(key=context.key), context_mark=function_name.mark, problem=( 'found highlight group {0} not defined in the following colorschemes: {1}\n' '(Group name was obtained from function documentation.)' ).format(divider_hl_group, list_sep.join(r)), problem_mark=divider_hl_group.mark, ) hadproblem = True if check_hl_group_name(divider_hl_group, function_name.mark, context, echoerr): hadproblem = True if hl_groups: greg = re.compile(r'``([^`]+)``( \(gradient\))?') parsed_hl_groups = [] for line, mark_args, pointer in hl_groups: for s in line.split(', '): required_pack = [] sub_pointer = pointer for subs in s.split(' or '): match = greg.match(subs) try: if not match: continue hl_group = MarkedUnicode( match.group(1), Mark(*mark_args, pointer=sub_pointer + match.start(1)) ) if check_hl_group_name(hl_group, function_name.mark, context, echoerr): hadproblem = True gradient = bool(match.group(2)) required_pack.append((hl_group, gradient)) finally: sub_pointer += len(subs) + len(' or ') parsed_hl_groups.append(required_pack) pointer += len(s) + len(', ') del hl_group, gradient for required_pack in parsed_hl_groups: rs = [ hl_exists(hl_group, data, context, echoerr, allow_gradients=('force' if gradient else False)) for hl_group, gradient in required_pack ] if all(rs): echoerr( context='Error while checking theme (key {key})'.format(key=context.key), problem=( 'found highlight groups list ({0}) with all groups not defined in some colorschemes\n' '(Group names were taken from function documentation.)' ).format(list_sep.join((h[0] for h in required_pack))), problem_mark=function_name.mark ) for r, h in zip(rs, required_pack): echoerr( context='Error while checking theme (key {key})'.format(key=context.key), problem='found highlight group {0} not defined in the following colorschemes: {1}'.format( h[0], list_sep.join(r)) ) hadproblem = True else: r = hl_exists(function_name, data, context, echoerr, allow_gradients=True) if r: echoerr( context='Error while checking theme (key {key})'.format(key=context.key), problem=( 'found highlight group {0} not defined in the following colorschemes: {1}\n' '(If not specified otherwise in documentation, ' 'highlight group for function segments\n' 'is the same as the function name.)' ).format(function_name, list_sep.join(r)), problem_mark=function_name.mark ) hadproblem = True return True, False, hadproblem elif context[-2][1].get('type') != 'segment_list': if function_name not in context[0][1].get('segment_data', {}): main_theme_name = data['main_config'].get('ext', {}).get(ext, {}).get('theme', None) if data['theme'] == main_theme_name: main_theme = {} else: main_theme = data['ext_theme_configs'].get(main_theme_name, {}) if ( function_name not in main_theme.get('segment_data', {}) and function_name not in data['ext_theme_configs'].get('__main__', {}).get('segment_data', {}) and not any(((function_name in theme.get('segment_data', {})) for theme in data['top_themes'].values())) ): echoerr(context='Error while checking segments (key {key})'.format(key=context.key), problem='found useless use of name key (such name is not present in theme/segment_data)', problem_mark=function_name.mark) return True, False, False
def enter_item(self, name, item): return self.enter(MarkedUnicode(name, item.mark), item)