コード例 #1
0
ファイル: checks.py プロジェクト: baymax84/powerline
def check_segment_module(module, data, context, echoerr):
	havemarks(module)
	with WithPath(data['import_paths']):
		try:
			__import__(str(module))
		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),
			        problem='failed to import module {0}'.format(module),
			        problem_mark=module.mark)
			return True, False, True
	return True, False, False
コード例 #2
0
def check_matcher_func(ext, match_name, data, context, echoerr):
    havemarks(match_name)
    import_paths = [
        os.path.expanduser(path)
        for path in context[0][1].get('common', {}).get('paths', [])
    ]

    match_module, separator, match_function = match_name.rpartition('.')
    if not separator:
        match_module = 'powerline.matchers.{0}'.format(ext)
        match_function = match_name
    with WithPath(import_paths):
        try:
            func = getattr(
                __import__(str(match_module), fromlist=[str(match_function)]),
                str(match_function))
        except ImportError:
            echoerr(context='Error while loading matcher functions',
                    problem='failed to load module {0}'.format(match_module),
                    problem_mark=match_name.mark)
            return True, False, True
        except AttributeError:
            echoerr(context='Error while loading matcher functions',
                    problem='failed to load matcher function {0}'.format(
                        match_function),
                    problem_mark=match_name.mark)
            return True, False, True

    if not callable(func):
        echoerr(context='Error while loading matcher functions',
                problem='loaded “function” {0} is not callable'.format(
                    match_function),
                problem_mark=match_name.mark)
        return True, False, True

    if hasattr(func, 'func_code') and hasattr(func.func_code, 'co_argcount'):
        if func.func_code.co_argcount != 1:
            echoerr(
                context='Error while loading matcher functions',
                problem=('function {0} accepts {1} arguments instead of 1. '
                         'Are you sure it is the proper function?').format(
                             match_function, func.func_code.co_argcount),
                problem_mark=match_name.mark)

    return True, False, False
コード例 #3
0
def check_logging_handler(handler_name, data, context, echoerr):
    havemarks(handler_name)
    import_paths = [
        os.path.expanduser(path)
        for path in context[0][1].get('common', {}).get('paths', [])
    ]

    handler_module, separator, handler_class = handler_name.rpartition('.')
    if not separator:
        handler_module = 'logging.handlers'
        handler_class = handler_name
    with WithPath(import_paths):
        try:
            handler = getattr(
                __import__(str(handler_module), fromlist=[str(handler_class)]),
                str(handler_class))
        except ImportError:
            echoerr(
                context='Error while loading logger class (key {key})'.format(
                    key=context.key),
                problem='failed to load module {0}'.format(handler_module),
                problem_mark=handler_name.mark)
            return True, False, True
        except AttributeError:
            echoerr(
                context='Error while loading logger class (key {key})'.format(
                    key=context.key),
                problem='failed to load handler class {0}'.format(
                    handler_class),
                problem_mark=handler_name.mark)
            return True, False, True

    if not issubclass(handler, logging.Handler):
        echoerr(context='Error while loading logger class (key {key})'.format(
            key=context.key),
                problem='loaded class {0} is not a logging.Handler subclass'.
                format(handler_class),
                problem_mark=handler_name.mark)
        return True, False, True

    return True, False, False