Skip to content

Commit

Permalink
SWIG Python wrapper guarded by --enable-python-bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
singh-lokendra committed Jun 24, 2019
1 parent ced6ff8 commit a9f0f84
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 4 deletions.
72 changes: 72 additions & 0 deletions .gitignore
@@ -0,0 +1,72 @@
*.pyc
**/*.deps/
/.ccls-cache/
/*.pc
/aclocal.m4
/autom4te.cache
/compile
/config.guess
/config.log
/config.status
/config.sub
/configure
/depcomp
/INSTALL
/install-sh
/m4/libtool.m4
/m4/lt~obsolete.m4
/m4/ltoptions.m4
/m4/ltsugar.m4
/m4/ltversion.m4
/Makefile
/Makefile.in
/missing

# Prerequisites
*.d

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app

lrx-comp
lrx-proc
lrx_config.h
lrx_config.h.in
multitrans
stamp-h1

/python/Makefile.in
/python/Makefile
/python/lex_tools_wrap.cpp
/python/lextools.py
/python/setup.py
/python/build*
*.egg-info/
*.egg
4 changes: 4 additions & 0 deletions Makefile.am
Expand Up @@ -21,4 +21,8 @@ if HAVE_IRSTLM
bin_PROGRAMS += irstlm_ranker
endif

if HAVE_PYTHON_BINDINGS
SUBDIRS = python
endif

EXTRA_DIST = lrx_compiler.h lrx_processor.h multi_translator.h tagger_output_processor.h
16 changes: 12 additions & 4 deletions configure.ac
Expand Up @@ -85,13 +85,21 @@ AX_CHECK_COMPILE_FLAG([-std=c++20], [CXXFLAGS="$CXXFLAGS -std=c++20"], [
])
])

AC_CONFIG_FILES([
Makefile
])
AC_OUTPUT
AC_CONFIG_FILES([python/setup.py])

AS_IF([test "x$irstlm" == "xno"],
[AC_MSG_NOTICE([IRSTLM is not enabled; you will not be able run monolingual rule-learning; enable using --with-irstlm])])

AS_IF([test "x$yasmet" == "xno"],
[AC_MSG_NOTICE([YASMET is not enabled; you will not be able run maximum-entropy training; enable using --with-yasmet])])

AM_PATH_PYTHON([3.4], [], [AC_MSG_WARN([Can't generate SWIG wrapper without Python])])

AC_ARG_ENABLE([python-bindings],
AS_HELP_STRING([--enable-python-bindings],
[build python bindings (default=disabled)]),
[enable_python_bindings=$enableval],
[enable_python_bindings=no])
AM_CONDITIONAL([HAVE_PYTHON_BINDINGS], [test x$enable_python_bindings = xyes])

AC_OUTPUT([Makefile python/Makefile])
9 changes: 9 additions & 0 deletions python/Makefile.am
@@ -0,0 +1,9 @@
SWIG_INTERFACE = lex_tools.i

BUILT_SOURCES = %_wrap.cpp

%_wrap.cpp: $(SWIG_INTERFACE) setup.py
$(PYTHON) setup.py build

install-exec-local:
$(PYTHON) setup.py install --prefix=$(DESTDIR)$(prefix)
60 changes: 60 additions & 0 deletions python/lex_tools.i
@@ -0,0 +1,60 @@
%module lextools

%{
#define SWIG_FILE_WITH_INIT
#include <lrx_processor.h>


class LRX: public LRXProcessor
{
public:
/**
* Imitates functionality of lrx_proc using file path
*/
void lrx_proc(char arg, char *dictionary_path, char *input_path, char *output_path);
};


void
LRX::lrx_proc(char arg, char *dictionary_path, char *input_path, char *output_path)
{
bool useMaxEnt = false;
FILE *in = fopen(dictionary_path, "rb");
load(in);
FILE *input = fopen(input_path, "r"), *output = fopen(output_path, "w");
switch(arg)
{
case 'm':
useMaxEnt = true;
break;
default:
useMaxEnt = false;
}
init();
if(useMaxEnt)
{
processME(input, output);
}
else
{
process(input, output);
}
fclose(in);
fclose(input);
fclose(output);
}

%}


%include <lrx_processor.h>


class LRX: public LRXProcessor
{
public:
/**
* Imitates functionality of lrx_proc using file path
*/
void lrx_proc(char arg, char *dictionary_path, char *input_path, char *output_path);
};
58 changes: 58 additions & 0 deletions python/setup.py.in
@@ -0,0 +1,58 @@
#!/usr/bin/env python3

"""
Setup for SWIG Python bindings for lex-tools
"""
from os import path
from distutils.core import Extension, setup
from distutils.command.build import build


class CustomBuild(build):
sub_commands = [
('build_ext', build.has_ext_modules),
('build_py', build.has_pure_modules),
('build_clib', build.has_c_libraries),
('build_scripts', build.has_scripts),
]


def get_sources():
sources = ['lex_tools.i']
cc_sources = ['lrx_processor.cc']
rel_path = '..'
sources.extend(path.join(rel_path, f) for f in cc_sources)
return sources

def get_include_dirs():
# get rid of '-I' in LTTOOLBOX_CFLAGS and split list from 1, to remove extra element ''
# Strip extra space at end
dirs = '@LTTOOLBOX_CFLAGS@'.split('-I')[1:]
dirs += '@LIBXML_LIBS@'.split('-I')[1:]
dirs = [x.strip() for x in dirs]
return dirs + ['..']


lextools_module = Extension(
name='_lextools',
sources=get_sources(),
swig_opts = ["-c++", "-I..", "-Wall"],
include_dirs=get_include_dirs(),
library_dirs=['/usr/include/libxml2', '/usr/local/lib'],
extra_compile_args='@CXXFLAGS@'.split(),
extra_link_args=['-lxml2', '-llttoolbox3'],
)

setup(
name='@PACKAGE@',
version='@PACKAGE_VERSION@',
description='SWIG interface to @PACKAGE_NAME@',
long_description="SWIG interface to @PACKAGE_NAME@ for use in apertium-python",
# TODO: author, maintainer, url
author_email='@PACKAGE_BUGREPORT@',
license='GPL-3.0+',
maintainer_email='@PACKAGE_BUGREPORT@',
cmdclass={'build': CustomBuild},
ext_modules=[lextools_module],
py_modules=['lextools'],
)

0 comments on commit a9f0f84

Please sign in to comment.