reorganise python interface

This commit is contained in:
Vitor Santos Costa
2017-05-08 18:49:00 +01:00
parent 6e7846e210
commit d1e39368f4
15 changed files with 173 additions and 39 deletions

View File

@@ -0,0 +1,111 @@
# This is a CMake example for Python
INCLUDE(NewUseSWIG)
if (PYTHONLIBS_FOUND)
include(FindPythonModule)
file(RELATIVE_PATH RELATIVE_SOURCE ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR})
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH})
INCLUDE_DIRECTORIES("${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_SOURCE_DIR}/CXX")
SET_SOURCE_FILES_PROPERTIES(../yap.i PROPERTIES CPLUSPLUS ON)
SET_SOURCE_FILES_PROPERTIES(../yap.i PROPERTIES SWIG_FLAGS "-O;-py3")
SET_SOURCE_FILES_PROPERTIES(../yap.i PROPERTIES SWIG_MODULE_NAME yap)
SET_SOURCE_FILES_PROPERTIES(../yap.i PROPERTIES OUTPUT_NAME yap)
SWIG_ADD_LIBRARY(Py2YAP LANGUAGE python SHARED SOURCES ../yap.i )
if (WIN32)
SWIG_LINK_LIBRARIES(Py2YAP YAPPython libYap ${PYTHON_LIBRARIES} )
else()
SWIG_LINK_LIBRARIES( Py2YAP libYap YAP++ YAPPython ${PYTHON_LIBRARIES} )
endif()
set_target_properties ( ${SWIG_MODULE_Py2YAP_REAL_NAME}
PROPERTIES
NO_SONAME ON
OUTPUT_NAME _yap
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" )
# inform we are compiling YAP
# s used in MSYS
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import sysconfig; print( sysconfig.get_path( 'platlib' ) )"
OUTPUT_VARIABLE _ABS_PYTHON_MODULE_PATH
OUTPUT_STRIP_TRAILING_WHITESPACE)
configure_file("setup.py.cmake" ${CMAKE_CURRENT_BINARY_DIR}/setup.py)
get_filename_component(ABS_PYTHON_MODULE_PATH ${_ABS_PYTHON_MODULE_PATH} ABSOLUTE)
file(RELATIVE_PATH _REL_PYTHON_MODULE_PATH ${CMAKE_INSTALL_PREFIX} ${_ABS_PYTHON_MODULE_PATH})
#
# set ( PYTHON_MODULE_PATH
# ${ABS_PYTHON_MODULE_PATH}
# )
#
# INSTALL ( FILES ${CMAKE_CURRENT_BINARY_DIR}/yap.py DESTINATION ${PYTHON_MODULE_PATH} )
# INSTALL ( FILES ${CMAKE_CURRENT_SOURCE_DIR}/yapi.py DESTINATION ${PYTHON_MODULE_PATH} )
# INSTALL ( FILES ${CMAKE_CURRENT_SOURCE_DIR}/yapi.yap DESTINATION ${libpl} )
#
#
# INSTALL ( TARGETS ${SWIG_MODULE_Py2YAP_REAL_NAME}
# RUNTIME DESTINATION ${PYTHON_MODULE_PATH}
# ARCHIVE DESTINATION ${PYTHON_MODULE_PATH}
# LIBRARY DESTINATION ${PYTHON_MODULE_PATH}
# )
add_custom_target(Py2YAP ALL
DEPENDS ../yap.i libYap YAP++ YAPPython _Py2YAP
)
set (dlls $<TARGET_FILE:matrix>
$<TARGET_FILE:regexp>
$<TARGET_FILE:yap_rl>
$<TARGET_FILE:tries>
$<TARGET_FILE:itries>
$<TARGET_FILE:sys>
$<TARGET_FILE:yap_random>)
if (TARGET real)
list( APPEND dlls $<TARGET_FILE:real>
)
endif()
add_custom_command(TARGET Py2YAP
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory yap4py/os
COMMAND ${CMAKE_COMMAND} -E make_directory yap4py/pl
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/__init__.py ${dlls} $<TARGET_FILE:libYap> $<TARGET_FILE:YAPPython> $<TARGET_FILE:YAP++> ${CMAKE_CURRENT_SOURCE_DIR}/yapi.yap ${pl_library} ${CMAKE_CURRENT_BINARY_DIR}/yap4py
COMMAND ${CMAKE_COMMAND} -E copy ${pl_os_library} ${CMAKE_CURRENT_BINARY_DIR}/yap4py/os
COMMAND ${CMAKE_COMMAND} -E copy ${pl_boot_library} ${CMAKE_CURRENT_BINARY_DIR}/yap4py/pl
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/README.md ${CMAKE_CURRENT_SOURCE_DIR}/MANIFEST.in ${CMAKE_CURRENT_BINARY_DIR}
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/yapi.py ${CMAKE_CURRENT_BINARY_DIR}/yap4py
COMMAND ${PYTHON_EXECUTABLE} setup.py sdist bdist_wheel
VERBATIM
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)
if (WITH_DOCS AND DOXYGEN_FOUND)
set(CMAKE_SWIG_FLAGS -DDOXYGEN=${DOXYGEN_FOUND})
# Run doxygen to only generate the xml
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/doc/xml/ftdi_8c.xml
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/doc
COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_BINARY_DIR}/Doxyfile.xml
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
DEPENDS ${c_headers};${c_sources};${cpp_sources};${cpp_headers}
)
# generate .i from doxygen .xml
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/ftdi1_doc.i
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/doxy2swig.py -n
${CMAKE_BINARY_DIR}/doc/xml/ftdi_8c.xml
${CMAKE_CURRENT_BINARY_DIR}/ftdi1_doc.i
DEPENDS ${CMAKE_BINARY_DIR}/doc/xml/ftdi_8c.xml
)
add_custom_target(doc_i DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/ftdi1_doc.i)
add_dependencies(${SWIG_MODULE_ftdi1_REAL_NAME} doc_i)
ENDIF ()
ENDIF ()

