Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

Pass an eg.globals variable to a plugin?

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
speedreeder
Posts: 10
Joined: Fri Dec 09, 2011 8:23 pm

Pass an eg.globals variable to a plugin?

Post by speedreeder »

I'm trying to modify a plugin so that it will accept an eg.globals variable that is created by a python script when it's macro gets run. I worked the variable into the _init_.py file, but that only gets called when adding the plugin, so it only uses whatever value the variable is when the plugin gets added. How do I make that more dynamic? The plugin runs an action at the end of the macro that needs the updated variable value, but it just keeps giving me the old value.

Confusing, I know.
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Pass an eg.globals variable to a plugin?

Post by Pako »

You've probably not read this post: viewtopic.php?f=10&t=604&start=15#p10256.
Bitmonster wrote:The eg.globals namespace is exclusive for the user's Python scripts and commands.
You probably know that just Bitmonster is the author of this program.
He probably knew why he wrote it.
You'd have to try to find some other way.
What plugin is it?
What do you want to do?

But it is possible that you just do some mistake and it can work.
We can not help you if we do not know anything about it.


Pako
speedreeder
Posts: 10
Joined: Fri Dec 09, 2011 8:23 pm

Re: Pass an eg.globals variable to a plugin?

Post by speedreeder »

Oh hey, I have no problem with how the program is written. I'm sure there's a reason for how it is. The plugin is Google Voice by cfull1. It's a little old, so I've been trying to make some improvements for my own use.

Specifically, I've been trying to incorporate it as part of my TV download macro, so that when a new episode is downloaded it sends me an SMS including the name of the episode. I have a python script that (among other things) stores the name of the episode in eg.globals.FileName. I then added some code to the _init_.py that modified the "Send SMS" action so that it would recognize the user input of '%n' in the message box and then substitute eg.globals.FileName variable for it (see the while panel.Affirmed() towards the bottom):

Code: Select all

class SendSMS(eg.ActionBase):
    text = Text
    def __call__(self, number='', msg=''):
        try:
            self.plugin.voice.send_sms(number, msg)
        except Exception,e:
            print str(e)
            print "Number is not valid"
        
    def Configure(self, number="", msg=""):
        FileNameCheck = 0
        theTime = strftime("%H:%M:%S", localtime())
        try:
            eg.globals.FileName
        except (NameError, AttributeError):
            FileNameCheck = 1
        text = self.text
        panel = eg.ConfigPanel()
        numCtrl = panel.TextCtrl(number)
        msgCtrl = panel.TextCtrl(msg, style=wx.TE_MULTILINE)
        msgCtrl.SetMinSize((-1, 100))
        #msgCtrl.SetMaxSize((-1, 60))
        number = panel.StaticText(text.number)
        message = panel.StaticText("Message \n%n = eg.globals.FileName\n%t = Current Time")
        panel.sizer.Add(number, 0, wx.EXPAND)
        panel.sizer.Add((5, 5))
        panel.sizer.Add(numCtrl, 0, wx.EXPAND)
        panel.sizer.Add((5, 5))
        panel.sizer.Add(message, 0, wx.EXPAND)
        panel.sizer.Add((5, 5))
        panel.sizer.Add(msgCtrl, 0, wx.EXPAND)
        panel.sizer.Add((5, 5))
        while panel.Affirmed():
            preMsg = msgCtrl.GetValue()
            if FileNameCheck == 0:
                a = re.sub(r'%n', eg.globals.FileName, preMsg)
            b = re.sub(r'%t', theTime, a)
            panel.SetResult(numCtrl.GetValue(),b)
How can I get this variable into the plugin, without using eg.globals? Perhaps some Get method?
speedreeder
Posts: 10
Joined: Fri Dec 09, 2011 8:23 pm

Re: Pass an eg.globals variable to a plugin?

Post by speedreeder »

Figured it out! I just made a python command to send the SMS, instead of using the action:

Totally forgot that you can get the Python version of an action by creating the action and then copying it into a text editor. You can then call that in a Python script by using the "eg.plugins" syntax.

Code: Select all

import eg
message = eg.globals.FileName + " is ready."
eg.plugins.GoogleVoice.SendSMS(u'1112223333', message)
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Pass an eg.globals variable to a plugin?

Post by Pako »

Correctly it should be done like this:

Code: Select all

class SendSMS(eg.ActionBase):

    def __call__(self, number='', msg='', flnm=''):
        try:
            from time import strftime, localtime
            msg = msg.replace("%n",eg.ParseString(flnm))
            msg = msg.replace("%t",strftime("%H:%M:%S", localtime()))
            self.plugin.voice.send_sms(number, eg.ParseString(msg))
        except Exception,e:
            print str(e)
            print "Number is not valid"
       
    def Configure(self, number="", msg="", flnm=""):
        panel = eg.ConfigPanel()
        numCtrl = panel.TextCtrl(number)
        filenameCtrl = panel.TextCtrl(flnm)
        msgCtrl = panel.TextCtrl(msg, style=wx.TE_MULTILINE)
        msgCtrl.SetMinSize((-1, 100))
        #msgCtrl.SetMaxSize((-1, 60))
        number = panel.StaticText("Number")
        filename = panel.StaticText("Filename")
        message = panel.StaticText("Message (%n = Filename, %t = Current Time)")
        panel.sizer.Add(number, 0, wx.EXPAND)
        panel.sizer.Add((-1, 2))
        panel.sizer.Add(numCtrl, 0, wx.EXPAND)
        panel.sizer.Add((-1, 10))
        panel.sizer.Add(filename, 0, wx.EXPAND)
        panel.sizer.Add((-1, 2))
        panel.sizer.Add(filenameCtrl, 0, wx.EXPAND)
        panel.sizer.Add((-1, 10))
        panel.sizer.Add(message, 0, wx.EXPAND)
        panel.sizer.Add((-1, 2))
        panel.sizer.Add(msgCtrl, 0, wx.EXPAND)
        while panel.Affirmed():
            panel.SetResult(
                numCtrl.GetValue(),
                msgCtrl.GetValue(),
                filenameCtrl.GetValue(),
            )   
eg.globals.FileName.png
Never would have an expression like eg.globals occur in the plugin source text !

Pako
Post Reply