[SOLVED]Best Way to Pass Config Prefs to Menu Items

Posted on
Sun Feb 02, 2014 9:00 am
DaveL17 offline
User avatar
Posts: 6765
Joined: Aug 20, 2013
Location: Chicago, IL, USA

[SOLVED]Best Way to Pass Config Prefs to Menu Items

I would like to call the value of a config pref from within a script located in the Menu Items folder. Right now, I'm doing this by sending the value to an Indigo Variable--which is easy and works, but seems really inelegant.

My understanding is that each script loads into its own host process, so presumably this would be the case with scripts located within the same plugin package. It seems to me that I would have to add the Server Plugin folder to the search path and either (1) call the pluginConfig prefs directly, or (2) import what I need from plugin.py.

What's the preferred method to get config prefs from other places within the package?

Or

What is the preferred method to make everything contained within the package available to everything else in the same package?

Thanks,
Dave

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Mon Feb 03, 2014 11:59 am
jay (support) offline
Site Admin
User avatar
Posts: 18230
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Best Way to Pass Config Prefs to Menu Items

So, it took us a while to figure out what you were talking about - the "Menu Items" folder isn't something that appears to be used very much so it took some head scratching for it to dawn on us (I'm getting old so the memory isn't what it used to be).

Anyway - scripts in that directory are executed in their own process - so they don't have access to plugin prefs from the plugin in which they reside. Think of them as standalone Indigo Python scripts that are executed from the Execute Script action pointing to a Python file - that's basically what they are.

When you say:

What's the preferred method to get config prefs from other places within the package?


I'm a bit unclear what you're going for. Preferences are plugin-based - so plugins have prefs for the plugin itself and any object types that it defines (devices, actions, events, etc.). So you can access/change those from any python code that is run as part of the normal operation of the plugin itself (which leads to building your own plugin as stated above). Scripts external to those plugin processes will never have write access to that data since they could break the plugin's operation.

However, if you're just looking for a way to share generic data between scripts, then there are a couple of options:

  1. Do what you're doing now and store it in variables. This has the advantage that it provides you with a UI to change/set the data without having to build a plugin with the various config UIs.
  2. You can store the data in separate files which each script can load.
  3. You can create an attachment script that handles the file i/o from #2 above then have all your scripts just load that module (see the Shared Classes and Methods section of the Scripting Tutorial for details). I believe this answers your second question - it's definitely the pythonic way of sharing objects/methods.

Indigo doesn't present a generic database-like interface for scripts (as opposed to plugins) other than Indigo variables.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Feb 03, 2014 12:47 pm
DaveL17 offline
User avatar
Posts: 6765
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Best Way to Pass Config Prefs to Menu Items

Thanks guys. I should've worded my question more carefully; sorry for the work you had to do to figure out what I was talking about!

So here's the deal with my original question:

- I have a plugin that grabs an XML file from a server, and this server's IP address is specified in the plugin's configuration menu. The plugin is used to support devices that will get data from this XML file (1-wire sensors).

- I have a python script located in the Menu Items folder for the client to be able to dump a copy of the parsed (recursed?) XML file to the log (thus needing the same IP address). I was looking for a way to read (read only) the IP from the config menu prefs for this supplemental logging script--which you rightly surmised. I figured that this would only be needed occasionally, so it seemed that this was the logical place to put it (the Menu Items folder).

Your options are helpful--I like option 2 in particular--but as I'm writing this, I think there might be another way to come at the problem. I wonder if I couldn't just add a button to the config menu to do the same thing (dump the XML to the log), and then include the necessary code from my supplemental script in my base plugin.py file. This would eliminate the need to do anything outside of the original process. I think I will try that tonight.

Again, sorry for the head scratcher!
Dave

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Mon Feb 03, 2014 1:23 pm
jay (support) offline
Site Admin
User avatar
Posts: 18230
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Best Way to Pass Config Prefs to Menu Items

Adding the code to the plugin seems like the most logical course of action. And the button would work - or just implement your menu in MenuItems.xml - it's probably really simple:

Code: Select all
<MenuItems>
    <MenuItem id="menu0">
        <Name>Dump XML</Name>
        <CallbackMethod>dumpXML</CallbackMethod>
    </MenuItem>
</MenuItems>


In your plugin, you just implement the dumpXML method:

Code: Select all
def dumpXML(self):
    # implement your dump here


<MenuItem>s in the MenuItems.xml file are added to the same menu - in fact if you have both MenuItems.xml and scripts in the "Menu Items" folder, the menu items in MenuItems.xml are shown first then any scripts in that folder.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Feb 03, 2014 1:45 pm
DaveL17 offline
User avatar
Posts: 6765
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Best Way to Pass Config Prefs to Menu Items

jay (support) wrote:
<MenuItem>s in the MenuItems.xml file are added to the same menu - in fact if you have both MenuItems.xml and scripts in the "Menu Items" folder, the menu items in MenuItems.xml are shown first then any scripts in that folder.

Sweet. This is great.

Thanks so much!
Dave

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Mon Feb 03, 2014 7:35 pm
DaveL17 offline
User avatar
Posts: 6765
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Best Way to Pass Config Prefs to Menu Items

Just to follow-up. I included the code within a dumpXML method in the plugin, and implemented the button within the config dialog. I was thinking that it works well as a part of the preference validation process.

It works perfectly.

Thanks again.
Dave

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests