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

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

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

Post by krambriw » Sun Dec 13, 2009 7:47 am

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

Prinz
Plugin Developer
Posts: 194
Joined: Mon Apr 07, 2008 4:58 am

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

Post by Prinz » Sun Dec 13, 2009 9:18 am

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
Mein HTPC:
Mainboard: Gigabyte GA-G33M-DS2R
CPU: Intel E5200
OS: WinXP SP3
Graphic card: NVIDIA GeForce 210 512MB
TV-Cards: 2 * Digital Everywhere FloppyDTV-C, Terratec Cinergy 1200 DVB-C
HTPC software: DVB Viewer / EventGhost

krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

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

Post by krambriw » Sun Dec 13, 2009 10:01 pm

Dear Prinz,
Thank you very much!

Best regards, Walter

krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

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

Post by krambriw » Mon Dec 21, 2009 6:23 am

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


krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

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

Post by krambriw » Mon Dec 21, 2009 10:45 am

Sorry, needed to bump this up

stottle
Plugin Developer
Posts: 636
Joined: Sun Apr 26, 2009 10:59 pm

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

Post by stottle » Mon Dec 21, 2009 5:11 pm

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

krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

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

Post by krambriw » Mon Dec 21, 2009 6:30 pm

Yes, it is too simplified. I will try to make a working skeleton

Thanks for your support,

Walter

krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

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

Post by krambriw » Mon Dec 21, 2009 8:19 pm

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 257 times

Prinz
Plugin Developer
Posts: 194
Joined: Mon Apr 07, 2008 4:58 am

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

Post by Prinz » Mon Dec 21, 2009 9:07 pm

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
Mein HTPC:
Mainboard: Gigabyte GA-G33M-DS2R
CPU: Intel E5200
OS: WinXP SP3
Graphic card: NVIDIA GeForce 210 512MB
TV-Cards: 2 * Digital Everywhere FloppyDTV-C, Terratec Cinergy 1200 DVB-C
HTPC software: DVB Viewer / EventGhost

krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

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

Post by krambriw » Mon Dec 21, 2009 9:30 pm

Oh great, thank you very much, I will try this approach

Best regards, Walter

Post Reply