ipyllm 0.5.0__py3-none-any.whl → 0.7.0.dev4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
ipyllm/config.py ADDED
@@ -0,0 +1,72 @@
1
+ # Copyright 2026 Juan Pablo de la Cruz Gutiérrez
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import configparser
16
+ from pathlib import Path
17
+
18
+ from IPython.core.error import UsageError
19
+
20
+ CONFIG_FILE = ".ipyllmrc"
21
+ CONFIG_SECTION = "ipyllm"
22
+ CONFIG_KEYS = ("model", "require_approval", "debug", "save_session", "chain_limit")
23
+
24
+
25
+ def find_config_file() -> Path | None:
26
+ """Return the config file to use: current directory first, then ``$HOME``."""
27
+ for candidate in (Path.cwd() / CONFIG_FILE, Path.home() / CONFIG_FILE):
28
+ if candidate.is_file():
29
+ return candidate
30
+ return None
31
+
32
+
33
+ def read_config_values(path: Path) -> dict[str, str]:
34
+ """Parse a config file with a section, a single section, or no header."""
35
+ try:
36
+ content = path.read_text(encoding="utf-8")
37
+ except OSError as exc:
38
+ raise UsageError(f"Cannot read config file {path}: {exc}") from exc
39
+ parser = configparser.ConfigParser(interpolation=None)
40
+ try:
41
+ parser.read_string(content)
42
+ except configparser.MissingSectionHeaderError:
43
+ parser.read_string(f"[{CONFIG_SECTION}]\n{content}")
44
+ except configparser.Error as exc:
45
+ raise UsageError(f"Cannot parse config file {path}: {exc}") from exc
46
+ if CONFIG_SECTION in parser:
47
+ selected = parser[CONFIG_SECTION]
48
+ elif parser.sections():
49
+ selected = parser[parser.sections()[0]]
50
+ else:
51
+ selected = parser.defaults()
52
+ return {
53
+ key: value for key, value in selected.items() if key not in ("DEFAULT", "__name__")
54
+ }
55
+
56
+
57
+ def parse_bool(value: str, key: str) -> bool:
58
+ normalized = value.strip().lower()
59
+ if normalized in {"1", "true", "on", "yes", "y"}:
60
+ return True
61
+ if normalized in {"0", "false", "off", "no", "n"}:
62
+ return False
63
+ raise ValueError(f"expected on or off, got {value!r}")
64
+
65
+
66
+ def format_config_file(values: dict[str, str]) -> str:
67
+ """Render config values as an ``.ipyllmrc`` document."""
68
+ lines = [f"[{CONFIG_SECTION}]"]
69
+ for key in CONFIG_KEYS:
70
+ if key in values:
71
+ lines.append(f"{key} = {values[key]}")
72
+ return "\n".join(lines) + "\n"