What is the proper place to store settings for an extension? Python

Hi, I’m using a file in the tmp directory. If there is an API, it would be good. My intention is to store caches for files and json used for the extension. I saw FileSystems service and I would like the files that I’m saving can persist when an upgrade happens to the extension.

class Settings:
    """
    Store and load settings
    """

    def __init__(self):
        self.base_directory = "/tmp"
        self.settingsfile = "stablehordesettings.json"
        self.file = self.base_directory + "/" + self.settingsfile

    def load(self) -> json:
        if not os.path.exists(self.file):
            return {"api_key": ANONYMOUS}
        with open(self.file) as myfile:
            return json.loads(myfile.read())

    def save(self, settings: json):
        with open(self.file, "w") as myfile:
            myfile.write(json.dumps(settings))
        os.chmod(self.file, 0o600)

Personally I would split them: I would store settings in registrymodifications.xcu (which would need an XCU/XCS config data/schema files in your extension) - which makes migrating their LibreOffice settings along with your extension’s settings much easier; and would store caches and stuff like that where @elmau suggested.

Thanks, @elmau, @karolus , @mikekaganski I used the code in this extension.

Feel free to add any comments or suggestions to have best practices. Sticking to json for simplicity.

My extension https://extensions.libreoffice.org/en/extensions/show/1983 stores localized strings in Localization.xcu based on the declarations in Localization.xcs.

For this, I generally use the “config” directory within the user profile.

You need service: 'com.sun.star.util.PathSettings'

https://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1util_1_1XPathSettings.html

1 Like

Thanks @elmau ,I tried

from scriptforge import CreateScriptService
bas = CreateScriptService("Basic")
doc = CreateScriptService("Document", bas.ThisComponent)
fso = CreateScriptService("FileSystem")
sRoot = doc.FileSystem

But I’ not able to get to FileSystem to get ConfigFolder. What would be the way to get the config folder?

I have edited my previous reply.

1 Like
import uno
from pathlib import Path

create_service = XSCRIPTCONTEXT.ctx.ServiceManager.createInstance
# https://api.libreoffice.org/docs/idl/ref/servicecom_1_1sun_1_1star_1_1util_1_1PathSubstitution.html
path_finder = create_service("com.sun.star.util.PathSubstitution")

config_url = path_finder.substituteVariables("$(user)/config",True)
config_path = uno.fileUrlToSystemPath( config_url)
setting_path = Path( config_path ) / "stablehordesettings.json" 
with setting_path.open('w') as json_file:
    do sth…

and of course: !!! NO need for scriptforge !!!

@ikks
edit: if your extension itself is installed for single-user:

from pathlib import Path

config_path = list(Path(__file__).parents[10].glob("**/user/config"))[0]

OOMacros Explained 8.9.1

But wait, the PathSettings service was deprecated with LO 4.3 and you should now use the
“thePathSettings” singleton.

Use the component context to get the singleton