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.

78 lines
3.4KB

  1. # -*- coding: utf-8 -*-
  2. # MIT License
  3. #
  4. # Copyright (c) 2017-2018 Roxanne Gibson
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in all
  14. # copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23. class Menu:
  24. __slots__ = ["name", "params", "formatted_params", "title", "content"]
  25. def __init__(self, name, *params, settings=None):
  26. self.name = name
  27. self.params = list(params).append("Exit")
  28. if settings:
  29. self.formatted_params = self._parse_params(settings, self.name)
  30. else:
  31. self.formatted_params = self.params
  32. self.title = "'Roxbot Settings: {}'\n".format(self.name)
  33. self.content = self._format_content(self.title, self.formatted_params, "```python", "```")
  34. @staticmethod
  35. def _format_content(title, params, prefix="", suffix=""):
  36. separator = "—————————————————————————————"
  37. choices = "\n"
  38. for x, setting in enumerate(params):
  39. if setting == "Exit":
  40. choices += "[0] Exit\n"
  41. elif setting != "convert":
  42. if setting != [*params][x]: # Just in case params is dict_keys, we make a new list.
  43. choices += "[{}] {}\n".format(x + 1, setting)
  44. else:
  45. choices += "[{}] Edit '{}'\n".format(x+1, setting)
  46. return prefix + title + separator + choices + suffix
  47. @staticmethod
  48. def _parse_params(settings, name):
  49. params = [*settings.keys()]
  50. params_copy = settings.copy().keys()
  51. for param in params_copy:
  52. if settings["convert"].get(param) == "bool":
  53. # Enable/Disable Parse
  54. if param == "enabled":
  55. options = ["Enable '{}'".format(name), "Disable '{}'".format(name)]
  56. else:
  57. options = ["Enable '{}'".format(param), "Disable '{}'".format(param)]
  58. params.remove(param)
  59. params = [*options, *params]
  60. elif isinstance(settings.get(param), list):
  61. # Add and Remove Parse
  62. options = ["Add {}".format(param), "Remove {}".format(param)]
  63. params.remove(param)
  64. params = [*params, *options]
  65. elif isinstance(settings.get(param), (int, str)):
  66. # Set parse
  67. options = "Set {}".format(param)
  68. params.remove(param)
  69. params = [*params, options]
  70. return params