moonraker: replace all references of "plugins" to "components"

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Arksine 2021-03-18 08:12:56 -04:00
parent 0c455fcc0d
commit ed217fb3e9
1 changed files with 49 additions and 46 deletions

View File

@ -28,7 +28,7 @@ INIT_TIME = .25
LOG_ATTEMPT_INTERVAL = int(2. / INIT_TIME + .5) LOG_ATTEMPT_INTERVAL = int(2. / INIT_TIME + .5)
MAX_LOG_ATTEMPTS = 10 * LOG_ATTEMPT_INTERVAL MAX_LOG_ATTEMPTS = 10 * LOG_ATTEMPT_INTERVAL
CORE_PLUGINS = [ CORE_COMPONENTS = [
'database', 'file_manager', 'klippy_apis', 'machine', 'database', 'file_manager', 'klippy_apis', 'machine',
'data_store', 'shell_command', 'proc_stats'] 'data_store', 'shell_command', 'proc_stats']
@ -67,7 +67,7 @@ class Server:
self.klippy_state = "disconnected" self.klippy_state = "disconnected"
self.klippy_disconnect_evt = None self.klippy_disconnect_evt = None
self.subscriptions = {} self.subscriptions = {}
self.failed_plugins = [] self.failed_components = []
# Server/IOLoop # Server/IOLoop
self.server_running = False self.server_running = False
@ -103,10 +103,10 @@ class Server:
'process_status_update', self._process_status_update, 'process_status_update', self._process_status_update,
need_klippy_reg=False) need_klippy_reg=False)
# Plugin initialization # Component initialization
self.plugins = {} self.components = {}
self._load_plugins(config) self._load_components(config)
self.klippy_apis = self.lookup_plugin('klippy_apis') self.klippy_apis = self.lookup_component('klippy_apis')
config.validate_config() config.validate_config()
def start(self): def start(self):
@ -130,56 +130,57 @@ class Server:
if log and item is not None: if log and item is not None:
logging.info(item) logging.info(item)
# ***** Plugin Management ***** # ***** Component Management *****
def _load_plugins(self, config): def _load_components(self, config):
# load core plugins # load core components
for plugin in CORE_PLUGINS: for component in CORE_COMPONENTS:
self.load_plugin(config, plugin) self.load_component(config, component)
# check for optional plugins # check for optional components
opt_sections = set([s.split()[0] for s in config.sections()]) - \ opt_sections = set([s.split()[0] for s in config.sections()]) - \
set(['server', 'authorization', 'system_args']) set(['server', 'authorization', 'system_args'])
for section in opt_sections: for section in opt_sections:
self.load_plugin(config, section, None) self.load_component(config, section, None)
def load_plugin(self, config, plugin_name, default=Sentinel): def load_component(self, config, component_name, default=Sentinel):
if plugin_name in self.plugins: if component_name in self.components:
return self.plugins[plugin_name] return self.components[component_name]
# Make sure plugin exists # Make sure component exists
mod_path = os.path.join( mod_path = os.path.join(
os.path.dirname(__file__), 'plugins', plugin_name + '.py') os.path.dirname(__file__), 'components', component_name + '.py')
if not os.path.exists(mod_path): if not os.path.exists(mod_path):
msg = f"Plugin ({plugin_name}) does not exist" msg = f"Component ({component_name}) does not exist"
logging.info(msg) logging.info(msg)
self.failed_plugins.append(plugin_name) self.failed_components.append(component_name)
if default == Sentinel: if default == Sentinel:
raise ServerError(msg) raise ServerError(msg)
return default return default
try: try:
module = importlib.import_module("plugins." + plugin_name) module = importlib.import_module("components." + component_name)
func_name = "load_plugin" func_name = "load_component"
if hasattr(module, "load_plugin_multi"): if hasattr(module, "load_component_multi"):
func_name = "load_plugin_multi" func_name = "load_component_multi"
if plugin_name not in CORE_PLUGINS and func_name == "load_plugin": if component_name not in CORE_COMPONENTS and \
config = config[plugin_name] func_name == "load_component":
config = config[component_name]
load_func = getattr(module, func_name) load_func = getattr(module, func_name)
plugin = load_func(config) component = load_func(config)
except Exception: except Exception:
msg = f"Unable to load plugin ({plugin_name})" msg = f"Unable to load component: ({component_name})"
logging.exception(msg) logging.exception(msg)
self.failed_plugins.append(plugin_name) self.failed_components.append(component_name)
if default == Sentinel: if default == Sentinel:
raise ServerError(msg) raise ServerError(msg)
return default return default
self.plugins[plugin_name] = plugin self.components[component_name] = component
logging.info(f"Plugin ({plugin_name}) loaded") logging.info(f"Component ({component_name}) loaded")
return plugin return component
def lookup_plugin(self, plugin_name, default=Sentinel): def lookup_component(self, component_name, default=Sentinel):
plugin = self.plugins.get(plugin_name, default) component = self.components.get(component_name, default)
if plugin == Sentinel: if component == Sentinel:
raise ServerError(f"Plugin ({plugin_name}) not found") raise ServerError(f"Component ({component_name}) not found")
return plugin return component
def register_notification(self, event_name, notify_name=None): def register_notification(self, event_name, notify_name=None):
wsm = self.moonraker_app.get_websocket_manager() wsm = self.moonraker_app.get_websocket_manager()
@ -375,7 +376,7 @@ class Server:
vsd_config = config.get('virtual_sdcard', {}) vsd_config = config.get('virtual_sdcard', {})
vsd_path = vsd_config.get('path', None) vsd_path = vsd_config.get('path', None)
if vsd_path is not None: if vsd_path is not None:
file_manager = self.lookup_plugin('file_manager') file_manager = self.lookup_component('file_manager')
file_manager.register_directory('gcodes', vsd_path) file_manager.register_directory('gcodes', vsd_path)
else: else:
logging.info( logging.info(
@ -413,7 +414,7 @@ class Server:
else: else:
if rpc_method == "gcode/script": if rpc_method == "gcode/script":
script = web_request.get_str('script', "") script = web_request.get_str('script', "")
data_store = self.lookup_plugin('data_store') data_store = self.lookup_component('data_store')
data_store.store_gcode_command(script) data_store.store_gcode_command(script)
return await self._request_standard(web_request) return await self._request_standard(web_request)
@ -481,17 +482,17 @@ class Server:
async def _stop_server(self, exit_reason="restart"): async def _stop_server(self, exit_reason="restart"):
self.server_running = False self.server_running = False
for method in ["on_exit", "close"]: for method in ["on_exit", "close"]:
for name, plugin in self.plugins.items(): for name, component in self.components.items():
if not hasattr(plugin, method): if not hasattr(component, method):
continue continue
func = getattr(plugin, method) func = getattr(component, method)
try: try:
ret = func() ret = func()
if asyncio.iscoroutine(ret): if asyncio.iscoroutine(ret):
await ret await ret
except Exception: except Exception:
logging.exception( logging.exception(
f"Error executing '{method}()' for plugin: {name}") f"Error executing '{method}()' for component: {name}")
try: try:
if self.klippy_connection.is_connected(): if self.klippy_connection.is_connected():
self.klippy_disconnect_evt = Event() self.klippy_disconnect_evt = Event()
@ -518,15 +519,17 @@ class Server:
return "ok" return "ok"
async def _handle_info_request(self, web_request): async def _handle_info_request(self, web_request):
file_manager = self.lookup_plugin('file_manager', None) file_manager = self.lookup_component('file_manager', None)
reg_dirs = [] reg_dirs = []
if file_manager is not None: if file_manager is not None:
reg_dirs = file_manager.get_registered_dirs() reg_dirs = file_manager.get_registered_dirs()
return { return {
'klippy_connected': self.klippy_connection.is_connected(), 'klippy_connected': self.klippy_connection.is_connected(),
'klippy_state': self.klippy_state, 'klippy_state': self.klippy_state,
'plugins': list(self.plugins.keys()), 'components': list(self.components.keys()),
'failed_plugins': self.failed_plugins, 'failed_components': self.failed_components,
'plugins': list(self.components.keys()),
'failed_plugins': self.failed_components,
'registered_directories': reg_dirs} 'registered_directories': reg_dirs}
async def _handle_config_request(self, web_request): async def _handle_config_request(self, web_request):