Skip to content

trading_agent.terminal.terminal_settings

Remembers the terminal program's choices between runs: the desk, the theme, the side panel and its folded cards.

They are kept in ~/.local/state/trading-agent/terminal.json, beside the prompt history. A missing or unreadable file gives the defaults.

Typical usage example:

settings = TerminalSettings.load() settings.desk = 'scalper' settings.save()

TerminalSettings

TerminalSettings(file)

The remembered choices.

Attributes:

Name Type Description
desk str | None

The desk the chat opened on last, or None.

theme

The theme's name.

side_panel_open

Whether the right-hand panel is shown.

collapsed_cards list[str]

The side panel cards that are folded, by short name.

file

Where the choices are kept.

Creates the defaults.

Parameters:

Name Type Description Default
file Path

Where the choices are kept.

required
Source code in src/trading_agent/terminal/terminal_settings.py
def __init__(self, file: Path):
    """Creates the defaults.

    Args:
        file (Path): Where the choices are kept.
    """
    self.desk: str | None = None
    self.theme = terminal_themes.TerminalThemes.DARK
    self.side_panel_open = True
    self.collapsed_cards: list[str] = []
    self.file = file

state_directory classmethod

state_directory()

The folder for the terminal program's state.

Returns:

Type Description
Path

$XDG_STATE_HOME/trading-agent, or ~/.local/state/trading-agent.

Source code in src/trading_agent/terminal/terminal_settings.py
@classmethod
def state_directory(cls) -> Path:
    """The folder for the terminal program's state.

    Returns:
        Path: $XDG_STATE_HOME/trading-agent, or ~/.local/state/trading-agent.
    """
    base = os.environ.get('XDG_STATE_HOME')
    if base:
        return Path(base) / 'trading-agent'
    return Path.home() / '.local' / 'state' / 'trading-agent'

load classmethod

load(file=None)

Reads the remembered choices.

Parameters:

Name Type Description Default
file Path | None

The file, or None for terminal.json in the state folder.

None

Returns:

Type Description
TerminalSettings

The choices, or the defaults when the file is missing or unreadable.

Source code in src/trading_agent/terminal/terminal_settings.py
@classmethod
def load(cls, file: Path | None = None) -> Self:
    """Reads the remembered choices.

    Args:
        file (Path | None): The file, or None for terminal.json in the state folder.

    Returns:
        TerminalSettings: The choices, or the defaults when the file is missing or unreadable.
    """
    if file is None:
        file = cls.state_directory() / 'terminal.json'
    settings = cls(file)
    try:
        stored = json.loads(file.read_text(encoding='utf-8'))
    except OSError, ValueError:
        return settings
    if not isinstance(stored, dict):
        return settings
    if isinstance(stored.get('desk'), str):
        settings.desk = stored['desk']
    if stored.get('theme') in (
        terminal_themes.TerminalThemes.DARK,
        terminal_themes.TerminalThemes.LIGHT,
    ):
        settings.theme = stored['theme']
    if isinstance(stored.get('side_panel_open'), bool):
        settings.side_panel_open = stored['side_panel_open']
    collapsed = stored.get('collapsed_cards')
    if isinstance(collapsed, list):
        for card_id in collapsed:
            if isinstance(card_id, str):
                settings.collapsed_cards.append(card_id)
    return settings

save

save()

Writes the choices, ignoring a folder that cannot be written.

Source code in src/trading_agent/terminal/terminal_settings.py
def save(self) -> None:
    """Writes the choices, ignoring a folder that cannot be written."""
    try:
        self.file.parent.mkdir(parents=True, exist_ok=True)
        self.file.write_text(
            json.dumps(
                {
                    'desk': self.desk,
                    'theme': self.theme,
                    'side_panel_open': self.side_panel_open,
                    'collapsed_cards': self.collapsed_cards,
                },
                indent=2,
            ),
            encoding='utf-8',
        )
    except OSError:
        return