TexLiLy/utils.py
LoaD Accumulator 60278db59e
[ADD] Add basic LilyPond support.
Right now, it's using the lily!show command, which might change
(check out #4)
2023-06-14 13:01:27 +02:00

68 lines
1.6 KiB
Python

from nio import *
def set_client(c: AsyncClient) -> None:
global client
client = c
def get_client() -> AsyncClient:
return client
def set_config(c: dict) -> None:
global config
config = c
def get_config() -> dict:
return config
async def send_msg(client: Client, room: str, msg: str):
content = {"msgtype": "m.text", "body": msg}
await client.room_send(room, message_type="m.room.message", content=content)
def create_user_dir(user: str) -> str:
import hashlib
import os
# NOTE: I would have used the user directly, but the Spec allows MXIDs
# with '/' and UNIX-based OSes will **not** like that.
sha = hashlib.sha256(bytes(user, 'ascii')).hexdigest()
path = os.path.join(get_config()["data"], sha)
if not os.path.exists(path):
os.makedirs(path)
return os.path.abspath(path)
async def send_png(room: str, filename: str) -> None:
import imagesize
import os
basename = os.path.basename(filename)
w, h = imagesize.get(filename)
file = open(filename, "r+b")
size = os.path.getsize(filename)
filename = os.path.basename(filename)
resp, keys = await client.upload(file, content_type="image/png", filename=basename)
if not isinstance(resp, UploadResponse):
raise OSError("Couldn't upload file.")
content = {
"body": basename,
"info": {
"size": size,
"mimetype": "image/png",
"w": w, "h": h
},
"msgtype": "m.image",
"url": resp.content_uri,
}
file.close()
await client.room_send(room, message_type="m.room.message", content=content)