Populate enum in settings with code
#1
Is it possible to create a dynamic enum in the addon settings that I populate from python code?
Reply
#2
Sort of, but it's not pretty. Basically, you write the settings.xml file to disk from within your code.
Reply
#3
(2012-03-26, 18:17)Bstrdsmkr Wrote: Sort of, but it's not pretty. Basically, you write the settings.xml file to disk from within your code.

You're right not pretty, but a clever solution! Smile
Reply
#4
Hi,

yesterday I was searching for the answer at this question. The responses on this post weren't clear but they point me in a possible solution. I need to populate a labelenum with the serial ports of the system to connect the xbmc with an arduino. My settings entry is:

<setting id="arduino_port" label="30674" type="labelenum" values="COM5|COM7" />

where 30674 is the entry of the strings.po

The serial COM ports can vary from one computer to another so modify this entry on the fly is necessary for my purposes.

As some one in http://stackoverflow.com explains, exists two settings.xml

1.-The settings.xml where the addon settings are defined. This is the file you (as the add-on author) need to define. It should be located at xbmc\addons\<SCRIPT NAME>\resources\settings.xml.

2.- The (autogenerated) user related settings.xml where XBMC stores the chosen values. This is located at xbmc\userdata\addon_data\<SCRIPT NAME>\resources\settings.xml. You should NOT modify it.

The location of this files depends on the platform (Linux, Windows, Raspi, ...).

I use pyserial to read all the serial COM ports, create a string with the COM ports separated with a |. Something like "COM5|COM7". This string is saved in the labelenum.

To read and write the settings.xml file I use "import xml.etree.ElementTree as ET" but I know you can use some modules like BeautilSoup or somethink like this.

The code I use is this one:

Code:
def scan(self):
        """scan for available ports. return a list of tuples (num, name)"""
        available_ports = ""
        sep = ""
        available = []
        for i in range(256):
            try:
                s = serial.Serial(i)
                available.append( (i, s.portstr))

                available_ports = available_ports + sep + s.portstr

                sep = "|"

                s.close()  
            except serial.SerialException:
                pass

       # If no com ports exists, we save a - to avoid errors
        if available_ports is "":
            available_ports = "-"

        # CMainFile is a helper class with some useful functions based on Eldorados/eldorado-xbmc-addons
        settings_file = os.path.join(CMainFile.addon.get_path(), 'resources', "settings.xml")
        print "SETTING FILE %s" % settings_file
        tree = ET.ElementTree(file=settings_file)

      
        #Search for the entry in the settings file
        for elems in tree.iterfind(".//setting[@id='{0}']/..".format("arduino_port")):
            for elem in elems:
                if elem.tag == 'setting':
                    if elem.attrib['id'] == 'arduino_port':
                        elem.set('values', available_ports)



        tree.write(settings_file)

        return available

This is a sample, so any improvement is welcomed. I test it on Windows so I don't know if this code works on any other platform.

Regards
Reply
#5
Thanks for the post drJoju that got me to something I've been trying to do for a while. Rather than iterating it might be worth trying this, the logic works for me.

Code:
for elem in root.findall(".//setting[@id='arduino_port']"):
             elem.set('values', available_ports)

Martin
Reply
#6
Use "fileenum". Much better than programmatically editing settings.xml
Reply
#7
Ho Tokai,

where is the fileenum type in the page http://wiki.xbmc.org/index.php?title=Add-on_settings?

Regards
Reply
#8
(2014-08-24, 22:25)drJoju Wrote: where is the fileenum type in the page http://wiki.xbmc.org/index.php?title=Add-on_settings?

It's obviously a secret.. Set 'values' to a path, and it will display the items in this path. Then use 'mask' to filter file type.
Somebody should put this up on the wiki..
Reply
#9
Hi takoi,

I like secrets! Rofl. Good answer. I will do some tests and try to update the Wiki if they let me.

Best regards
Reply
#10
(2014-08-23, 20:18)emveepee Wrote: Thanks for the post drJoju that got me to something I've been trying to do for a while. Rather than iterating it might be worth trying this, the logic works for me.

Code:
for elem in root.findall(".//setting[@id='arduino_port']"):
             elem.set('values', available_ports)

Martin

To follow up on my earlier post this code needs ElementTree 1.3 so it don't work on Android or OSX yet.

I still find settings.xml better than fileenum.in my case. Adding and removing files isn't efficient.

Martin
Reply

Logout Mark Read Team Forum Stats Members Help
Populate enum in settings with code0