aboutsummaryrefslogtreecommitdiff
path: root/src_py
diff options
context:
space:
mode:
Diffstat (limited to 'src_py')
-rw-r--r--src_py/opcut/common.py11
-rw-r--r--src_py/opcut/server.py33
2 files changed, 23 insertions, 21 deletions
diff --git a/src_py/opcut/common.py b/src_py/opcut/common.py
index 5f6f26b..0bda8c5 100644
--- a/src_py/opcut/common.py
+++ b/src_py/opcut/common.py
@@ -1,5 +1,5 @@
-from pathlib import Path
import enum
+import importlib.resources
import typing
from hat import json
@@ -7,11 +7,10 @@ from hat import json
mm: float = 72 / 25.4
-package_path: Path = Path(__file__).parent
-
-json_schema_repo: json.SchemaRepository = json.SchemaRepository(
- json.json_schema_repo,
- json.SchemaRepository.from_json(package_path / 'json_schema_repo.json'))
+with importlib.resources.path(__package__, 'json_schema_repo.json') as _path:
+ json_schema_repo: json.SchemaRepository = json.SchemaRepository(
+ json.json_schema_repo,
+ json.SchemaRepository.from_json(_path))
class Panel(typing.NamedTuple):
diff --git a/src_py/opcut/server.py b/src_py/opcut/server.py
index 939222c..379a571 100644
--- a/src_py/opcut/server.py
+++ b/src_py/opcut/server.py
@@ -1,5 +1,6 @@
-from pathlib import Path
import asyncio
+import contextlib
+import importlib.resources
import subprocess
import sys
@@ -10,27 +11,29 @@ import aiohttp.web
from opcut import common
-static_dir: Path = common.package_path / 'ui'
-
-
async def create(host: str,
port: int
) -> 'Server':
server = Server()
server._async_group = aio.Group()
- app = aiohttp.web.Application()
- app.add_routes([
- aiohttp.web.get('/', server._root_handler),
- aiohttp.web.post('/calculate', server._calculate_handler),
- aiohttp.web.post('/generate', server._generate_handler),
- aiohttp.web.static('/', static_dir)])
-
- runner = aiohttp.web.AppRunner(app)
- await runner.setup()
- server.async_group.spawn(aio.call_on_cancel, runner.cleanup)
-
try:
+ exit_stack = contextlib.ExitStack()
+ static_dir = exit_stack.enter_context(
+ importlib.resources.path(__package__, 'ui'))
+ server.async_group.spawn(aio.call_on_cancel, exit_stack.close)
+
+ app = aiohttp.web.Application()
+ app.add_routes([
+ aiohttp.web.get('/', server._root_handler),
+ aiohttp.web.post('/calculate', server._calculate_handler),
+ aiohttp.web.post('/generate', server._generate_handler),
+ aiohttp.web.static('/', static_dir)])
+
+ runner = aiohttp.web.AppRunner(app)
+ await runner.setup()
+ server.async_group.spawn(aio.call_on_cancel, runner.cleanup)
+
site = aiohttp.web.TCPSite(runner=runner,
host=host,
port=port,