sqlnbfmt 0.1.4__tar.gz → 0.1.5__tar.gz
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.
- {sqlnbfmt-0.1.4/sqlnbfmt.egg-info → sqlnbfmt-0.1.5}/PKG-INFO +4 -5
- {sqlnbfmt-0.1.4 → sqlnbfmt-0.1.5}/README.md +4 -5
- {sqlnbfmt-0.1.4 → sqlnbfmt-0.1.5}/pyproject.toml +1 -1
- {sqlnbfmt-0.1.4 → sqlnbfmt-0.1.5}/sqlnbfmt/formatter.py +29 -6
- {sqlnbfmt-0.1.4 → sqlnbfmt-0.1.5/sqlnbfmt.egg-info}/PKG-INFO +4 -5
- {sqlnbfmt-0.1.4 → sqlnbfmt-0.1.5}/LICENSE +0 -0
- {sqlnbfmt-0.1.4 → sqlnbfmt-0.1.5}/setup.cfg +0 -0
- {sqlnbfmt-0.1.4 → sqlnbfmt-0.1.5}/sqlnbfmt/__init__.py +0 -0
- {sqlnbfmt-0.1.4 → sqlnbfmt-0.1.5}/sqlnbfmt.egg-info/SOURCES.txt +0 -0
- {sqlnbfmt-0.1.4 → sqlnbfmt-0.1.5}/sqlnbfmt.egg-info/dependency_links.txt +0 -0
- {sqlnbfmt-0.1.4 → sqlnbfmt-0.1.5}/sqlnbfmt.egg-info/entry_points.txt +0 -0
- {sqlnbfmt-0.1.4 → sqlnbfmt-0.1.5}/sqlnbfmt.egg-info/requires.txt +0 -0
- {sqlnbfmt-0.1.4 → sqlnbfmt-0.1.5}/sqlnbfmt.egg-info/top_level.txt +0 -0
- {sqlnbfmt-0.1.4 → sqlnbfmt-0.1.5}/tests/test_formatter.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: sqlnbfmt
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.5
|
|
4
4
|
Summary: A tool to format SQL code in Jupyter notebooks.
|
|
5
5
|
Author-email: Qing <qingye779@gmail.com>
|
|
6
6
|
License: MIT
|
|
@@ -61,14 +61,13 @@ pip install pre-commit
|
|
|
61
61
|
2. Add to `.pre-commit-config.yaml`:
|
|
62
62
|
```yaml
|
|
63
63
|
repos:
|
|
64
|
-
- repo:
|
|
64
|
+
- repo: https://github.com/flyersworder/sqlnbfmt
|
|
65
|
+
rev: v0.1.1
|
|
65
66
|
hooks:
|
|
66
67
|
- id: sqlnbfmt
|
|
67
68
|
name: sqlnbfmt
|
|
68
|
-
entry: sqlnbfmt
|
|
69
|
-
language: system
|
|
70
69
|
types: [jupyter]
|
|
71
|
-
args: [--config, config.yaml, --dialect,
|
|
70
|
+
args: [--config, config.yaml, --dialect, postgres]
|
|
72
71
|
```
|
|
73
72
|
Please fun the following command in your CMD for help:
|
|
74
73
|
|
|
@@ -45,14 +45,13 @@ pip install pre-commit
|
|
|
45
45
|
2. Add to `.pre-commit-config.yaml`:
|
|
46
46
|
```yaml
|
|
47
47
|
repos:
|
|
48
|
-
- repo:
|
|
48
|
+
- repo: https://github.com/flyersworder/sqlnbfmt
|
|
49
|
+
rev: v0.1.1
|
|
49
50
|
hooks:
|
|
50
51
|
- id: sqlnbfmt
|
|
51
52
|
name: sqlnbfmt
|
|
52
|
-
entry: sqlnbfmt
|
|
53
|
-
language: system
|
|
54
53
|
types: [jupyter]
|
|
55
|
-
args: [--config, config.yaml, --dialect,
|
|
54
|
+
args: [--config, config.yaml, --dialect, postgres]
|
|
56
55
|
```
|
|
57
56
|
Please fun the following command in your CMD for help:
|
|
58
57
|
|
|
@@ -140,4 +139,4 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
|
|
|
140
139
|
- All contributors and early adopters who helped shape this tool
|
|
141
140
|
|
|
142
141
|
---
|
|
143
|
-
Made with ♥️ by the sqlnbfmt team
|
|
142
|
+
Made with ♥️ by the sqlnbfmt team
|
|
@@ -223,27 +223,41 @@ class SQLStringFormatter(ast.NodeTransformer):
|
|
|
223
223
|
return "".join(sql_parts), placeholders
|
|
224
224
|
|
|
225
225
|
def format_sql_node(
|
|
226
|
-
self,
|
|
226
|
+
self,
|
|
227
|
+
node: Union[ast.Constant, ast.JoinedStr],
|
|
228
|
+
force_single_line: bool = False,
|
|
229
|
+
in_function: bool = False
|
|
227
230
|
) -> Optional[ast.AST]:
|
|
228
231
|
"""Formats SQL code in AST nodes."""
|
|
229
232
|
try:
|
|
230
233
|
if isinstance(node, ast.Constant) and isinstance(node.value, str):
|
|
231
234
|
if not self.is_likely_sql(node.value):
|
|
232
235
|
return None
|
|
236
|
+
|
|
233
237
|
formatted_sql = format_sql_code(
|
|
234
238
|
node.value,
|
|
235
239
|
self.dialect,
|
|
236
240
|
self.config,
|
|
237
241
|
force_single_line=force_single_line,
|
|
238
242
|
)
|
|
243
|
+
|
|
239
244
|
if formatted_sql != node.value:
|
|
240
245
|
self.changed = True
|
|
241
|
-
|
|
246
|
+
if in_function and '\n' in formatted_sql:
|
|
247
|
+
# Apply Black-style indentation for function arguments
|
|
248
|
+
lines = formatted_sql.split('\n')
|
|
249
|
+
indented_lines = [lines[0]] + [
|
|
250
|
+
" " * 4 + line if line.strip() else line
|
|
251
|
+
for line in lines[1:-1]
|
|
252
|
+
] + [" " * 4 + lines[-1] if lines[-1].strip() else lines[-1]]
|
|
253
|
+
formatted_sql = '\n'.join(indented_lines)
|
|
254
|
+
|
|
255
|
+
# Reconstruct the string with appropriate quotes
|
|
242
256
|
if '\n' in formatted_sql:
|
|
243
257
|
formatted_str = f'"""\n{formatted_sql}\n"""'
|
|
244
258
|
else:
|
|
245
259
|
formatted_str = f'"""{formatted_sql}"""'
|
|
246
|
-
|
|
260
|
+
|
|
247
261
|
formatted_node = ast.parse(formatted_str).body[0].value
|
|
248
262
|
return formatted_node
|
|
249
263
|
|
|
@@ -273,10 +287,18 @@ class SQLStringFormatter(ast.NodeTransformer):
|
|
|
273
287
|
|
|
274
288
|
if formatted_sql != sql_str:
|
|
275
289
|
self.changed = True
|
|
290
|
+
# Apply Black-style indentation for function arguments
|
|
291
|
+
if in_function and '\n' in formatted_sql:
|
|
292
|
+
lines = formatted_sql.split('\n')
|
|
293
|
+
indented_lines = [lines[0]] + [
|
|
294
|
+
" " * 4 + line if line.strip() else line
|
|
295
|
+
for line in lines[1:-1]
|
|
296
|
+
] + [" " * 4 + lines[-1] if lines[-1].strip() else lines[-1]]
|
|
297
|
+
formatted_sql = '\n'.join(indented_lines)
|
|
298
|
+
|
|
276
299
|
# Reconstruct the f-string
|
|
277
300
|
new_values = []
|
|
278
301
|
if not placeholders:
|
|
279
|
-
# If no placeholders, just create a simple f-string
|
|
280
302
|
new_values.append(ast.Constant(value=formatted_sql))
|
|
281
303
|
else:
|
|
282
304
|
pattern = re.compile('|'.join(re.escape(k) for k in placeholders.keys() if k))
|
|
@@ -340,12 +362,13 @@ class SQLStringFormatter(ast.NodeTransformer):
|
|
|
340
362
|
if any(name in func_name for name in self.config.function_names):
|
|
341
363
|
for idx, arg in enumerate(node.args):
|
|
342
364
|
if isinstance(arg, (ast.Constant, ast.JoinedStr)):
|
|
343
|
-
|
|
365
|
+
# Format with Black-style indentation for function arguments
|
|
366
|
+
formatted_node = self.format_sql_node(arg, in_function=True)
|
|
344
367
|
if formatted_node:
|
|
345
368
|
node.args[idx] = formatted_node
|
|
346
369
|
for keyword in node.keywords:
|
|
347
370
|
if isinstance(keyword.value, (ast.Constant, ast.JoinedStr)):
|
|
348
|
-
formatted_node = self.format_sql_node(keyword.value)
|
|
371
|
+
formatted_node = self.format_sql_node(keyword.value, in_function=True)
|
|
349
372
|
if formatted_node:
|
|
350
373
|
keyword.value = formatted_node
|
|
351
374
|
return self.generic_visit(node)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: sqlnbfmt
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.5
|
|
4
4
|
Summary: A tool to format SQL code in Jupyter notebooks.
|
|
5
5
|
Author-email: Qing <qingye779@gmail.com>
|
|
6
6
|
License: MIT
|
|
@@ -61,14 +61,13 @@ pip install pre-commit
|
|
|
61
61
|
2. Add to `.pre-commit-config.yaml`:
|
|
62
62
|
```yaml
|
|
63
63
|
repos:
|
|
64
|
-
- repo:
|
|
64
|
+
- repo: https://github.com/flyersworder/sqlnbfmt
|
|
65
|
+
rev: v0.1.1
|
|
65
66
|
hooks:
|
|
66
67
|
- id: sqlnbfmt
|
|
67
68
|
name: sqlnbfmt
|
|
68
|
-
entry: sqlnbfmt
|
|
69
|
-
language: system
|
|
70
69
|
types: [jupyter]
|
|
71
|
-
args: [--config, config.yaml, --dialect,
|
|
70
|
+
args: [--config, config.yaml, --dialect, postgres]
|
|
72
71
|
```
|
|
73
72
|
Please fun the following command in your CMD for help:
|
|
74
73
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|