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.

Saving changed config data

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
indyjason79
Posts: 4
Joined: Sun Sep 01, 2013 1:17 am

Saving changed config data

Post by indyjason79 »

I'm working on creating my first HomeAutomation plugin, but have a question when it comes to persisting data.

If I have a variable that is passed into Configure and __start__, and the value gets changed at run-time, how do I persist the change so the proper value is loaded via __start__ when the plugin starts next time?

Example:

Code: Select all

def __start__(self, var1):
  self.myvar = var1

def SomeFunction(self):
  self.myvar = 'Changed Value' #How do I persist this change between eg restarts?

def Configure(self, var1):
  self.myvar = var1
  varCtrl = wx.TextCtrl(configPanel, wx.ID_ANY, self.var1, wx.DefaultPosition, wx.DefaultSize, 0)

  while configPanel.Affirmed():
    configPanel.SetResult(varCtrl.GetValue())
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Saving changed config data

Post by krambriw »

Hello,

I would not change variables in the configuration data during run-time. Instead I would declare the needed variables separately.

There are several ways possible to make them persistent
- the class eg.PersistentData
- database (like sqlite3)
- python module shelve
- python module pickle
...others

Earlier this was the easiest and worked fine but with new Windows version and the rights you are running EG with, it may not work for you

First is to declare your persistent variables like this:

Code: Select all

class MyPersistentVariables(eg.PersistentData):
    myVar1 = 'nothing'
    myVar2 = 'anything'
    myVar3 = False
During run-time you can set new values:

Code: Select all

def SomeFunction(self):
     MyPersistentVariables.myVar1 = 'Changed Value' #How do I persist this change between eg restarts?
If you still would like or need to assign a new value to one of your other variables, you can do it at any time, even at start-up

Code: Select all

def __start__(self):
    self.myvar = MyPersistentVariables.myVar1
Disadvantages I have experienced is when running EG under later versions of Windows (from 7 and up) is that EG might fail to update its configuration. But it might work for you. If not I suggest that you check the other alternatives already from start. Google for python shelve or jump start using a database from scratch (my choice).
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Saving changed config data

Post by Pako »

Ways to solve it, it is certainly more.
I do not have a good experience with eg.PersistentData and for that reason I sometimes use tricks like the following example:

Code: Select all

from threading import currentThread


eg.RegisterPlugin(
    name="My test plugin",
    description = "My test plugin",
    author = "Me",
    version = 0.0,
    guid = "{9E389D26-A9B3-4720-9632-435BF03E321D}",
    kind="other",
    createMacrosOnAdd = True,
)

class MyTestPlugin(eg.PluginBase):

    def __init__(self):
        self.AddAction(SetValue)
    

    def __start__(self, var1 = ""):
        self.myvar = var1
        print "self.myvar =",self.myvar


    def SaveDoc(self, trItem, args):
        eg.actionThread.Func(trItem.SetArguments)(args) # __stop__ / __start__        
        eg.document.SetIsDirty()
        eg.document.Save()


    def SomeFunction(self, value):
        #self.myvar = value #How do I persist this change between eg restarts?
        if self.myvar != value:
            ct = currentThread()
            trItem = self.info.treeItem
            args = list(trItem.GetArguments())
            args[0] = value
            if ct == eg.actionThread._ThreadWorker__thread:
                trItem.SetArguments(args) #automatically __stop__/__start__  !!!    
                eg.document.SetIsDirty()
                eg.document.Save()
            else:
                eg.scheduler.AddTask(0.01, self.SaveDoc, trItem, args)
        

    def Configure(self, var1 = ""):
        configPanel = eg.ConfigPanel(self)
        self.myvar = self.myvar if hasattr(self, "myvar") else var1
        varCtrl = wx.TextCtrl(configPanel, wx.ID_ANY, self.myvar)
        configPanel.sizer.Add(varCtrl, 0, wx.EXPAND|wx.ALL, 10)        

        def onVarCtrl(evt):
            self.myvar = varCtrl.GetValue()
            evt.Skip()
        varCtrl.Bind(wx.EVT_TEXT, onVarCtrl)

        while configPanel.Affirmed():
            #configPanel.SetResult(varCtrl.GetValue())
            configPanel.SetResult(self.myvar,)
#===============================================================================

class SetValue(eg.ActionBase):

    class text:
        value = "Value:"

    def __call__(
        self,
        value = ""
    ):
        value = eg.ParseString(value)
        self.plugin.SomeFunction(value)


    def Configure(
        self,
        value = ""
    ):
        panel = eg.ConfigPanel(self)
        valueLbl = wx.StaticText(panel, -1, self.text.value)
        valueCtrl = wx.TextCtrl(panel, -1, value)
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        mainSizer.Add(valueLbl)
        mainSizer.Add(valueCtrl, 0, wx.EXPAND|wx.TOP, 2)
        panel.sizer.Add(mainSizer, 0, wx.EXPAND|wx.ALL, 10)
        while panel.Affirmed():
            panel.SetResult(
                valueCtrl.GetValue(),
            )       
Warning:
When the value of the variable is changed, the plugin will be stopped and restarted (this is normal when you open the plugin configuration dialog and change some parameter).

Pako
You know flattr ? You can Image
indyjason79
Posts: 4
Joined: Sun Sep 01, 2013 1:17 am

Re: Saving changed config data

Post by indyjason79 »

Thanks for the suggestions! For what I'm trying to accomplish I've decided to just save all my user data to xml like ScheduleGhost does.
Post Reply