_settings vs class _init_

Hello, everybody,

I have a little problem I don't understand. Maybe you can help me.

I am writing a plugin that uses the "octoprint.filemanager.extension_tree" hook. The passed method uses _settings from octoprint.plugin.SettingsPlugin mixin. This worked fine until now, but when I add a init to my plugin class, the hook is called before _settings is available. Does anyone know why this happens and if I can work around this behaviour?

Maybe move your self._settings call into either on_startup or on_after_startup callbacks? If you could provide context of the code it might be more helpful.

Show us the code. How you call your init and then super-it is important. For example, here's a mistake I recently did...

class FileInfoScreen(Screen):
    def __init__(self, **kwargs):
        super(FileInfoScreen, self).__init__()
        ...

It's subtle but it needs to be this instead on the third line:

        super(FileInfoScreen, self).__init__(**kwargs)

this is the code of my __ init__.py


#!/usr/bin/python
# coding=utf-8

import os

from .chitu_comm import chitu_comm
from .sla_analyser import sla_AnalysisQueue
from .sla_printer import Sla_printer, gcode_modifier

import octoprint.plugin

import octoprint.filemanager
import octoprint.filemanager.util
from octoprint.filemanager import ContentTypeMapping


class Sla_plugin(   octoprint.plugin.SettingsPlugin,
                    octoprint.plugin.SimpleApiPlugin,
                    octoprint.plugin.AssetPlugin,
                    octoprint.plugin.TemplatePlugin,
                    octoprint.plugin.StartupPlugin,
                    octoprint.plugin.EventHandlerPlugin):


    def __init__(self):         # the init which makes the difference. 
        self.gcode_modifier = gcode_modifier()

    ##############################################
    #        allowed file extesions part         #
    ##############################################
    
    @property
    def allowed(self):
        if self._settings is None:
            return str("cbddlp, photon")
        else:
            return str(self._settings.get(["allowedExten"]))

    def get_extension_tree(self, *args, **kwargs):

        return dict(machinecode=dict(sla_bin=ContentTypeMapping(self.allowed.replace(" ", "").split(","), "application/octet-stream")))
    
    ##############################################
    #                  Settings                  #
    ##############################################

    def get_template_configs(self):
		return [dict(type="settings", custom_bindings=False)]

    def get_settings_defaults(self):

        return dict(
            
            allowedExten = 'cbddlp, photon',
            workAsFlashDrive = True, #false printer use separate flash drive
            flashDriveImageSize = 1,#GB
            chitu_comm = True,

            useHeater = False,
            heaterTemp = 30,# C
            heaterTime = 20,#min
            resinGauge = True,
            enableLight = False, #ir cam light
            printerInternalConfig = [dict(
                #allGcodeFileEntrys
            )],
            mainpowerSwitch = None,#net/gpio
            photonFileEditor = False,
            tempSensorPrinter = None,#1wire/ntc
            tempSensorBed = None,#1wire/ntc

            helloCommand = "M4002"
        )

    ##############################################
    #                UDP Upload                  #
    ##############################################

    def on_after_startup(self):
        
        if self._settings.get(["chitu_comm"]):

            Chitu_comm = chitu_comm(self)
            Chitu_comm.start_listen_reqest()
            self._logger.info("chitubox udp reciver enabeled")

    ##############################################
    #               File analysis                #
    ##############################################
    def get_sla_analysis_factory(*args, **kwargs):
        return dict(sla_bin=sla_AnalysisQueue)


    ##############################################
    #               Priterfactory                #
    ##############################################
    
    def get_sla_printer_factory(self,components):
        return Sla_printer(components["file_manager"],components["analysis_queue"],components["printer_profile_manager"])
    

    ##############################################
    #               gcode modifier               #
    ##############################################
    """def get_gcode_receive_modifier(self):
        self.gcode_modifier

    def get_gcode_send_modifier(self):
        self.gcode_modifier
    """


__plugin_name__ = "Sla_plugin"
__plugin_pythoncompat__ = ">=2.7,<4"

def __plugin_load__():
	global __plugin_implementation__
	__plugin_implementation__ = Sla_plugin()

	global __plugin_hooks__
	__plugin_hooks__ = {
		
        "octoprint.filemanager.extension_tree"  : __plugin_implementation__.get_extension_tree,
        "octoprint.filemanager.analysis.factory": __plugin_implementation__.get_sla_analysis_factory,
        "octoprint.printer.factory"             : __plugin_implementation__.get_sla_printer_factory
        #'octoprint.comm.protocol.gcode.sending' : __plugin_implementation__.gcode_modifier.get_gcode_send_modifier,
        #'octoprint.comm.protocol.gcode.received': __plugin_implementation__.gcode_modifier.get_gcode_receive_modifier
    }

Try instead:

    def __init__(self, **kwargs):
        super(Sla_plugin, self).__init__(**kwargs)

it works. Thanks. I'm gonna have to google something before I understand why it works, but it works.^^

Presumbably that "laundry list" of mixins is the **kwargs. (You made essentially the same mistake that I wrote about earlier.) Since you didn't init with at least the SettingsPlugin mixin then you wouldn't get access to settings.