def build_parser(self): config_dict = self._config.get_config_dict() enable_neovim = get_config_value(config_dict, ['cmdargs', 'env', 'neovim'], False) parser = Parser(self._plugins, enable_neovim=enable_neovim) return parser
def _build_lint_target(path, config_dict): # type: (Path, Dict[str, Any]) -> AbstractLintTarget if path == _stdin_symbol: stdin_alt_path = get_config_value(config_dict, ['cmdargs', 'stdin_display_name']) # NOTE: In Python 3, sys.stdin is a string not bytes. Then we can get bytes by sys.stdin.buffer. # But in Python 2, sys.stdin.buffer is not defined. But we can get bytes by sys.stdin directly. is_python_3 = hasattr(sys.stdin, 'buffer') if is_python_3: lint_target = LintTargetBufferedStream( alternate_path=Path(stdin_alt_path), buffered_io=sys.stdin.buffer ) else: # NOTE: Python 2 on Windows opens sys.stdin in text mode, and # binary data that read from it becomes corrupted on \r\n # SEE: https://stackoverflow.com/questions/2850893/reading-binary-data-from-stdin/38939320#38939320 if sys.platform == 'win32': # set sys.stdin to binary mode import os, msvcrt msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) lint_target = LintTargetBufferedStream( alternate_path=Path(stdin_alt_path), buffered_io=sys.stdin ) return CachedLintTarget(lint_target) else: lint_target = LintTargetFile(path) return CachedLintTarget(lint_target)
def _build_lint_target( path, config_dict): # type: (Path, Dict[str, Any]) -> AbstractLintTarget if path == _stdin_symbol: stdin_alt_path = get_config_value(config_dict, ['cmdargs', 'stdin_display_name']) # NOTE: In Python 3, sys.stdin is a string not bytes. Then we can get bytes by sys.stdin.buffer. # But in Python 2, sys.stdin.buffer is not defined. But we can get bytes by sys.stdin directly. is_python_3 = hasattr(sys.stdin, 'buffer') if is_python_3: lint_target = LintTargetBufferedStream( alternate_path=Path(stdin_alt_path), buffered_io=sys.stdin.buffer) else: # NOTE: Python 2 on Windows opens sys.stdin in text mode, and # binary data that read from it becomes corrupted on \r\n # SEE: https://stackoverflow.com/questions/2850893/reading-binary-data-from-stdin/38939320#38939320 if sys.platform == 'win32': # set sys.stdin to binary mode import os, msvcrt msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) lint_target = LintTargetBufferedStream( alternate_path=Path(stdin_alt_path), buffered_io=sys.stdin) return CachedLintTarget(lint_target) else: lint_target = LintTargetFile(path) return CachedLintTarget(lint_target)
def __init__(self, policy_set, config_dict_global): # type: (PolicySet, Dict[str, Any]) -> None self._is_debug = get_config_value(config_dict_global, ['cmdargs', 'verbose'], False) self._plugins = { 'scope': ScopePlugin(), } self._policy_set = policy_set self._config_dict_global = config_dict_global self._parser = self.build_parser() self._listeners_map = {}
def build_parser(self): enable_neovim = get_config_value(self._config_dict_global, ['cmdargs', 'env', 'neovim'], False) parser = Parser([self._plugins['scope']], enable_neovim=enable_neovim) return parser
def test_get_config_value_when_empty(self): config_dict = {} self.assertIsNone(get_config_value(config_dict, ['a']))
def test_get_config_value_when_given_default_but_not_used(self): config_dict = {'a': 'A'} self.assertEqual(get_config_value(config_dict, ['a'], 'DEFAULT'), 'A')
def test_get_config_value_when_given_default(self): config_dict = {'a': 'A'} self.assertEqual(get_config_value(config_dict, ['c', 'b'], 'DEFAULT'), 'DEFAULT')
def test_get_config_value_when_nested_2_depth(self): config_dict = {'a': {'b': 'B'}} self.assertIs(get_config_value(config_dict, ['a', 'b']), 'B')
def test_get_config_value_when_nested_1_depth(self): config_dict = {'a': 'A'} self.assertIs(get_config_value(config_dict, ['a']), 'A')
def test_get_config_value_when_target_is_depth_2_unexistent_dict(self): config_dict = {'a': 'A'} self.assertEqual(get_config_value(config_dict, ['c', 'b']), None)
def test_get_config_value_when_target_is_dict(self): config_dict = {'a': {'b': 'B'}} self.assertEqual(get_config_value(config_dict, ['a']), {'b': 'B'})