/
utils.py
47 lines (36 loc) · 1.07 KB
/
utils.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
"""
cophi.dkpro.utils
~~~~~~~~~~~~~~~~~
This module implements general helper functions.
"""
import logging
import subprocess
from cophi import dkpro, text
logger = logging.getLogger(__name__)
handler = logging.StreamHandler()
formatter = logging.Formatter("%(message)s")
handler.setFormatter(formatter)
logger.addHandler(handler)
def call(args: list) -> None:
"""Call a subprocess.
Parameter:
args (list): The subprocess’ arguments.
"""
for message in _process(args):
logger.info(message)
def _process(args: list) -> None:
"""Construct a process.
Parameter:
args (list): The subprocess’ arguments.
"""
popen = subprocess.Popen(args,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
# Yield every line of stdout:
for line in iter(popen.stdout.readline, ""):
yield line.strip()
popen.stdout.close()
code = popen.wait()
if code:
raise subprocess.CalledProcessError(code, args)