|
| 1 | +import argparse |
| 2 | +import re |
| 3 | +import sys |
| 4 | + |
| 5 | +def is_one_sentence_per_line(line) -> bool: |
| 6 | + """ |
| 7 | + Check if a line contains only one complete sentence. |
| 8 | + |
| 9 | + :param line: The line of file to check. |
| 10 | + """ |
| 11 | + # this assumes a sentence ends with a period, question mark, or exclamation mark. |
| 12 | + sentence_endings = re.findall(r'[.!?]', line) |
| 13 | + # allow empty lines or lines with only one sentence |
| 14 | + return len(sentence_endings) <= 1 |
| 15 | + |
| 16 | +def check_changed_lines(filename) -> None: |
| 17 | + """ |
| 18 | + Check only changed lines for violations. |
| 19 | +
|
| 20 | + .. warning:: It checks only added / modified lines for the viaolation in the file, |
| 21 | + and ignores everything else including removed lines. |
| 22 | + |
| 23 | + :param filename: The name of the file to check. |
| 24 | + """ |
| 25 | + with open(filename, 'r', encoding='utf-8') as file: |
| 26 | + lines = file.readlines() |
| 27 | + |
| 28 | + violations = [] |
| 29 | + for lineno, line in enumerate(lines, start=1): |
| 30 | + line = line.strip() |
| 31 | + # check only added lines, ignore everything else |
| 32 | + if line.startswith("+") and not is_one_sentence_per_line(line[1:]): |
| 33 | + violations.append((lineno, line[1:])) |
| 34 | + |
| 35 | + if violations: |
| 36 | + print(f"\n⚠️ Found {len(violations)} violations:") |
| 37 | + for lineno, line in violations: |
| 38 | + print(f" ❌ Line {lineno}: {line}") |
| 39 | + # exit with non-zero status code to fail github actions |
| 40 | + sys.exit(1) |
| 41 | + else: |
| 42 | + print("✅ No violations found.") |
| 43 | + |
| 44 | +if __name__ == "__main__": |
| 45 | + parser = argparse.ArgumentParser( |
| 46 | + description="Check if modified contents contain only one complete sentence per line.") |
| 47 | + parser.add_argument( |
| 48 | + 'filename', type=str, help='The name of the file to check.') |
| 49 | + args = parser.parse_args() |
| 50 | + |
| 51 | + check_changed_lines(args.filename) |
0 commit comments