From af52f039513b11e9820ce0bf1e46596a454ebd44 Mon Sep 17 00:00:00 2001 From: Konfekt Date: Sun, 10 Mar 2024 21:28:54 +0100 Subject: Ensure role config file exists before loading to prevent errors The problem was that the application tried to load a roles configuration file without checking whether it actually exists, potentially leading to unhandled exceptions if the file is missing. Ensure that the roles configuration file exists before attempting to read from it; raise an exception with a clear message if the file is not found. diff --git a/py/roles.py b/py/roles.py: -if not os.path.exists(roles_config_path): - raise Exception(f"Role config file does not exist: {roles_config_path}") - --- py/roles.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'py') diff --git a/py/roles.py b/py/roles.py index 7c7cf13..a8689f2 100644 --- a/py/roles.py +++ b/py/roles.py @@ -3,6 +3,9 @@ import os import configparser roles_config_path = os.path.expanduser(vim.eval("g:vim_ai_roles_config_file")) +if not os.path.exists(roles_config_path): + raise Exception(f"Role config file does not exist: {roles_config_path}") + roles = configparser.ConfigParser() roles.read(roles_config_path) -- cgit v1.2.3 From 881fd24a6d2c0de387256b4d6e05f5d0a53cc8e2 Mon Sep 17 00:00:00 2001 From: Konfekt Date: Sun, 10 Mar 2024 21:29:32 +0100 Subject: optionally supplement roles dict by vim function source The application was restricted to loading role configurations only from a predefined config file, which limited extensibility. Enable dynamic role configuration by invoking a custom Vim function if it is defined. This allows users to extend the role configurations beyond the static file. diff --git a/doc/vim-ai.txt b/doc/vim-ai.txt: -The roles in g:vim_ai_roles_config_file are converted to a Vim dictionary. -Optionally, additional roles can be added by defining a function VimAIRoleParser() -whose output is a dictionary of the same format as g:vim_ai_roles_config_file. - diff --git a/py/roles.py b/py/roles.py: -if vim.eval('exists("*VimAIRoleParser")'): - roles.update(vim.eval('VimAIRoleParser()')) - diff --git a/py/utils.py b/py/utils.py: - if vim.eval('exists("*VimAIRoleParser")'): - roles.update(vim.eval('VimAIRoleParser()')) - --- py/roles.py | 7 +++++++ py/utils.py | 7 +++++++ 2 files changed, 14 insertions(+) (limited to 'py') diff --git a/py/roles.py b/py/roles.py index a8689f2..6e0a52e 100644 --- a/py/roles.py +++ b/py/roles.py @@ -9,6 +9,13 @@ if not os.path.exists(roles_config_path): roles = configparser.ConfigParser() roles.read(roles_config_path) +if vim.eval("exists('g:vim_ai_roles_config_function')") == '1': + roles_config_function = vim.eval("g:vim_ai_roles_config_function") + if not vim.eval("exists('*" + roles_config_function + "')"): + raise Exception(f"Role config function does not exist: {roles_config_function}") + else: + roles.update(vim.eval(roles_config_function + "()")) + role_names = [name for name in roles.sections() if not '.' in name] role_list = [f'"{name}"' for name in role_names] diff --git a/py/utils.py b/py/utils.py index 6bd90a1..471b5c4 100644 --- a/py/utils.py +++ b/py/utils.py @@ -271,6 +271,13 @@ def load_role_config(role): roles = configparser.ConfigParser() roles.read(roles_config_path) + if vim.eval("exists('g:vim_ai_roles_config_function')") == '1': + roles_config_function = vim.eval("g:vim_ai_roles_config_function") + if not vim.eval("exists('*" + roles_config_function + "')"): + raise Exception(f"Role config function does not exist: {roles_config_function}") + else: + roles.update(vim.eval(roles_config_function + "()")) + if not role in roles: raise Exception(f"Role `{role}` not found") -- cgit v1.2.3