Page 1 of 1

Question: How do I create "getter actions"???

Posted: Sun Dec 13, 2009 7:47 am
by krambriw
No plugin should ever assign something to eg.globals or change anything in it. Never!

The eg.globals namespace is exclusive for the user's Python scripts and commands.
If your plugin wants to publish variables to the user, the preferred way is to implement getter actions, that simply return the value. This way the user can also easily see all informations your plugin can provide.
I read the above. Anybody that can give a hint/sample code how this "getter action" is made?

Kind regards, Walter

Re: Question: How do I create "getter actions"???

Posted: Sun Dec 13, 2009 9:18 am
by Prinz
Hi,
krambriw wrote:I read the above. Anybody that can give a hint/sample code how this "getter action" is made?
It's a simple action, which is doing nothing. Only the return value of this action is the contents of a variable or array of the plugin.

After executing this action, the user can work with this return value using "eg.result".

Example:

Code: Select all

class GetContentsOfVariable( eg.ActionClass ) :

    def __call__( self ) :
        return self.plugin.variable
Regards
Prinz

Re: Question: How do I create "getter actions"???

Posted: Sun Dec 13, 2009 10:01 pm
by krambriw
Dear Prinz,
Thank you very much!

Best regards, Walter

Re: Question: How do I create "getter actions"???

Posted: Mon Dec 21, 2009 6:23 am
by krambriw
Hello again,

After experimenting a bit, the Getter action worked of course perfectly so thanks for the help?

However, I run into another interesting nut to crack;

I have below the very simplified code: A thread class, a plugin class and the action class for the "getter"

When the thread initialises, it gets the "variable" current value from the plugin as input. When it runs, its responsibility is to change the variable in the plugin if the value changes

The getter action reads the variable from the plugin on demand

The getter works as expected but the thread never succeeds in changing the value in the plugin...any suggestions?

In other words maybe I can say that I would like to change a variable value in a plugin class from a thread class

Kind regards, Walter

Simplified thread

Code: Select all

class MyThread(Thread):
    
    def __init__(
        self,
        variable
    ):
        Thread.__init__(self, name="test")
        self.name = name
        self.aVariable= aVariable

   
    def run(self):
        while (self.abort == False):
        if self.aVariable != aVariablePrevious:    
            self.plugin.variable = self.aVariable
        
The simplified plugin code

Code: Select all

class MyPlugin(eg.PluginClass):
   
    def __init__(self):
        self.started = False


    def __start__(
        self,
        variable
    ):
        self.variable = variable
        self.started = True

The getter action

Code: Select all

class GetContentsOfVariable( eg.ActionClass ) :

    def __call__( self ) :
        return self.plugin.variable


Re: Question: How do I create "getter actions"???

Posted: Mon Dec 21, 2009 10:45 am
by krambriw
Sorry, needed to bump this up

Re: Question: How do I create "getter actions"???

Posted: Mon Dec 21, 2009 5:11 pm
by stottle
Your simplified code looks incomplete.

I think you should define both aVariable and aVariablePrevious in the thread init, as the if statement should raise an exception since aVariablePrevious isn't defined. Also, I don't see where your thread gets a reference to plugin, so I think your code would raise an exception there if it didn't at aVariablePrevious.

Is there a more complete example?

Brett

Re: Question: How do I create "getter actions"???

Posted: Mon Dec 21, 2009 6:30 pm
by krambriw
Yes, it is too simplified. I will try to make a working skeleton

Thanks for your support,

Walter

Re: Question: How do I create "getter actions"???

Posted: Mon Dec 21, 2009 8:19 pm
by krambriw
I have created a simple skeleton plugin. It's a scheduler that runs threads that just increments a counter every fifth second and creaes an event.

What I want to do is in this part of the code

Code: Select all

    def run(self):
        while (self.abort == False):
            self.finished.wait(5)
            self.finished.clear()
            if self.abort:
                break
            eg.TriggerEvent(str(self.t_variable))
            self.t_variable += 1
            self.variable = self.t_variable
I would like to change the last line above to

self.plugin.variable = self.t_variable

but it fails

Best regards, Walter
__init__.py
(8.45 KiB) Downloaded 236 times

Re: Question: How do I create "getter actions"???

Posted: Mon Dec 21, 2009 9:07 pm
by Prinz
Hi,

the class SchedulerThread is not based on the eg.ActionClass. In this case the variable self.plugin is unknown.

You can solve this problem, if the plugin is added up as a parameter to the __init__ of the SchedulerThread class.

Eample:

Code: Select all

class SchedulerThread(Thread):
    def __init__(self,
        name,
        variable,
        plugin
    ):
    self.plugin = plugin
If the the class is instanciated in the plugin class, the following statement can be used:

Code: Select all

        t = SchedulerThread(
            schedulerName,
            variable,
            self
            )
Regards
Prinz

Re: Question: How do I create "getter actions"???

Posted: Mon Dec 21, 2009 9:30 pm
by krambriw
Oh great, thank you very much, I will try this approach

Best regards, Walter