View File

@@ -0,0 +1,8 @@
recursive-include yap4py *.dylib
recursive-include yap4py *.dll
recursive-include yap4py *.so
recursive-include yap4py/prolog *.yap
recursive-include yap4py *.yss
recursive-include yap4py/prolog *.pl
recursive-include yap4py/prolog *.r
recursive-include yap4py *.md

View File

@@ -0,0 +1,17 @@
import imp
import os
import ctypes
import glob
import os.path
import sys
global yap_lib_path
yap_lib_path = os.path.dirname(__file__)
def load( dll ):
dll = glob.glob(os.path.join(yap_lib_path,dll))[0]
dll = os.path.abspath(dll)
ctypes.CDLL(dll, mode=ctypes.RTLD_GLOBAL)
load('libYap*')

View File

@@ -0,0 +1,14 @@
"""The main routine of the yap python project."""
import sys
import yapi
def main(args=None):
"""The main routine."""
if args is None:
args = sys.argv[1:]
yap.yapi.live(args)
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,3 @@
// make Python happy...

View File

@@ -0,0 +1,97 @@
"""A setuptools based setup module.
See:
https://packaging.python.org/en/latest/distributing.html
https://github.com/pypa/sampleproject
"""
# Always prefer setuptools over distutils
from setuptools import setup
from setuptools.extension import Extension
# To use a consistent encoding
from codecs import open
from os import path, makedirs, walk
from shutil import copytree, rmtree, copy2, move
from glob import glob
from pathlib import Path
import platform
import os.path
my_extra_link_args = []
if platform.system() == 'Darwin':
my_extra_link_args = ['-Wl,-rpath','-Wl,${_ABS_PYTHON_MODULE_PATH}']
so = 'dylib'
#or dll in glob('yap/dlls/*'):
# move( dll ,'lib' )
pls = []
for (r,d,fs) in walk('yap/pl'):
for f in fs:
pls += [os.path.join(r, f)]
for (r,d,fs) in walk('yap'):
for f in fs:
pls += [os.path.join(r, f)]
# for f in glob( 'lib*.*' ):
# ofile.write(f+"\n")
cplus=['${RELATIVE_SOURCE}CXX/yapi.cpp']
py2yap=['${RELATIVE_SOURCE}packages/python/python.c',
'${RELATIVE_SOURCE}packages/python/pl2py.c',
'${RELATIVE_SOURCE}packages/python/pybips.c',
'${RELATIVE_SOURCE}packages/python/py2pl.c',
'${RELATIVE_SOURCE}packages/python/pl2pl.c',
'${RELATIVE_SOURCE}packages/python/pypreds.c'
]
python_sources = ['yapPYTHON_wrap.cxx']+py2yap+cplus
here = path.abspath(path.dirname(__file__))
# Get the long description from the README file
extensions=[Extension('_yap', python_sources,
define_macros = [('MAJOR_VERSION', '1'),
('MINOR_VERSION', '0'),
('_YAP_NOT_INSTALLED_', '1'),
('YAP_PYTHON', '1')],
runtime_library_dirs=['yap4py','${libdir}','${bindir}'],
swig_opts=['-modern', '-c++', '-py3','-I${RELATIVE_SOURCE}/CXX'],
library_dirs=['../../..','../../../CXX','../../packages/python',"${dlls}","${bindir}", '.'],
extra_link_args=my_extra_link_args,
extra_compile_args=['-g'],
libraries=['Yap','${GMP_LIBRARIES}'],
include_dirs=['../../..',
'${GMP_INCLUDE_DIRS}',
'${RELATIVE_SOURCE}H',
'${RELATIVE_SOURCE}H/generated',
'${RELATIVE_SOURCE}OPTYap',
'${RELATIVE_SOURCE}os',
'${RELATIVE_SOURCE}include',
'${RELATIVE_SOURCE}CXX', '.']
)]
setup(
name='YAP4Py',
version='${YAP_FULL_VERSION}',
description='The YAP Prolog compiler as a Python Library',
url='https://github.com/vscosta/yap-6.3',
author='Vitor Santos Costa',
author_email='vsc@dcc.fc.up.pt',
license='Artistic',
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'Topic :: Software Development :: Build Tools',
'License :: OSI Approved :: Artistic License',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7'],
keywords=['Logic Programing'],
#spackage_data={'': ['yap']},
include_package_data=True,
ext_modules = extensions,
py_modules = ['yap'],
zip_safe=False,
eager_resources = ['yap4py'],
packages=['yap4py'] # find_packages()
#package_dir = {'':'yap4py'}
)

