""" This module implements the `se join-words` command. """ import argparse import glob import shutil import subprocess import git import se def join_words() -> int: """ Entry point for `se join-words` """ parser = argparse.ArgumentParser(description="Call interactive-sr for the two-word phrase passed in as one word and commit the changes with the appropriate message.") parser.add_argument("word", metavar="WORD", help="The single-word version of one of the two-word phrases to be joined; use `words` to get a list of the words to process.") parser.add_argument("-n", "--nocommit", action="store_true", help="Do not perform a `git commit` after the interactive replacements are finished.") args = parser.parse_args() # Check for required utilities vim_path = shutil.which("vim") if vim_path is None: se.print_error("Couldn’t locate vim. Is it installed?") return se.MissingDependencyException.code regex = {} commitmsg = {} do_commit = not args.nocommit files = r'src/epub/text/*' regex['someone'] = r'/\v([Ss])ome one/\1omeone/' regex['anyone'] = r'/\v(<[Aa])ny one/\1nyone/' regex['everyone'] = r'/\v([Ee])very one(\s+of)@\!/\1veryone/' regex['everything'] = r'/\v([Ee])very thing/\1verything/' regex['anything'] = r'/\v(<[Aa])ny thing/\1nything/' regex['forever'] = r'/\v([Ff])or ever(>)/\1orever\2/' regex['anyway'] = r'/\v(in\s+)@<\!(<[Aa])ny way/\2nyway/' regex['yourself'] = r'/\v([Yy])our self/\1ourself/' regex['meantime'] = r'/\v([Mm])ean time/\1eantime/' regex['anyhow'] = r'/\v([Aa])ny how/\1nyhow/' regex['anybody'] = r'/\v([Aa])ny body/\1nybody/' regex['everybody'] = r'/\v([Ee])very body/\1verybody/' regex['shortcut'] = r'/\v([Ss])hort cut/\1hortcut/' regex['meanwhile'] = r'/\v([Mm])ean while/\1eanwhile/' commitmsg['someone'] = '[Editorial] some one -> someone' commitmsg['anyone'] = '[Editorial] any one -> anyone' commitmsg['everyone'] = '[Editorial] every one -> everyone' commitmsg['everything'] = '[Editorial] every thing -> everything' commitmsg['anything'] = '[Editorial] any thing -> anything' commitmsg['forever'] = '[Editorial] for ever -> forever' commitmsg['anyway'] = '[Editorial] any way -> anyway' commitmsg['yourself'] = '[Editorial] your self -> yourself' commitmsg['meantime'] = '[Editorial] mean time -> meantime' commitmsg['anyhow'] = '[Editorial] any how -> anyhow' commitmsg['anybody'] = '[Editorial] any body -> anybody' commitmsg['everybody'] = '[Editorial] every body -> everybody' commitmsg['shortcut'] = '[Editorial] short cut -> shortcut' commitmsg['meanwhile'] = '[Editorial] mean while -> meanwhile' if args.word == "words": for word in regex: print(word) elif args.word in regex: # same call that interactive-sr makes targets = glob.glob(files) subprocess.call([vim_path, "+silent set title", f"+silent bufdo set eventignore-=Syntax | %s{regex[args.word]}gce | silent update", "+silent qa"] + targets) git_command = git.cmd.Git("src/epub/text/") if "nothing to commit" in git_command.status(): print(f"No occurrences of '{args.word}' were found or changed") elif do_commit: git_command.commit("-am " + commitmsg[args.word]) else: se.print_error(f"Unsupported word '{args.word}'") return 0