forked from morevnaproject-org/papagayo-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRhubarb.py
58 lines (48 loc) · 1.72 KB
/
Rhubarb.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
import json
import signal
import subprocess
import os
RHUBARB_PATH = './rhubarb/rhubarb'
class RhubarbTimeoutException(Exception):
def __init__(self):
super().__init__()
class Rhubarb:
def __init__(self, file_path=None):
self.file_path = file_path
self.process = None
def _signal_handler(self, signum, frame):
print('Rhubarb не отвечает более 30 секунд')
raise RhubarbTimeoutException()
def _read_log(self):
signal.signal(signal.SIGALRM, self._signal_handler)
signal.alarm(30)
log = self.process.stderr.readline().decode('utf-8').strip()
signal.alarm(0)
return log
def _read_result(self):
signal.signal(signal.SIGALRM, self._signal_handler)
signal.alarm(1)
line = ''
result = ''
while line != '}':
line = self.process.stdout.readline().decode('utf-8').strip()
result += line
signal.alarm(0)
return json.loads(result)
def run(self):
if not os.path.exists(RHUBARB_PATH):
return None
args = [RHUBARB_PATH, self.file_path, '--machineReadable', '-f', 'json']
self.process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
result_is_ready = False
log = ''
while True:
if result_is_ready:
result = self._read_result()
return result['mouthCues']
else:
log = self._read_log()
log_formatted = json.loads(log)
print(log_formatted['log']['message'])
if log_formatted['log']['message'] == 'Application terminating normally.':
result_is_ready = True