Skip to content

trading_agent.requests.permission_grants

The user's standing permissions: a desk may use a tool that normally needs permission, until the user revokes it.

A grant is made by "Always allow" on a permission request, and is kept in workspace/shared/permission_grants.json.

Typical usage example:

grants = PermissionGrants(layout) grants.grant('main', 'flatten_desk') grants.allows('main', 'flatten_desk')

PermissionGrant dataclass

PermissionGrant(desk, tool_name, granted_at)

One standing permission.

Attributes:

Name Type Description
desk str

The desk allowed.

tool_name str

The tool it may use without asking.

granted_at str

When the user granted it, as ISO 8601 text in UTC.

PermissionGrants

PermissionGrants(layout)

Reads, adds and revokes standing permissions.

Creates the store.

Parameters:

Name Type Description Default
layout WorkspaceLayout

The workspace whose shared folder holds the grants file.

required
Source code in src/trading_agent/requests/permission_grants.py
def __init__(self, layout: workspace_layout.WorkspaceLayout):
    """Creates the store.

    Args:
        layout (workspace_layout.WorkspaceLayout): The workspace whose shared folder holds the grants file.
    """
    self._path = layout.shared_directory / 'permission_grants.json'

all

all()

Every standing permission, oldest first.

Returns:

Type Description
list[PermissionGrant]

The grants.

Source code in src/trading_agent/requests/permission_grants.py
def all(self) -> list[PermissionGrant]:
    """Every standing permission, oldest first.

    Returns:
        list[PermissionGrant]: The grants.
    """
    if not self._path.is_file():
        return []
    grants = []
    for entry in json.loads(self._path.read_text(encoding='utf-8')):
        grants.append(PermissionGrant(**entry))
    return grants

allows

allows(desk, tool_name)

Says whether a desk may use a tool without asking.

Parameters:

Name Type Description Default
desk str

The desk.

required
tool_name str

The tool.

required

Returns:

Type Description
bool

True when the user granted it.

Source code in src/trading_agent/requests/permission_grants.py
def allows(self, desk: str, tool_name: str) -> bool:
    """Says whether a desk may use a tool without asking.

    Args:
        desk (str): The desk.
        tool_name (str): The tool.

    Returns:
        bool: True when the user granted it.
    """
    for grant in self.all():
        if grant.desk == desk and grant.tool_name == tool_name:
            return True
    return False

grant

grant(desk, tool_name)

Lets a desk use a tool without asking from now on.

Parameters:

Name Type Description Default
desk str

The desk.

required
tool_name str

The tool.

required

Returns:

Type Description
PermissionGrant

The grant, or the existing one.

Source code in src/trading_agent/requests/permission_grants.py
def grant(self, desk: str, tool_name: str) -> PermissionGrant:
    """Lets a desk use a tool without asking from now on.

    Args:
        desk (str): The desk.
        tool_name (str): The tool.

    Returns:
        PermissionGrant: The grant, or the existing one.
    """
    grants = self.all()
    for existing in grants:
        if existing.desk == desk and existing.tool_name == tool_name:
            return existing
    grant = PermissionGrant(
        desk=desk,
        tool_name=tool_name,
        granted_at=datetime.datetime.now(datetime.UTC).isoformat(),
    )
    grants.append(grant)
    self._write(grants)
    return grant

revoke

revoke(desk, tool_name)

Makes a desk ask again before using a tool.

Parameters:

Name Type Description Default
desk str

The desk.

required
tool_name str

The tool.

required

Returns:

Type Description
bool

True when a grant was removed, False when there was none.

Source code in src/trading_agent/requests/permission_grants.py
def revoke(self, desk: str, tool_name: str) -> bool:
    """Makes a desk ask again before using a tool.

    Args:
        desk (str): The desk.
        tool_name (str): The tool.

    Returns:
        bool: True when a grant was removed, False when there was none.
    """
    grants = self.all()
    kept = []
    for grant in grants:
        if grant.desk != desk or grant.tool_name != tool_name:
            kept.append(grant)
    if len(kept) == len(grants):
        return False
    self._write(kept)
    return True