sqlalign 1.0.1__py3-none-any.whl → 1.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sqlalign/cli.py +61 -3
- sqlalign/gui.py +3 -1
- sqlalign/lint.py +26 -7
- sqlalign/py.typed +0 -0
- sqlalign/sqlfluffconfig.py +4 -1
- {sqlalign-1.0.1.dist-info → sqlalign-1.0.2.dist-info}/METADATA +28 -10
- {sqlalign-1.0.1.dist-info → sqlalign-1.0.2.dist-info}/RECORD +10 -9
- {sqlalign-1.0.1.dist-info → sqlalign-1.0.2.dist-info}/WHEEL +0 -0
- {sqlalign-1.0.1.dist-info → sqlalign-1.0.2.dist-info}/entry_points.txt +0 -0
- {sqlalign-1.0.1.dist-info → sqlalign-1.0.2.dist-info}/licenses/LICENSE +0 -0
sqlalign/cli.py
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import argparse
|
|
2
2
|
import difflib
|
|
3
|
+
import errno
|
|
4
|
+
import os
|
|
5
|
+
import shutil
|
|
3
6
|
import sys
|
|
7
|
+
import tempfile
|
|
4
8
|
from fnmatch import fnmatch
|
|
5
9
|
from pathlib import Path
|
|
6
10
|
|
|
@@ -11,6 +15,43 @@ from sqlalign.sqlfluffconfig import sqlfluff_config
|
|
|
11
15
|
from sqlalign.style import PRESETS, SUPPORTED_DIALECTS, Style
|
|
12
16
|
|
|
13
17
|
|
|
18
|
+
def _write_atomically(path: Path, text: str) -> None:
|
|
19
|
+
"""Replace the contents of `path` in a single step.
|
|
20
|
+
|
|
21
|
+
`open(path, "w")` truncates before it writes, so an interrupted run leaves
|
|
22
|
+
the file short: the input is destroyed and the replacement never arrives.
|
|
23
|
+
Writing a neighbour and renaming it means what is on disk is either the
|
|
24
|
+
whole old file or the whole new one, and a run that is cut off half way
|
|
25
|
+
through a repository has changed nothing it did not finish.
|
|
26
|
+
|
|
27
|
+
The temp file is created in the target's own directory so the rename cannot
|
|
28
|
+
cross a filesystem, and the symlink is resolved first so a linked file is
|
|
29
|
+
written THROUGH rather than replaced by a regular file.
|
|
30
|
+
"""
|
|
31
|
+
target = path.resolve()
|
|
32
|
+
# `os.replace` only needs a writable directory, so a read-only file would
|
|
33
|
+
# otherwise be silently overwritten -- which `open(path, "w")` refused.
|
|
34
|
+
if not os.access(target, os.W_OK):
|
|
35
|
+
raise PermissionError(errno.EACCES, os.strerror(errno.EACCES), str(target))
|
|
36
|
+
|
|
37
|
+
try:
|
|
38
|
+
handle, name = tempfile.mkstemp(dir=target.parent,
|
|
39
|
+
prefix=f".{target.name}.", suffix=".tmp")
|
|
40
|
+
except OSError as e:
|
|
41
|
+
# An unwritable DIRECTORY fails here. Reported against the file the user
|
|
42
|
+
# named rather than the temp file they have never heard of.
|
|
43
|
+
raise OSError(e.errno, e.strerror, str(target)) from e
|
|
44
|
+
temp = Path(name)
|
|
45
|
+
try:
|
|
46
|
+
with os.fdopen(handle, "w", newline="") as f:
|
|
47
|
+
f.write(text)
|
|
48
|
+
shutil.copymode(target, temp)
|
|
49
|
+
os.replace(temp, target)
|
|
50
|
+
except BaseException:
|
|
51
|
+
temp.unlink(missing_ok=True)
|
|
52
|
+
raise
|
|
53
|
+
|
|
54
|
+
|
|
14
55
|
def _is_excluded(path: Path, root: Path, patterns: list[str]) -> bool:
|
|
15
56
|
"""Whether `path` matches any exclude glob, tested against its path relative
|
|
16
57
|
to the directory being expanded (posix separators, `fnmatch` semantics)."""
|
|
@@ -117,7 +158,10 @@ def build_parser() -> argparse.ArgumentParser:
|
|
|
117
158
|
help="(experimental) open the settings panel with a live preview, and exit")
|
|
118
159
|
p.add_argument("--report", action="store_true",
|
|
119
160
|
help="print a coverage summary: how many statements were "
|
|
120
|
-
"formatted, and what the rest declined on"
|
|
161
|
+
"formatted, and what the rest declined on. Adds output "
|
|
162
|
+
"without changing the mode, so on its own it still "
|
|
163
|
+
"rewrites; pair it with `--check` to survey without "
|
|
164
|
+
"writing")
|
|
121
165
|
p.add_argument("--max-declines", type=int, metavar="N",
|
|
122
166
|
help="exit 1 if more than N statements are passed through "
|
|
123
167
|
"unformatted (implies --report)")
|
|
@@ -283,6 +327,14 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
283
327
|
print(f"sqlalign: {e}", file=sys.stderr)
|
|
284
328
|
rc = 2
|
|
285
329
|
continue
|
|
330
|
+
except UnicodeDecodeError:
|
|
331
|
+
# Guessing at an encoding would mean writing the file back in a
|
|
332
|
+
# different one, which is precisely the kind of change sqlalign
|
|
333
|
+
# exists not to make. The file is reported and left exactly as it is.
|
|
334
|
+
print(f"sqlalign: {name}: not valid UTF-8 — left unchanged",
|
|
335
|
+
file=sys.stderr)
|
|
336
|
+
rc = 2
|
|
337
|
+
continue
|
|
286
338
|
try:
|
|
287
339
|
style = style_for(path)
|
|
288
340
|
except configfile.ConfigError as e: # a broken config must not be guessed past
|
|
@@ -335,8 +387,14 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
335
387
|
print(f"would reformat {name}")
|
|
336
388
|
rc = max(rc, 1)
|
|
337
389
|
elif formatted != original:
|
|
338
|
-
|
|
339
|
-
|
|
390
|
+
try:
|
|
391
|
+
_write_atomically(path, formatted)
|
|
392
|
+
except OSError as e:
|
|
393
|
+
# A per-file failure is reported and skipped, like an unreadable
|
|
394
|
+
# file above -- one unwritable file must not abort the run.
|
|
395
|
+
print(f"sqlalign: {e}", file=sys.stderr)
|
|
396
|
+
rc = 2
|
|
397
|
+
continue
|
|
340
398
|
|
|
341
399
|
if args.lint:
|
|
342
400
|
# Lint what would be written, so --check and --stdout report on the
|
sqlalign/gui.py
CHANGED
|
@@ -20,6 +20,7 @@ from __future__ import annotations
|
|
|
20
20
|
|
|
21
21
|
import pathlib
|
|
22
22
|
import queue
|
|
23
|
+
import sys
|
|
23
24
|
import threading
|
|
24
25
|
|
|
25
26
|
from sqlalign.config import Width
|
|
@@ -242,7 +243,8 @@ def run(dialect: str = "postgres") -> int: # pragma: no cover - needs a dis
|
|
|
242
243
|
except ImportError:
|
|
243
244
|
print("sqlalign: --gui needs tkinter, which is missing from this Python.\n"
|
|
244
245
|
" On Debian/Ubuntu: apt install python3-tk\n"
|
|
245
|
-
" On macOS with pyenv: install a Python built against Tcl/Tk."
|
|
246
|
+
" On macOS with pyenv: install a Python built against Tcl/Tk.",
|
|
247
|
+
file=sys.stderr)
|
|
246
248
|
return 2
|
|
247
249
|
|
|
248
250
|
root = tk.Tk()
|
sqlalign/lint.py
CHANGED
|
@@ -162,12 +162,31 @@ def _lint_bodies(text: str, args: list[str], path: Path, dialect: str) -> str:
|
|
|
162
162
|
return "\n".join(lines) + "\n"
|
|
163
163
|
|
|
164
164
|
|
|
165
|
+
def _series(version: str) -> tuple[int, int] | None:
|
|
166
|
+
"""`(major, minor)` from a version string, or None if it does not parse."""
|
|
167
|
+
try:
|
|
168
|
+
major, minor = version.split(".")[:2]
|
|
169
|
+
return int(major), int(minor)
|
|
170
|
+
except ValueError:
|
|
171
|
+
return None
|
|
172
|
+
|
|
173
|
+
|
|
165
174
|
def version_warning() -> str | None:
|
|
166
|
-
"""A note when the installed sqlfluff is
|
|
167
|
-
checked against
|
|
168
|
-
|
|
175
|
+
"""A note when the installed sqlfluff is NEWER than the release the mappings
|
|
176
|
+
were checked against.
|
|
177
|
+
|
|
178
|
+
Only the series is compared. A patch release does not add or rename a rule,
|
|
179
|
+
so warning on one would fire for every user in the window between a sqlfluff
|
|
180
|
+
point release and the next sqlalign — noise that says nothing about their
|
|
181
|
+
config. An OLDER sqlfluff is silent too: the floor on the `lint` extra is
|
|
182
|
+
what bounds that, and it is verified.
|
|
183
|
+
|
|
184
|
+
Anything this cannot positively rule out warns, since the cost of the note
|
|
185
|
+
is far below the cost of a config whose rules have quietly moved.
|
|
186
|
+
"""
|
|
169
187
|
installed = sqlfluff_version()
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
return
|
|
188
|
+
here, checked = _series(installed), _series(TESTED_SQLFLUFF)
|
|
189
|
+
if here is not None and checked is not None and here <= checked:
|
|
190
|
+
return None
|
|
191
|
+
return (f"sqlfluff {installed} installed, coexistence config was checked "
|
|
192
|
+
f"against {TESTED_SQLFLUFF} — rules may have moved")
|
sqlalign/py.typed
ADDED
|
File without changes
|
sqlalign/sqlfluffconfig.py
CHANGED
|
@@ -35,7 +35,10 @@ from sqlalign.style import Style
|
|
|
35
35
|
# so a future reader can tell whether the config predates their linter: rule
|
|
36
36
|
# names and config keys are versioned surface, and a silent mismatch reads as
|
|
37
37
|
# "sqlalign generated a broken config" rather than "these have drifted apart".
|
|
38
|
-
|
|
38
|
+
#
|
|
39
|
+
# Bumping it means re-running the coexistence tests against that release, which
|
|
40
|
+
# exercise every rule and config key below through the real linter.
|
|
41
|
+
TESTED_SQLFLUFF = "4.3.0"
|
|
39
42
|
|
|
40
43
|
# style knob -> (sqlfluff rule, config key, {style value: sqlfluff value})
|
|
41
44
|
_MAPPINGS = (
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: sqlalign
|
|
3
|
-
Version: 1.0.
|
|
3
|
+
Version: 1.0.2
|
|
4
4
|
Summary: A SQL formatter that cannot change what your SQL means
|
|
5
5
|
Project-URL: Homepage, https://sqlalign.lumaru.app/
|
|
6
6
|
Project-URL: Documentation, https://sqlalign.lumaru.app/v1/
|
|
@@ -139,14 +139,29 @@ LEFT JOIN shipping_addresses addr ON addr.order_id = ord.order_id
|
|
|
139
139
|
## Install
|
|
140
140
|
|
|
141
141
|
```sh
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
142
|
+
pip install sqlalign
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
That puts a `sqlalign` command on your PATH. To keep it out of a project's
|
|
146
|
+
environment, `uv tool install sqlalign` does the same thing in isolation.
|
|
147
|
+
|
|
148
|
+
`--lint` runs sqlfluff over the formatted result and needs the optional extra:
|
|
149
|
+
|
|
150
|
+
```sh
|
|
151
|
+
pip install 'sqlalign[lint]'
|
|
145
152
|
```
|
|
146
153
|
|
|
147
154
|
Requires Python ≥ 3.12. Runtime dependency: `sqlglot` (pinned to the 30.14.x
|
|
148
155
|
line — the layout engine depends on exact AST shapes; see `pyproject.toml`).
|
|
149
156
|
|
|
157
|
+
To work on sqlalign itself, install from a clone instead — that adds the dev
|
|
158
|
+
tools (`pytest`, `ruff`, `sqlfluff`):
|
|
159
|
+
|
|
160
|
+
```sh
|
|
161
|
+
git clone https://github.com/pua2/sqlalign && cd sqlalign
|
|
162
|
+
uv sync
|
|
163
|
+
```
|
|
164
|
+
|
|
150
165
|
## Usage
|
|
151
166
|
|
|
152
167
|
```sh
|
|
@@ -296,7 +311,7 @@ would otherwise mean a team believes it has a setting it doesn't have.
|
|
|
296
311
|
| `--lint` | off | after formatting, run sqlfluff over the result (needs `pip install 'sqlalign[lint]'`) |
|
|
297
312
|
| `--body-blank-lines N` | `1` | blank lines between the elements of a `$$` body |
|
|
298
313
|
| `--gui` | — | *(experimental)* open a settings panel with a live preview, and exit |
|
|
299
|
-
| `--report` | off | print a coverage summary: how many statements formatted, and what the rest declined on |
|
|
314
|
+
| `--report` | off | print a coverage summary: how many statements formatted, and what the rest declined on. Adds output without changing the mode — pair with `--check` to survey without writing |
|
|
300
315
|
| `--max-declines N` | — | exit 1 if more than N statements pass through unformatted (implies `--report`) |
|
|
301
316
|
| `--show-config` | off | print effective settings as TOML and exit |
|
|
302
317
|
| `--no-strict-config` | off | warn on unknown config keys instead of failing |
|
|
@@ -363,7 +378,9 @@ The fixtures are the spec — grow them, never weaken them:
|
|
|
363
378
|
|
|
364
379
|
## Documentation
|
|
365
380
|
|
|
366
|
-
The
|
|
381
|
+
The documentation site is at **<https://sqlalign.lumaru.app/>**.
|
|
382
|
+
|
|
383
|
+
The [settings reference](https://sqlalign.lumaru.app/v1/settings.html) is the page to start with: it
|
|
367
384
|
shows every setting with the same SQL rendered under each of its values.
|
|
368
385
|
|
|
369
386
|
To read the site locally:
|
|
@@ -372,8 +389,8 @@ To read the site locally:
|
|
|
372
389
|
python3 -m http.server -d docs 8000 # then open http://localhost:8000/
|
|
373
390
|
```
|
|
374
391
|
|
|
375
|
-
**The site is generated and committed.**
|
|
376
|
-
|
|
392
|
+
**The site is generated and committed.** GitHub Pages runs no build step, so the
|
|
393
|
+
HTML in `docs/v1/` is what ships. Prose is authored in
|
|
377
394
|
`docs/guide/*.md`; every configuration example is produced by *running the
|
|
378
395
|
formatter* at build time, so no example on the site can drift from what the tool
|
|
379
396
|
does. After editing a guide page, a setting, or anything that changes output:
|
|
@@ -393,8 +410,9 @@ working untouched.
|
|
|
393
410
|
## Contributing
|
|
394
411
|
|
|
395
412
|
See [CONTRIBUTING.md](CONTRIBUTING.md). In short: `uv sync`, then `uv run pytest
|
|
396
|
-
-q` and `uv run ruff check .` must both be clean
|
|
397
|
-
`tests/fixtures/expected/` are the
|
|
413
|
+
-q` and `uv run ruff check .` must both be clean — CI runs the same checks on
|
|
414
|
+
every pull request. The goldens in `tests/fixtures/expected/` are the
|
|
415
|
+
specification, not snapshots.
|
|
398
416
|
|
|
399
417
|
Release notes are in [CHANGELOG.md](CHANGELOG.md).
|
|
400
418
|
|
|
@@ -2,18 +2,19 @@ sqlalign/__init__.py,sha256=f0XHp1fXFgh6edJswAhQ9IBQl1TsI_BvQZZKBV3kH6Y,536
|
|
|
2
2
|
sqlalign/align.py,sha256=RzjlWERxjq9aNGhPA-E_TjS-5ntnOFEKiXzwBqFEfk4,7236
|
|
3
3
|
sqlalign/bodylint.py,sha256=lBmEIOkAQNWov8X4O9T4ynH4nOUiAsWrFomWSQkc6VQ,5085
|
|
4
4
|
sqlalign/casing.py,sha256=w97se03KoCe7V5y6UzKyepjxWZtSucGoBCpVBN0kb40,7866
|
|
5
|
-
sqlalign/cli.py,sha256=
|
|
5
|
+
sqlalign/cli.py,sha256=LLDlIZM77D6By8MXbTFDkBMEvUmHfGOAw6HlXGI-SZA,20565
|
|
6
6
|
sqlalign/commas.py,sha256=vJ0CXVpAvCrhrnkwk6o5U6y6o1TeJZ-mhKaz-w2xJdA,2855
|
|
7
7
|
sqlalign/config.py,sha256=VVfP9jPKAkRjim6ydUY4LhZTUv3lvKIrKU2McPR8faU,1063
|
|
8
8
|
sqlalign/configfile.py,sha256=74rjIl5mGJSyIn7bwO0aeKe7HOm-qN6pB4byz-L2JRE,8672
|
|
9
9
|
sqlalign/formatter.py,sha256=hgPKbqgCqhBFUKf_AKluUiJIFeMLApinemy1wxnyoYg,31079
|
|
10
|
-
sqlalign/gui.py,sha256=
|
|
10
|
+
sqlalign/gui.py,sha256=PVikAiZcgia6JtYONMg0fGYqNJLfh4AIck-a0ryVlvE,31081
|
|
11
11
|
sqlalign/ir.py,sha256=cHKTXgGOuOJgFQwsO7lTzVtca1ZKP-PFZYxybhKgmrY,1161
|
|
12
12
|
sqlalign/keywordcase.py,sha256=UA054AOnLVvVZCw6J4nzOKC73CY2xP6kOAUFCG-pgf4,5497
|
|
13
|
-
sqlalign/lint.py,sha256=
|
|
13
|
+
sqlalign/lint.py,sha256=JLf-vdtaFyQBHLtr3qFpPpkEJwOOuRzNdzJ_ahutK6k,8214
|
|
14
14
|
sqlalign/plpgsql.py,sha256=OPUo1rusB_HwH59jaHVfkp41S1C4wMWav9--3GeSu20,17547
|
|
15
|
+
sqlalign/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
15
16
|
sqlalign/splitter.py,sha256=gkp-yU9HNkC1lT7c3OwLYnFZUSAoyfihpaFxD3dhS0A,9821
|
|
16
|
-
sqlalign/sqlfluffconfig.py,sha256
|
|
17
|
+
sqlalign/sqlfluffconfig.py,sha256=-mixGe5K5duFPfzPcQOi0urExVlXPTYlQKFTvHsHB3s,4802
|
|
17
18
|
sqlalign/style.py,sha256=LjCxfOD--GVyDus1h99WyGJfQr1yDjIDVXntabvHJM0,11599
|
|
18
19
|
sqlalign/templating.py,sha256=o8J83gYH1PoabmbLjOSYIVLOpUrimzmPsd-oGp_pt6U,3793
|
|
19
20
|
sqlalign/layout/__init__.py,sha256=579cugPzcjKLruNwX8SbkUyWNu-r_t1s1eJJzPTf7cE,14436
|
|
@@ -30,8 +31,8 @@ sqlalign/layout/select.py,sha256=PHE4wuTScaJCpkZehvqaFLk_EqRlZYH7PdNTBFYk7bo,273
|
|
|
30
31
|
sqlalign/layout/setop.py,sha256=hjve3Qxi2CsaFEwijg6srhfMdEUlv2b9MmOUp6BNip0,3427
|
|
31
32
|
sqlalign/layout/subquery.py,sha256=IIi4Cy8ZQ3WNBmPvmifuTRofXcWibakHCaaj9Uh2MfA,12496
|
|
32
33
|
sqlalign/layout/window.py,sha256=X8YJ32hQGriwRQsDM2ZDvmaU6YHunJvcMi7UShmGJrU,9943
|
|
33
|
-
sqlalign-1.0.
|
|
34
|
-
sqlalign-1.0.
|
|
35
|
-
sqlalign-1.0.
|
|
36
|
-
sqlalign-1.0.
|
|
37
|
-
sqlalign-1.0.
|
|
34
|
+
sqlalign-1.0.2.dist-info/METADATA,sha256=-tBM0XfrbhQjLYpE3WF1fayau8aVmZIQ98b2hJ7V5qw,20224
|
|
35
|
+
sqlalign-1.0.2.dist-info/WHEEL,sha256=zOwg4jB6zX2kU910N-cMawjivD6tO8NEWvE12je1bVk,87
|
|
36
|
+
sqlalign-1.0.2.dist-info/entry_points.txt,sha256=pHPkRH7HQwwvx1mdDjdvGZ_xMXMg0gaWtTOmvrONPuc,47
|
|
37
|
+
sqlalign-1.0.2.dist-info/licenses/LICENSE,sha256=qvo2BvsFUcC3_3LOk36ZKcaMp_j1AFMfBoCNRFcyrco,1076
|
|
38
|
+
sqlalign-1.0.2.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|