View File

@@ -0,0 +1,120 @@
import yap
import os.path
import sys
# debugging support.
import pdb
from collections import namedtuple
yap_lib_path = os.path.dirname(__file__)
use_module = namedtuple( 'use_module', 'file')
bindvars = namedtuple( 'bindvars', 'list')
library = namedtuple( 'library', 'list')
v = namedtuple( '_', 'slot')
def numbervars( engine, l ):
return engine.fun(bindvars(l))
def query_prolog(engine, s):
def answer(q):
try:
return q.next()
except Exception as e:
print(e.args[1])
return False
#
# construct a query from a one-line string
# q is opaque to Python
q = engine.query(s)
# vs is the list of variables
# you can print it out, the left-side is the variable name,
# the right side wraps a handle to a variable
# pdb.set_trace()
vs = q.namedVars()
#pdb.set_trace()
# atom match either symbols, or if no symbol exists, sttrings, In this case
# variable names should match strings
#for eq in vs:
# if not isinstance(eq[0],str):
# print( "Error: Variable Name matches a Python Symbol")
# return
ask = True
# launch the query
while answer(q):
# this new vs should contain bindings to vars
vs= q.namedVars()
print( vs )
gs = numbervars( engine, vs)
print(gs)
i=0
# iterate
for eq in gs:
name = eq[0]
binding = eq[1]
# this is tricky, we're going to bind the variables in the term so thay we can
# output X=Y. The Python way is to use dictionares.
#Instead, we use the T function to tranform the Python term back to Prolog
if name != binding:
print(name + " = " + str(binding))
#ok, that was Prolog code
print("yes")
# deterministic = one solution
if q.deterministic():
# done
q.close()
return
if ask:
s = input("more(;), all(*), no(\\n), python(#) ?").lstrip()
if s.startswith(';') or s.startswith('y'):
continue
elif s.startswith('#'):
try:
exec(s.lstrip('#'))
except:
raise
elif s.startswith('*') or s.startswith('a'):
ask = False
continue
else:
break
print("No (more) answers")
q.close()
return
def live():
args = yap.YAPEngineArgs()
args.setYapLibDir(yap_lib_path)
args.setYapShareDir(yap_lib_path)
#args.setYapPrologBootFile(os.path.join(yap_lib_path."startup.yss"))
engine = yap.YAPEngine( args )
engine.goal( use_module( library('yapi') ) )
loop = True
while loop:
try:
s = input("?- ")
if not s:
loop = False
query_prolog(engine, s)
except SyntaxError as err:
print("Syntax Error error: {0}".format(err))
except EOFError:
return
except RuntimeError as err:
print("YAP Execution Error: {0}".format(err))
except ValueError:
print("Could not convert data to an integer.")
except:
print("Unexpected error:", sys.exc_info()[0])
raise
engine.close()
#
# initialize engine
# engine = yap.YAPEngine();
# engine = yap.YAPEngine(yap.YAPParams());
live()

