Remove context_key, init_context and context_has_marks functions

This commit is contained in:
ZyX 2014-09-19 22:09:32 +04:00
parent 346c7ab41e
commit 479305e815
6 changed files with 61 additions and 60 deletions

View File

@ -21,7 +21,7 @@ from powerline.lint.checks import (check_matcher_func, check_ext, check_config,
check_highlight_groups, check_highlight_group, check_full_segment_data,
get_all_possible_functions, check_segment_data_key)
from powerline.lint.spec import Spec
from powerline.lint.context import init_context
from powerline.lint.context import Context
def open_file(path):
@ -389,7 +389,7 @@ def check(paths=None, debug=False, echoerr=echoerr, require_ext=None):
if used_main_spec.match(
main_config,
data={'configs': configs, 'lists': lists},
context=init_context(main_config),
context=Context(main_config),
echoerr=ee
)[1]:
hadproblem = True
@ -407,7 +407,7 @@ def check(paths=None, debug=False, echoerr=echoerr, require_ext=None):
ee(problem=str(e))
hadproblem = True
else:
if colors_spec.match(colors_config, context=init_context(colors_config), echoerr=ee)[1]:
if colors_spec.match(colors_config, context=Context(colors_config), echoerr=ee)[1]:
hadproblem = True
if lhadproblem[0]:
@ -432,7 +432,7 @@ def check(paths=None, debug=False, echoerr=echoerr, require_ext=None):
hadproblem = True
top_colorscheme_configs[colorscheme] = config
data['colorscheme'] = colorscheme
if top_colorscheme_spec.match(config, context=init_context(config), data=data, echoerr=ee)[1]:
if top_colorscheme_spec.match(config, context=Context(config), data=data, echoerr=ee)[1]:
hadproblem = True
ext_colorscheme_configs = defaultdict(lambda: {})
@ -464,7 +464,7 @@ def check(paths=None, debug=False, echoerr=echoerr, require_ext=None):
spec = shell_colorscheme_spec
else:
spec = colorscheme_spec
if spec.match(config, context=init_context(config), data=data, echoerr=ee)[1]:
if spec.match(config, context=Context(config), data=data, echoerr=ee)[1]:
hadproblem = True
colorscheme_configs = {}
@ -533,7 +533,7 @@ def check(paths=None, debug=False, echoerr=echoerr, require_ext=None):
else:
data['theme_type'] = 'regular'
spec = theme_spec
if spec.match(config, context=init_context(config), data=data, echoerr=ee)[1]:
if spec.match(config, context=Context(config), data=data, echoerr=ee)[1]:
hadproblem = True
for top_theme, config in top_theme_configs.items():
@ -548,7 +548,7 @@ def check(paths=None, debug=False, echoerr=echoerr, require_ext=None):
}
data['theme_type'] = 'top'
data['theme'] = top_theme
if top_theme_spec.match(config, context=init_context(config), data=data, echoerr=ee)[1]:
if top_theme_spec.match(config, context=Context(config), data=data, echoerr=ee)[1]:
hadproblem = True
return hadproblem

View File

