-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileProcessor.py
100 lines (84 loc) · 3.67 KB
/
fileProcessor.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
97
98
99
100
import os
import sys
import subprocess
import constants
from configuration import Configuration
# TODO parameterized (default: false) delete of ly and mscx files after processing them
class FileProcessor:
#region private properties ################################################
__config: Configuration
#endregion ################################################################
#region public methods ####################################################
def processFiles(self, files: list) -> None:
if (self.__config.lilypondExecutable is not None):
self.__processLilyFiles(list(
filter(lambda lst: lst[0] == constants.keyLilipond, files)))
# end if
if (self.__config.musescoreExecutable is not None):
self.__processMuseFiles(list(
filter(lambda lst: lst[0] == constants.keyMusescore, files)))
# end processFiles
#endregion ################################################################
#region constructor #######################################################
def __init__(self, config: Configuration) -> None:
self.__config = config
# end constructor
#endregion ################################################################
#region private methods ###################################################
def __returnCodeToString(self, code: int) -> str:
result: str
if (code == 0):
result = 'OK'
else:
result = 'ERROR'
return result
# end returnCodeToString
def __processLilyFiles(self, files: list) -> None:
for f in files:
print('Processing {0}-file {1}'.format(f[0], f[1]), end=' ')
# check if it works without this f[1] = f[1].replace('\\', '/')
finished = subprocess.run(
[self.__config.lilypondExecutable, '-o', os.path.dirname(f[1]), f[1]], capture_output=True)
print('->', self.__returnCodeToString(finished.returncode))
if (finished.returncode != 0):
print(finished.stderr)
print(finished.stdout)
#end if
if (self.__config.verbose and finished.returncode == 0):
print(finished.stdout)
#end if
pdfFile = f[1].replace(".ly", ".pdf")
self.__deleteFile(pdfFile)
croppedPdfFile = f[1].replace(".ly", ".cropped.pdf")
self.__deleteFile(croppedPdfFile)
# end for
# end processLilyFiles
def __processMuseFiles(self, files: list) -> None:
for f in files:
print('Processing {0}-file {1}'.format(f[0], f[1]), end=' ')
mp3 = f[1].replace('\\', '/').replace('mscx', 'mp3')
sys.stdout.flush()
# leider funktioniert 'musescore -j' nicht wenn es aus python gestartet wird
# also machen wir eine nach dem anderen
finished = subprocess.run(
[self.__config.musescoreExecutable, '-o', mp3, f[1]])
print('->', self.__returnCodeToString(finished.returncode))
if (self.__config.verbose):
print(finished.stdout)
# end for
# end processMuseFiles
def __deleteFile(self, filename: str):
if (self.__config.verbose):
print('Deleting {0}'.format(filename))
#end if
try:
os.remove(filename)
except OSError as err:
print('Error deleting {0}'.format(filename))
print(err)
if (self.__config.verbose):
print('{0} deleted'.format(filename))
#end if
#end deleteFile
#endregion ################################################################
# end class