__init__.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import pkgutil
  2. import sys
  3. # allowed command modules
  4. import tclCommands.TclCommandExteriors
  5. import tclCommands.TclCommandInteriors
  6. import tclCommands.TclCommandAddPolygon
  7. import tclCommands.TclCommandAddPolyline
  8. import tclCommands.TclCommandExportGcode
  9. import tclCommands.TclCommandCncjob
  10. __all__ = []
  11. for loader, name, is_pkg in pkgutil.walk_packages(__path__):
  12. module = loader.find_module(name).load_module(name)
  13. __all__.append(name)
  14. def register_all_commands(app, commands):
  15. """
  16. Static method which register all known commands.
  17. Command should be for now in directory tclCommands and module should start with TCLCommand
  18. Class have to follow same name as module.
  19. we need import all modules in top section:
  20. import tclCommands.TclCommandExteriors
  21. at this stage we can include only wanted commands with this, auto loading may be implemented in future
  22. I have no enough knowledge about python's anatomy. Would be nice to include all classes which are descendant etc.
  23. :param app: FlatCAMApp
  24. :param commands: array of commands which should be modified
  25. :return: None
  26. """
  27. tcl_modules = {k: v for k, v in sys.modules.items() if k.startswith('tclCommands.TclCommand')}
  28. for key, mod in tcl_modules.items():
  29. if key != 'tclCommands.TclCommand':
  30. class_name = key.split('.')[1]
  31. class_type = getattr(mod, class_name)
  32. command_instance = class_type(app)
  33. for alias in command_instance.aliases:
  34. commands[alias] = {
  35. 'fcn': command_instance.execute_wrapper,
  36. 'help': command_instance.get_decorated_help()
  37. }