Skip to content
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
40 changes: 40 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: CI

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.11"]

steps:
- uses: actions/checkout@v4

- name: Install uv
uses: astral-sh/setup-uv@v5
with:
enable-cache: true

- name: Set up Python ${{ matrix.python-version }}
run: uv python install ${{ matrix.python-version }}

- name: Install dependencies
run: uv sync --dev

- name: Run ruff check
run: uv run ruff check .

- name: Run ruff format check
run: uv run ruff format --check .

- name: Run mypy
run: uv run mypy

- name: Run pytest
run: uv run pytest
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.14.6
hooks:
- id: ruff
args: [--fix]
- id: ruff-format
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ To install the plugin, use pip:
pip install git+https://github.com/disguise-one/python-plugin
```

## Usage
## Publish Plugin

The `DesignerPlugin` class allows you to publish a plugin for the Disguise Designer application. The `port` parameter corresponds to an HTTP server that serves the plugin's web user interface. Below is an example of how to use it (without a server, for clarity).

Expand Down Expand Up @@ -51,7 +51,7 @@ async def main():
asyncio.run(main())
```

## Plugin options
### Publish options

If you would prefer not to use the `d3plugin.json` file, construct the `DesignerPlugin` object directly. The plugin's name and port number are required parameters. Optionally, the plugin can specify `hostname`, which can be used to direct Designer to a specific hostname when opening the plugin's web UI, and other metadata parameters are available, also.

Expand Down
82 changes: 80 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,99 @@ name = "disguise-designer-plugin"
version = "1.1.0"
description = "A plugin for the Disguise Designer application."
authors = [
{ name = "Tom Whittock", email = "tom.whittock@disguise.one" }
{ name = "Tom Whittock", email = "tom.whittock@disguise.one" },
{ name = "Taegyun Ha", email = "taegyun.ha@disguise.one" }
]
dependencies = [
"zeroconf>=0.39.0"
"zeroconf>=0.39.0",
]
requires-python = ">=3.11"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent"
]
readme = "README.md"

[tool.setuptools.packages.find]
where = ["src"]

[project.urls]
Homepage = "https://github.com/disguise-one/python-plugin"
Issues = "https://github.com/disguise-one/python-plugin/issues"

# Package development
[dependency-groups]
dev = [
"mypy>=1.18.2",
"pre-commit>=4.4.0",
"pytest>=9.0.1",
"ruff>=0.14.5",
]

[tool.ruff]
line-length = 100
target-version = "py311"
exclude = [
"test",
".venv",
".devcontainer",
]

[tool.ruff.lint]
select = [
"E", # pycodestyle errors
"W", # pycodestyle warnings
"F", # pyflakes
"I", # isort
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"UP", # pyupgrade
]
ignore = [
"E501", # line too long (handled by formatter)
]

[tool.ruff.format]
quote-style = "double"
indent-style = "space"

[tool.mypy]
python_version = "3.11"
mypy_path = "src"
files = ["src"]
exclude = [
".venv/",
".devcontainer/",
]
warn_return_any = true
warn_unused_configs = true
disallow_untyped_defs = true
disallow_incomplete_defs = true
check_untyped_defs = true
disallow_untyped_calls = true
disallow_untyped_decorators = false
warn_redundant_casts = true
warn_unused_ignores = true
warn_no_return = true
warn_unreachable = true
strict_equality = true
strict_optional = true
no_implicit_optional = true
show_error_codes = true
show_column_numbers = true

[[tool.mypy.overrides]]
module = "zeroconf.*"
ignore_missing_imports = true

[tool.pytest.ini_options]
testpaths = ["test"]
python_files = ["test_*.py"]
python_classes = ["Test*"]
python_functions = ["test_*"]
addopts = [
"-v",
"--strict-markers",
"--strict-config",
]
2 changes: 2 additions & 0 deletions src/designer_plugin/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from .designer_plugin import DesignerPlugin

__all__ = ["DesignerPlugin"]
83 changes: 46 additions & 37 deletions src/designer_plugin/designer_plugin.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import asyncio
import socket
from json import load as json_load

from zeroconf import ServiceInfo, Zeroconf
from zeroconf.asyncio import AsyncZeroconf
import asyncio, socket
from typing import Dict, Optional
from json import load as json_load


class DesignerPlugin:
"""When used as a context manager (using the `with` statement), publish a plugin using DNS-SD for the Disguise Designer application"""

