-
Notifications
You must be signed in to change notification settings - Fork 1
Adds config files for the API #104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
57fdb6d
9b597f8
d10c60f
ff3455b
bd7bbb6
c3f8201
ec27bf8
e854456
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,35 +1,38 @@ | ||||||||||
| import logging | ||||||||||
| from dataclasses import dataclass | ||||||||||
| from dataclasses import field | ||||||||||
| import os | ||||||||||
| import tomllib | ||||||||||
| from pathlib import Path | ||||||||||
|
|
||||||||||
| from dotenv import load_dotenv | ||||||||||
| from pydantic import BaseModel | ||||||||||
| from pydantic import Field | ||||||||||
|
|
||||||||||
| from pqnstack.constants import BellState | ||||||||||
| from pqnstack.constants import QKDEncodingBasis | ||||||||||
| from pqnstack.pqn.protocols.measurement import MeasurementConfig | ||||||||||
|
|
||||||||||
| load_dotenv() | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is necessary since
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed |
||||||||||
|
|
||||||||||
|
|
||||||||||
| logger = logging.getLogger(__name__) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| @dataclass | ||||||||||
| class CHSHSettings: | ||||||||||
| class CHSHSettings(BaseModel): | ||||||||||
| # Specifies which half waveplate to use for the CHSH experiment. First value is the provider's name, second is the motor name. | ||||||||||
| hwp: tuple[str, str] = ("", "") | ||||||||||
| request_hwp: tuple[str, str] = ("", "") | ||||||||||
| measurement_config: MeasurementConfig = field(default_factory=lambda: MeasurementConfig(5)) | ||||||||||
| measurement_config: MeasurementConfig = Field(default_factory=lambda: MeasurementConfig(integration_time_s=5)) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| @dataclass | ||||||||||
| class QKDSettings: | ||||||||||
| class QKDSettings(BaseModel): | ||||||||||
| hwp: tuple[str, str] = ("", "") | ||||||||||
| request_hwp: tuple[str, str] = ("", "") | ||||||||||
| bitstring_length: int = 4 | ||||||||||
| discriminating_threshold = 10 | ||||||||||
| measurement_config: MeasurementConfig = field(default_factory=lambda: MeasurementConfig(5)) | ||||||||||
| discriminating_threshold: int = 10 | ||||||||||
| measurement_config: MeasurementConfig = Field(default_factory=lambda: MeasurementConfig(integration_time_s=5)) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| @dataclass | ||||||||||
| class Settings: | ||||||||||
| class Settings(BaseModel): | ||||||||||
| router_name: str | ||||||||||
| router_address: str | ||||||||||
| router_port: int | ||||||||||
|
|
@@ -39,11 +42,31 @@ class Settings: | |||||||||
| timetagger: tuple[str, str] | None = None # Name of the timetagger to use for the CHSH experiment. | ||||||||||
|
|
||||||||||
|
|
||||||||||
| static_typecheck_msg = "Please set the global 'settings' variable before use." | ||||||||||
| def load_settings_from_toml(config_path: str | Path) -> Settings: | ||||||||||
| """Load settings from a TOML configuration file with Pydantic validation.""" | ||||||||||
| config_path = Path(config_path) | ||||||||||
|
|
||||||||||
| with Path.open(config_path, "rb") as f: | ||||||||||
| config_data = tomllib.load(f) | ||||||||||
|
|
||||||||||
| # Pydantic will handle all validation and type conversion automatically | ||||||||||
| return Settings(**config_data) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def get_settings() -> Settings: | ||||||||||
|
Comment on lines
62
to
63
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| raise NotImplementedError(static_typecheck_msg) | ||||||||||
| """Load settings from the config file specified in API_CONFIG_PATH environment variable.""" | ||||||||||
| config_path = os.getenv("API_CONFIG_PATH") | ||||||||||
|
|
||||||||||
| if config_path is None: | ||||||||||
| logger.warning("API_CONFIG_PATH environment variable not found, using default value './config.toml'") | ||||||||||
| config_path = "./config.toml" | ||||||||||
|
|
||||||||||
| config_file = Path(config_path) | ||||||||||
| if not config_file.exists(): | ||||||||||
| msg = f"Configuration file not found: {config_file.absolute()} or 'API_CONFIG_PATH' environment variable is not set" | ||||||||||
| raise FileNotFoundError(msg) | ||||||||||
|
|
||||||||||
| return load_settings_from_toml(config_path) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| settings = get_settings() | ||||||||||
|
|
||||||||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.