Skip to content

Commit 72bf4d0

Browse files
committed
feat(config): add mode and hybrid_reseed_interval settings
1 parent b840c91 commit 72bf4d0

1 file changed

Lines changed: 26 additions & 5 deletions

File tree

src/trueentropy/config.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from __future__ import annotations
2828

2929
from dataclasses import dataclass
30-
from typing import Any
30+
from typing import Any, Literal
3131

3232
# -----------------------------------------------------------------------------
3333
# Source Metadata
@@ -72,6 +72,10 @@ class TrueEntropyConfig:
7272
enable_timing: bool = True
7373
enable_system: bool = True
7474

75+
# Mode configuration
76+
mode: Literal["DIRECT", "HYBRID"] = "DIRECT"
77+
hybrid_reseed_interval: float = 60.0
78+
7579
# Network-dependent sources
7680
enable_network: bool = True
7781
enable_external: bool = True
@@ -80,12 +84,18 @@ class TrueEntropyConfig:
8084

8185
def __post_init__(self) -> None:
8286
"""Validate configuration after initialization."""
83-
# Ensure at least one source is enabled
87+
# Ensure at least one source is enabled (if in DIRECT mode or collecting)
8488
if not any(self.enabled_sources):
89+
# In HYBRID mode, we technically need sources too for reseeding,
90+
# but we could rely on os.urandom initial seed if desperate.
91+
# For now, strict validation is safer.
8592
raise ValueError(
8693
"At least one entropy source must be enabled. " "Cannot disable all harvesters."
8794
)
8895

96+
if self.hybrid_reseed_interval <= 0:
97+
raise ValueError("hybrid_reseed_interval must be positive")
98+
8999
# -------------------------------------------------------------------------
90100
# Properties
91101
# -------------------------------------------------------------------------
@@ -204,6 +214,8 @@ def get_config() -> TrueEntropyConfig:
204214

205215
def configure(
206216
*,
217+
mode: Literal["DIRECT", "HYBRID"] | None = None,
218+
hybrid_reseed_interval: float | None = None,
207219
offline_mode: bool | None = None,
208220
enable_timing: bool | None = None,
209221
enable_system: bool | None = None,
@@ -216,10 +228,12 @@ def configure(
216228
Configure TrueEntropy globally.
217229
218230
This function updates the global configuration used by all
219-
entropy collection functions. You can either set offline_mode=True
231+
entropy collection functions. You can switch modes, set offline_mode
220232
to disable all network sources, or configure individual sources.
221233
222234
Args:
235+
mode: Operation mode ("DIRECT" or "HYBRID")
236+
hybrid_reseed_interval: Seconds between re-seeds in HYBRID mode
223237
offline_mode: If True, disables all network-dependent sources.
224238
If False, enables all sources. If None, ignored.
225239
enable_timing: Enable/disable CPU timing harvester
@@ -234,15 +248,17 @@ def configure(
234248
235249
Example:
236250
>>> import trueentropy
251+
>>> # Enable hybrid mode for better performance
252+
>>> trueentropy.configure(mode="HYBRID", hybrid_reseed_interval=120)
237253
>>> # Enable offline mode
238254
>>> trueentropy.configure(offline_mode=True)
239-
>>> # Or disable specific sources
240-
>>> trueentropy.configure(enable_weather=False, enable_radioactive=False)
241255
"""
242256
global _global_config
243257

244258
# Start with current config values
245259
new_config = {
260+
"mode": _global_config.mode,
261+
"hybrid_reseed_interval": _global_config.hybrid_reseed_interval,
246262
"enable_timing": _global_config.enable_timing,
247263
"enable_system": _global_config.enable_system,
248264
"enable_network": _global_config.enable_network,
@@ -251,6 +267,11 @@ def configure(
251267
"enable_radioactive": _global_config.enable_radioactive,
252268
}
253269

270+
if mode is not None:
271+
new_config["mode"] = mode
272+
if hybrid_reseed_interval is not None:
273+
new_config["hybrid_reseed_interval"] = hybrid_reseed_interval
274+
254275
# Handle offline_mode convenience flag
255276
if offline_mode is True:
256277
# Disable all network sources

0 commit comments

Comments
 (0)