ebm.cmd.helpers module

load_environment_from_dotenv() None[source]

Load environment variables from a .env file located in the current working directory.

If a .env file is found, its contents are loaded into the environment.

configure_json_log(log_directory: str | bool = False) None[source]

Configure JSON logging using the loguru logger.

This function sets up structured JSON logging to a file, with the log file path determined by the LOG_DIRECTORY environment variable or the provided log_directory argument. If LOG_DIRECTORY is set to ‘TRUE’, the default directory ‘log’ is used. If it is set to ‘FALSE’, logging is skipped.

Parameters

log_directorystr or bool, optional

The directory where the log file should be saved. If set to False, logging is disabled unless overridden by the LOG_DIRECTORY environment variable.

Environment Variables

LOG_DIRECTORYstr

Overrides the log_directory argument when set. Special values: - ‘TRUE’: uses default directory ‘log’ - ‘FALSE’: disables logging

Notes

  • The log file is named using the current timestamp in ISO format (without colons).

  • The log file is serialized in JSON format.

  • The directory is created if it does not exist.

Examples

>>> configure_json_log("logs")
>>> os.environ["LOG_DIRECTORY"] = "TRUE"
>>> configure_json_log(False)
configure_loglevel(log_format: str | None = None, level: str = 'INFO') None[source]

Configure the loguru logger with a specified log level and format.

By default, sets the log level to INFO unless either: - The ‘–debug’ flag is present in the command-line arguments (sys.argv), or - The environment variable DEBUG is set to ‘TRUE’ (case-insensitive).

If debug mode is enabled, the log level is set to DEBUG and a filter is applied to suppress DEBUG logs from the ‘ebm.model.file_handler’ logger.

Parameters

log_formatstr, optional

Custom format string for log messages. If not provided, the default format is used.

levelstr, optional

Default log level to use when debug mode is not active. Defaults to ‘INFO’.

Returns

None

open_file(file_to_open: Path | str) None[source]

Open a file or directory using the default application based on the operating system.

This function attempts to open a file or directory by delegating to platform-specific utilities: - On Windows, it uses os.startfile. - On macOS, it uses the open command. - On Linux or other Unix-like systems, it uses the xdg-open command.

Parameters

file_to_openpathlib.Path or str

The path of the file or directory to be opened.

Raises

FileNotFoundError

If the specified file or directory does not exist.

OSError

If there is an issue invoking the platform-specific command to open the file.

Notes

  • The file path can be specified as either a pathlib.Path object or a string.

  • This function logs the action using the loguru logger.

Examples

Open a file specified as a string:

>>> open_file("/path/to/file.txt")

Open a file using a pathlib.Path object:

>>> from pathlib import Path
>>> open_file(Path("/path/to/file.txt"))