View File

@@ -0,0 +1 @@
recursive-include yap_kernel/resources *.*

View File

@@ -0,0 +1,6 @@
A Jupyter Kernel for YAP (#yap_kernel)
=======================
This kernel supports interaction with YAP Prolog.

View File

@@ -7,7 +7,7 @@
from __future__ import print_function
# the name of the package
name = 'yap_kernel'
name = 'ipykernel'
#-----------------------------------------------------------------------------
# Minimal Python version sanity check
@@ -27,34 +27,50 @@ PY3 = (sys.version_info[0] >= 3)
# get on with it
#-----------------------------------------------------------------------------
import os
from glob import glob
import os
import shutil
from distutils.core import setup
packages = ["${CMAKE_CURRENT_SOURCE_DIR}"]
pjoin = os.path.join
here = os.path.abspath(os.path.dirname(__file__))
pkg_root = pjoin(here, name)
packages = []
for d, _, _ in os.walk(pjoin(here, name)):
if os.path.exists(pjoin(d, '__init__.py')):
packages.append(d[len(here)+1:].replace(os.path.sep, '.'))
package_data = {
'ipykernel': ['resources/*.*'],
}
version_ns = {}
with open(pjoin(here, name, '_version.py')) as f:
exec(f.read(), {}, version_ns)
setup_args = dict(
name = 'yap_kernel',
version = '0.0.1',
packages = ["yap_kernel"],
package_dir = {'': '${CMAKE_SOURCE_DIR}/packages/python/yap_kernel' },
description = "YAP Kernel for Jupyter",
long_description="A simple YAP kernel for Jupyter/IPython",
url="https://github.com/vscosta/yap-6.3",
author='Vitor Santos Costa, based on the the IPython',
author_email='vsc@dcc.fc.up.pt',
name = name,
version = version_ns['__version__'],
scripts = glob(pjoin('scripts', '*')),
packages = packages,
py_modules = ['ipykernel_launcher'],
package_data = package_data,
description = "IPython Kernel for Jupyter",
author = 'IPython Development Team',
author_email = 'ipython-dev@scipy.org',
url = 'http://ipython.org',
license = 'BSD',
platforms = "Linux, Mac OS X, Windows",
keywords = ['Interactive', 'Interpreter', 'Shell', 'Web'],
data_files=[('share/Yap/js', ['${CMAKE_SOURCE_DIR}/misc/editors/prolog.js'])],
classifiers = [
'Intended Audience :: Developers',
'Intended Audience :: System Administrators',
'Intended Audience :: Science/Research',
'License :: OSI Approved :: BSD License',
'Programming Language :: Prolog',
'Programming Language :: Python',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
],
@@ -71,8 +87,22 @@ install_requires = setuptools_args['install_requires'] = [
'tornado>=4.0',
]
if any(a.startswith(('bdist', 'build', 'install')) for a in sys.argv):
from ipykernel.kernelspec import write_kernel_spec, make_ipkernel_cmd, KERNEL_NAME
argv = make_ipkernel_cmd(executable='python')
dest = os.path.join(here, 'data_kernelspec')
if os.path.exists(dest):
shutil.rmtree(dest)
write_kernel_spec(dest, overrides={'argv': argv})
setup_args['data_files'] = [
(pjoin('share', 'jupyter', 'kernels', KERNEL_NAME), glob(pjoin(dest, '*'))),
]
extras_require = setuptools_args['extras_require'] = {
'test:python_version=="2.7"': ['mock', 'nose_warnings_filters'],
'test:python_version=="2.7"': ['mock'],
'test': ['nose_warnings_filters', 'nose-timer'],
}
if 'setuptools' in sys.modules:

View File

@@ -0,0 +1,113 @@
#!/usr/bin/env python
# coding: utf-8
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
from __future__ import print_function
# the name of the package
name = 'yap_kernel'
#-----------------------------------------------------------------------------
# Minimal Python version sanity check
#-----------------------------------------------------------------------------
import sys
v = sys.version_info
if v[:2] < (2,7) or (v[0] >= 3 and v[:2] < (3,3)):
error = "ERROR: %s requires Python version 2.7 or 3.3 or above." % name
print(error, file=sys.stderr)
sys.exit(1)
PY3 = (sys.version_info[0] >= 3)
#-----------------------------------------------------------------------------
# get on with it
#-----------------------------------------------------------------------------
from glob import glob
import os
import shutil
from distutils.core import setup
pjoin = os.path.join
here = os.path.abspath(os.path.dirname(__file__))
pkg_root = pjoin(here, name)
packages = []
for d, _, _ in os.walk(pjoin(here, name)):
if os.path.exists(pjoin(d, '__init__.py')):
packages.append(d[len(here)+1:].replace(os.path.sep, '.'))
package_data = {
'ipykernel': ['resources/*.*'],
}
version_ns = {}
with open(pjoin(here, name, '_version.py')) as f:
exec(f.read(), {}, version_ns)
setup_args = dict(
name = name,
version = version_ns['__version__'],
scripts = glob(pjoin('scripts', '*')),
packages = packages,
package_dir = {'':'${CMAKE_CURRENT_SOURCE_DIR}'},
py_modules = ['ipykernel_launcher'],
package_data = package_data,
description = "IPython Kernel for Jupyter",
author = 'IPython Development Team',
author_email = 'ipython-dev@scipy.org',
url = 'http://ipython.org',
license = 'BSD',
platforms = "Linux, Mac OS X, Windows",
keywords = ['Interactive', 'Interpreter', 'Shell', 'Web'],
classifiers = [
'Intended Audience :: Developers',
'Intended Audience :: System Administrators',
'Intended Audience :: Science/Research',
'License :: OSI Approved :: BSD License',
'Programming Language :: Python',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
],
)
if 'develop' in sys.argv or any(a.startswith('bdist') for a in sys.argv):
import setuptools
setuptools_args = {}
install_requires = setuptools_args['install_requires'] = [
'ipython>=4.0.0',
'traitlets>=4.1.0',
'jupyter_client',
'tornado>=4.0',
]
if any(a.startswith(('bdist', 'build', 'install')) for a in sys.argv):
from ipykernel.kernelspec import write_kernel_spec, make_ipkernel_cmd, KERNEL_NAME
argv = make_ipkernel_cmd(executable='python')
dest = os.path.join(here, 'data_kernelspec')
if os.path.exists(dest):
shutil.rmtree(dest)
write_kernel_spec(dest, overrides={'argv': argv})
setup_args['data_files'] = [
(pjoin('share', 'jupyter', 'kernels', KERNEL_NAME), glob(pjoin(dest, '*'))),
]
extras_require = setuptools_args['extras_require'] = {
'test:python_version=="2.7"': ['mock'],
'test': ['nose_warnings_filters', 'nose-timer'],
}
if 'setuptools' in sys.modules:
setup_args.update(setuptools_args)
if __name__ == '__main__':
setup(**setup_args)