commit d4fcf2ad6ce57be3b0cf3a892cd1ec2e92963c2e Author: Lokendra Singh Date: Wed Jun 26 02:43:05 2019 +0530 Wrapper for lrx-proc -m (#46) * Added: Wrapper for lrx-proc -m * Implemented changes suggested build-swig-wrapper: clone lttoolbox for building wrapper utils.py: Better name for flags 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..4d32922 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 @@ -27,32 +29,48 @@ def to_alpha3_code(code: str) -> str: def execute_pipeline(inp: str, commands: List[List[str]]) -> str: """ - Args: - inp (str) - commands (List[List[str]]) + Executes the given list of commands and returns the final output Returns: str """ end = inp.encode() for command in commands: + # On Windows platform the file can't be opened once again + # The file is first opened in python for writing and then + # opened again with swig wrapper + # NamedTemporaryFile gets deleted (if delete=True) upon closing, + # so manually delete the file afterwards + input_file = tempfile.NamedTemporaryFile(delete=False) + output_file = tempfile.NamedTemporaryFile(delete=False) + arg = command[1][1] if len(command) == 3 else '' + path = command[-1] + used_wrapper = 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() + lttoolbox.LtLocale.tryToSetLocale() + 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]: + lextools.LtLocale.tryToSetLocale() + 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) + used_wrapper = False + if used_wrapper: + 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..8564f98 100755 --- a/build-swig-wrapper.sh +++ b/build-swig-wrapper.sh @@ -2,8 +2,14 @@ set -e sudo apt-get install -y swig build-essential python3-setuptools -git clone -b swig_wrapper https://github.com/Vaydheesh/lttoolbox.git +git clone https://github.com/apertium/lttoolbox.git 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.git +cd apertium-lex-tools +./autogen.sh --enable-python-bindings && make +cd python +python3 setup.py install