2024-07-09 11:35:50 +03:00
|
|
|
from multiprocessing.shared_memory import SharedMemory
|
2024-07-09 14:49:48 +03:00
|
|
|
from typing import Callable, NamedTuple, Optional
|
2024-07-09 11:35:50 +03:00
|
|
|
import asyncio
|
|
|
|
import socketio
|
|
|
|
import aiohttp
|
|
|
|
from PIL import (
|
|
|
|
Image,
|
|
|
|
ImageFont,
|
|
|
|
ImageDraw,
|
|
|
|
ImageFilter,
|
|
|
|
ImageSequence,
|
|
|
|
ImageChops,
|
|
|
|
)
|
|
|
|
from base64 import b64decode
|
|
|
|
import signal
|
|
|
|
import os
|
|
|
|
import time
|
2024-07-09 14:46:50 +03:00
|
|
|
import json
|
|
|
|
import datetime
|
2024-07-09 11:35:50 +03:00
|
|
|
|
|
|
|
|
2024-07-09 14:46:50 +03:00
|
|
|
class Animation(NamedTuple):
|
|
|
|
x: int
|
|
|
|
y: int
|
|
|
|
frames: list[Image.Image]
|
|
|
|
spf: float
|
2024-07-09 11:35:50 +03:00
|
|
|
|
|
|
|
|
2024-07-09 14:46:50 +03:00
|
|
|
OFFSET_STATE = 0
|
|
|
|
OFFSET_AVOID = 125000
|
|
|
|
OFFSET_CANVAS = 250000
|
|
|
|
OFFSET_MASK = 375000
|
|
|
|
|
2024-07-09 14:49:48 +03:00
|
|
|
|
2024-07-09 11:35:50 +03:00
|
|
|
class Manager:
|
2024-07-09 14:46:50 +03:00
|
|
|
def __init__(self, settings_path: str):
|
2024-07-09 11:35:50 +03:00
|
|
|
self.shmem: Optional[SharedMemory] = None
|
|
|
|
self.shmem_name = "omcb-bot"
|
2024-07-09 14:46:50 +03:00
|
|
|
|
|
|
|
self.settings_path = settings_path
|
|
|
|
|
2024-07-09 11:35:50 +03:00
|
|
|
self.base = "https://onemillioncheckboxes.com"
|
|
|
|
self.last_update = 0
|
|
|
|
|
|
|
|
self.bits_toggled_on = 0
|
|
|
|
self.bits_toggled_off = 0
|
|
|
|
self.last_printout = 0
|
|
|
|
|
2024-07-09 14:46:50 +03:00
|
|
|
self.fonts: dict[tuple[str, int], ImageFont.FreeTypeFont] = {}
|
|
|
|
self.default_font_size = 8
|
|
|
|
self.animations: list[Animation] = []
|
|
|
|
self.animation_functions: list[Callable] = []
|
|
|
|
|
|
|
|
def reload_config(self):
|
|
|
|
with open(self.settings_path, "r") as fp:
|
|
|
|
settings = json.load(fp)
|
|
|
|
|
|
|
|
assert self.shmem is not None
|
|
|
|
|
|
|
|
print("Resetting shmem...")
|
2024-07-09 16:26:19 +03:00
|
|
|
self.shmem.buf[OFFSET_AVOID:] = bytes(500000 - OFFSET_AVOID)
|
2024-07-09 14:46:50 +03:00
|
|
|
|
|
|
|
if fontconfig := settings.get("default_font"):
|
|
|
|
self.default_font_size = int(fontconfig.get("size", 8))
|
|
|
|
self.fonts["default", self.default_font_size] = self.get_font(
|
2024-07-09 14:49:48 +03:00
|
|
|
fontconfig["path"], self.default_font_size
|
2024-07-09 14:46:50 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
for avoid in settings.get("avoid", []):
|
|
|
|
if avoid["type"] == "rect":
|
|
|
|
self.add_avoid_rect(
|
|
|
|
avoid["x"], avoid["y"], avoid["w"], avoid["h"]
|
|
|
|
)
|
|
|
|
elif avoid["type"] == "range":
|
|
|
|
self.add_avoid_range(
|
|
|
|
range(avoid["start"], avoid["stop"], avoid.get("step", 1))
|
|
|
|
)
|
|
|
|
elif avoid["type"] == "image":
|
|
|
|
with Image.open(avoid["path"]).convert("LA") as im:
|
|
|
|
assert im.width == 1000 and im.height == 1000
|
2024-07-09 17:09:12 +03:00
|
|
|
pixels = im.load()
|
|
|
|
assert pixels is not None
|
2024-07-09 14:46:50 +03:00
|
|
|
for y in range(im.height):
|
|
|
|
for x in range(im.width):
|
2024-07-09 17:09:12 +03:00
|
|
|
_, a = pixels[(x, y)] # type: ignore
|
2024-07-09 14:46:50 +03:00
|
|
|
if a > 128:
|
|
|
|
self.add_avoid_index(x + y * 1000)
|
|
|
|
else:
|
|
|
|
raise ValueError(f"invalid avoid: {avoid}")
|
|
|
|
print("AVOID", avoid)
|
|
|
|
|
|
|
|
for elem in settings.get("elements", []):
|
|
|
|
if elem["type"].startswith("~"):
|
|
|
|
continue
|
|
|
|
elif elem["type"] == "text":
|
|
|
|
self.put_text(
|
|
|
|
elem["x"],
|
|
|
|
elem["y"],
|
|
|
|
elem["text"],
|
|
|
|
elem.get("font", "default"),
|
|
|
|
elem.get("size", self.default_font_size),
|
|
|
|
elem.get("negative", False),
|
|
|
|
elem.get("padding", 2),
|
|
|
|
)
|
|
|
|
elif elem["type"] in ("text_anim", "text_animation"):
|
|
|
|
frames: list[Image.Image] = []
|
|
|
|
for line in elem["lines"]:
|
|
|
|
if isinstance(line, str):
|
|
|
|
frames.append(
|
|
|
|
self.make_text_image(
|
|
|
|
line,
|
|
|
|
elem.get("font", "default"),
|
|
|
|
elem.get("size", 8),
|
|
|
|
elem.get("negative", False),
|
|
|
|
elem.get("padding", 2),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
elif isinstance(line, dict):
|
|
|
|
frames.append(
|
|
|
|
self.make_text_image(
|
|
|
|
line["text"],
|
|
|
|
line.get("font", elem.get("font", "default")),
|
2024-07-09 14:49:48 +03:00
|
|
|
line.get(
|
|
|
|
"size",
|
|
|
|
elem.get("size", self.default_font_size),
|
|
|
|
),
|
2024-07-09 14:46:50 +03:00
|
|
|
line.get(
|
|
|
|
"negative", elem.get("negative", False)
|
|
|
|
),
|
|
|
|
line.get("padding", elem.get("padding", 2)),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
raise TypeError(f"invalid line: {line}")
|
|
|
|
self.put_animation(
|
|
|
|
elem["x"], elem["y"], frames, elem.get("spf", 10)
|
|
|
|
)
|
|
|
|
elif elem["type"] in ("anim", "animation"):
|
|
|
|
with Image.open(elem["path"]) as anim:
|
2024-07-09 14:49:48 +03:00
|
|
|
self.put_animation(
|
|
|
|
elem["x"],
|
|
|
|
elem["y"],
|
|
|
|
[
|
|
|
|
frame.convert("LA")
|
|
|
|
for frame in ImageSequence.Iterator(anim)
|
|
|
|
],
|
|
|
|
elem.get("spf", 10),
|
|
|
|
)
|
2024-07-09 14:46:50 +03:00
|
|
|
elif elem["type"] == "image":
|
|
|
|
with Image.open(elem["path"]).convert("LA") as im:
|
|
|
|
self.put_image(elem["x"], elem["y"], im)
|
|
|
|
elif elem["type"] == "time":
|
|
|
|
pos_x, pos_y = elem["x"], elem["y"]
|
|
|
|
|
|
|
|
time_format = elem.get("format", "%Y%m%dT%H%M%S UTC")
|
|
|
|
time_font = elem.get("font", "default")
|
|
|
|
time_font_size = elem.get("size", self.default_font_size)
|
|
|
|
time_negative = elem.get("negative", False)
|
|
|
|
time_outline = elem.get("outline", 2)
|
|
|
|
|
|
|
|
def update_time():
|
|
|
|
now = datetime.datetime.now(datetime.timezone.utc)
|
2024-07-09 14:49:48 +03:00
|
|
|
self.put_image(
|
|
|
|
pos_x,
|
|
|
|
pos_y,
|
|
|
|
self.make_text_image(
|
|
|
|
now.strftime(time_format),
|
|
|
|
time_font,
|
|
|
|
time_font_size,
|
|
|
|
time_negative,
|
|
|
|
time_outline,
|
|
|
|
),
|
|
|
|
)
|
2024-07-09 14:46:50 +03:00
|
|
|
|
|
|
|
self.animation_functions.append(update_time)
|
|
|
|
elif elem["type"] == "shrek":
|
|
|
|
shrek_x, shrek_y = elem["x"], elem["y"]
|
|
|
|
|
2024-07-09 14:49:48 +03:00
|
|
|
shrek_font = self.get_font(
|
|
|
|
elem.get("font", "default"),
|
|
|
|
elem.get("size", self.default_font_size),
|
|
|
|
)
|
2024-07-09 14:46:50 +03:00
|
|
|
|
2024-07-09 16:26:19 +03:00
|
|
|
shrek_spf = elem.get("spf", 10)
|
|
|
|
|
2024-07-09 14:46:50 +03:00
|
|
|
with open(elem["path"], "r") as fp:
|
|
|
|
lyrics = list(map(str.strip, fp))
|
|
|
|
|
|
|
|
def update_shrek():
|
|
|
|
with Image.new("LA", (325, 10), (0, 255)) as im:
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
draw.rectangle((0, 0, 325, 10), fill=(0, 255))
|
|
|
|
now = datetime.datetime.now(datetime.timezone.utc)
|
|
|
|
line = lyrics[
|
2024-07-09 16:26:19 +03:00
|
|
|
int(now.timestamp() / shrek_spf) % len(lyrics)
|
2024-07-09 14:46:50 +03:00
|
|
|
]
|
2024-07-09 14:49:48 +03:00
|
|
|
draw.text(
|
|
|
|
(2, -1), line, font=shrek_font, fill=(255, 255)
|
|
|
|
)
|
2024-07-09 14:46:50 +03:00
|
|
|
self.put_image(shrek_x, shrek_y, im)
|
|
|
|
|
|
|
|
self.animation_functions.append(update_shrek)
|
|
|
|
else:
|
|
|
|
raise TypeError(f"invalid element: {elem}")
|
|
|
|
print("ADD", elem)
|
|
|
|
|
2024-07-09 11:35:50 +03:00
|
|
|
async def listener(self):
|
|
|
|
sio = socketio.AsyncClient()
|
|
|
|
sio.on("connect", self.on_connect)
|
|
|
|
sio.on("batched_bit_toggles", self.on_batched_bit_toggles)
|
|
|
|
sio.on("full_state", self.on_full_state)
|
|
|
|
|
2024-07-09 16:26:19 +03:00
|
|
|
await sio.connect(self.base.replace("http", "ws"))
|
2024-07-09 11:35:50 +03:00
|
|
|
await sio.wait()
|
|
|
|
|
|
|
|
def update_shmem(self, state: bytes):
|
2024-07-09 14:46:50 +03:00
|
|
|
assert self.shmem is not None
|
2024-07-09 14:49:48 +03:00
|
|
|
self.shmem.buf[OFFSET_STATE : OFFSET_STATE + 125000] = state
|
2024-07-09 11:35:50 +03:00
|
|
|
|
|
|
|
async def on_connect(self):
|
|
|
|
async with aiohttp.ClientSession() as http:
|
|
|
|
async with http.get(f"{self.base}/api/initial-state") as req:
|
|
|
|
data = await req.json()
|
|
|
|
buffer = b64decode(data["full_state"].encode() + b"=")
|
|
|
|
self.update_shmem(buffer)
|
|
|
|
self.last_update = data["timestamp"]
|
|
|
|
|
|
|
|
async def on_full_state(self, data):
|
2024-07-09 14:46:50 +03:00
|
|
|
assert self.shmem is not None
|
2024-07-09 11:35:50 +03:00
|
|
|
buffer = b64decode(data["full_state"].encode() + b"=")
|
|
|
|
self.update_shmem(buffer)
|
|
|
|
self.last_update = data["timestamp"]
|
|
|
|
|
|
|
|
async def on_batched_bit_toggles(self, data):
|
2024-07-09 14:46:50 +03:00
|
|
|
assert self.shmem is not None
|
2024-07-09 11:35:50 +03:00
|
|
|
bits_on, bits_off, timestamp = data
|
|
|
|
if timestamp < self.last_update:
|
|
|
|
print("old update, ignoring")
|
|
|
|
|
|
|
|
self.last_update = timestamp
|
|
|
|
|
|
|
|
self.bits_toggled_on += len(bits_on)
|
|
|
|
self.bits_toggled_off = len(bits_off)
|
|
|
|
|
2024-07-09 14:46:50 +03:00
|
|
|
for ndx in bits_on:
|
|
|
|
byte, bit = divmod(ndx, 8)
|
2024-07-09 14:49:48 +03:00
|
|
|
self.shmem.buf[OFFSET_STATE + byte] |= 0x80 >> bit
|
2024-07-09 14:46:50 +03:00
|
|
|
for ndx in bits_off:
|
|
|
|
byte, bit = divmod(ndx, 8)
|
|
|
|
self.shmem.buf[OFFSET_STATE + byte] &= 0xFF ^ (0x80 >> bit)
|
2024-07-09 11:35:50 +03:00
|
|
|
|
|
|
|
since_last_printout = time.time() - self.last_printout
|
|
|
|
if since_last_printout >= 5:
|
|
|
|
self.last_printout = time.time()
|
|
|
|
print()
|
2024-07-09 14:46:50 +03:00
|
|
|
print(
|
|
|
|
f"Toggled on: {self.bits_toggled_on / since_last_printout}/s"
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
f"Toggled off: {self.bits_toggled_off / since_last_printout}/s"
|
|
|
|
)
|
2024-07-09 11:35:50 +03:00
|
|
|
self.bits_toggled_on = self.bits_toggled_off = 0
|
|
|
|
|
|
|
|
def on_sigusr1(self, signum, frame):
|
2024-07-09 14:46:50 +03:00
|
|
|
assert self.shmem is not None
|
2024-07-09 11:35:50 +03:00
|
|
|
print("Caught SIGUSR1, dumping state")
|
|
|
|
buf = bytes(self.shmem.buf[:])
|
|
|
|
with Image.new("RGB", (1000, 1000), 0) as im:
|
|
|
|
for i in range(1000000):
|
|
|
|
y, x = divmod(i, 1000)
|
2024-07-09 14:46:50 +03:00
|
|
|
byte, bit = divmod(i, 8)
|
|
|
|
im.putpixel(
|
|
|
|
(x, y),
|
|
|
|
(
|
|
|
|
255 if (buf[OFFSET_MASK + byte] << bit) & 0x80 else 0,
|
2024-07-09 14:49:48 +03:00
|
|
|
(
|
|
|
|
255
|
|
|
|
if (buf[OFFSET_CANVAS + byte] << bit) & 0x80
|
|
|
|
else 0
|
|
|
|
),
|
2024-07-09 14:46:50 +03:00
|
|
|
255 if (buf[OFFSET_STATE + byte] << bit) & 0x80 else 0,
|
|
|
|
),
|
|
|
|
)
|
2024-07-09 11:35:50 +03:00
|
|
|
im.save("state.png")
|
2024-07-09 14:46:50 +03:00
|
|
|
with Image.new("L", (1000, 1000), 0) as im:
|
|
|
|
for i in range(1000000):
|
|
|
|
y, x = divmod(i, 1000)
|
|
|
|
byte, bit = divmod(i, 8)
|
|
|
|
im.putpixel(
|
|
|
|
(x, y),
|
|
|
|
255 if (buf[OFFSET_AVOID + byte] << bit) & 0x80 else 0,
|
|
|
|
)
|
|
|
|
im.save("avoid.png")
|
|
|
|
print("Dump done")
|
|
|
|
|
|
|
|
def on_sigusr2(self, signum, frame):
|
|
|
|
print("Reloading config")
|
|
|
|
self.reload_config()
|
2024-07-09 11:35:50 +03:00
|
|
|
|
|
|
|
async def animator(self):
|
|
|
|
while True:
|
2024-07-09 14:46:50 +03:00
|
|
|
for animation in self.animations:
|
|
|
|
frame = int(time.time() / animation.spf) % len(
|
|
|
|
animation.frames
|
|
|
|
)
|
|
|
|
self.put_image(
|
|
|
|
animation.x, animation.y, animation.frames[frame]
|
|
|
|
)
|
|
|
|
for func in self.animation_functions:
|
|
|
|
func()
|
2024-07-09 11:35:50 +03:00
|
|
|
await asyncio.sleep(0.1)
|
|
|
|
|
|
|
|
def add_avoid_range(self, rng: range):
|
2024-07-09 14:46:50 +03:00
|
|
|
assert self.shmem is not None
|
2024-07-09 11:35:50 +03:00
|
|
|
for ndx in rng:
|
2024-07-09 14:46:50 +03:00
|
|
|
byte, bit = divmod(ndx, 8)
|
2024-07-09 14:49:48 +03:00
|
|
|
self.shmem.buf[OFFSET_AVOID + byte] |= 0x80 >> bit
|
2024-07-09 11:35:50 +03:00
|
|
|
|
|
|
|
def add_avoid_rect(self, sx: int, sy: int, w: int, h: int):
|
|
|
|
for y in range(sy, sy + h):
|
|
|
|
ox = y * 1000
|
|
|
|
self.add_avoid_range(range(sx + ox, sx + w + ox))
|
|
|
|
|
|
|
|
def add_avoid_index(self, index: int):
|
2024-07-09 14:46:50 +03:00
|
|
|
assert self.shmem is not None
|
2024-07-09 11:35:50 +03:00
|
|
|
assert 0 <= index < 1000000
|
2024-07-09 14:46:50 +03:00
|
|
|
byte, bit = divmod(index, 8)
|
2024-07-09 14:49:48 +03:00
|
|
|
self.shmem.buf[OFFSET_AVOID + byte] |= 0x80 >> bit
|
2024-07-09 11:35:50 +03:00
|
|
|
|
|
|
|
def add_avoid_image(self, im: Image.Image):
|
|
|
|
assert im.width == 1000
|
|
|
|
assert im.height == 1000
|
2024-07-09 17:09:12 +03:00
|
|
|
pixels = im.load()
|
|
|
|
assert pixels is not None
|
2024-07-09 11:35:50 +03:00
|
|
|
for i in range(1000000):
|
|
|
|
y, x = divmod(i, 1000)
|
2024-07-09 17:09:12 +03:00
|
|
|
la = pixels[(x, y)]
|
2024-07-09 11:35:50 +03:00
|
|
|
assert isinstance(la, (tuple, list)) and len(la) == 2
|
2024-07-09 14:46:50 +03:00
|
|
|
if la[1] > 128:
|
2024-07-09 11:35:50 +03:00
|
|
|
self.add_avoid_index(i)
|
|
|
|
|
2024-07-09 14:46:50 +03:00
|
|
|
def set_index(self, index: int, value: bool):
|
|
|
|
assert 0 <= index <= 1000000
|
|
|
|
assert self.shmem is not None
|
|
|
|
byte, bit = divmod(index, 8)
|
2024-07-09 14:49:48 +03:00
|
|
|
self.shmem.buf[OFFSET_MASK + byte] |= 0x80 >> bit
|
2024-07-09 14:46:50 +03:00
|
|
|
if value:
|
2024-07-09 14:49:48 +03:00
|
|
|
self.shmem.buf[OFFSET_CANVAS + byte] |= 0x80 >> bit
|
2024-07-09 14:46:50 +03:00
|
|
|
else:
|
|
|
|
self.shmem.buf[OFFSET_CANVAS + byte] &= 0xFF ^ (0x80 >> bit)
|
|
|
|
|
|
|
|
def clear_index(self, index: int):
|
|
|
|
assert 0 <= index <= 1000000
|
|
|
|
assert self.shmem is not None
|
|
|
|
byte, bit = divmod(index, 8)
|
|
|
|
self.shmem.buf[OFFSET_MASK + byte] &= 0xFF ^ (0x80 >> bit)
|
|
|
|
self.shmem.buf[OFFSET_CANVAS + byte] &= 0xFF ^ (0x80 >> bit)
|
|
|
|
|
|
|
|
def put_image(self, ox: int, oy: int, im: Image.Image):
|
2024-07-09 17:09:12 +03:00
|
|
|
pixels = im.load()
|
|
|
|
assert pixels is not None
|
2024-07-09 14:46:50 +03:00
|
|
|
for y in range(im.height):
|
|
|
|
for x in range(im.width):
|
2024-07-09 17:09:12 +03:00
|
|
|
la: tuple[int, int] = pixels[(x, y)] # type: ignore
|
2024-07-09 14:46:50 +03:00
|
|
|
if la[1]:
|
|
|
|
self.set_index(x + ox + (y + oy) * 1000, la[0] > 0)
|
|
|
|
|
|
|
|
def put_text(
|
|
|
|
self,
|
|
|
|
x: int,
|
|
|
|
y: int,
|
|
|
|
text: str,
|
|
|
|
font_name: str = "default",
|
|
|
|
font_size: int = 8,
|
|
|
|
negative: bool = False,
|
|
|
|
outline: int = 2,
|
|
|
|
):
|
|
|
|
self.put_image(
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
self.make_text_image(
|
|
|
|
text, font_name, font_size, negative, outline
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
def make_text_image(
|
|
|
|
self,
|
|
|
|
text: str,
|
|
|
|
font_name: str = "default",
|
|
|
|
font_size: int = 8,
|
|
|
|
negative: bool = False,
|
|
|
|
outline: int = 2,
|
|
|
|
):
|
|
|
|
font = self.get_font(font_name, font_size)
|
|
|
|
left, top, right, bottom = font.getbbox(text, anchor="lt")
|
|
|
|
with Image.new(
|
|
|
|
"RGBA",
|
|
|
|
(int(right - left) + outline * 2, int(bottom - top) + outline * 2),
|
|
|
|
0,
|
|
|
|
) as im:
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
draw.rectangle((0, 0, im.width, im.height), (0, 0, 0, 0))
|
|
|
|
draw.text(
|
|
|
|
(left + outline, top + outline),
|
|
|
|
text,
|
|
|
|
font=font,
|
|
|
|
fill=(255, 255, 255, 255),
|
|
|
|
anchor="lt",
|
|
|
|
)
|
|
|
|
|
|
|
|
alpha = im.convert("L").filter(
|
|
|
|
ImageFilter.MaxFilter(outline * 2 + 1)
|
|
|
|
)
|
|
|
|
im.putalpha(alpha)
|
|
|
|
if negative:
|
|
|
|
im = ImageChops.invert(im)
|
|
|
|
im.putalpha(alpha) # ty PIL
|
|
|
|
return im.convert("LA")
|
|
|
|
|
|
|
|
def put_animation(
|
|
|
|
self, x: int, y: int, frames: list[Image.Image], spf: float = 10
|
|
|
|
):
|
|
|
|
self.animations.append(Animation(x, y, frames, spf))
|
|
|
|
|
|
|
|
def get_font(
|
|
|
|
self, font_name: str, font_size: int
|
|
|
|
) -> ImageFont.FreeTypeFont:
|
|
|
|
if font := self.fonts.get((font_name, font_size)):
|
|
|
|
return font
|
|
|
|
print("FONT", font_name, font_size)
|
|
|
|
font = ImageFont.truetype(font_name, font_size)
|
|
|
|
self.fonts[font_name, font_size] = font
|
|
|
|
return font
|
|
|
|
|
2024-07-09 11:35:50 +03:00
|
|
|
async def __aenter__(self):
|
2024-07-09 14:46:50 +03:00
|
|
|
print("Acquiring shared memory")
|
|
|
|
self.shmem = SharedMemory(self.shmem_name, True, 500000)
|
|
|
|
print("Loading config...")
|
|
|
|
try:
|
|
|
|
self.reload_config()
|
|
|
|
except Exception:
|
|
|
|
self.shmem.close()
|
|
|
|
self.shmem.unlink()
|
|
|
|
raise
|
2024-07-09 11:35:50 +03:00
|
|
|
return self
|
|
|
|
|
|
|
|
async def __aexit__(self, a, b, c):
|
|
|
|
if self.shmem:
|
|
|
|
print("cleaning up shmem")
|
|
|
|
self.shmem.close()
|
|
|
|
self.shmem.unlink()
|
|
|
|
|
|
|
|
|
2024-07-09 14:47:59 +03:00
|
|
|
async def main(cfg_path: str = "./settings.json", *_):
|
2024-07-09 11:35:50 +03:00
|
|
|
print(f"PID: {os.getpid()}")
|
2024-07-09 14:47:59 +03:00
|
|
|
async with Manager(cfg_path) as mgr:
|
2024-07-09 11:35:50 +03:00
|
|
|
signal.signal(signal.SIGUSR1, mgr.on_sigusr1)
|
2024-07-09 14:46:50 +03:00
|
|
|
signal.signal(signal.SIGUSR2, mgr.on_sigusr2)
|
|
|
|
|
|
|
|
print("Listening...")
|
2024-07-09 14:49:48 +03:00
|
|
|
await asyncio.gather(mgr.listener(), mgr.animator())
|
2024-07-09 11:35:50 +03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-07-09 14:47:59 +03:00
|
|
|
from sys import argv
|
2024-07-09 14:49:48 +03:00
|
|
|
|
2024-07-09 14:47:59 +03:00
|
|
|
asyncio.run(main(*argv[1:]))
|