Skip to content

Commit 184c9b7

Browse files
committed
Add a format session for nox to format source files
1 parent ad4c72d commit 184c9b7

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Internal changes
1616
- Update and freeze dependencies for documentation generation
1717
- Refactored the code to parse MI records to decrease the number of regex matches to perform
1818
- Added `__all__` to all modules, which means that star imports (like `from pygdbmi.gdbmiparser import *`) will not pollute the namespace with modules used by pygdbmi itself
19+
- Added `nox -s format` to re-format the source code using the correct options
1920

2021
## 0.10.0.2
2122

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,18 @@ nox -s tests
171171
nox -s lint
172172
```
173173

174+
To format code using the correct settings use
175+
176+
```
177+
nox -s format
178+
```
179+
180+
Or, to format only specified files, use
181+
182+
```
183+
nox -s format -- example.py pygdbmi/IoManager.py
184+
```
185+
174186
## Similar projects
175187

176188
- [tsgdbmi](https://github.com/Guyutongxue/tsgdbmi) A port of pygdbmi to TypeScript

noxfile.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import itertools
12
import nox # type: ignore
23
from pathlib import Path
34
import shutil
@@ -15,13 +16,37 @@ def tests(session):
1516
session.run("pytest", *session.posargs)
1617

1718

19+
LINTED_PATHS = set(
20+
str(p.resolve())
21+
for p in itertools.chain(
22+
# All top-level Python files.
23+
Path(".").glob("*.py"),
24+
# Plus Python files in these specified directories.
25+
*(Path(d).glob("**/*.py") for d in ("pygdbmi", "tests"))
26+
)
27+
)
28+
29+
30+
# `format` is a builtin so the function is named differently.
31+
@nox.session(name="format")
32+
def format_(session):
33+
"""Re-format all Python source files or, if positionals are passed, only the
34+
specified files."""
35+
files = LINTED_PATHS
36+
if session.posargs:
37+
# Only use positional arguments which are linted files.
38+
files = files & {str(Path(f).resolve()) for f in session.posargs}
39+
40+
session.install("black", "flake8", "mypy", "check-manifest")
41+
session.run("black", *files)
42+
43+
1844
@nox.session()
1945
def lint(session):
2046
session.install(*["black", "flake8", "mypy", "check-manifest"])
21-
files = ["pygdbmi", "tests"] + [str(p) for p in Path(".").glob("*.py")]
22-
session.run("black", "--check", *files)
23-
session.run("flake8", *files)
24-
session.run("mypy", *files) #
47+
session.run("black", "--check", *LINTED_PATHS)
48+
session.run("flake8", *LINTED_PATHS)
49+
session.run("mypy", *LINTED_PATHS)
2550
session.run("check-manifest")
2651
session.run("python", "setup.py", "check", "--metadata", "--strict")
2752

0 commit comments

Comments
 (0)