Last active
November 25, 2024 15:46
-
-
Save miy4/5e06d4a4f46f41a33b04b8eb3a575eb0 to your computer and use it in GitHub Desktop.
Add extra configuration to a new Python project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import sys | |
| from pathlib import Path | |
| if not Path("pyproject.toml").exists(): | |
| print( | |
| "Ensure you did:\n uv init --app --package\n or uv init --app", | |
| file=sys.stderr, | |
| ) | |
| sys.exit(1) | |
| archives = [ | |
| { | |
| "name": ".vscode/settings.json", | |
| "data": """ | |
| { | |
| "editor.rulers": [ 80, 88 ], | |
| "[python]": { | |
| "editor.defaultFormatter": "charliermarsh.ruff", | |
| "editor.formatOnSave": true, | |
| "editor.codeActionsOnSave": { | |
| "source.fixAll": "explicit", | |
| "source.organizeImports": "explicit", | |
| }, | |
| }, | |
| "mypy-type-checker.args": [ | |
| "--disallow-untyped-defs" | |
| ], | |
| } | |
| """.strip(), | |
| }, | |
| { | |
| "name": ".vscode/extensions.json", | |
| "data": """ | |
| { | |
| "recommendations": [ | |
| "ms-python.python", | |
| "charliermarsh.ruff", | |
| "ms-python.mypy-type-checker" | |
| ], | |
| } | |
| """.strip(), | |
| }, | |
| ] | |
| for archive_item in archives: | |
| new_file = Path(archive_item["name"]) | |
| new_file_dir = new_file.parent | |
| new_file_dir.mkdir(parents=True, exist_ok=True) | |
| if new_file.exists(): | |
| print(f"File already exists: {new_file}\nAborted.", file=sys.stderr) | |
| sys.exit(1) | |
| with new_file.open("w") as f: | |
| f.write(archive_item["data"]) | |
| print(f"Created: {new_file}") | |
| pyproject_extra_config = """ | |
| [tool.ruff] | |
| # Same as Black default values | |
| line-length = 88 | |
| indent-width = 4 | |
| # Assume Python 3.12 or later | |
| target-version = "py312" | |
| [tool.ruff.lint] | |
| select = ["ALL"] | |
| ignore = [ | |
| "D100", # undocumented-public-module | |
| "D203", # one-blank-line-before-class | |
| "D213", # multi-line-summary-second-line | |
| "T201", # print | |
| "T203", # p-print | |
| ] | |
| fixable = ["ALL"] | |
| unfixable = [] | |
| [tool.ruff.lint.per-file-ignores] | |
| # Ignore unused imports and lack of docstrings in __init__.py | |
| # Unused imports in __init__.py might be part of the public interface | |
| # or trigger side effects. | |
| "__init__.py" = ["F401", "D"] | |
| [tool.ruff.format] | |
| # Same as Black default values | |
| quote-style = "double" | |
| indent-style = "space" | |
| skip-magic-trailing-comma = false | |
| line-ending = "auto" | |
| """ | |
| pyproject_toml = Path("pyproject.toml") | |
| with pyproject_toml.open("a") as f: | |
| f.write(pyproject_extra_config) | |
| print("Updated: pyproject.toml") | |
| print("Done.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment