extensions.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import extensions
  2. import modules.shared as shared
  3. state = {}
  4. available_extensions = []
  5. def load_extensions():
  6. global state
  7. for i, name in enumerate(shared.args.extensions):
  8. if name in available_extensions:
  9. print(f'Loading the extension "{name}"... ', end='')
  10. exec(f"import extensions.{name}.script")
  11. state[name] = [True, i]
  12. print('Ok.')
  13. # This iterator returns the extensions in the order specified in the command-line
  14. def iterator():
  15. for name in sorted(state, key=lambda x : state[x][1]):
  16. if state[name][0] == True:
  17. yield eval(f"extensions.{name}.script"), name
  18. # Extension functions that map string -> string
  19. def apply_extensions(text, typ):
  20. for extension, _ in iterator():
  21. if typ == "input" and hasattr(extension, "input_modifier"):
  22. text = extension.input_modifier(text)
  23. elif typ == "output" and hasattr(extension, "output_modifier"):
  24. text = extension.output_modifier(text)
  25. elif typ == "bot_prefix" and hasattr(extension, "bot_prefix_modifier"):
  26. text = extension.bot_prefix_modifier(text)
  27. return text
  28. def create_extensions_block():
  29. # Updating the default values
  30. for extension, name in iterator():
  31. for param in extension.params:
  32. _id = f"{name}-{param}"
  33. if _id in shared.settings:
  34. extension.params[param] = shared.settings[_id]
  35. # Creating the extension ui elements
  36. for extension, name in iterator():
  37. if hasattr(extension, "ui"):
  38. extension.ui()