feat(config): add named configuration profiles
Profiles declared as `profile <name> { ... }` in kitty.conf are now collected
while the configuration is read and applied only when selected, so switching
between projects no longer requires duplicating or editing kitty.conf.
The selected profile is layered over the global configuration, meaning it only
overrides the options it declares. Selection happens with --profile or
KITTY_PROFILE, reloading keeps the active profile, and an unknown or malformed
profile is reported without blocking startup.
This commit is contained in:
9 files changed
+303
-9
No files matched your search
@@ -215,6 +215,7 @@ def generate_class(defn: Definition, loc: str) -> tuple[str, str]:
|
||||
a(' config_paths: tuple[str, ...] = ()')
|
||||
a(' all_config_paths: tuple[str, ...] = ()')
|
||||
a(' config_overrides: tuple[str, ...] = ()')
|
||||
a(" profile_name: str = ''")
|
||||
a('')
|
||||
a(' def __init__(self, options_dict: dict[str, typing.Any] | None = None) -> None:')
|
||||
if defn.has_color_table:
|
||||
|
||||
+95
-3
@@ -245,12 +245,67 @@ def geninclude(path: str) -> list[str]:
|
||||
|
||||
|
||||
include_keys = 'include', 'globinclude', 'envinclude', 'geninclude'
|
||||
profile_start_pat = re.compile(r'^profile\s+([A-Za-z0-9_.-]+)\s*\{$')
|
||||
ProfileLines = list[tuple[str, int, str]]
|
||||
|
||||
|
||||
class RecursiveInclude(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class ProfileCollector:
|
||||
"""Gathers the contents of ``profile <name> { ... }`` blocks instead of parsing them.
|
||||
|
||||
Blocks are buffered so that the caller can parse them separately, after the
|
||||
rest of the configuration, which is what allows a profile to only override
|
||||
the options it declares. One collector is used per file, so a block cannot
|
||||
span an include.
|
||||
"""
|
||||
|
||||
__slots__ = ('profiles', 'current', 'start')
|
||||
|
||||
def __init__(self, profiles: dict[str, ProfileLines]) -> None:
|
||||
self.profiles = profiles
|
||||
self.current: str | None = None
|
||||
self.start: tuple[str, int] = ('', 0)
|
||||
|
||||
def handle(self, line: str, file: str, line_num: int) -> bool:
|
||||
stripped = line.strip()
|
||||
if self.current is None:
|
||||
m = profile_start_pat.match(stripped)
|
||||
if m is not None:
|
||||
self.current = m.group(1)
|
||||
self.start = (file, line_num)
|
||||
self.profiles.setdefault(self.current, [])
|
||||
return True
|
||||
if stripped == '}':
|
||||
raise ValueError('Unexpected "}" outside of a profile block')
|
||||
return False
|
||||
if stripped == '}':
|
||||
self.current = None
|
||||
return True
|
||||
if profile_start_pat.match(stripped) is not None:
|
||||
raise ValueError(f'Profile blocks cannot be nested, found one inside the profile {self.current!r}')
|
||||
self.profiles[self.current].append((file, line_num, line))
|
||||
return True
|
||||
|
||||
def finish(self) -> None:
|
||||
if self.current is not None:
|
||||
file, line_num = self.start
|
||||
where = file or 'the configuration'
|
||||
raise ValueError(f'The profile {self.current!r} opened on line {line_num} of {where} was never closed')
|
||||
|
||||
|
||||
def handle_profile_line(collector: ProfileCollector, line: str, line_num: int, accumulate_bad_lines: list[BadLine] | None) -> bool:
|
||||
try:
|
||||
return collector.handle(line, currently_parsing.file, line_num)
|
||||
except ValueError as e:
|
||||
if accumulate_bad_lines is None:
|
||||
raise
|
||||
accumulate_bad_lines.append(BadLine(line_num, line.rstrip(), e, currently_parsing.file))
|
||||
return True
|
||||
|
||||
|
||||
class Memory:
|
||||
def __init__(self, accumulate_bad_lines: list[BadLine] | None) -> None:
|
||||
self.s: set[str] = set()
|
||||
@@ -283,6 +338,7 @@ def parse_line(
|
||||
memory: Memory,
|
||||
accumulate_bad_lines: list[BadLine] | None = None,
|
||||
allow_geninclude: bool = True,
|
||||
profiles: dict[str, ProfileLines] | None = None,
|
||||
) -> None:
|
||||
line = line.strip()
|
||||
if not line or line.startswith('#'):
|
||||
@@ -312,6 +368,7 @@ def parse_line(
|
||||
accumulate_bad_lines,
|
||||
effective_config_lines,
|
||||
allow_geninclude=allow_geninclude,
|
||||
profiles=profiles,
|
||||
)
|
||||
return
|
||||
elif key == 'geninclude':
|
||||
@@ -338,6 +395,7 @@ def parse_line(
|
||||
accumulate_bad_lines,
|
||||
effective_config_lines,
|
||||
allow_geninclude=allow_geninclude,
|
||||
profiles=profiles,
|
||||
)
|
||||
return
|
||||
else:
|
||||
@@ -350,7 +408,16 @@ def parse_line(
|
||||
try:
|
||||
with open(val, encoding='utf-8', errors='replace') as include:
|
||||
with currently_parsing.set_file(val):
|
||||
_parse(include, parse_conf_item, ans, memory, accumulate_bad_lines, effective_config_lines, allow_geninclude=allow_geninclude)
|
||||
_parse(
|
||||
include,
|
||||
parse_conf_item,
|
||||
ans,
|
||||
memory,
|
||||
accumulate_bad_lines,
|
||||
effective_config_lines,
|
||||
allow_geninclude=allow_geninclude,
|
||||
profiles=profiles,
|
||||
)
|
||||
except FileNotFoundError:
|
||||
log_error(f'Could not find included config file: {val}, ignoring')
|
||||
except OSError:
|
||||
@@ -370,8 +437,10 @@ def _parse(
|
||||
accumulate_bad_lines: list[BadLine] | None = None,
|
||||
effective_config_lines: Callable[[str, str], None] | None = None,
|
||||
allow_geninclude: bool = True,
|
||||
profiles: dict[str, ProfileLines] | None = None,
|
||||
) -> None:
|
||||
name = getattr(lines, 'name', None)
|
||||
collector = ProfileCollector(profiles) if profiles is not None else None
|
||||
effective_config_lines = effective_config_lines or (lambda a, b: None)
|
||||
if name:
|
||||
base_path_for_includes = os.path.abspath(name) if name.endswith(os.path.sep) else os.path.dirname(os.path.abspath(name))
|
||||
@@ -410,7 +479,11 @@ def _parse(
|
||||
next_line = ''
|
||||
try:
|
||||
with currently_parsing.set_line(line, line_num):
|
||||
parse_line(line, parse_conf_item, ans, base_path_for_includes, effective_config_lines, memory, accumulate_bad_lines, allow_geninclude)
|
||||
if collector is not None and handle_profile_line(collector, line, line_num, accumulate_bad_lines):
|
||||
continue
|
||||
parse_line(
|
||||
line, parse_conf_item, ans, base_path_for_includes, effective_config_lines, memory, accumulate_bad_lines, allow_geninclude, profiles
|
||||
)
|
||||
except Exception as e:
|
||||
if accumulate_bad_lines is None:
|
||||
raise
|
||||
@@ -418,6 +491,15 @@ def _parse(
|
||||
except StopIteration:
|
||||
break
|
||||
|
||||
if collector is not None:
|
||||
try:
|
||||
collector.finish()
|
||||
except ValueError as e:
|
||||
if accumulate_bad_lines is None:
|
||||
raise
|
||||
file, line_num = collector.start
|
||||
accumulate_bad_lines.append(BadLine(line_num, '', e, file))
|
||||
|
||||
|
||||
def parse_config_base(
|
||||
lines: Iterable[str],
|
||||
@@ -426,8 +508,18 @@ def parse_config_base(
|
||||
accumulate_bad_lines: list[BadLine] | None = None,
|
||||
effective_config_lines: Callable[[str, str], None] | None = None,
|
||||
allow_geninclude: bool = True,
|
||||
profiles: dict[str, ProfileLines] | None = None,
|
||||
) -> None:
|
||||
_parse(lines, parse_conf_item, ans, Memory(accumulate_bad_lines), accumulate_bad_lines, effective_config_lines, allow_geninclude)
|
||||
_parse(
|
||||
lines,
|
||||
parse_conf_item,
|
||||
ans,
|
||||
Memory(accumulate_bad_lines),
|
||||
accumulate_bad_lines,
|
||||
effective_config_lines,
|
||||
allow_geninclude,
|
||||
profiles=profiles,
|
||||
)
|
||||
|
||||
|
||||
def merge_dicts(defaults: dict[str, Any], newvals: dict[str, Any]) -> dict[str, Any]:
|
||||
|
||||
Reference in new issue
Block a user