You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.6 KiB

7 years ago
  1. #!/usr/bin/python3
  2. import argparse
  3. from string import ascii_lowercase
  4. import cmd2
  5. import re
  6. import os
  7. from candidates import candidates, filter_possibilities
  8. from dictionary import DEFAULT, ALTERNATE
  9. from word_remove_dialog import RemoveWordsActivity
  10. LOWERCASE = set(ascii_lowercase)
  11. DICTS = {"default": DEFAULT, "alt": ALTERNATE}
  12. # argparsers
  13. dictionary_manage_parser = argparse.ArgumentParser()
  14. commands = dictionary_manage_parser.add_mutually_exclusive_group()
  15. commands.add_argument(
  16. "-l", "--list", action="store_true", help="List available dictionaries"
  17. )
  18. commands.add_argument("-s", "--switch", help="Dictionary to swap to")
  19. letter_set_manage = argparse.ArgumentParser()
  20. letter_set_manage.add_argument("letter", nargs="+")
  21. guessing = argparse.ArgumentParser()
  22. guessing.add_argument("pattern", help="Pattern to match")
  23. class MainLoop(cmd2.Cmd):
  24. """Loop for wordscape commands
  25. """
  26. prompt = "<{}> $ "
  27. def __init__(self):
  28. self.dict = "default"
  29. self.excludes = set()
  30. self.init_letters(input("Enter letters: "))
  31. super().__init__()
  32. def init_letters(self, letters):
  33. if not isinstance(letters, str):
  34. letters = "".join(letters)
  35. letters = letters.lower()
  36. self.letters = [l for l in letters if l in LOWERCASE]
  37. self.prompt = MainLoop.prompt.format(", ".join(self.letters))
  38. self.candidates = candidates(self.letters, DICTS[self.dict], self.excludes)
  39. @cmd2.with_argparser(dictionary_manage_parser)
  40. def do_dict(self, args):
  41. """list/switch dict"""
  42. if args.list:
  43. print(
  44. "\n".join(
  45. (
  46. "{} {}".format("*" if key == self.dict else " ", key)
  47. for key in DICTS.keys()
  48. )
  49. )
  50. )
  51. else:
  52. DICTS[args.switch]
  53. self.dict = args.switch
  54. @cmd2.with_argparser(letter_set_manage)
  55. def do_change_letters(self, args):
  56. """Change the letters on the board"""
  57. self.init_letters(args.letter)
  58. @cmd2.with_argparser(guessing)
  59. def do_find(self, args):
  60. """Find words that match a pattern"""
  61. pattern = re.compile(args.pattern + "$")
  62. matching_words = filter_possibilities(self.candidates, pattern)
  63. app = RemoveWordsActivity(matching_words, self.excludes)
  64. if os.name == "nt":
  65. app.run(fork=False)
  66. else:
  67. app.run()
  68. was_canceled, new_exludes = app.get_results()
  69. if not was_canceled:
  70. self.excludes.update(new_exludes)
  71. if __name__ == "__main__":
  72. MainLoop().cmdloop()