Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: temporarily enable a renderer #113

Merged
merged 11 commits into from
Apr 20, 2023
Merged
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
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
3 changes: 2 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

from docutils import nodes
from docutils.parsers.rst import Directive, directives
from higlass import __version__
from pkg_resources import parse_version

from higlass import __version__

# -*- coding: utf-8 -*-
#
# higlass documentation build configuration file, created by
Expand Down
1 change: 1 addition & 0 deletions src/higlass/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,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 server
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
3 changes: 2 additions & 1 deletion test/test_api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from __future__ import annotations

import higlass as hg
import pytest

import higlass as hg


@pytest.mark.parametrize(
"args,expected",
Expand Down
29 changes: 29 additions & 0 deletions test/test_display.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from unittest.mock import MagicMock

import pytest

from higlass._display import HTMLRenderer, RendererRegistry, renderers


Expand Down Expand Up @@ -31,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
1 change: 1 addition & 0 deletions test/test_scale.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest

from higlass._scale import Scale


Expand Down
3 changes: 2 additions & 1 deletion test/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from __future__ import annotations

import pytest
from higlass._utils import copy_unique, ensure_list
from pydantic import BaseModel

from higlass._utils import copy_unique, ensure_list


def test_copy_unique():
class Person(BaseModel):
Expand Down