Commit 2c711f41 authored by Jonathan Hanks's avatar Jonathan Hanks

Initial work at a python wrapper of the completion library.

Implemented w/ pybind11.
parent f340287c
...@@ -11,3 +11,32 @@ add_subdirectory(third_party) ...@@ -11,3 +11,32 @@ add_subdirectory(third_party)
add_subdirectory(bash) add_subdirectory(bash)
add_subdirectory(lib) add_subdirectory(lib)
add_subdirectory(src) add_subdirectory(src)
if (PYTHON2_EXECUTABLE)
message("Python2 enabled at ${PYTHON2_EXECUTABLE}")
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/python2")
execute_process(COMMAND ${CMAKE_COMMAND} -DPYTHON_EXECUTABLE=${PYTHON2_EXECUTABLE} -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX} -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} ${CMAKE_SOURCE_DIR}/python
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/python2"
)
add_custom_target(python2_module ALL
${CMAKE_COMMAND} --build .
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/python2
)
install(CODE "execute_process(COMMAND ${CMAKE_COMMAND} --build . --target install
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/python2)")
endif()
if (PYTHON3_EXECUTABLE)
message("Python3 enabled at ${PYTHON3_EXECUTABLE}")
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/python3")
execute_process(COMMAND ${CMAKE_COMMAND} -DPYTHON_EXECUTABLE=${PYTHON3_EXECUTABLE} -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX} -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} ${CMAKE_SOURCE_DIR}/python
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/python3"
)
add_custom_target(python3_module ALL
${CMAKE_COMMAND} --build .
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/python3
)
install(CODE "execute_process(COMMAND ${CMAKE_COMMAND} --build . --target install
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/python3)")
endif()
find_package(Boost REQUIRED) find_package(Boost REQUIRED)
add_library(completion INTERFACE) add_library(libcompletion INTERFACE)
target_include_directories(completion INTERFACE ${CMAKE_CURRENT_SOURCE_DIR} ${Boost_INCLUDEDIR}) target_include_directories(libcompletion INTERFACE ${CMAKE_CURRENT_SOURCE_DIR} ${Boost_INCLUDEDIR})
target_requires_cpp11(completion INTERFACE) target_requires_cpp11(libcompletion INTERFACE)
if (SKIP_TESTS)
else()
add_executable(test_completion_lib add_executable(test_completion_lib
tests/test_main.cc tests/test_main.cc
tests/test_completion_lib.cc) tests/test_completion_lib.cc)
target_link_libraries(test_completion_lib PUBLIC target_link_libraries(test_completion_lib PUBLIC
completion libcompletion
catch2) catch2)
add_test(NAME completion_lib add_test(NAME completion_lib
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/test_completion_lib) COMMAND ${CMAKE_CURRENT_BINARY_DIR}/test_completion_lib)
\ No newline at end of file endif()
\ No newline at end of file
cmake_minimum_required(VERSION 3.0)
project(complete_chan_python)
MESSAGE("Starting config for ${PYTHON_EXECUTABLE}")
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../cmake;${CMAKE_MODULE_PATH}")
find_package(pybind11)
include(Cpp11)
enable_testing()
set(SKIP_TESTS 1)
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/third_party)
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
add_subdirectory(../third_party ${CMAKE_CURRENT_BINARY_DIR}/third_party)
add_subdirectory(../lib ${CMAKE_CURRENT_BINARY_DIR}/lib)
pybind11_add_module(completion MODULE python_completion.cc)
target_link_libraries(completion PRIVATE libcompletion)
#--------------------------------------------------------------------
# Determine where packages should be installed
#--------------------------------------------------------------------
set( pyexecdir_script "from distutils.sysconfig import get_python_lib; print( get_python_lib(1,0,\"${CMAKE_INSTALL_PREFIX}\") );" )
execute_process(
COMMAND ${PYTHON_EXECUTABLE} -c "${pyexecdir_script}"
OUTPUT_VARIABLE pyexecdir
OUTPUT_STRIP_TRAILING_WHITESPACE
)
install(TARGETS completion
DESTINATION ${pyexecdir})
MESSAGE("Ending config for ${PYTHON_EXECUTABLE}")
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <completion.hh>
namespace py = pybind11;
completion::string_list
complete( const std::string& db_path, const std::string& key )
{
auto db = completion::load_database( db_path );
return completion::search( db.channels.begin( ), db.channels.end( ), key );
}
completion::string_list
complete_iterator( py::iterator it, const std::string& key )
{
completion::Database db;
while ( it != it.end( ) )
{
std::string s = ( *it ).cast< std::string >( );
if ( !s.empty( ) && s.back( ) == '\n' )
{
s.pop_back( );
}
if ( !s.empty( ) )
{
auto space = s.find( ' ' );
if ( space < std::string::npos )
{
s.resize( space );
}
db.channels.emplace_back( std::move( s ) );
}
++it;
}
completion::sort( db );
return completion::search( db.channels.begin( ), db.channels.end( ), key );
}
PYBIND11_PLUGIN( completion )
{
py::module m( "completion", "LIGO channel name completion" );
m.def( "complete", &complete, "Given a filename return the next possible "
"completions for the given key" );
m.def( "complete_iterator", &complete_iterator, "Given an iterator of "
"channel names, return the "
"next possibly completions "
"for the given key" );
return m.ptr( );
}
\ No newline at end of file
...@@ -9,7 +9,7 @@ else ...@@ -9,7 +9,7 @@ else
exit 1 exit 1
fi fi
SOURCE_DIRS="src lib" SOURCE_DIRS="src lib python"
for dir in ${SOURCE_DIRS}; do for dir in ${SOURCE_DIRS}; do
echo "Reformatting in ${dir}" echo "Reformatting in ${dir}"
......
add_executable(ligo_channel_completion ligo_channel_completion.cpp) add_executable(ligo_channel_completion ligo_channel_completion.cpp)
target_link_libraries(ligo_channel_completion PUBLIC completion) target_link_libraries(ligo_channel_completion PUBLIC libcompletion)
target_requires_cpp11(ligo_channel_completion PUBLIC) target_requires_cpp11(ligo_channel_completion PUBLIC)
install(TARGETS ligo_channel_completion DESTINATION lib) install(TARGETS ligo_channel_completion DESTINATION lib)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment