]>
Commit | Line | Data |
---|---|---|
65465a42 MF |
1 | #!/usr/bin/env python3 |
2 | ||
3 | """Update & sort personal dictionaries.""" | |
4 | ||
5 | import argparse | |
6 | import locale | |
a9f7adb9 MF |
7 | from pathlib import Path |
8 | import subprocess | |
65465a42 MF |
9 | import sys |
10 | ||
11 | ||
12 | def get_parser(): | |
13 | """Return a CLI parser.""" | |
14 | parser = argparse.ArgumentParser(description=__doc__) | |
a9f7adb9 MF |
15 | parser.add_argument( |
16 | "--commit", action="store_true", help="commit to git" | |
17 | ) | |
18 | parser.add_argument("words", nargs="*", help="words to add to the dictionary") | |
65465a42 MF |
19 | return parser |
20 | ||
a9f7adb9 | 21 | |
65465a42 MF |
22 | def main(argv): |
23 | parser = get_parser() | |
24 | opts = parser.parse_args(argv) | |
25 | ||
a9f7adb9 MF |
26 | # For stable sorting. |
27 | locale.setlocale(locale.LC_COLLATE, "en_US.UTF8") | |
65465a42 | 28 | |
a9f7adb9 | 29 | home = Path.home() |
65465a42 MF |
30 | |
31 | # Pull out the current set of words. | |
a9f7adb9 MF |
32 | # Strip any git conflicts to make things easier. |
33 | pws = home / ".aspell.en.pws" | |
34 | words = set( | |
35 | x.strip() for x in pws.read_text(encoding="utf-8").splitlines() if " " not in x | |
36 | ) | |
37 | words -= {"<<<<<<<", "|||||||", "=======", ">>>>>>>"} | |
65465a42 MF |
38 | |
39 | # Add any words from the user. | |
40 | words.update(opts.words) | |
41 | ||
42 | # Update the aspell dict. | |
43 | words = sorted(words, key=locale.strxfrm) | |
a9f7adb9 MF |
44 | content = "\n".join(words) + "\n" |
45 | header = f"personal_ws-1.1 en {len(words)} \n" | |
46 | with pws.open("w", encoding="utf-8") as fp: | |
47 | fp.write(header) | |
48 | fp.write(content) | |
65465a42 MF |
49 | |
50 | # Update Chrome's dict. | |
a9f7adb9 MF |
51 | chrome_pws = home / ".config" / "google-chrome" / "Custom Dictionary.txt" |
52 | chrome_pws.write_text(content, encoding="utf-8") | |
53 | ||
54 | if opts.commit: | |
55 | subprocess.run( | |
56 | ["git", "commit", "-m", "dict: add more words", pws, chrome_pws], cwd=home | |
57 | ) | |
65465a42 MF |
58 | |
59 | ||
a9f7adb9 | 60 | if __name__ == "__main__": |
65465a42 | 61 | sys.exit(main(sys.argv[1:])) |