-
Notifications
You must be signed in to change notification settings - Fork 0
/
PondFile.py
96 lines (75 loc) · 2.43 KB
/
PondFile.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import os
class PondDoc:
def __init__(self):
self.__header = str()
self.__paper = str()
self.__layout = str()
self.__score = str()
self.__functions = []
@property
def header(self):
return self.__header
@header.setter
def header(self, value):
self.__header = str(value)
@property
def paper(self):
return self.__paper
@paper.setter
def paper(self, value):
self.__paper = str(value)
@property
def layout(self):
return self.__layout
@layout.setter
def layout(self, value):
self.__layout = str(value)
@property
def score(self):
return self.__score
@score.setter
def score(self, value):
self.__score = str(value)
@property
def functions(self):
return "\n".join(self.__functions)
def add_function(self, name, value):
self.__functions.append(f"{name} = {value}")
def create_file(self):
return "\n".join([self.header, self.paper, self.functions,
self.score, self.layout])
class PondRender:
def __init__(self, **config):
self._file_name = "temp_pypond.ly"
self._folder_path = os.path.join("ly_files")
self._auto_write = False
self._version = '\\version "2.22.1"'
self._format = "png"
self._resolution = 200
self.current_file = self._version + "\n"
self.set_config(**config)
@property
def __file_path(self):
return os.path.join(self._folder_path, self._file_name)
@property
def __render_options(self):
return (f"-o{self._folder_path} "
f"-f{self._format} -dbackend=eps -dresolution={self._resolution} "
f"-dno-gs-load-fonts -dinclude-eps-fonts "
f""
f"{self.__file_path}")
def set_config(self, **config):
for name, value in config.items():
attr_name = f"_{name}"
setattr(self, attr_name, value)
def update(self, new_file):
if isinstance(new_file, PondDoc):
new_file = new_file.create_file()
self.current_file = self._version + "\n" + new_file
if self._auto_write:
self.write()
def write(self):
with open(self.__file_path, 'wt') as file:
file.write(self.current_file)
def render(self):
os.system(f"lilypond {self.__render_options} {self.__file_path}")