Auto generating Python bindings (#70)

Auto generating python bindings
This commit is contained in:
Erik Fröjdh
2020-01-07 15:47:38 +01:00
committed by GitHub
parent 086cbacd84
commit ed2a69744b
12 changed files with 481 additions and 268 deletions

View File

@ -0,0 +1,84 @@
"""
This file is used to auto generate Python bindings for the
sls::Detector class. The tool needs the libclang bindings
to be installed.
When the Detector API is updated this file should be run
manually
"""
from clang import cindex
import subprocess
import argparse
from parse import system_include_paths
default_build_path = "/home/l_frojdh/sls/build/"
fpath = "../../slsDetectorSoftware/src/Detector.cpp"
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--build_path", help="Path to the build database", type = str, default=default_build_path)
cargs = parser.parse_args()
db = cindex.CompilationDatabase.fromDirectory(cargs.build_path)
index = cindex.Index.create()
args = db.getCompileCommands(fpath)
args = list(iter(args).__next__().arguments)[0:-1]
args = args + "-x c++ --std=c++11".split()
syspath = system_include_paths('clang++')
incargs = ["-I" + inc for inc in syspath]
args = args + incargs
tu = index.parse(fpath, args=args)
m = []
ag = []
lines = []
def get_arguments(node):
args = [a.type.spelling for a in node.get_arguments()]
args = [
"py::arg() = Positions{}" if item == "sls::Positions" else "py::arg()"
for item in args
]
args = ', '.join(args)
if args:
args = f', {args}'
return args
def visit(node):
if node.kind == cindex.CursorKind.CLASS_DECL:
if node.displayname == "Detector":
for child in node.get_children():
if (
child.kind == cindex.CursorKind.CXX_METHOD
and child.access_specifier == cindex.AccessSpecifier.PUBLIC
):
m.append(child)
args = get_arguments(child)
lines.append(f'.def(\"{child.spelling}\", &Detector::{child.spelling}{args})')
for child in node.get_children():
visit(child)
visit(tu.cursor)
with open('../src/detector_in.cpp') as f:
data = f.read()
s = ''.join(lines)
s += ';'
text = data.replace('[[FUNCTIONS]]', s)
warning = '/* WARINING This file is auto generated any edits might be overwritten without warning */\n\n'
with open('../src/detector.cpp', 'w') as f:
f.write(warning)
f.write(text)
# run clang format on the output
subprocess.run(['clang-format', '../src/detector.cpp', '-i'])

View File

@ -1,4 +1,9 @@
import re
import subprocess
from subprocess import PIPE
import os
def remove_comments(text):
def replacer(match):
s = match.group(0)
@ -10,4 +15,42 @@ def remove_comments(text):
r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',
re.DOTALL | re.MULTILINE
)
return re.sub(pattern, replacer, text)
return re.sub(pattern, replacer, text)
#based on ccsyspath: https://github.com/AndrewWalker/ccsyspath
def compiler_preprocessor_verbose(compiler, extraflags):
"""Capture the compiler preprocessor stage in verbose mode
"""
lines = []
with open(os.devnull, 'r', encoding='utf-8') as devnull:
cmd = [compiler, '-E']
cmd += extraflags
cmd += ['-', '-v']
p = subprocess.Popen(cmd, stdin=devnull, stdout=PIPE, stderr=PIPE)
p.wait()
lines = p.stderr.read()
lines = lines.splitlines()
return lines
def system_include_paths(compiler, cpp=True):
extraflags = []
if cpp:
extraflags = b'-x c++'.split()
lines = compiler_preprocessor_verbose(compiler, extraflags)
lines = [ line.strip() for line in lines ]
start = lines.index(b'#include <...> search starts here:')
end = lines.index(b'End of search list.')
lines = lines[start+1:end]
paths = []
for line in lines:
line = line.replace(b'(framework directory)', b'')
line = line.strip()
paths.append(line)
paths = [p.decode('utf-8') for p in paths]
return paths