Reading and writing to settings file

  • Hi, I am new to the world of Enigma2 and python but have a php background.


    I have written some basic plugins for Enigma2 which work, but now I am trying to read and write to the settings file: etc/enigma2/settings


    The problem I have is that I don't know what the exact name of the value will be so cannot address it using code from other plugins.


    I am looking for a way to remove



    config.plugins.test.random=false
    config.plugins.test.anotherrandom=false
    config.plugins.test.something=false
    config.plugins.test.different=false


    from the settings file, then save a list with similar data back to the settings file.


    I understand that I need


    config.plugins.test = ConfigSubsection()



    but without knowing the actual names of random, another random etc I am very lost of how to do what I am aiming for.


    Many thanks n advance
    Woosh

  • If you don't now the name you can't read it. You must anyway first define the name of the config element to store any information.

    Gruss
    Dre


    Boxen (im Einsatz): DM920, DM900, DMOne
    Developer Project Merlin - we are OpenSource

  • HI dre, thanks for the reply


    Ok, so if I could re-structure my plugin process and knew the name of each entry, how would I loop to read the entires?


    list one to see if in settinga:
    icon1
    icon2
    icon3


    in settings:
    config.plugins.test.icon1 = pixel


    config.plugins.test.icon1 = myicon


    config.plugins.test.icon1 = picture






    config.plugins.test = ConfigSubsection()


    loop code?



    Also, what is the contents of config.plugins.test and is it a list?


    Thanks again.


    Please bear in mind I am a noob

  • first you need the ConfigSubsection:


    Code
    config.plugins.test = ConfigSubsection()



    Then you need a ConfigSubList:


    Code
    config.plugins.test.icon = ConfigSubList()


    It's recommendable to have a config entry holding the length of the ConfigSubList:


    Code
    config.plugins.test.count = ConfigInteger(0)


    Now, first you need to initialise the config:



    And here's how you read it finally:

    Code
    count = config.plugins.test.count.value
                    if count != 0:
                            for i in range(0,count):
                                    pixel = config.plugins.test.icon[i].pixel.value
                                    name = config.plugins.test.icon[i].name.value
                                    picture = config.plugins.text.icon[i].picture.value


    As there is quite likely more than one entry you should assign the values to a list.


    Credit goes to Dr.Best who helped me with that when I had the problem.

    Gruss
    Dre


    Boxen (im Einsatz): DM920, DM900, DMOne
    Developer Project Merlin - we are OpenSource

  • Thank you very much indeed.


    I am now trying to digest all that and piece it together. I am not saying I will be able to make sense of it or make it work but I will try.


    So all of that is required to read those three settings? I dread to think what saving it is going to look like.



    Thanks again.

  • the values of current settings you get with config.name_of_setting.value and this will work even for settings which still hold the default value, because these are NOT written to the settings file to reduce the number of Flash writes.


    Changing the value of a Setting explicitely (if not using Config Entries in a menu) works like this:


    config.name_of_setting.value=xxxx


    Saving you can do like this, but then the setting is written only when enigma2 restarts:


    config.name_of_setting.save()


    Of in a save routine for all your config values:


    for x in self["config"].list:
    x[1].save()
    # make sure it is in settings file


    This will force the settings file to be written immediately without restarting enigma2:


    configfile.save()


    Finally for setting names which you don't find in the settings file look at mytest.py (for enigma2 standard ones) or the Plugin's plugin.py which defines them

    2 Mal editiert, zuletzt von Lost in Translation ()

  • Thanks both, my head is really pickled but I will plod on and see what I can do with your info.


    Thanks again, I am sure this won't be the end.

  • OK so I am lost and can't get it to work for me, just a lot of crashes.


    This is what I am trying to achieve.


    My config file is on a web server and gets loaded into a list when a button is pressed. The contents of the file is names of icons:


    icon1
    icon4
    icon5
    icon7
    icon9


    The file can list up to 36 icons



    my settings file currently contains:


    config.plugins.test.icon1=false
    config.plugins.test.icon3=false
    config.plugins.test.icon5=false
    config.plugins.test.icon9=false


    I download this file and save it locally. I save it into a tmp folder so I can delete it when I am finished. This forces a new download the next time the plugin runs.


    Code
    target = 'http://www.webserver.co.uk/foldername/configfile.txt'configfile = urllib.URLopener()
    configfile.retrieve(target + target_url, "/usr/lib/enigma2/python/Plugins/Extensions/test/tmp/configfile.txt") # having a local copy here allows me to delete it aferwards forcing a new file load next time


    I now read the file into a list

    Code
    data = /usr/lib/enigma2/python/Plugins/Extensions/test/tmp/configfile.txt"list = []file = open(data, 'r') for line in file:     list.append(line)     listcount = len(list)



    now this is where I get completely stuck and don't know what I I am doing. Even trying with one value not in a list I cannot get it to work.


    Python
    from Components.config import config, ConfigSubsection, ConfigSelection, ConfigDirectory, ConfigYesNo, ConfigOnOff, Config, ConfigInteger, ConfigSubList, ConfigText, getConfigListEntry, configfile
    
    
    		config.plugins = ConfigSubsection()config.plugins.test = ConfigSubList()


    From here I get lost.


    What I need to happen is:


    Anything that is in the configfile gets added to the settings file


    config.plugins.test.icon1=false
    config.plugins.test.icon4=false
    config.plugins.test.icon5=false
    config.plugins.test.icon7=false
    config.plugins.test.icon9=false


    Anything that is no longer in the configfile gets removed from settings


    config.plugins.test.icon5=false
    config.plugins.test.icon9=false
    The settings file then gets saved

    configfile.save()




    To remove them I think I would need to loop from 1 - 36. This would be before adding the new ones.

    Code
    x = 0while x < 36:     config.plugins.test.icon[x] = false # find every entry and remove     x += 1




    I am at a loss. I am sorry if it simple for you, but this really has me beaten.


    Thanks in advance again.


    PS, the code window is joining my lines together for some reason. I have not coded it like that.

    Einmal editiert, zuletzt von wooshman ()

  • Please post the crashlog.


    Honestly, I don't get why you want to store something in the config when it should be replaced the next time you open the plugin. Why don't you simply download the data to /tmp and display it? It's anyway better to use twisted instead of urllib.

    Gruss
    Dre


    Boxen (im Einsatz): DM920, DM900, DMOne
    Developer Project Merlin - we are OpenSource

  • The reason is because the values are already stored in settings by another plugin and I have no control over how the plugin works. Instead of using the other plugin to manually make changes, I want to be able to change the icons using a remote file. There is no way for me to physically run the original plugin on all boxes and this will make the users life easier, I can do it via a remote file, they simply open he new plugin and the work is done.


    As for twisted vs urllib, I don't know what this is as I am so new to python and learning as I go.


    I don't necessarily need to remove the setting if it already exists and will be put back there, it can just remain there, but I am using simple logic as I am unsure of what you can do with it all.

  • I'm sorry, but you are still NOT getting the point how enigma2 SETTINGS work. YOU don't have to put them into the settings file, as this is just the cache to remember them when enigma2 is stopped and load thghem from there during startup so that they are persistent over a reboot/restart.


    You simply set them how you want and then when enigma2 stops it will be written to the settings file by enigma2 shutdown code.


    The enigma2 code NEVER reads the settings from the file during runtime, only during startup it reloads them from there. During run-time it always used the cached values loaded from the file to memory and they are directly changed and read from THERE.


    The whole thing does NOT work like a Windows registry or the typical Linux configuration files.

  • Ok, that is fair comment, I didn't know it worked like that. The third party plugin instantly save to settings so I thought I needed to do the same.


    It doesn't change the fact that I still have to read existing values and compare them to new settings though weather in settings or in memory. I still have a list of icons added by a third party plugin and I need to be able to either keep/replace or append depending on what icons are in my list. I do know that the third party plugin saves to settings all the time.


    I understand what you are saying, but the end result required is still the same.


    Would you mind me sending you a PM please so I can explain better and precisely?


    Thanks
    Woosh

  • The problem is that I still don't have the complete picture. I understand that you want to temporaly overwrite settings written by an other plugin. But I don't know what the other plugin does. And the way you describe everything and making sure we only get a blur picture I'm pretty sure it does something illegal.

    Gruss
    Dre


    Boxen (im Einsatz): DM920, DM900, DMOne
    Developer Project Merlin - we are OpenSource

  • The board is for support, NOT our PM box and the boardrules apply also for PMs, so there is no added value either.


    Finally you got all the necessary code pieces posted and explained, if you are insisting not to use them it is not our fault :face_with_rolling_eyes:


  • The problem is that I still don't have the complete picture. I understand that you want to temporaly overwrite settings written by an other plugin. But I don't know what the other plugin does. And the way you describe everything and making sure we only get a blur picture I'm pretty sure it does something illegal.

    No, it is not illegal in the least Dre, I merely want to edit the settings of a third party open source plugin (which are stored in the settings file) via a remote config file instead of the end user having to load the third party plugin and go through all the settings themselves each time it is needed. I am actually trying to make life easier for people by removing the need for them to edit anything. Currently I don't want to spell out what I am working on as it is an idea I have had for a while now which is why it is a blurred picture. Once released the code will be freely available. It is also for the same reason I asked about sending a PM so the idea doesn't get used before my own release. I didn't realise that trying to protect my idea was against board rules.


    I appreciate that all the code required is in front of me, but piecing it together is what I am struggling with. Nowhere have I insisted that I will not use it. I came and asked for help, why would I not use the information given in return.


    Sorry to have wasted your support time gutemine, asking questions as someone completely new to python, a great way to encourage people.

  • HI Dre


    I do not give up easily. I can write to and remove entries from the settings file providing I include the name of the entry in full using both of your replies.


    myval = "something"



    config.plugins.test.icon7 = ConfigText(default = "")
    config.plugins.test.icon7.value = myval
    config.plugins.test.icon7.save()


    configfile.save()



    Using the above I can create a new settings entry, edit a value or remove from the settings file.


    Please can you tell me (and believe me I have tried everything I can think of) do do it this way


    lines = []
    # code here to populate list with icon names



    a = 0
    aa = len(lines)
    while a < aa:


    config.plugins.iptvplayer.lines[a] = ConfigText(default = "")
    config.plugins.iptvplayer.lines[a].value = myval
    config.plugins.iptvplayer.lines[a].save()
    a += 1


    configfile.save()


    I have tried append, but I think this is screen menu related, I have tried ' + lines[a] + ' I have tried all sorts but haven't figured it out.


    Many thanks in advance.

  • it's better to use to code tags. This makes it better readable.


    try this:

    Code
    config.plugins.test.icon.save()
    config.plugins.test.save()

    Gruss
    Dre


    Boxen (im Einsatz): DM920, DM900, DMOne
    Developer Project Merlin - we are OpenSource

  • Thanks Dre, sorry, I forgot about using code tags. I tried setting icon = lines[a] and although never crashed it didn't do anything in the settings file either.





    I have shown a message box to check that lines[a] is the name of the icon and also aa to check the total len(lines) and they both show the expected result


    The above is all the code I have for this part of the plugin, which as I said before works if I know the name of icon and add it directly, but am failing to be able to use the name stored in lines[a].

    5 Mal editiert, zuletzt von wooshman ()

  • have you tried to add a print to check the config entry after the assignment?


    Code
    print config.plugins.test.icon[a].name.value


    Isn't config.plugins.text.icon your ConfigSubList? At least in my example above it was.

    Gruss
    Dre


    Boxen (im Einsatz): DM920, DM900, DMOne
    Developer Project Merlin - we are OpenSource