Failed Conditions
Pull Request — master (#1127)
by Mischa
01:56
created

coalib.output.printers.EspeakPrinter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %
Metric Value
dl 0
loc 28
rs 10
wmc 5
1
import subprocess
2
from pyprint.Printer import Printer
3
from pyprint.ClosableObject import ClosableObject
4
5
from coalib.misc.Constants import Constants
6
7
8
class EspeakPrinter(Printer, ClosableObject):
9
    def __init__(self):
10
        """
11
        Raises EnvironmentError if VoiceOutput is impossible.
12
        """
13
        Printer.__init__(self)
14
        ClosableObject.__init__(self)
15
        # TODO retrieve language from get_locale and select appropriate voice
16
17
        try:
18
            self.espeak = subprocess.Popen(['espeak'], stdin=subprocess.PIPE)
19
        except OSError:  # pragma: no cover
20
            print("eSpeak doesn't seem to be installed. You cannot use the "
21
                  "voice output feature without eSpeak. It can be downloaded"
22
                  " from http://espeak.sourceforge.net/ or installed via "
23
                  "your usual package repositories.")
24
            raise EnvironmentError
25
        except:  # pragma: no cover
26
            print("Failed to execute eSpeak. An unknown error occurred.",
27
                  Constants.THIS_IS_A_BUG)
28
            raise EnvironmentError
29
30
    def _close(self):
31
        self.espeak.stdin.close()
32
33
    def _print(self, output, **kwargs):
34
        self.espeak.stdin.write(output.encode())
35
        self.espeak.stdin.flush()
36