def __init__(self,
name: str,
port: int,
hostname: Optional[str] = None,
url: Optional[str] = None,
requires_session: bool = False,
is_disguise: bool = False):
def __init__(
self,
name: str,
port: int,
hostname: str | None = None,
url: str | None = None,
requires_session: bool = False,
is_disguise: bool = False,
):
self.name = name
self.port = port
self.hostname = hostname or socket.gethostname()
Expand All @@ -22,36 +26,37 @@ def __init__(self,
self.requires_session = requires_session
self.is_disguise = is_disguise

self._zeroconf: Zeroconf | None = None
self._azeroconf: AsyncZeroconf | None = None

@staticmethod
def default_init(port: int, hostname: Optional[str] = None):
def default_init(port: int, hostname: str | None = None) -> "DesignerPlugin":
"""Initialize the plugin options with the values in d3plugin.json."""
return DesignerPlugin.from_json_file(
file_path="./d3plugin.json",
port=port,
hostname=hostname
file_path="./d3plugin.json", port=port, hostname=hostname
)

@staticmethod
def from_json_file(file_path, port: int, hostname: Optional[str] = None):
def from_json_file(file_path: str, port: int, hostname: str | None = None) -> "DesignerPlugin":
"""Convert a JSON file (expected d3plugin.json) to PluginOptions. hostname and port are required."""
with open(file_path, 'r') as f:
with open(file_path) as f:
options = json_load(f)
return DesignerPlugin(
name=options['name'],
name=options["name"],
port=port,
hostname=hostname,
url=options.get('url', None),
requires_session=options.get('requiresSession', False),
is_disguise=options.get('isDisguise', False)
url=options.get("url", None),
requires_session=options.get("requiresSession", False),
is_disguise=options.get("isDisguise", False),
)

@property
def service_info(self):
def service_info(self) -> ServiceInfo:
"""Convert the options to a dictionary suitable for DNS-SD service properties."""
properties={
b"t": b'web',
b"s": b'true' if self.requires_session else b'false',
b"d": b'true' if self.is_disguise else b'false',
properties = {
b"t": b"web",
b"s": b"true" if self.requires_session else b"false",
b"d": b"true" if self.is_disguise else b"false",
}
if self.custom_url:
properties[b"u"] = self.custom_url.encode()
Expand All @@ -61,21 +66,25 @@ def service_info(self):
name=f"{self.name}._d3plugin._tcp.local.",
port=self.port,
properties=properties,
server=f"{self.hostname}.local."
server=f"{self.hostname}.local.",
)

def __enter__(self):
self.zeroconf = Zeroconf()
self.zeroconf.register_service(self.service_info)
def __enter__(self) -> "DesignerPlugin":
self._zeroconf = Zeroconf()
self._zeroconf.register_service(self.service_info)
return self

def __exit__(self, exc_type, exc_value, traceback):
self.zeroconf.close()
def __exit__(self, exc_type, exc_value, traceback): # type: ignore
if self._zeroconf:
self._zeroconf.close()
self._zeroconf = None

async def __aenter__(self):
self.zeroconf = AsyncZeroconf()
asyncio.create_task(self.zeroconf.async_register_service(self.service_info))
async def __aenter__(self) -> "DesignerPlugin":
self._azeroconf = AsyncZeroconf()
asyncio.create_task(self._azeroconf.async_register_service(self.service_info))
return self

async def __aexit__(self, exc_type, exc_value, traceback):
await self.zeroconf.async_close()
async def __aexit__(self, exc_type, exc_value, traceback): # type: ignore
if self._azeroconf:
await self._azeroconf.async_close()
self._azeroconf = None
1 change: 1 addition & 0 deletions test/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys

sys.path.append("./src")

from designer_plugin import DesignerPlugin
9 changes: 6 additions & 3 deletions test/test_plugin.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from unittest.mock import patch, mock_open
from json import JSONDecodeError
from json import dumps as json_dumps
from unittest import TestCase
from json import dumps as json_dumps, JSONDecodeError
from unittest.mock import mock_open, patch

from . import DesignerPlugin


def _escaped(name):
"""Escape the name for Zeroconf."""
invalid_removed = ''.join(c if 0x20 <= ord(c) <= 0x7E else '\\%02X' % ord(c) for c in name)
Expand Down Expand Up @@ -76,7 +79,7 @@ def test_name_override(self):
_zeroconf().register_service.assert_called_once()
service_info = _zeroconf(
).register_service.mock_calls[0].args[0]
self.assertEqual(service_info.name, f"Different Name._d3plugin._tcp.local.")
self.assertEqual(service_info.name, "Different Name._d3plugin._tcp.local.")
self.assertEqual(service_info.type, "_d3plugin._tcp.local.")
self.assertEqual(service_info.port, 9999)
self.assertEqual(service_info.server, f"{plugin.hostname}.local.")
Expand Down
Loading