Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced

Source of release 0.4.1

0.4.1 / src / configuration / store.luc

## File ownership is separate from parsing and application. Defaults are created
## exclusively; malformed or existing user files are never rewritten at startup.
from luce_std import files
import workspace.paths as paths
import configuration.settings as settings
import configuration.theme as theme
from luce_ai import config as ai
import language.default_grammars as default_grammars
import language.grammars as grammars

pub class Store:
    pub let directory: str
    pub let settings_path: str
    pub let theme_path: str
    pub let ai_path: str
    pub let grammars_dir: str

    pub func init(self, directory: str = "") -> unit!:
        let chosen = directory if directory != "" else paths.join(files.home_directory(), ".luced")
        files.ensure_directory(chosen)
        let absolute = files.canonical(chosen)
        let settings_path = paths.join(absolute, "settings.toml")
        let theme_path = paths.join(absolute, "theme.toml")
        let ai_path = paths.join(absolute, "ai.toml")
        let grammars_dir = paths.join(absolute, "grammars")
        files.ensure_directory(grammars_dir)
        default_grammars.seed(grammars_dir)
        grammars.install_bundled(grammars_dir)
        create_default(settings_path, settings.defaults())
        create_default(theme_path, theme.defaults())
        create_default(ai_path, ai.defaults())
        self.directory = absolute
        self.settings_path = settings_path
        self.theme_path = theme_path
        self.ai_path = ai_path
        self.grammars_dir = grammars_dir

    pub func read_settings(self) -> str!:
        return files.read_text(self.settings_path, limit = 65536)

    pub func read_theme(self) -> str!:
        return files.read_text(self.theme_path, limit = 65536)

    pub func read_ai(self) -> str!:
        return files.read_text(self.ai_path, limit = 65536)

func create_default(path: str, contents: str) -> unit!:
    files.create_text(path, contents) catch failure:
        if failure.code != files.already_exists:
            error(failure.code, failure.message)