-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlilypond.py
71 lines (56 loc) · 2.42 KB
/
lilypond.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
import shutil
import subprocess
from datetime import datetime
from typing import List
from telegram import Update
from telegram.ext import CallbackContext
import constants
def generate_filename(user: str):
counter = 0
while True:
filename = datetime.utcnow().strftime("%y%m%d%H%M%S") + "-" + user + \
("" if counter == 0 else f"-{str(counter)}")
if any(filename in file for file in os.listdir(constants.USER_FILES_DIR)):
counter += 1
continue
else:
return filename
def lilypond_compile(update: Update, context: CallbackContext):
text = update.inline_query.query if update.inline_query else update.effective_message.text
username = update.effective_user.username or str(update.effective_user.id)
# where do we store the file
filename = generate_filename(username)
src_file = f"{constants.USER_FILES_DIR}/{filename}.ly"
# write the file
text = ("" if "\\version" in text else f'\\version "{constants.LILY_VERSION}"'
) + f'\\include "{constants.LILYSETTINGS_PATH}"\n{text}'
with open(src_file, "w") as file:
file.write(text)
# compile
try:
# noinspection SpellCheckingInspection
output, error = lilypond_process(
["-dbackend=eps", "-dresolution=300", "--png", "--loglevel=WARN", f"--output={constants.USER_FILES_DIR}/",
src_file])
except Exception as exc:
output = ""
error = f"An error has occurred. Reporting to the dev...\n{type(exc).__name__}: {exc}"
shutil.copy(src_file, f"{constants.ERROR_FILES_DIR}/{filename}.ly")
context.dispatcher.dispatch_error(update, exc)
# prettify output
error = error.replace(f"{os.getcwd()}/", "") \
.replace(f"{constants.USER_FILES_DIR}/", "") \
.replace(f"{filename}.ly:", "") \
.replace("\n\n", "\n")
return filename, output, error
def add_padding(file_path):
import cv2
return cv2.imwrite(file_path,
cv2.copyMakeBorder(cv2.imread(file_path), 30, 30, 30, 30,
cv2.BORDER_CONSTANT, value=[255, 255, 255]))
def lilypond_process(args: List[str]):
args.insert(0, "lilypond")
process = subprocess.run(args, stderr=subprocess.PIPE, stdout=subprocess.PIPE,
errors='replace', env={**os.environ, "LANG": "en"})
return process.stdout, process.stderr