Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:

- name: Run checks
run: |
python3 precommit.py
python3 continuous_integration/precommit.py

- name: Upload Coverage
run: coveralls --service=github
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Development

.. code-block:: bash

python precommit.py
python continuous_integration/precommit.py

Versioning
==========
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ def parse_readme(lines: List[str]) -> Tuple[List[Block], List[str]]:
mtch = HELP_STARTS_RE.match(lines[i])
if mtch:
command = mtch.group("command")
help_ends = ".. Help ends: {}".format(command)
help_ends = f".. Help ends: {command}"
try:
end_index = lines.index(help_ends, i)
except ValueError:
end_index = -1

if end_index == -1:
return [], ["Could not find the end marker {!r}".format(help_ends)]
return [], [f"Could not find the end marker {help_ends!r}"]

blocks.append(
Block(command=command, start_line_idx=i + 1, end_line_idx=end_index)
Expand All @@ -84,18 +84,18 @@ def capture_output_lines(command: str) -> List[str]:
f"The python interpreter could not be found: {command_parts[0]}"
)

proc = subprocess.Popen(
with subprocess.Popen(
command_parts,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
)
output, err = proc.communicate()
if err:
raise RuntimeError(
f"The command {command!r} failed with exit code {proc.returncode} and "
f"stderr:\n{err}"
)
) as proc:
output, err = proc.communicate()
if err:
raise RuntimeError(
f"The command {command!r} failed with exit code {proc.returncode} and "
f"stderr:\n{err}"
)

return output.splitlines()

Expand Down Expand Up @@ -126,16 +126,16 @@ def diff(got_lines: List[str], expected_lines: List[str]) -> Optional[str]:
result.append("Expected:")
for i, line in enumerate(expected_lines):
if i >= len(got_lines) or line != got_lines[i]:
print("DIFF: {:2d}: {!r}".format(i, line))
print(f"DIFF: {i:2d}: {line!r}")
else:
print("OK : {:2d}: {!r}".format(i, line))
print(f"OK : {i:2d}: {line!r}")

result.append("Got:")
for i, line in enumerate(got_lines):
if i >= len(expected_lines) or line != expected_lines[i]:
print("DIFF: {:2d}: {!r}".format(i, line))
print(f"DIFF: {i:2d}: {line!r}")
else:
print("OK : {:2d}: {!r}".format(i, line))
print(f"OK : {i:2d}: {line!r}")

return "\n".join(result)

Expand All @@ -152,20 +152,22 @@ def main() -> int:
args = parser.parse_args()
overwrite = bool(args.overwrite)

this_dir = pathlib.Path(os.path.realpath(__file__)).parent
pth = this_dir / "README.rst"
repo_root = pathlib.Path(os.path.realpath(__file__)).parent.parent
pth = repo_root / "README.rst"

text = pth.read_text(encoding="utf-8")
lines = text.splitlines()

blocks, errors = parse_readme(lines=lines)
if errors:
print("One or more errors in {}:".format(pth), file=sys.stderr)
print(f"One or more errors in {pth}:", file=sys.stderr)
for error in errors:
print(error, file=sys.stderr)

return -1

assert blocks is not None

if len(blocks) == 0:
return 0

Expand All @@ -185,6 +187,7 @@ def main() -> int:
result.extend(code_block_lines)
previous_block = block

assert previous_block is not None
result.extend(lines[previous_block.end_line_idx :])
result.append("") # new line at the end of file

Expand All @@ -198,9 +201,11 @@ def main() -> int:
expected_lines = lines[block.start_line_idx : block.end_line_idx]
expected_lines = [line.rstrip() for line in expected_lines]

error = diff(got_lines=code_block_lines, expected_lines=expected_lines)
if error:
print(error, file=sys.stderr)
maybe_error = diff(
got_lines=code_block_lines, expected_lines=expected_lines
)
if maybe_error:
print(maybe_error, file=sys.stderr)
return -1

return 0
Expand Down
36 changes: 24 additions & 12 deletions precommit.py → continuous_integration/precommit.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import subprocess
import sys

# pylint: disable=missing-docstring


class Step(enum.Enum):
BLACK = "black"
Expand Down Expand Up @@ -60,21 +62,20 @@ def main() -> int:
selects = (
[Step(value) for value in args.select]
if args.select is not None
else [value for value in Step]
else [value for value in Step] # pylint: disable=unnecessary-comprehension
)
skips = [Step(value) for value in args.skip] if args.skip is not None else []

repo_root = pathlib.Path(__file__).parent
repo_root = pathlib.Path(__file__).parent.parent

if Step.BLACK in selects and Step.BLACK not in skips:
print("Black'ing...")
# fmt: off
black_targets = [
"abnf_to_regexp",
"precommit.py",
"check_version_consistent.py",
"check_help_in_readme.py",
"dev_scripts"
"continuous_integration",
"dev_scripts",
"tests"
]
# fmt: on

