def make_docutils_conf(repo_path: pathlib.Path, templates: Environment) -> List[str]: """ Add configuration for ``Docutils``. :param repo_path: Path to the repository root. :param templates: """ file = PathPlus(repo_path / templates.globals["docs_dir"] / "docutils.conf") file.parent.maybe_make(parents=True) if not file.is_file(): file.write_text('\n'.join([ "[restructuredtext parser]", "tab_width = 4", '', '', ])) conf = ConfigUpdater() conf.read(str(file)) required_sections = ["restructuredtext parser"] for section in required_sections: if section not in conf.sections(): conf.add_section(section) conf["restructuredtext parser"]["tab_width"] = 4 file.write_clean(str(conf)) return [file.relative_to(repo_path).as_posix()]
def check_fn(obtained_filename, expected_filename): print(obtained_filename, expected_filename) expected_filename = PathPlus(expected_filename) template = Template(expected_filename.read_text()) expected_filename.write_text( template.render( sphinx_version=sphinx.version_info, python_version=sys.version_info, )) return check_text_files(obtained_filename, expected_filename, encoding="UTF-8")
def check_fn(obtained_filename, expected_filename): __tracebackhide__ = True expected_filename = PathPlus(expected_filename) template = Template(expected_filename.read_text()) expected_filename.write_text( template.render( sphinx_version=sphinx.version_info, python_version=sys.version_info, docutils_version=docutils_version, **jinja2_namespace or {}, )) return check_text_files(obtained_filename, expected_filename, encoding="UTF-8")
class Reformatter: """ Reformat a Python source file. :param filename: The filename to reformat. :param config: The ``formate`` configuration, parsed from a TOML file (or similar). .. autosummary-widths:: 5/16 11/16 """ #: The filename being reformatted. filename: str #: The filename being reformatted, as a POSIX-style path. file_to_format: PathPlus #: The ``formate`` configuration, parsed from a TOML file (or similar). config: FormateConfigDict def __init__(self, filename: PathLike, config: FormateConfigDict): self.file_to_format = PathPlus(filename) self.filename = self.file_to_format.as_posix() self.config = config self._unformatted_source = self.file_to_format.read_text() self._reformatted_source: Optional[str] = None def run(self) -> bool: """ Run the reformatter. :return: Whether the file was changed. """ hooks = parse_hooks(self.config) reformatted_source = StringList(call_hooks(hooks, self._unformatted_source, self.filename)) reformatted_source.blankline(ensure_single=True) self._reformatted_source = str(reformatted_source) return self._reformatted_source != self._unformatted_source def get_diff(self) -> str: """ Returns the diff between the original and reformatted file content. """ # Based on yapf # Apache 2.0 License after = self.to_string().split('\n') before = self._unformatted_source.split('\n') return coloured_diff( before, after, self.filename, self.filename, "(original)", "(reformatted)", lineterm='', ) def to_string(self) -> str: """ Return the reformatted file as a string. :rtype: .. latex:clearpage:: """ if self._reformatted_source is None: raise ValueError("'Reformatter.run()' must be called first!") return self._reformatted_source def to_file(self) -> None: """ Write the reformatted source to the original file. """ self.file_to_format.write_text(self.to_string())
def test_append_pathplus(self): file = PathPlus("paths_append_test_file.txt") file.write_text("initial content\n") file.append_text("appended content") assert file.read_text() == "initial content\nappended content" file.unlink()
class Reformatter: """ Reformat a Python source file. :param filename: :param yapf_style: The name of the yapf style, or the path to the yapf style file. :param isort_config: The filename of the isort configuration file. """ def __init__(self, filename: PathLike, yapf_style: str, isort_config: Config): self.file_to_format = PathPlus(filename) self.filename = self.file_to_format.as_posix() self.yapf_style = yapf_style self.isort_config = isort_config self._unformatted_source = self.file_to_format.read_text() self._reformatted_source: Optional[str] = None def run(self) -> bool: """ Run the reformatter. :return: Whether the file was changed. """ quote_formatted_code = reformat_quotes(self._unformatted_source) yapfed_code = FormatCode(quote_formatted_code, style_config=self.yapf_style)[0] generic_formatted_code = reformat_generics(yapfed_code) # TODO: support spaces try: isorted_code = StringList( isort.code(generic_formatted_code, config=self.isort_config)) except FileSkipComment: isorted_code = StringList(generic_formatted_code) isorted_code.blankline(ensure_single=True) self._reformatted_source = str(isorted_code) # Fix for noqa comments being pushed to new line self._reformatted_source = noqa_reformat(self._reformatted_source) return self._reformatted_source != self._unformatted_source def get_diff(self) -> str: """ Returns the diff between the original and reformatted file content. """ # Based on yapf # Apache 2.0 License if self._reformatted_source is None: raise ValueError("'Reformatter.run()' must be called first!") before = self._unformatted_source.splitlines() after = self._reformatted_source.splitlines() return coloured_diff( before, after, self.filename, self.filename, "(original)", "(reformatted)", lineterm='', ) def to_string(self) -> str: """ Return the reformatted file as a string. """ if self._reformatted_source is None: raise ValueError("'Reformatter.run()' must be called first!") return self._reformatted_source def to_file(self) -> None: """ Write the reformatted source to the original file. """ if self._reformatted_source is None: raise ValueError("'Reformatter.run()' must be called first!") self.file_to_format.write_text(self._reformatted_source)