-
Notifications
You must be signed in to change notification settings - Fork 66
/
sort_spelling_wordlist.py
executable file
·55 lines (40 loc) · 1.59 KB
/
sort_spelling_wordlist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
import argparse
import sys
def sort_and_clean_file(file_path):
"""Sort, deduplicate, and clean lines in the file, then write changes if necessary."""
with open(file_path, "r") as file_:
lines = file_.readlines()
# Remove blank lines and strip leading/trailing whitespace
cleaned_lines = [line.strip() for line in lines if line.strip()]
# Remove duplicates while preserving the original case
unique_lines = list(dict.fromkeys(cleaned_lines))
# Sort lines case-insensitively but preserve their original case
sorted_lines = sorted(unique_lines, key=str.lower)
# Prepare content strings
original_content = "".join(lines)
new_content = "\n".join(sorted_lines)
new_content.strip()
new_content += "\n"
# Write the file only if there's a change
if original_content != new_content:
with open(file_path, "w") as file:
file.write(new_content)
return True # Indicates changes were made
return False # Indicates no changes were made
def main():
"""Main function to handle argument parsing and call the sorting function."""
parser = argparse.ArgumentParser(description="Sort and clean lines in a text file.")
parser.add_argument(
"--qa",
action="store_true",
help="If true, script will exit with 1 if there's any change to spelling_wordlist.txt.",
)
args = parser.parse_args()
changes_made = sort_and_clean_file("./spelling_wordlist.txt")
if args.qa and changes_made:
sys.exit(1)
else:
sys.exit(0)
if __name__ == "__main__":
main()