From: Mike Frysinger Date: Wed, 4 May 2022 19:13:13 +0000 (-0400) Subject: aspell-sort-pws: automate a bit more X-Git-Url: https://git.wh0rd.org/?a=commitdiff_plain;h=a9f7adb91f938d894e294bc8f5f5026726aafa13;p=home.git aspell-sort-pws: automate a bit more --- diff --git a/.bin/aspell-sort-pws b/.bin/aspell-sort-pws index 24c7243..97dd293 100755 --- a/.bin/aspell-sort-pws +++ b/.bin/aspell-sort-pws @@ -4,48 +4,58 @@ import argparse import locale -import os +from pathlib import Path +import subprocess import sys def get_parser(): """Return a CLI parser.""" parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument('words', nargs='*', - help='words to add to the dictionary') + parser.add_argument( + "--commit", action="store_true", help="commit to git" + ) + parser.add_argument("words", nargs="*", help="words to add to the dictionary") return parser + def main(argv): parser = get_parser() opts = parser.parse_args(argv) - locale.setlocale(locale.LC_COLLATE, 'en_US.UTF8') + # For stable sorting. + locale.setlocale(locale.LC_COLLATE, "en_US.UTF8") - home = os.path.expanduser('~') + home = Path.home() # Pull out the current set of words. - pws = os.path.join(home, '.aspell.en.pws') - with open(pws, encoding='utf-8') as fp: - lines = [x.strip() for x in fp.readlines()] - header = lines[0] - assert header.startswith('personal_ws-1.1 en ') - words = set(lines[1:]) + # Strip any git conflicts to make things easier. + pws = home / ".aspell.en.pws" + words = set( + x.strip() for x in pws.read_text(encoding="utf-8").splitlines() if " " not in x + ) + words -= {"<<<<<<<", "|||||||", "=======", ">>>>>>>"} # Add any words from the user. words.update(opts.words) # Update the aspell dict. words = sorted(words, key=locale.strxfrm) - header = 'personal_ws-1.1 en %s ' % (len(words),) - with open(pws, 'w', encoding='utf-8') as fp: - fp.write(header + '\n') - fp.write('\n'.join(words) + '\n') + content = "\n".join(words) + "\n" + header = f"personal_ws-1.1 en {len(words)} \n" + with pws.open("w", encoding="utf-8") as fp: + fp.write(header) + fp.write(content) # Update Chrome's dict. - chrome_pws = os.path.join(home, '.config', 'google-chrome', 'Custom Dictionary.txt') - with open(chrome_pws, 'w', encoding='utf-8') as fp: - fp.write('\n'.join(words) + '\n') + chrome_pws = home / ".config" / "google-chrome" / "Custom Dictionary.txt" + chrome_pws.write_text(content, encoding="utf-8") + + if opts.commit: + subprocess.run( + ["git", "commit", "-m", "dict: add more words", pws, chrome_pws], cwd=home + ) -if __name__ == '__main__': +if __name__ == "__main__": sys.exit(main(sys.argv[1:]))