Kivy: calling app.config.get in .kv only returns string values bounded by " ", even with raw=True - the same call in .py correctly returns raw string

42 views Asked by At

I am attempting to use a settings.ini file within a kivy project. This settings file is simply a list of filepaths to various graphical assets.

In the .py file, all calls to self.config behave as expected.

.py behaviour:

self.config.get('Section', 'key')  
>"Subfolder/filename.png"

but

self.config.get('Section', 'key', raw=True)   
>Subfolder/filename.png

and

self.config['Section']['key']    
>Subfolder/filename.png

The problem lies in the .kv calls - my result is always, always bounded by double apostrophes.

.kv behaviour:

source: app.config.get(('Section', 'key')
>"Subfolder/filename.png"

and

source: app.config.get((section='Section', option='key', raw=True))`  
>"Subfolder/filename.png"

and

source: app.config['Section']['key']   
>"Subfolder/filename.png"

which of course, is not a valid filepath or filename - not when the double apostrophes are part of the filepath string.

I am trying to avoid assigning graphical assets within the .py file for the sake of abstraction/code reuse. I assume I am missing something obvious and/or simple.

To confirm this I did a len on the strings in the .py file, so

print(f"string length of config.get: {len(self.config.get('Section', 'key')}")
>string length of config.get: 22


print(f"string length of raw string: {len(self.config.get('Section', 'key', raw=True))}")    
>string length of raw string: 20

Edited to add:

Sample settings.ini file:

[Background]
attractor=Lang/back_00.png
main=Lang/back_01.png
cooldown=Lang/back_02.png

[Red_Digits]
zero=Lang/Red_Digits/0.png
one=Lang/Red_Digits/1.png
two=Lang/Red_Digits/2.png
three=Lang/Red_Digits/3.png
four=Lang/Red_Digits/4.png
five=Lang/Red_Digits/5.png
six=Lang/Red_Digits/6.png
seven=Lang/Red_Digits/7.png
eight=Lang/Red_Digits/8.png
nine=Lang/Red_Digits/9.png

[Digits]
zero=Lang/Digits/0.png
one=Lang/Digits/1.png
two=Lang/Digits/2.png
three=Lang/Digits/3.png
four=Lang/Digits/4.png
five=Lang/Digits/5.png
six=Lang/Digits/6.png
seven=Lang/Digits/7.png
eight=Lang/Digits/8.png
nine=Lang/Digits/9.png

I did try messing with the .ini file - using colons rather than '=', apostrophes, double apostrophes... but when I call for a raw value in main.py, I get the expected behaviour.

If I directly type the above filepath into my main.kv, i.e.:

source: 'Lang/back_00.png'

The image loads correctly, kivy is pretty good with relative paths.

###---UPDATE:

I used the standard python configparser, which performed as I would expect... when I, line by line, fed it into the kivy config, I encountered the above issue.

config_path = str(pathlib.Path(__file__).parent.absolute()) + "/" + "settings.ini"
std_config = ConfigParser()
std_config.read(config_path)

class MyApp(App):

    def build_config(self, config):
        for section in std_config.sections():               
            for key, value in std_config.items(section):                    
                config.setdefaults(section, {key: value})
            print(f"config.get: {config.get('Background', 'attractor')}")

which yields:

>>config.get: "Lang/back_00.png"

As a workaround, I'm just using:

config_path = str(pathlib.Path(__file__).parent.absolute()) + "/" + "settings.ini"
std_config = ConfigParser()
std_config.read(config_path)


class MyApp(App):

    assets = std_config

    def build(self):

        try:
            flyer = Builder.load_file('main.kv')

        except Exception as e:
            print(f"[MyApp.build]: {e}")

        finally:
            return flyer

Which works a treat... but doesn't answer the question of why I can't just use Kivy's built in config tools.

0

There are 0 answers