commit 9860bd29ee07daf9ccd58cdc4d8f178278f93d0b Author: vaydheesh Date: Mon Jun 24 15:57:36 2019 +0530 Added: Wrapper for lrx-proc -m diff --git a/.travis.yml b/.travis.yml index 014be6f..e237c55 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,11 +10,11 @@ python: install: - pip install pipenv - travis_retry pipenv install --dev --system - - ./build-swig-wrapper.sh before_script: - wget http://apertium.projectjj.com/apt/install-nightly.sh -O - | sudo bash - sudo apt-get -f --allow-unauthenticated install apertium-all-dev - sudo apt-get -f --allow-unauthenticated install apertium-nno-nob apertium-es-en apertium-eng + - ./build-swig-wrapper.sh script: - flake8 --verbose apertium - if [[ $TRAVIS_PYTHON_VERSION != 3.4 ]]; then diff --git a/apertium/utils.py b/apertium/utils.py index d55ee95..3cf9901 100644 --- a/apertium/utils.py +++ b/apertium/utils.py @@ -1,7 +1,9 @@ +import os import subprocess import tempfile from typing import List +import lextools import lttoolbox import apertium # noqa: F401 @@ -36,23 +38,35 @@ def execute_pipeline(inp: str, commands: List[List[str]]) -> str: """ end = inp.encode() for command in commands: + lttoolbox.LtLocale.tryToSetLocale() + input_file = tempfile.NamedTemporaryFile('w', delete=False) + output_file = tempfile.NamedTemporaryFile('r', delete=False) + arg = command[1][1] if len(command) == 3 else '' + path = command[-1] + wrapper_flag = True + input_file_name, output_file_name = input_file.name, output_file.name + with open(input_file_name, 'w') as input_file: + text = end.decode() + input_file.write(text) if 'lt-proc' == command[0]: - arg = command[1][1] if len(command) == 3 else '' - path = command[-1] - with tempfile.NamedTemporaryFile('w') as input_file, tempfile.NamedTemporaryFile('r') as output_file: - text = end.decode() - input_file.write(text) - input_file.flush() - lttoolbox.LtLocale.tryToSetLocale() - fst = lttoolbox.FST() - if not fst.valid(): - raise ValueError('FST Invalid') - fst.lt_proc(arg, path, input_file.name, output_file.name) - end = output_file.read().encode() + fst = lttoolbox.FST() + if not fst.valid(): + raise ValueError('FST Invalid') + fst = lttoolbox.FST() + fst.lt_proc(arg, path, input_file_name, output_file_name) + elif 'lrx-proc' == command[0]: + lrx = lextools.LRX() + lrx.lrx_proc(arg, path, input_file.name, output_file.name) else: apertium.logger.warning('Calling subprocess %s', command[0]) proc = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE) end, _ = proc.communicate(end) + wrapper_flag = False + if wrapper_flag: + with open(output_file_name) as output_file: + end = output_file.read().encode() + os.remove(input_file_name) + os.remove(output_file_name) return end.decode() diff --git a/build-swig-wrapper.sh b/build-swig-wrapper.sh index a305cc1..8dce337 100755 --- a/build-swig-wrapper.sh +++ b/build-swig-wrapper.sh @@ -7,3 +7,9 @@ cd lttoolbox ./autogen.sh --enable-python-bindings && make cd python python3 setup.py install + +git clone -b swig_wrapper https://github.com/Vaydheesh/apertium-lex-tools +cd apertium-lex-tools +./autogen.sh --enable-python-bindings && make +cd python +python3 setup.py install