Expand All @@ -93,7 +94,12 @@ def main() -> int:
if Step.MYPY in selects and Step.MYPY not in skips:
print("Mypy'ing...")
# fmt: off
mypy_targets = ["abnf_to_regexp", "tests", "dev_scripts"]
mypy_targets = [
"abnf_to_regexp",
"tests",
"dev_scripts",
"continuous_integration"
]
subprocess.check_call(
[
sys.executable,
Expand All @@ -108,13 +114,17 @@ def main() -> int:
if Step.PYLINT in selects and Step.PYLINT not in skips:
# fmt: off
print("Pylint'ing...")
pylint_targets = ["abnf_to_regexp", "tests", "dev_scripts"]
pylint_targets = [
"abnf_to_regexp",
"tests",
"dev_scripts",
"continuous_integration"
]
subprocess.check_call(
[
sys.executable,
"-m",
"pylint",
"--rcfile=pylint.rc"
"pylint"
] + pylint_targets, cwd=str(repo_root)
)
# fmt: on
Expand Down Expand Up @@ -166,7 +176,9 @@ def main() -> int:
"Checking that the version is consistent between "
"abnf_to_regexp/__init__.py and pyproject.toml ..."
)
subprocess.check_call([sys.executable, "check_version_consistent.py"])
subprocess.check_call(
[sys.executable, "continuous_integration/check_version_consistent.py"]
)
else:
print(
"Skipped checking that the versions in abnf_to_regexp/__init__.py "
Expand All @@ -175,7 +187,7 @@ def main() -> int:

if Step.CHECK_HELP_IN_README in selects and Step.CHECK_HELP_IN_README not in skips:
if sys.version_info < (3, 10):
cmd = [sys.executable, "check_help_in_readme.py"]
cmd = [sys.executable, "continuous_integration/check_help_in_readme.py"]
if overwrite:
cmd.append("--overwrite")

Expand Down
10 changes: 0 additions & 10 deletions mypy.ini

This file was deleted.

11 changes: 0 additions & 11 deletions pylint.rc

This file was deleted.

39 changes: 38 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,41 @@ packages = ["abnf_to_regexp"]
abnf_to_regexp = ["py.typed"]

[tool.setuptools.exclude-package-data]
"*" = ["tests*"]
"*" = ["tests*"]

[tool.pylint.format]
max-line-length = 120

[tool.pylint."messages control"]
disable = [
"too-few-public-methods",
"len-as-condition",
"duplicate-code",
"no-else-raise",
"no-else-return",
"too-many-locals",
"too-many-branches",
"too-many-nested-blocks",
"too-many-return-statements",
"unsubscriptable-object",
"not-an-iterable",
"broad-except",
"too-many-statements",
"protected-access",
"unnecessary-pass",
"use-dict-literal",
]

[tool.mypy]

[[tool.mypy.overrides]]
module = "abnf"
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = "regex"
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = "sortedcontainers"
ignore_missing_imports = true
4 changes: 0 additions & 4 deletions requirements.txt

This file was deleted.

33 changes: 13 additions & 20 deletions tests/test_abnf_transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ def test_wider_than_gamuth(self) -> None:
)

def test_misses_between_ranges(self) -> None:
for (_, end1), (start2, _) in (
abnf_to_regexp.abnf_transformation.pairwise(
abnf_to_regexp.abnf_transformation._LETTER_ORD_RANGES
)
for (_, end1), (start2, _) in abnf_to_regexp.abnf_transformation.pairwise(
abnf_to_regexp.abnf_transformation._LETTER_ORD_RANGES
):
if start2 - end1 <= 2:
continue
Expand All @@ -58,39 +56,35 @@ def test_misses_between_ranges(self) -> None:
)

def test_hits_with_start_and_end_between_ranges(self) -> None:
for (start1, end1), (start2, end2) in (
abnf_to_regexp.abnf_transformation.pairwise(
abnf_to_regexp.abnf_transformation._LETTER_ORD_RANGES
)
for (start1, end1), (
start2,
end2,
) in abnf_to_regexp.abnf_transformation.pairwise(
abnf_to_regexp.abnf_transformation._LETTER_ORD_RANGES
):
self.assertTrue(
abnf_to_regexp.abnf_transformation._range_overlaps_with_a_letter_range(
start_ord=min(start1 + 1, end1),
end_ord=min(start2 + 1, end2)
start_ord=min(start1 + 1, end1), end_ord=min(start2 + 1, end2)
)
)

def test_hit_with_point_ranges(self) -> None:
for start, end in abnf_to_regexp.abnf_transformation._LETTER_ORD_RANGES:
self.assertTrue(
abnf_to_regexp.abnf_transformation._range_overlaps_with_a_letter_range(
start_ord=start,
end_ord=start
start_ord=start, end_ord=start
)
)

self.assertTrue(
abnf_to_regexp.abnf_transformation._range_overlaps_with_a_letter_range(
start_ord=end,
end_ord=end
start_ord=end, end_ord=end
)
)

def test_misses_between_ranges_with_point_range(self) -> None:
for (_, end1), (start2, _) in (
abnf_to_regexp.abnf_transformation.pairwise(
abnf_to_regexp.abnf_transformation._LETTER_ORD_RANGES
)
for (_, end1), (start2, _) in abnf_to_regexp.abnf_transformation.pairwise(
abnf_to_regexp.abnf_transformation._LETTER_ORD_RANGES
):
if start2 - end1 <= 1:
continue
Expand All @@ -104,8 +98,7 @@ def test_misses_between_ranges_with_point_range(self) -> None:
def test_with_explicit_case(self) -> None:
self.assertTrue(
abnf_to_regexp.abnf_transformation._range_overlaps_with_a_letter_range(
start_ord=ord('b'),
end_ord=ord('d')
start_ord=ord("b"), end_ord=ord("d")
)
)

Expand Down
Loading