1919
2020import argparse
2121from pathlib import Path
22+ from sys import stderr
2223
2324DESCRIPTION = """
2425Add words to the codespell whitelist sanely
3536 "-v" , "--verbose" , action = "store_true" , help = "Enable verbose output"
3637)
3738
39+
3840def find_project_root () -> Path :
39- """Locate the root directory of this project"""
41+ """Locate the root directory of this project. """
4042 # Search upwards from script location
4143 script_dir = Path (__file__ ).resolve ().parent
4244 for parent in [script_dir , * script_dir .parents ]:
@@ -46,60 +48,76 @@ def find_project_root() -> Path:
4648 raise FileNotFoundError ("Unable to locate project root directory!" )
4749
4850
49- def main () -> None :
51+ def main () -> int :
5052 """
5153 `whitelist` accepts any number of strings, adds them to the whitelist, then
5254 sorts the list and maintain visible sections for each leading character.
55+
56+ Returns:
57+ - 0 on success
58+ - 1 on error
59+
5360 """
5461 args = parser .parse_args ()
5562 new_words = args .words
5663 verbose = args .verbose
5764
58- project_root = find_project_root ()
59- whitelist_file = project_root / "whitelist.txt"
65+ try :
66+ project_root = find_project_root ()
67+ whitelist_file = project_root / "whitelist.txt"
6068
61- # Read existing whitelist (create empty list if file doesn't exist)
62- existing_words = []
63- with open (whitelist_file , "r+" , encoding = "utf-8" ) as f :
64- content = f .read ()
65- existing_words = [w .strip () for w in content .split ("\n " ) if w .strip ()]
69+ # Read existing whitelist (create empty list if file doesn't exist)
70+ existing_words = []
71+ with open (whitelist_file , "r+" , encoding = "utf-8" ) as f :
72+ content = f .read ()
73+ existing_words = [w .strip () for w in content .split ("\n " ) if w .strip ()]
6674
67- if verbose :
68- print (f"Adding { len (new_words )} new words: { new_words } " )
75+ if verbose :
76+ print (f"Adding { len (new_words )} new words: { new_words } " )
6977
70- # Combine and remove duplicates
71- all_words = set (existing_words + new_words )
78+ # Combine and remove duplicates
79+ all_words = set (existing_words + new_words )
7280
73- if verbose :
74- print (f"Total unique entries: { len (all_words )} " )
81+ if verbose :
82+ print (f"Total unique entries: { len (all_words )} " )
7583
76- # Sort alphabetically
77- sorted_words = sorted (all_words , key = str .casefold )
84+ # Sort alphabetically
85+ sorted_words = sorted (all_words , key = str .casefold )
7886
79- # Add blank lines before each new letter
80- words_with_separators = []
81- previous_letter = ""
87+ # Add blank lines before each new letter
88+ words_with_separators : list [ str ] = []
89+ previous_letter = ""
8290
83- for word in sorted_words :
84- current_letter = word [0 ].lower ()
91+ for word in sorted_words :
92+ current_letter = word [0 ].lower ()
8593
86- if current_letter != previous_letter and words_with_separators :
87- words_with_separators .append ("" )
94+ if current_letter != previous_letter and words_with_separators :
95+ words_with_separators .append ("" )
8896
89- words_with_separators .append (word )
90- previous_letter = current_letter
97+ words_with_separators .append (word )
98+ previous_letter = current_letter
9199
92- if verbose :
93- print (f"Added { word } " )
100+ if verbose :
101+ print (f"Added { word } " )
102+
103+ # Create the whitelist content
104+ whitelist_content = "\n " .join (words_with_separators )
105+
106+ # Write to whitelist file
107+ with open (whitelist_file , "w" , encoding = "utf-8" ) as f :
108+ f .write (whitelist_content )
109+
110+ print (f"Successfully updated { whitelist_file } " )
94111
95- # Create the whitelist content
96- whitelist_content = "\n " .join (words_with_separators )
112+ except KeyboardInterrupt :
113+ print ("Aborted manually." , file = stderr )
114+ return 1
97115
98- # Write to whitelist file
99- with open ( whitelist_file , "w" , encoding = "utf-8" ) as f :
100- f . write ( whitelist_content )
116+ except Exception as err :
117+ print ( f"Unknown error! { err } " )
118+ return 1
101119
102- print ( f" \n Successfully updated { whitelist_file } " )
120+ return 0
103121
104122
105123if __name__ == "__main__" :
0 commit comments