@ -9,8 +9,8 @@ from powerline.lib.threaded import ThreadedSegment
from powerline.lib.unicode import unicode
from powerline.lint.markedjson.markedvalue import MarkedUnicode
from powerline.lint.markedjson.error import DelayedEchoErr, Mark
from powerline.lint.selfcheck import havemarks, context_has_marks
from powerline.lint.context import context_key, list_sep, list_themes, new_context_item
from powerline.lint.selfcheck import havemarks
from powerline.lint.context import list_sep, list_themes, new_context_item
from powerline.lint.imp import WithPath, import_function, import_segment
from powerline.lint.spec import Spec
from powerline.lint.inspect import getconfigargspec
@ -112,7 +112,6 @@ def check_ext(ext, data, context, echoerr):
def check_config(d, theme, data, context, echoerr):
context_has_marks(context)
if len(context) == 4:
ext = context[-2][0]
else:
@ -135,10 +134,9 @@ def check_config(d, theme, data, context, echoerr):
def check_top_theme(theme, data, context, echoerr):
context_has_marks(context)
havemarks(theme)
if theme not in data['configs']['top_themes']:
echoerr(context='Error while checking extension configuration (key {key})'.format(key=context_key(context)),
echoerr(context='Error while checking extension configuration (key {key})'.format(key=context.key),
context_mark=context[-2][0].mark,
problem='failed to find top theme {0}'.format(theme),
problem_mark=theme.mark)
@ -152,7 +150,7 @@ def check_color(color, data, context, echoerr):
and color not in data['colors_config'].get('gradients', {})):
echoerr(
context='Error while checking highlight group in colorscheme (key {key})'.format(
key=context_key(context)),
key=context.key),
problem='found unexistent color or gradient {0}'.format(color),
problem_mark=color.mark
)
@ -228,7 +226,7 @@ def check_group(group, data, context, echoerr):
if not_found:
new_echoerr(
context='Error while checking group definition in colorscheme (key {key})'.format(
key=context_key(context)),
key=context.key),
problem='name {0} is not present in {1} {2} colorschemes: {3}'.format(
group, tofind, ext, ', '.join(not_found)),
problem_mark=group.mark
@ -238,13 +236,12 @@ def check_group(group, data, context, echoerr):
def check_key_compatibility(segment, data, context, echoerr):
context_has_marks(context)
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(context)),
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
@ -255,7 +252,7 @@ def check_key_compatibility(segment, data, context, echoerr):
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)),
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)),
@ -266,7 +263,7 @@ def check_key_compatibility(segment, data, context, echoerr):
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)),
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))
@ -275,7 +272,7 @@ def check_key_compatibility(segment, data, context, echoerr):
if not (segment_type == 'function' or (keys & highlight_keys)):
echoerr(
context='Error while checking segments (key {key})'.format(key=context_key(context)),
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. '
@ -295,7 +292,7 @@ def check_segment_module(module, data, context, echoerr):
except ImportError as e:
if echoerr.logger.level >= logging.DEBUG:
echoerr.logger.exception(e)
echoerr(context='Error while checking segments (key {key})'.format(key=context_key(context)),
echoerr(context='Error while checking segments (key {key})'.format(key=context.key),
problem='failed to import module {0}'.format(module),
problem_mark=module.mark)
return True, False, True
@ -382,7 +379,7 @@ def check_segment_function(function_name, data, context, echoerr):
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)),
context='Error while checking theme (key {key})'.format(key=context.key),
problem=(
'found highlight group {0} not defined in the following colorschemes: {1}\n'
'(Group name was obtained from function documentation.)'
@ -421,7 +418,7 @@ def check_segment_function(function_name, data, context, echoerr):
]
if all(rs):
echoerr(
context='Error while checking theme (key {key})'.format(key=context_key(context)),
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.)'
@ -430,7 +427,7 @@ def check_segment_function(function_name, data, context, echoerr):
)
for r, h in zip(rs, required_pack):
echoerr(
context='Error while checking theme (key {key})'.format(key=context_key(context)),
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))
)
@ -439,7 +436,7 @@ def check_segment_function(function_name, data, context, echoerr):
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(context)),
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, '
@ -463,7 +460,7 @@ def check_segment_function(function_name, data, context, echoerr):
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(context)),
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)
@ -501,7 +498,7 @@ def hl_exists(hl_group, data, context, echoerr, allow_gradients=False):
if allow_gradients is False and not hascolor and hasgradient:
echoerr(
context='Error while checking highlight group in theme (key {key})'.format(
key=context_key(context)),
key=context.key),
context_mark=hl_group.mark,
problem='group {0} is using gradient {1} instead of a color'.format(hl_group, color),
problem_mark=color.mark
@ -511,7 +508,7 @@ def hl_exists(hl_group, data, context, echoerr, allow_gradients=False):
if allow_gradients == 'force' and not hadgradient:
echoerr(
context='Error while checking highlight group in theme (key {key})'.format(
key=context_key(context)),
key=context.key),
context_mark=hl_group.mark,
problem='group {0} should have at least one gradient color, but it has no'.format(hl_group),
problem_mark=group_config.mark
@ -525,7 +522,7 @@ def check_highlight_group(hl_group, data, context, echoerr):
r = hl_exists(hl_group, data, context, echoerr)
if r:
echoerr(
context='Error while checking theme (key {key})'.format(key=context_key(context)),
context='Error while checking theme (key {key})'.format(key=context.key),
problem='found highlight group {0} not defined in the following colorschemes: {1}'.format(
hl_group, list_sep.join(r)),
problem_mark=hl_group.mark
@ -539,14 +536,14 @@ def check_highlight_groups(hl_groups, data, context, echoerr):
rs = [hl_exists(hl_group, data, context, echoerr) for hl_group in hl_groups]
if all(rs):
echoerr(
context='Error while checking theme (key {key})'.format(key=context_key(context)),
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'.format(
list_sep.join((unicode(h) for h in hl_groups))),
problem_mark=hl_groups.mark
)
for r, hl_group in zip(rs, hl_groups):
echoerr(
context='Error while checking theme (key {key})'.format(key=context_key(context)),
context='Error while checking theme (key {key})'.format(key=context.key),
problem='found highlight group {0} not defined in the following colorschemes: {1}'.format(
hl_group, list_sep.join(r)),
problem_mark=hl_group.mark
@ -611,14 +608,14 @@ def check_args_variant(func, args, data, context, echoerr):
if required_args - present_args:
echoerr(
context='Error while checking segment arguments (key {key})'.format(key=context_key(context)),
context='Error while checking segment arguments (key {key})'.format(key=context.key),
context_mark=args.mark,
problem='some of the required keys are missing: {0}'.format(list_sep.join(required_args - present_args))
)
hadproblem = True
if not all_args >= present_args:
echoerr(context='Error while checking segment arguments (key {key})'.format(key=context_key(context)),
echoerr(context='Error while checking segment arguments (key {key})'.format(key=context.key),
context_mark=args.mark,
problem='found unknown keys: {0}'.format(list_sep.join(present_args - all_args)),
problem_mark=next(iter(present_args - all_args)).mark)
@ -642,7 +639,6 @@ def check_args_variant(func, args, data, context, echoerr):
def check_args(get_functions, args, data, context, echoerr):
context_has_marks(context)
new_echoerr = DelayedEchoErr(echoerr)
count = 0
hadproblem = False
@ -657,7 +653,7 @@ def check_args(get_functions, args, data, context, echoerr):
if new_echoerr:
new_echoerr.echo_all()
else:
echoerr(context='Error while checking segment arguments (key {key})'.format(key=context_key(context)),
echoerr(context='Error while checking segment arguments (key {key})'.format(key=context.key),
context_mark=context[-2][1].mark,
problem='no suitable segments found')

View File

@ -5,6 +5,7 @@ import itertools
from powerline.lib.unicode import unicode
from powerline.lint.markedjson.markedvalue import MarkedUnicode
from powerline.lint.selfcheck import havemarks
class JStr(unicode):
@ -16,14 +17,6 @@ key_sep = JStr('/')
list_sep = JStr(', ')
def context_key(context):
return key_sep.join((c[0] for c in context))
def init_context(config):
return ((MarkedUnicode('', config.mark), config),)
def new_context_item(key, value):
return ((value.keydict[key], value[key]),)
@ -42,3 +35,23 @@ def list_themes(data, context):
return [(ext, theme) for theme in data['ext_theme_configs'].values()]
else:
return [(ext, context[0][1])]
class Context(tuple):
def __new__(cls, base, other=None):
if other is not None:
return tuple.__new__(cls, tuple.__add__(base, other))
else:
return tuple.__new__(cls, ((MarkedUnicode('', base.mark), base),))
def __add__(self, arg):
assert(len(arg) == 1)
assert(type(arg) is tuple)
assert(len(arg[0]) == 2)
assert(type(arg[0]) is tuple)
havemarks(arg[0][0], arg[0][1])
return Context.__new__(Context, self, arg)
@property
def key(self):
return key_sep.join((c[0] for c in self))

View File

@ -3,8 +3,7 @@ from __future__ import (unicode_literals, division, absolute_import, print_funct
import sys
from powerline.lint.selfcheck import havemarks, context_has_marks
from powerline.lint.context import context_key
from powerline.lint.selfcheck import havemarks
class WithPath(object):
@ -20,26 +19,25 @@ class WithPath(object):
def import_function(function_type, name, data, context, echoerr, module):
context_has_marks(context)
havemarks(name, module)
with WithPath(data['import_paths']):
try:
func = getattr(__import__(str(module), fromlist=[str(name)]), str(name))
except ImportError:
echoerr(context='Error while checking segments (key {key})'.format(key=context_key(context)),
echoerr(context='Error while checking segments (key {key})'.format(key=context.key),
context_mark=name.mark,
problem='failed to import module {0}'.format(module),
problem_mark=module.mark)
return None
except AttributeError:
echoerr(context='Error while loading {0} function (key {key})'.format(function_type, key=context_key(context)),
echoerr(context='Error while loading {0} function (key {key})'.format(function_type, key=context.key),
problem='failed to load function {0} from module {1}'.format(name, module),
problem_mark=name.mark)
return None
if not callable(func):
echoerr(context='Error while checking segments (key {key})'.format(key=context_key(context)),
echoerr(context='Error while checking segments (key {key})'.format(key=context.key),
context_mark=name.mark,
problem='imported "function" {0} from module {1} is not callable'.format(name, module),
problem_mark=module.mark)

View File

@ -14,9 +14,3 @@ def havemarks(*args, **kwargs):
havemarks(key, val, origin=(origin + '[' + unicode(i) + ']/' + unicode(key)))
elif isinstance(v, list):
havemarks(*v, origin=(origin + '[' + unicode(i) + ']'))
def context_has_marks(context):
for i, v in enumerate(context):
havemarks(v[0], origin='context key')
havemarks(v[1], origin='context val')

View File

@ -10,7 +10,7 @@ from powerline.lib.unicode import unicode
from powerline.lint.markedjson.error import echoerr, DelayedEchoErr
from powerline.lint.markedjson.markedvalue import MarkedUnicode
from powerline.lint.selfcheck import havemarks
from powerline.lint.context import context_key, list_sep, new_context_item
from powerline.lint.context import list_sep, new_context_item
class Spec(object):
@ -74,7 +74,7 @@ class Spec(object):
havemarks(value)
if type(value.value) not in types:
echoerr(
context=self.cmsg.format(key=context_key(context)),
context=self.cmsg.format(key=context.key),
context_mark=context_mark,
problem='{0!r} must be a {1} instance, not {2}'.format(
value,
@ -90,7 +90,7 @@ class Spec(object):
havemarks(value)
proceed, echo, hadproblem = func(value, data, context, echoerr)
if echo and hadproblem:
echoerr(context=self.cmsg.format(key=context_key(context)),
echoerr(context=self.cmsg.format(key=context.key),
context_mark=context_mark,
problem=msg_func(value),
problem_mark=value.mark)
@ -114,7 +114,7 @@ class Spec(object):
else:
proceed, echo, fhadproblem = item_func(item, data, context, echoerr)
if echo and fhadproblem:
echoerr(context=self.cmsg.format(key=context_key(context) + '/list item ' + unicode(i)),
echoerr(context=self.cmsg.format(key=context.key + '/list item ' + unicode(i)),
context_mark=value.mark,
problem=msg_func(item),
problem_mark=item.mark)
@ -332,7 +332,7 @@ class Spec(object):
else:
if not valspec.isoptional:
hadproblem = True
echoerr(context=self.cmsg.format(key=context_key(context)),
echoerr(context=self.cmsg.format(key=context.key),
context_mark=None,
problem='required key is missing: {0}'.format(key),
problem_mark=value.mark)
@ -362,7 +362,7 @@ class Spec(object):
else:
hadproblem = True
if self.ufailmsg:
echoerr(context=self.cmsg.format(key=context_key(context)),
echoerr(context=self.cmsg.format(key=context.key),
context_mark=None,
problem=self.ufailmsg(key),
problem_mark=key.mark)