def _get_nits(self, filename): """Iterate over the instances style checker and yield Nits. :param filename: str pointing to a file within the buildroot. """ try: python_file = PythonFile.parse(filename, root=self._root_dir) except CheckSyntaxError as e: yield e.as_nit() return if noqa_file_filter(python_file): return if self._excluder: # Filter out any suppressed plugins check_plugins = [(plugin_name, plugin_factory) for plugin_name, plugin_factory in self._plugin_factories.items() if self._excluder.should_include(filename, plugin_name)] else: check_plugins = self._plugin_factories.items() for plugin_name, plugin_factory in check_plugins: for i, nit in enumerate(plugin_factory(python_file)): if i == 0: # NB: Add debug log header for nits from each plugin, but only if there are nits from it. self.log.debug('Nits from plugin {} for {}'.format(plugin_name, filename)) if not nit.has_lines_to_display: yield nit continue if all(not line_contains_noqa(line) for line in nit.lines): yield nit
def create_python_file(self, file_content): if self.file_required: tmpdir = safe_mkdtemp() with open(os.path.join(tmpdir, 'file.py'), 'w') as fp: fp.write(file_content) fp.close() return PythonFile.parse('file.py', root=tmpdir) else: return PythonFile.from_statement(file_content)
def _get_nits(self, filename): """Iterate over the instances style checker and yield Nits. :param filename: str pointing to a file within the buildroot. """ try: python_file = PythonFile.parse(filename, root=self._root_dir) except CheckSyntaxError as e: yield e.as_nit() return if noqa_file_filter(python_file): return if self._excluder: # Filter out any suppressed plugins check_plugins = [ (plugin_name, plugin_factory) for plugin_name, plugin_factory in self._plugin_factories.items() if self._excluder.should_include(filename, plugin_name) ] else: check_plugins = self._plugin_factories.items() for plugin_name, plugin_factory in check_plugins: for i, nit in enumerate(plugin_factory(python_file)): if i == 0: # NB: Add debug log header for nits from each plugin, but only if there are nits from it. self.log.debug('Nits from plugin {} for {}'.format( plugin_name, filename)) if not nit.has_lines_to_display: yield nit continue if all(not line_contains_noqa(line) for line in nit.lines): yield nit
def test_python_file_absolute_path_and_root_fails(self): with self.assertRaises(ValueError): PythonFile.parse('/absolute/dir', root='/other/abs/dir')