Skip to content

Commit

Permalink
feat: temporarily enable a renderer (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
manzt authored Apr 20, 2023
1 parent 4307f47 commit 3bc97e0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
8 changes: 7 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ jobs:
pip install -e .[dev]
- name: Linting with Ruff
run: ruff .
# TODO: fix me! For some reason ruff's isorting
# doesn't detect higlass as a local module, so
# there are differences with import block sorting locally
# and in CI. We should hopefully enable this rule in the future.
run: |
ruff --ignore I001 .
ruff src/ # everything seems to be working in src ...
- name: Formatting with Black
run: black --check .
Expand Down
1 change: 1 addition & 0 deletions src/higlass/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from higlass_schema import *

import higlass.tilesets
from higlass._display import renderers
from higlass.api import *
from higlass.fuse import fuse
from higlass.server import HiGlassServer, _create_tileset_helper
Expand Down
21 changes: 21 additions & 0 deletions src/higlass/_display.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import contextlib
import json
import uuid
from dataclasses import dataclass, field
Expand Down Expand Up @@ -140,6 +141,24 @@ def __call__(self, viewconf: dict, **metadata):
return {"text/html": html}


@contextlib.contextmanager
def managed_enable(registry: RendererRegistry, reset: str | None):
"""Temporarily enables a renderer.
Parameters
----------
registry : PluginRegistry
The plugin registry to potentially reset the active plugin.
reset : str | None
The previous name of the active plugin.
"""
try:
yield registry.get()
finally:
registry.active = reset


@dataclass
class RendererRegistry:
"""A registery for multiple HiGlass renderers.
Expand Down Expand Up @@ -185,7 +204,9 @@ def enable(self, name: str):
"""
if name not in self.renderers:
raise ValueError(f"Renderer '{name}' has not been registered.")
prev = self.active
self.active = name
return managed_enable(self, prev)

def get(self):
"""Get the enabled renderer."""
Expand Down
28 changes: 28 additions & 0 deletions test/test_display.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from unittest.mock import MagicMock

import pytest

from higlass._display import HTMLRenderer, RendererRegistry, renderers
Expand Down Expand Up @@ -32,6 +34,32 @@ def renderer(viewconf: dict, **metadata):
assert mimebundle["text/plain"] == "some content"


def test_temporary_renderer():
registry = RendererRegistry()
mock = MagicMock()

def renderer(viewconf: dict, **metadata):
return {
"text/plain": "some content",
}

registry.register("foo", renderer)
registry.register("mock", mock)

registry.enable("foo")

with registry.enable("mock") as render:
assert registry.active == "mock"
render({"hello": "world"})

mock.assert_called_once_with({"hello": "world"})

render = registry.get()
mimebundle = render({})
assert registry.active == "foo"
assert mimebundle["text/plain"] == "some content"


def test_html_renderer():
"""Just a smoke test to make sure our html gets a unique ID"""

Expand Down

0 comments on commit 3bc97e0

Please sign in to comment.