Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/getting-started/prerequisites.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
1. **Benchling App with Admin Privileges**: Liminal builds on top of Benchling's LIMS system. You will need to have a Benchling App with admin privileges to your Benchling tenant(s). Liminal needs admin privileges in order to manipulate Benchling schemas through the Benchling API and SDK. Ensure the app is configured as an admin on all your entity schemas and dropdowns. [Follow instructions here](https://docs.benchling.com/docs/getting-started-benchling-apps#installing-your-app) to install a Benchling App with admin privileges.
1. **Benchling App with Admin Privileges**: Liminal builds on top of Benchling's LIMS system. You will need to have a Benchling App with admin privileges to your Benchling tenant(s). Liminal needs admin privileges in order to manipulate Benchling schemas through the Benchling API and SDK. Ensure the app is configured as an admin on your registry and on all entity schemas and dropdowns within the registry. [Follow instructions here](https://docs.benchling.com/docs/getting-started-benchling-apps#installing-your-app) to install a Benchling App with admin privileges.

2. **Python**: Liminal supports Python versions 3.10 through 3.13 for package users.

Expand Down
7 changes: 7 additions & 0 deletions docs/reference/benchling-connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ connection = BenchlingConnection(

Whether your Benchling tenant has access to fieldsets.

- **registry_id: Optional[str] = None**

The API id of the registry to scope this connection to, for example `src_abc123`. Only needed when the tenant has more than one registry. When set, Liminal reads and writes only the entity schemas and dropdowns in that registry, and the results schemas owned by the registry's organization. For a tenant with multiple registries and no `registry_id` set, Liminal raises an error in `BenchlingService`.

!!! tip
For a tenant with several registries, give each registry its own Liminal directory (`env.py` and `versions/`), with one `BenchlingConnection` per tenant that defines that registry's `registry_id`. Each registry then keeps its own revision history in its own `liminal_remote` schema.

- **config_flags: TenantConfigFlags = TenantConfigFlags()**

Set of config flags that are configured on the tenant level, that defines specific features of the tenant. These can be updated on Benchling's end by contacting their support team.
Expand Down
14 changes: 4 additions & 10 deletions liminal/base/base_dropdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Any

from liminal.connection.benchling_service import BenchlingService
from liminal.dropdowns.utils import get_benchling_dropdown_summaries
from liminal.dropdowns.utils import get_benchling_dropdown_id_by_name


class BaseDropdown(ABC):
Expand Down Expand Up @@ -52,15 +52,9 @@ def get_id(cls, benchling_service: BenchlingService) -> str:
str
The id of the dropdown.
"""
all_dropdowns = get_benchling_dropdown_summaries(benchling_service)
dropdowns_found_by_name = [
d for d in all_dropdowns if d.name == cls.__benchling_name__
]
if len(dropdowns_found_by_name) == 0:
raise ValueError(f"No dropdowns found with name '{cls.__benchling_name__}'")
else:
dropdown = dropdowns_found_by_name[0]
return dropdown.id
return get_benchling_dropdown_id_by_name(
benchling_service, cls.__benchling_name__
)

@classmethod
def validate(cls, *values: str | None) -> None:
Expand Down
4 changes: 4 additions & 0 deletions liminal/connection/benchling_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class BenchlingConnection(BaseModel):
DEPRECATED: Liminal no longer uses Playwright to log into Benchling's internal API, so this is ignored. It will be removed in v6.
fieldsets: bool = False
Whether your Benchling tenant has access to fieldsets.
registry_id: str | None = None
The API id of the registry to scope this connection to, e.g. src_abc123. Only needed when the tenant has more than one registry.
When set, Liminal reads and writes only the schemas and dropdowns in this registry, and the result schemas owned by its organization.
config_flags: TenantConfigFlags = TenantConfigFlags()
Set of config flags that are configured on the tenant level. These can be updated on Benchling's end by contacting their support team.
"""
Expand All @@ -59,6 +62,7 @@ class BenchlingConnection(BaseModel):
internal_api_admin_password: str | None = None
playwright_data_dir: str | None = None
fieldsets: bool = False
registry_id: str | None = None
config_flags: TenantConfigFlags = TenantConfigFlags()

@model_validator(mode="before")
Expand Down
90 changes: 82 additions & 8 deletions liminal/connection/benchling_service.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import logging
import warnings
from functools import cached_property
from typing import TYPE_CHECKING, Any

from benchling_sdk.auth.client_credentials_oauth2 import ClientCredentialsOAuth2
from benchling_sdk.benchling import Benchling, BenchlingApiClientDecorator
from benchling_sdk.helpers.retry_helpers import RetryStrategy
from pydantic import BaseModel
from sqlalchemy import create_engine
from sqlalchemy.engine import Engine
from sqlalchemy.orm import Session, configure_mappers
Expand All @@ -28,6 +30,18 @@
REMOTE_REVISION_ID_FIELD_WH_NAME = "revision_id"


class RegistryRef(BaseModel):
"""The registry a connection writes to, and the organization that owns it."""

name: str
id: str
owner_id: str


class AmbiguousRegistryError(ValueError):
"""A lookup matched objects that may sit in different registries, and the connection doesn't name one."""


class BenchlingService(Benchling):
"""
Class that creates a connection object that can be used to connect to Benchling's API and/or Postgres warehouse.
Expand Down Expand Up @@ -95,6 +109,17 @@ def __init__(
raise ValueError(
"use_db is True but warehouse_connection_string not provided in BenchlingConnection."
)
if use_api and self.has_ambiguous_registries():
raise ValueError(
f"Tenant {self.benchling_tenant} has {len(self._registries)} registries. "
"Liminal does not allow ambiguous registries. Set registry_id on the BenchlingConnection to choose one. "
f"Registries found: {self._registries}"
)

@property
def tenant_registry_name(self) -> str:
"""The name of the tenant and registry this service is connected to. Raises if it can't be determined."""
return f"[Tenant {self.benchling_tenant}: Registry {self.registry_name}]"

@property
def session(self) -> Session:
Expand All @@ -103,17 +128,66 @@ def session(self) -> Session:

@property
def registry_id(self) -> str:
# This assumes there is only one registry (which has always been the case at DynoTx)
registries = self.registry.registries()
assert len(registries) == 1
return registries[0].id
"""The id of the registry this connection writes to. Raises if it can't be determined."""
return self._registry_ref.id

@property
def registry_name(self) -> str:
"""The name of the registry this connection writes to. Raises if it can't be determined."""
return self._registry_ref.name

@property
def organization_id(self) -> str:
# This assumes there is only one registry (which has always been the case at DynoTx)
registries = self.registry.registries()
assert len(registries) == 1
return registries[0].owner.id
"""The id of the organization that owns this connection's registry. Raises if it can't be determined."""
return self._registry_ref.owner_id

@cached_property
def _registries(self) -> list[RegistryRef]:
"""Every registry on the tenant."""
from liminal.utils import list_all_items_v3

return [
RegistryRef(id=r["id"], name=r["name"], owner_id=r["owner"]["id"])
for r in list_all_items_v3(
self, "/api/v3/registry/items", include_archived=None
)
]

@property
def _registry_ref(self) -> RegistryRef:
if self.connection.registry_id is not None:
registry_ref = next(
(r for r in self._registries if r.id == self.connection.registry_id),
None,
)
if registry_ref is None:
raise ValueError(
f"Tenant {self.benchling_tenant}: registry {self.connection.registry_id} not found."
)
return registry_ref
if len(self._registries) != 1:
raise ValueError(
f"Tenant {self.benchling_tenant} has {len(self._registries)} registries. "
"Set registry_id on the BenchlingConnection to choose one."
)
return self._registries[0]

def is_in_registry_scope(self, record: dict[str, Any]) -> bool:
"""Whether a v3 API record belongs to this connection's registry."""
registry_id = (record.get("registry") or {}).get("id")
if registry_id:
return registry_id == self.registry_id
return False

def is_in_organization_scope(self, record: dict[str, Any]) -> bool:
"""Whether a v3 API record belongs to this connection's organization."""
organization_id = (record.get("organization") or {}).get("id")
if organization_id:
return organization_id == self.organization_id
return False

def has_ambiguous_registries(self) -> bool:
return len(self._registries) > 1 and self.connection.registry_id is None

def __enter__(self) -> Session:
self._session = self.get_session()
Expand Down
5 changes: 4 additions & 1 deletion liminal/dropdowns/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ def list_dropdowns(
) -> list[dict[str, Any]]:
"""Fetch dropdowns from the v3 API."""
return list_all_items_v3(
benchling_service, f"{_DROPDOWN_API_PATH}/items", include_archived
benchling_service,
f"{_DROPDOWN_API_PATH}/items",
include_archived,
registry_scoped=True,
)


Expand Down
6 changes: 3 additions & 3 deletions liminal/dropdowns/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from liminal.dropdowns.utils import (
dropdown_exists_in_benchling,
get_benchling_dropdown_by_name,
get_benchling_dropdown_summary_by_name,
get_benchling_dropdown_id_by_name,
)

LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -118,15 +118,15 @@ def __init__(self, dropdown_name: str, new_dropdown_name: str) -> None:
self.new_dropdown_name = new_dropdown_name

def execute(self, benchling_service: BenchlingService) -> dict[str, Any]:
dropdown = get_benchling_dropdown_summary_by_name(
dropdown_id = get_benchling_dropdown_id_by_name(
benchling_service, self.dropdown_name
)
if dropdown_exists_in_benchling(benchling_service, self.new_dropdown_name):
raise ValueError(
f"Dropdown {self.new_dropdown_name} already exists in Benchling."
)
return update_dropdown_name(
benchling_service, dropdown.id, self.new_dropdown_name
benchling_service, dropdown_id, self.new_dropdown_name
)

def describe_operation(self) -> str:
Expand Down
47 changes: 17 additions & 30 deletions liminal/dropdowns/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Any

from benchling_sdk.models import ArchiveRecord
from benchling_sdk.models import Dropdown, DropdownOption, DropdownSummary
from benchling_sdk.models import Dropdown, DropdownOption

from liminal.connection import BenchlingService
from liminal.dropdowns.api import list_dropdowns, list_dropdowns_with_options
Expand All @@ -11,7 +11,7 @@
def get_benchling_dropdown_id_name_map(
benchling_service: BenchlingService,
) -> dict[str, str]:
return {d.id: d.name for d in get_benchling_dropdown_summaries(benchling_service)}
return {d["id"]: d["name"] for d in list_dropdowns(benchling_service)}


def get_benchling_dropdown_name_to_classname_map(
Expand All @@ -27,37 +27,26 @@ def get_benchling_dropdown_name_to_classname_map(
}


def get_benchling_dropdown_summaries(
benchling_service: BenchlingService,
) -> list[DropdownSummary]:
return [
dropdown
for sublist in benchling_service.dropdowns.list()
for dropdown in sublist
]


def get_benchling_dropdown_summary_by_name(
def get_benchling_dropdown_id_by_name(
benchling_service: BenchlingService, name: str
) -> DropdownSummary:
for dropdown in get_benchling_dropdown_summaries(benchling_service):
if dropdown.name == name:
return dropdown
raise Exception(f"Dropdown {name} not found in given list.")
) -> str:
"""Finds the id of a dropdown by name in the connection's registry. Names are unique within a registry."""
dropdown = next(
(d for d in list_dropdowns(benchling_service) if d["name"] == name), None
)
if dropdown is None:
raise ValueError(
f"Dropdown {name} not found in Benchling {benchling_service.tenant_registry_name}."
)
return dropdown["id"]


def get_benchling_dropdown_by_name(
benchling_service: BenchlingService, name: str
) -> Dropdown:
dropdown = None
for d in get_benchling_dropdown_summaries(benchling_service):
if d.name == name:
dropdown = d
if dropdown is None:
raise Exception(
f"Dropdown {name} not found in Benchling {benchling_service.benchling_tenant}."
)
return benchling_service.dropdowns.get_by_id(dropdown.id)
return benchling_service.dropdowns.get_by_id(
get_benchling_dropdown_id_by_name(benchling_service, name)
)


def _convert_dropdown_from_v3_to_v2_sdk(
Expand Down Expand Up @@ -107,9 +96,7 @@ def get_benchling_dropdowns_dict(
def dropdown_exists_in_benchling(
benchling_service: BenchlingService, name: str
) -> bool:
return any(
d.name == name for d in get_benchling_dropdown_summaries(benchling_service)
)
return any(d["name"] == name for d in list_dropdowns(benchling_service))


def get_schemas_with_dropdown(dropdown_name: str) -> list[str]:
Expand Down
4 changes: 3 additions & 1 deletion liminal/entity_schemas/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@ def list_entity_schemas(


@lru_cache
def list_entity_schemas_cached(
def list_entity_schemas_v2_cached(
benchling_service: BenchlingService,
) -> list[EntitySchema]:
return [
s
for schemas in benchling_service.schemas.list_entity_schemas()
for s in schemas
if s.registry_id == benchling_service.registry_id
]


Expand All @@ -74,6 +75,7 @@ def _list_entity_schemas_for_endpoint(
benchling_service,
f"{_entity_schema_specific_api_path(endpoint)}/items",
include_archived,
organization_scoped=True,
)


Expand Down
14 changes: 7 additions & 7 deletions liminal/entity_schemas/entity_schema_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from liminal.base.properties.base_name_template import BaseNameTemplate
from liminal.base.properties.base_schema_properties import BaseSchemaProperties
from liminal.connection.benchling_service import BenchlingService
from liminal.dropdowns.utils import get_benchling_dropdown_summary_by_name
from liminal.dropdowns.utils import get_benchling_dropdown_id_by_name
from liminal.entity_schemas.api import (
attach_entity_schema_field_definitions,
list_entity_schemas,
Expand Down Expand Up @@ -165,11 +165,11 @@ def update_from_props(
raise ValueError(
"Benchling SDK must be provided to update dropdown field."
)
dropdown_summary_id = get_benchling_dropdown_summary_by_name(
dropdown_id = get_benchling_dropdown_id_by_name(
benchling_service, update_props.dropdown_link
).id
)
self.linkDefinition = FieldLinkDefinitionModel(
id=dropdown_summary_id,
id=dropdown_id,
typename=BenchlingLinkDefinitionType.DROPDOWN,
)
self.displayPrecision = (
Expand Down Expand Up @@ -304,9 +304,9 @@ def from_benchling_props(
)
dropdown_id = None
if field_props.dropdown_link is not None:
dropdown_id = get_benchling_dropdown_summary_by_name(
dropdown_id = get_benchling_dropdown_id_by_name(
benchling_service, field_props.dropdown_link
).id
)
unit_id = None
if field_props.unit_name is not None and field_type.supports_unit():
unit_id = get_unit_id_from_name(benchling_service, field_props.unit_name)
Expand Down Expand Up @@ -554,7 +554,7 @@ def get_one(
)
if schema is None:
raise ValueError(
f"Schema {wh_schema_name} not found in Benchling {benchling_service.benchling_tenant}."
f"Schema {wh_schema_name} not found in Benchling {benchling_service.tenant_registry_name}."
)
schema_with_fields = attach_entity_schema_field_definitions(
benchling_service, schema
Expand Down
Loading