Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python binding の提供 #103

Merged
merged 10 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
FROM alpine:latest

RUN apk add --no-cache g++
RUN apk add --no-cache g++ make

RUN apk add --no-cache bash

WORKDIR /app

COPY . .
COPY scripts /app/scripts
COPY src /app/src
COPY Makefile /app/Makefile

RUN bash build.sh
RUN make build

ENTRYPOINT [ "./docker-entrypoint.sh" ]
ENTRYPOINT [ "scripts/docker-entrypoint.sh" ]
17 changes: 17 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.PHONY: setup

setup:
bash scripts/setup.sh

build: setup
g++ -std=c++23 build/almo.cpp -o build/almo

pybind: setup
bash scripts/pybind.sh build/pyalmo.cpp almo.so

all: build pybind

clean:
rm -rf build
rm almo.so

1 change: 0 additions & 1 deletion docker-entrypoint.sh → scripts/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
#!/bin/sh

exec /app/build/almo "$@"
20 changes: 20 additions & 0 deletions scripts/pybind.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
if [ $# -ne 2 ]; then
echo "引数の数が正しくありません"
exit 1
fi

input_file=$1
output_file=$2

g++ \
-O3 -shared -std=c++23 -undefined dynamic_lookup \
$(python3 -m pybind11 --includes) \
$input_file -o $output_file

if [ $? -eq 0 ]; then
echo "almo.so をビルドしました"
else
echo "ビルドエラー"
fi

7 changes: 1 addition & 6 deletions build.sh → scripts/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ done



g++ -std=c++23 "$build_dir"almo.cpp -o "$build_dir"almo

if [ $? -ne 0 ]; then
echo "ビルドに失敗しました。"
exit 1
fi


echo "ビルドが完了しました。"

24 changes: 24 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import os
from pybind11.setup_helpers import Pybind11Extension, build_ext
from setuptools import setup

os.system("bash scripts/setup.sh")
__version__ = "\"0.0.1\""

ext_modules = [
Pybind11Extension(
"almo",
["build/pyalmo.cpp"],
cxx_std=23,
define_macros=[("VERSION_INFO", __version__)],
),
]

setup(
name="almo",
author=["abap34", "ebi-fly13", "noya2"],
url="https://github.com/abap34/ALMO",
ext_modules=ext_modules,
cmdclass={"build_ext": build_ext},
zip_safe=False,
)
3 changes: 1 addition & 2 deletions src/almo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ void debug_graph(almo::Block ast){
std::cout << ast.to_dot(true) << std::endl;
}



int main(int argc, char* argv[]) {
Config config;
config.parse_arguments(argc, argv);
Expand Down Expand Up @@ -175,3 +173,4 @@ int main(int argc, char* argv[]) {

return 0;
}

30 changes: 16 additions & 14 deletions src/parse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ namespace almo {
return content;
}

// md ファイルの中身 (front YAML)
// md ファイルの中身 (front YAML を含む)
// を受け取って、 front YAML をパースした結果と残りの md ファイルの開始位置を返す
// TODO: きちんとした YAML パーサを使うようにする。
std::pair<std::vector<std::pair<std::string, std::string>>, int> parse_front(std::vector<std::string> content) {
Expand All @@ -810,24 +810,26 @@ namespace almo {
return { front_yaml, front_yaml_end };
}

// mdファイルの中身 (fron YAML を含まない)
// を受け取って、抽象構文木を返す。
Block parse_rest(std::vector<std::string> content) {
// パース
BlockParser parser = BlockParser();
Block ast = parser.processer(content);

return ast;
}


// mdファイルの内容から
// メタデータ (std::map<std::string, std::string>) と
// 抽象構文木の根 (Block) のペアを返す。
std::pair<std::map<std::string, std::string>, Block> parse(std::vector<std::string> content) {
auto [meta_data, meta_data_end] = parse_front(content);

// メタデータ以降の行を取り出し
std::vector<std::string> md_lines;
for (int i = meta_data_end; i < (int)content.size(); i++) {
md_lines.push_back(content[i]);
}
auto [front_yaml, front_yaml_end] = parse_front(content);

// 前処理
md_lines = preprocess(md_lines);
std::vector<std::string> rest_content(content.begin() + front_yaml_end, content.end());

// パース
BlockParser parser = BlockParser();
Block ast = parser.processer(md_lines);
Block ast = parse_rest(rest_content);

// meta_data を std::map に変換する
std::map<std::string, std::string> meta_data_map;
Expand All @@ -845,7 +847,7 @@ namespace almo {
meta_data_map["site_name"] = "";
meta_data_map["twitter_site"] = "";

for (auto [key, data] : meta_data) {
for (auto [key, data] : front_yaml) {
meta_data_map[key] = data;
}

Expand Down
41 changes: 41 additions & 0 deletions src/pyalmo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include "almo.cpp"


std::string md_to_html(std::string md_content, std::map<std::string, std::string> meta_data) {
almo::Block ast = almo::parse_rest(split(md_content, "\n"));
return ast.render(meta_data);
}


std::string md_to_json(std::string md_content) {
almo::Block ast = almo::parse_rest(split(md_content, "\n"));
return ast.to_json();
}


std::string md_to_dot(std::string md_content) {
almo::Block ast = almo::parse_rest(split(md_content, "\n"));
return ast.to_dot(true);
}


namespace py = pybind11;


PYBIND11_MODULE(almo, m) {
m.doc() = "almo interface for python.";

m.def("md_to_html", &md_to_html, "md to html");
m.def("md_to_json", &md_to_json, "md to json");
m.def("md_to_dot", &md_to_dot, "md to dot");


#ifdef VERSION_INFO
m.attr("__version__") = VERSION_INFO;
#else
m.attr("__version__") = "dev";
#endif
}