Reading and writing to settings file

  • My very first example using icon1 as the ConfigSubList was incorrect as I had to many elements. I know you will be shaking your head at me right now but this is the first real attempt at anything decent for a plugin and was a school boy error.


    Below is exactly what I am working with and I presume it will change any exmples you have given me for which I am sorry.


    This is what I am working with in the settings file

    Code
    config.plugins.test.icon1 = false
    config.plugins.test.icon2 = false
    config.plugins.test.icon3 = false
    config.plugins.test.icon4 = false
    config.plugins.test.icon5 = false


    and the content of my list.txt is

    Code
    icon1
    icon2
    icon3
    icon4
    icon5
    icon6
    icon7


    I have re-arranged my code to make it tidier. Reading the txt file is now done before anything else which leaves me with a neat

    Einmal editiert, zuletzt von wooshman ()

  • I've just realised that you are missing a "." when you assign myval to the config entry.


    But anyway: given the 5 icon config entries below your code from the previous post can't work. you overwrite the value in config.plugins.test.icon everytime in your while-loop. If you think you'll create icon1, icon2, ... in your loop then it's wrong. again: you need to have a ConfigSubList or it won't work.

    Gruss
    Dre


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

  • At present I want to remove them all from settings by giving a value of "" to all of them. But using the lines[a] is my problem as I can't seem to do it. If you could help with using lines[a] in place of my icon name I think I will then be able to use it again for adding the icons back to the settings file before I save it.


    I don't doubt what you are saying one little bit but don't really understand what ConfigSubList really is.
    config.plugins.test = ConfigSubsection()


    config.plugins.test. ?? ConfigSubList




    On a loop I am expecting this to happen


    Code
    config.plugins.test.icon1 = ConfigText(default = "")
    	config.plugins.test.icon1.value = myval
    	config.plugins.test.icon1.save()
    	config.plugins.test.icon2 = ConfigText(default = "")
    	config.plugins.test.icon2.value = myval
    	config.plugins.test.icon2.save()
    	config.plugins.test.icon3 = ConfigText(default = "")
    	config.plugins.test.icon3.value = myval
    	config.plugins.test.icon3.save()
    	config.plugins.test.save()


    Once this has been done I am going to read a second txt file which will could contain:

    Code
    icon3
    icon5
    icon11
    icon24


    which I will then loop through and add to the settings file:

    Code
    myval = "false"
    while b < bb:
    	icon = newlines[b]
    	config.plugins.test.icon = ConfigText(default = "")
    	config.plugins.test.icon.value = myval
    	config.plugins.test.icon.save()
    	config.plugins.test.save()
    b += 1
    configfile.save()

    Einmal editiert, zuletzt von wooshman ()

  • in your while-loops you simply overwrite config.plugins.test.icon everytime...and the last value is stored.

    Gruss
    Dre


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

  • but won't using


    icon = lines[a]


    in a loop change the value of icon to the next one?


    icon = icon1
    icon = icon2


    Or am I not explaining myself very well, have confused myself, or should give up lol

  • no, icon is a variable und you assign lines[a] to it.


    the config entry is config.plugins.test.icon

    Gruss
    Dre


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

  • Right OK, so they are two separate things completely then and can't be done the way I thought it could.


    So using your first example in the thread would let me use the value of lines[a] somehow?

  • Interesting discussion.
    Seems as if you talk about two different things, having a lot in common but also having a decent difference.
    If I got right, what the TS wants, there is a plugin A (which is written by someone else and can not be adapted), which writes entries into the settings of Enigma2.
    There is no way to "control" the way of writing or the structure to write. So this is a fixed requirement, set by the plugin A.
    The TS wants to write a plugin B, which should manipulate some settings of plugin A. The set of settings to manipulate is fixed (e.g. always three fields of a specific structure) but the number of sets is not.
    So it can happen, that plugin B adds some new sets, but it can also happen, that plugin B removes some sets, or it just updates existing sets.
    Plugin B never knows in advance, how many sets are available in the Enigma2 settings (written by plugin A). It only knows how many to write, because this will be determined by the content of some file available to plugin B.
    According to the first writings of the TS, the structure set has a "running index number" as part of the structure ("icon1", "icon2", "icon3").
    So his problem is, to code something, which is manipulating this kind of "dynamic" structure set within the Enigma2 settings (with "dynamic" being the "running index number").


    @TS: Is this the problem you are facing?

    DM900 SS, DM8000SSSS
    Kein Support per PN! Nutzt das Forum zum Fragen, dann haben auch andere etwas davon.

  • 100% correct I would say I couldn't have written it better myself but I failed to that that.


    The only thing which is slightly wrong is the running index number. I used that just for example, the actual data is various icon names


    so the actual line will look like
    config.plugins.test.pear=false


    config.plugins.test.salad=false

    config.plugins.test.chair=false



    but because they are icons it made more sense and was clearer to use the icon1, icon2, icon3 method. There is no index number on the icon name.


    Apart from that you know exactly what I have and what I am looking to achieve.


    Again, this is nothing illegal, I am purely trying to create plugin which a user can simply run and then close which saves them needed to go and enable or disable icons which can take time and introduces the risk of error. I would rather spend my time creating something that makes a users life easier.

  • that's what I understood as well but I still don't get why he's not following my example.


    Step 1:
    read the file content into a list (e.g. [false, true, false])


    Step 2:
    create the ConfigSubList (e.g. config.plugins.test.mysublist = ConfigSubList())
    create the CofigSubsection (as in the example I gave you in one of the first posts)


    Step 3:
    assign each value in the list to the config (config.plugins.test.mysublist with n items per mysublist)
    --> for the for config.plugins.test.mysublist[0].icon = False
    As it is a ConfigSubList you do not need to know how many entries you need. You simply create them when iterating the list (but don't forget to remove old one when no longer needed).


    Step 4:
    read the values from the config when needed.

    Gruss
    Dre


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

  • Thanks Dre
    Because I don't fully understand what it is I am actually writing. Not knowing what I am writing means I can't fix it when I break it.


    I have no doubt that those steps above will work, but writing the code to do that without understanding makes it very difficult.


    I will look closely at he above and see what I can do, but don't hold your breath.
    ----------------------------------


    Code
    def initProfileConfig():
            s = ConfigSubsection()
            s.pixel =  ConfigText(default = "")
            s.name = ConfigText(default = "")
            s.picture = ConfigText(default = "")
            config.plugins.test.icon.append(s)
            return s


    presumes I know the names - pixel, name, picture, but I don't. At this step they will only be in a list. Again, how would I loop the list to get get the above. Nowhere in any of this will I know the names of the icons without manually opening either the downloaded txt file or open the settings file.


    I have explained the best I can and provided my current files and code.

    Einmal editiert, zuletzt von wooshman ()

  • No, it doesn't! These are three config entries you can assign a value to. This has absolutely nothing to do with the content of your file.


    Each element of the sublist will have them:


    Config.plugins.test.icon[n].pixel
    Config.plugins.test.icon[n].name
    Config.plugins.test.icon[n].picture

    Gruss
    Dre


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

  • Next try to mediate. :smiling_face:


    The external file looks like that:
    pear=true
    salad=false
    tree=true


    Plugin A (the external plugin) already placed this in the settings (somewhen in the past):
    config.plugins.test.pear=false
    config.plugins.test.salad=false
    config.plugins.test.chair=false


    Plugin B "knows" only this prefix part of the entries in the settings for sure:
    config.plugin.test.


    Everything after this prefix is "unknown" to the plugin B.
    So the plugin B has to query the already existing entries starting with the known prefix (config.plugin.test.).
    The plugin B also has to read the file content and "merge" the result.
    The result in the settings should look like this:
    config.plugins.test.pear=true
    config.plugins.test.salad=false
    config.plugins.test.chair=false
    config.plugins.test.tree=true


    The problem is, how to build the variable "config.plugins.test.tree" and to add this to the settings, as well as how to query the already existing entries and to change the value of this entries.
    I would assume, that something like the creation of the variable name and the usage of "exec" is needed.
    Unfortunately I am not a Python in Enigma2 expert and don't know, if Enigma2 supports this kind of "hack".


    @TS: Is that your current problem?

    DM900 SS, DM8000SSSS
    Kein Support per PN! Nutzt das Forum zum Fragen, dann haben auch andere etwas davon.

  • OK


    So


    Config.plugins.test.icon[n].pixel
    Config.plugins.test.icon[n].name
    Config.plugins.test.icon[n].picture




    are "pixel", "name" and "picture" predefined things within Engima 2? Please can you confirm this for me?



    If so it was pure coincidence that I chose "pixel", "myicon" and "picture" in my example and this has confused me so much. I will also then start from the beginning asking questions. I know you are trying to make me work for my solution and appreciate that but when I am seeing "pixel" and "picture" in your code which I used as an example a long time ago it has confused the hell out of me.



    Your patience is appreciated

  • again kenatonline spot on with what there is and what is required.


    Dre is trying to help but with code snippets here and there with little explanation I am not getting very far but very confused.

  • pixel, name and picture were used by you in one of your posts (and maybe removed with an edit later). these are simply config elements in which you can store values. if you only need one element then just add one. but you MUST use ConfigSubList(). I don't understand why you're not using the code provided in one of the very first posts. It's all there except the exact example of assigning the values to the config element. But this should work similar to the reading.


    But maybe it's better I let you find your solution yourself as I don't want to further confuse you.

    Gruss
    Dre


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

  • Dre


    I am not being funny or awkward towards you but you have provided examples which are out of date compared to the new explanations and examples, this is why I am getting confused. I have already that my presvious early posts we not correct.


    create the CofigSubsection (as in the example I gave you in one of the first posts)


    Config.plugins.test.icon[n].pixel
    Config.plugins.test.icon[n].name
    Config.plugins.test.icon[n].picture

    is not relevant to where we are now at because I don't know the names


    To change your example to reflect wouldn't we have this:


    Code
    def initProfileConfig():
            s = ConfigSubsection()
            s.lines[a] =  ConfigText(default = "")
            s.lines[a] = ConfigText(default = "")
            s.lines[a]= ConfigText(default = "")
            config.plugins.test.append(s)
            return s


    Would this work though because you said lines[a] is a variable ??

  • "I would assume, that something like the creation of the variable name and the usage of "exec" is needed."

    kenatonline was spot on. With this I looked into what you meant and found a plugin as an example which indeed does exactly what you thought.


    exec('config.plugins.test.' + lines[a] + ' = ConfigText(default = "")')
    exec('config.plugins.test.' + lines[a] + '.value = myval')
    exec('config.plugins.test.' + lines[a] + '.save()')


    So now on the face of it I can read and write to the settings file with just a few lines of text.