| 34 | |
| 35 | |
| 36 | class code_writer_cmd: |
| 37 | parser: ArgumentParser |
| 38 | args: Namespace |
| 39 | suppress_output: bool |
| 40 | diffs_detected: bool |
| 41 | source_root: Path |
| 42 | pyproject_toml_path: Path |
| 43 | |
| 44 | def __init__(self, tool_script: str): |
| 45 | self.source_root = Path(tool_script).parent.parent |
| 46 | self.pyproject_toml_path = self.source_root / Path("pyproject.toml") |
| 47 | assert self.pyproject_toml_path.exists() |
| 48 | |
| 49 | self.parser = ArgumentParser() |
| 50 | self.parser.add_argument( |
| 51 | "--stdout", |
| 52 | action="store_true", |
| 53 | help="Write to stdout instead of saving to file", |
| 54 | ) |
| 55 | self.parser.add_argument( |
| 56 | "-c", |
| 57 | "--check", |
| 58 | help="Don't write the files back, just return the " |
| 59 | "status. Return code 0 means nothing would change. " |
| 60 | "Return code 1 means some files would be reformatted", |
| 61 | action="store_true", |
| 62 | ) |
| 63 | |
| 64 | def run_zimports(self, tempfile: str) -> None: |
| 65 | self._run_console_script( |
| 66 | str(tempfile), |
| 67 | { |
| 68 | "entrypoint": "zimports", |
| 69 | "options": f"--toml-config {self.pyproject_toml_path}", |
| 70 | }, |
| 71 | ) |
| 72 | |
| 73 | def run_black(self, tempfile: str) -> None: |
| 74 | self._run_console_script( |
| 75 | str(tempfile), |
| 76 | { |
| 77 | "entrypoint": "black", |
| 78 | "options": f"--config {self.pyproject_toml_path}", |
| 79 | }, |
| 80 | ) |
| 81 | |
| 82 | def _run_console_script(self, path: str, options: Dict[str, Any]) -> None: |
| 83 | """Run a Python console application from within the process. |
| 84 | |
| 85 | Used for black, zimports |
| 86 | |
| 87 | """ |
| 88 | |
| 89 | is_posix = os.name == "posix" |
| 90 | |
| 91 | entrypoint_name = options["entrypoint"] |
| 92 | |
| 93 | for entry in compat.importlib_metadata_get("console_scripts"): |
no outgoing calls
no test coverage detected