Skip to content

Commit

Permalink
Add Python 3.11 beta3 (#101)
Browse files Browse the repository at this point in the history
* There was a pylint error when importing linesep from os, but actually
  we shouldn't be using that, based on https://docs.python.org/3/library/os.html?highlight=linesep#os.linesep
* update github action and readme
  • Loading branch information
oliverxchen committed Jun 5, 2022
1 parent 4c7136b commit 7f69bb9
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10"] # , "3.11.0-beta.1"]
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11.0-beta.3"]

steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<p align="center">
<a href='https://coveralls.io/github/oliverxchen/sqlean'><img src='https://coveralls.io/repos/github/oliverxchen/sqlean/badge.svg' alt='Coverage Status' /></a>
<a href="https://www.python.org/"><img alt="Python versions: 3.7, 3.8, 3.9, 3.10" src="https://img.shields.io/badge/python-3.7%20%7C%203.8%20%7C%203.9%20%7C%203.10-blue"></a>
<a href="https://www.python.org/"><img alt="Python versions: 3.7, 3.8, 3.9, 3.10" src="https://img.shields.io/badge/python-3.7%20%7C%203.8%20%7C%203.9%20%7C%203.10%20%7C%203.11b-blue"></a>
<a href="https://pypi.org/project/sqlean/"><img alt="PyPI" src="https://img.shields.io/pypi/v/sqlean"></a>
<a href="https://github.com/psf/black"><img alt="Code style: black" src="https://img.shields.io/badge/code%20style-black-000000.svg"></a>
<a href="https://github.com/oliverxchen/sqlean/blob/main/LICENSE"><img alt="License: MIT" src="https://img.shields.io/badge/license-MIT-A31F34"></a>
Expand Down
11 changes: 6 additions & 5 deletions sqlean/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""CLI commands"""

import difflib
from os import linesep
from pathlib import Path
import re
from typing import Iterator, List, Optional
Expand All @@ -16,6 +16,7 @@


app = typer.Typer(add_completion=False)
LINESEP = "\n" # https://docs.python.org/3/library/os.html?highlight=linesep#os.linesep


@app.command()
Expand Down Expand Up @@ -175,7 +176,7 @@ def print_diff(raw: str, styled: str, target: Path) -> None:
tofile="with sqlean",
)
diff_str = process_diffs(diff)
diff_md = Markdown(f"""```diff{linesep}{diff_str}{linesep}```""")
diff_md = Markdown(f"""```diff{LINESEP}{diff_str}{LINESEP}```""")
rprint(diff_md)


Expand All @@ -191,8 +192,8 @@ def process_diffs(diff: Iterator[str]) -> str:
def write_file(styled: str, target: Path) -> None:
"""Writes the sqleaned file."""
# Ensure the file ends with a new line
if styled[-1] != linesep:
styled += linesep
if styled[-1] != LINESEP:
styled += LINESEP
with open(target, "wt", encoding="utf-8") as writer:
writer.write(styled)

Expand All @@ -201,7 +202,7 @@ def write_ignore_header(target: Path) -> None:
"""Writes the `# sqlean ignore` header."""
with open(target, "rt", encoding="utf-8") as reader:
content = reader.read()
write_file(f"# sqlean ignore{linesep}{content}", target)
write_file(f"# sqlean ignore{LINESEP}{content}", target)


def remove_ignore_header(target: Path, raw_list: List[str]) -> str:
Expand Down
64 changes: 33 additions & 31 deletions sqlean/sql_styler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Styling of SQL queries"""

from os import linesep
from typing import List, Optional, Set, Union

import black
Expand All @@ -11,6 +10,9 @@
from sqlean.custom_classes import CData, CToken, CTree


LINESEP = "\n" # https://docs.python.org/3/library/os.html?highlight=linesep#os.linesep


class CommentedListClass: # pylint: disable=too-many-instance-attributes
"""Generic object to hold a list of children that may contain comments, to style
them properly"""
Expand All @@ -19,7 +21,7 @@ def __init__( # pylint: disable=too-many-arguments, dangerous-default-value
self,
children: List[Union[CToken, CTree]] = [],
separator: str = ",",
line_separator: str = linesep,
line_separator: str = LINESEP,
has_ending_separator: bool = True,
item_types: Set[str] = set(),
indent: str = " ",
Expand Down Expand Up @@ -91,17 +93,17 @@ def _append_comment(
this_separator = self._get_separator_by_idx(idx - 1, is_inline=True)
self.output[-1] += this_separator
if lines_between == 0:
self.output.append(lines_between * linesep + " " + str(child))
self.output.append(lines_between * LINESEP + " " + str(child))
else:
self.output.append(lines_between * linesep + self._indent_if_comment(child))
self.output.append(lines_between * LINESEP + self._indent_if_comment(child))

def _indent_if_comment(self, child: Union[CToken, CTree]) -> str:
if isinstance(child, Token) and child.type == "COMMENT":
lines = [
f"{self.indent * child.type.indent_level}{line}"
for line in child.split(linesep)
for line in child.split(LINESEP)
]
return linesep.join(lines)
return LINESEP.join(lines)
return str(child)

def _get_separator_by_idx(self, idx: int, is_inline: bool) -> str:
Expand Down Expand Up @@ -145,8 +147,8 @@ def _simple_indent(self, node: CTree) -> str:

def _apply_indent(self, text: str, indent_level: int) -> str:
"""Apply indentation to text"""
lines = [f"{self.indent * indent_level}{line}" for line in text.split(linesep)]
return linesep.join(lines)
lines = [f"{self.indent * indent_level}{line}" for line in text.split(LINESEP)]
return LINESEP.join(lines)

def _token_indent_adjust_case(self, token: CToken) -> str:
return self._apply_indent(token.upper(), token.type.indent_level)
Expand All @@ -165,7 +167,7 @@ def _rollup(self, node: CTree) -> str:

def _rollup_linesep(self, node: CTree) -> str:
"""Join list with linesep"""
return linesep.join(self._stringify_children(node))
return LINESEP.join(self._stringify_children(node))

def _rollup_space(self, node: CTree) -> str:
"""Join list with space"""
Expand All @@ -187,11 +189,11 @@ def _apply_black(self, input_str: str) -> str:
def _change_indent_size(input_str: str, indent_size: int) -> str:
"""Change the default indent size of 4 used by black to the specified
indent size"""
lines = input_str.split(linesep)
lines = input_str.split(LINESEP)
for i, line in enumerate(lines):
if line.startswith(" " * 4):
lines[i] = line.replace(" " * 4, " " * indent_size)
return linesep.join(lines)
return LINESEP.join(lines)

def _rollup_list( # pylint: disable=too-many-arguments
self,
Expand Down Expand Up @@ -239,7 +241,7 @@ def COMMENT(self, token: Token) -> Token: # pylint: disable=invalid-name

@staticmethod
def _format_block_comment(block_comment: str) -> str:
lines = [line.lstrip() for line in block_comment.split(linesep)]
lines = [line.lstrip() for line in block_comment.split(LINESEP)]
output: List[str] = []
for line in lines:
if line.startswith("/*"):
Expand All @@ -248,7 +250,7 @@ def _format_block_comment(block_comment: str) -> str:
output.append(line)
else:
output.append(f" {line}")
return linesep.join(output)
return LINESEP.join(output)

def STANDARD_TABLE_NAME(self, token: CToken) -> str: # pylint: disable=invalid-name
"""print STANDARD_TABLE_NAME"""
Expand Down Expand Up @@ -282,7 +284,7 @@ def query_file(self, node: CTree) -> str:
output = self._rollup_list(
children=list(node.children),
separator="",
line_separator=2 * linesep,
line_separator=2 * LINESEP,
has_ending_separator=True,
item_types={"dbt_config"},
)
Expand All @@ -295,7 +297,7 @@ def query_expr(self, node: CTree) -> str:
return self._rollup_list(
children=self._treat_commas_in_query_expr(node),
separator="",
line_separator=2 * linesep,
line_separator=2 * LINESEP,
has_ending_separator=False,
item_types=set(),
)
Expand Down Expand Up @@ -327,9 +329,9 @@ def sub_query_expr(self, node: CTree) -> CToken:
"""Returns sub_query_expr as a CToken for identification by parent"""
output = (
self._apply_indent(str(node.children[0]), node.data.indent_level)
+ linesep
+ LINESEP
+ str(node.children[1])
+ linesep
+ LINESEP
+ self._apply_indent(str(node.children[2]), node.data.indent_level)
)
return CToken(
Expand All @@ -344,9 +346,9 @@ def with_clause(self, node: CTree) -> CToken:

value = (
self._apply_indent(f"{node.children[0]} AS (", node.data.indent_level)
+ linesep
+ LINESEP
+ str(node.children[3])
+ linesep
+ LINESEP
+ self._apply_indent(str(node.children[4]), node.data.indent_level)
)
return CToken(
Expand Down Expand Up @@ -388,7 +390,7 @@ def select_list(self, node: CTree) -> str:
return self._rollup_list(
children=list(node.children),
separator=",",
line_separator=linesep,
line_separator=LINESEP,
has_ending_separator=True,
item_types={"select_item"},
)
Expand Down Expand Up @@ -438,7 +440,7 @@ def when_list(self, node: CTree) -> str:
def common_case_expression(node: CTree) -> str:
"""print common_case_expression"""
case_line = f"CASE {node.children[1]}\n"
other_lines = linesep.join([str(item) for item in node.children[2:]])
other_lines = LINESEP.join([str(item) for item in node.children[2:]])
return case_line + other_lines

def else_clause(self, node: CTree) -> str:
Expand Down Expand Up @@ -482,7 +484,7 @@ def from_item(self, node: CTree) -> str:
return self._rollup_list(
children=list(node.children),
separator="",
line_separator=linesep,
line_separator=LINESEP,
has_ending_separator=False,
item_types=set(),
)
Expand Down Expand Up @@ -537,7 +539,7 @@ def using_list(self, node: CTree) -> str:
def on_clause(self, node: CTree) -> str:
"""rollup on_clause"""
output = self._apply_indent("ON", node.data.indent_level)
return output + linesep + str(node.children[1])
return output + LINESEP + str(node.children[1])


@v_args(tree=True)
Expand All @@ -548,9 +550,9 @@ class FromModifierMixin(BaseMixin):
def groupby_modifier(self, node: CTree) -> str:
"""rollup groupby_modifier"""
output = self._apply_indent("GROUP BY", node.data.indent_level)
output += linesep + str(node.children[1])
output += LINESEP + str(node.children[1])
if len(node.children) == 3:
output += linesep + str(node.children[2])
output += LINESEP + str(node.children[2])
return output

def field_list(self, node: CTree) -> str:
Expand All @@ -562,7 +564,7 @@ def field_list(self, node: CTree) -> str:
def orderby_modifier(self, node: CTree) -> str:
"""rollup orderby_modifier"""
output = self._apply_indent("ORDER BY", node.data.indent_level)
return output + linesep + str(node.children[1])
return output + LINESEP + str(node.children[1])

def orderby_list(self, node: CTree) -> str:
"""rollup orderby_list"""
Expand All @@ -577,7 +579,7 @@ def orderby_item(self, node: CTree) -> str:
def having_clause(self, node: CTree) -> str:
"""print having_clause"""
output = self._apply_indent("HAVING", node.data.indent_level)
return output + linesep + str(node.children[1])
return output + LINESEP + str(node.children[1])

def limit_clause(self, node: CTree) -> str:
"""rollup limit_clause"""
Expand Down Expand Up @@ -612,7 +614,7 @@ def arg_list(self, node: CTree) -> str:

def arg_item(self, node: CTree) -> str:
"""print arg_item"""
return self._rollup_space(node).replace(linesep, " ")
return self._rollup_space(node).replace(LINESEP, " ")

def data_type(self, node: CTree) -> str:
"""print data_type"""
Expand Down Expand Up @@ -693,19 +695,19 @@ def binary_bool_operation(node: CTree) -> str:
list_ = CommentedListClass(
children=list(node.children),
separator="",
line_separator=linesep,
line_separator=LINESEP,
has_ending_separator=False,
indent="",
)
output = list_.rollup_list()
return output.replace("AND" + linesep, "AND ").replace("OR" + linesep, "OR ")
return output.replace("AND" + LINESEP, "AND ").replace("OR" + LINESEP, "OR ")

@staticmethod
def parenthetical_bool_expression(node: CTree) -> str:
"""print parenthetical_bool_expression, removes line separators when
parenthesized"""
# TODO: Deal with long lines
output = str(node.children[1]).replace(linesep, " ")
output = str(node.children[1]).replace(LINESEP, " ")
return f"({output})"

def indented_bool_expression(self, node: CTree) -> str:
Expand Down

0 comments on commit 7f69bb9

Please sign in to comment.