]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/env python3 | |
2 | ||
3 | """Update & sort personal dictionaries.""" | |
4 | ||
5 | import argparse | |
6 | import locale | |
7 | from pathlib import Path | |
8 | import subprocess | |
9 | import sys | |
10 | ||
11 | ||
12 | def get_parser(): | |
13 | """Return a CLI parser.""" | |
14 | parser = argparse.ArgumentParser(description=__doc__) | |
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") | |
19 | return parser | |
20 | ||
21 | ||
22 | def main(argv): | |
23 | parser = get_parser() | |
24 | opts = parser.parse_args(argv) | |
25 | ||
26 | # For stable sorting. | |
27 | locale.setlocale(locale.LC_COLLATE, "en_US.UTF8") | |
28 | ||
29 | home = Path.home() | |
30 | ||
31 | # Pull out the current set of words. | |
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 -= {"<<<<<<<", "|||||||", "=======", ">>>>>>>"} | |
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) | |
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) | |
49 | ||
50 | # Update Chrome's dict. | |
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 | ) | |
58 | ||
59 | ||
60 | if __name__ == "__main__": | |
61 | sys.exit(main(sys.argv[1:])) |