]> git.wh0rd.org - home.git/blob - .bin/aspell-sort-pws
cros-board: update
[home.git] / .bin / aspell-sort-pws
1 #!/usr/bin/env python3
2
3 """Update & sort personal dictionaries."""
4
5 import argparse
6 import locale
7 import os
8 import sys
9
10
11 def get_parser():
12 """Return a CLI parser."""
13 parser = argparse.ArgumentParser(description=__doc__)
14 parser.add_argument('words', nargs='*',
15 help='words to add to the dictionary')
16 return parser
17
18 def main(argv):
19 parser = get_parser()
20 opts = parser.parse_args(argv)
21
22 locale.setlocale(locale.LC_COLLATE, 'en_US.UTF8')
23
24 home = os.path.expanduser('~')
25
26 # Pull out the current set of words.
27 pws = os.path.join(home, '.aspell.en.pws')
28 with open(pws, encoding='utf-8') as fp:
29 lines = [x.strip() for x in fp.readlines()]
30 header = lines[0]
31 assert header.startswith('personal_ws-1.1 en ')
32 words = set(lines[1:])
33
34 # Add any words from the user.
35 words.update(opts.words)
36
37 # Update the aspell dict.
38 words = sorted(words, key=locale.strxfrm)
39 header = 'personal_ws-1.1 en %s ' % (len(words),)
40 with open(pws, 'w', encoding='utf-8') as fp:
41 fp.write(header + '\n')
42 fp.write('\n'.join(words) + '\n')
43
44 # Update Chrome's dict.
45 chrome_pws = os.path.join(home, '.config', 'google-chrome', 'Custom Dictionary.txt')
46 with open(chrome_pws, 'w', encoding='utf-8') as fp:
47 fp.write('\n'.join(words) + '\n')
48
49
50 if __name__ == '__main__':
51 sys.exit(main(sys.argv[1:]))