Skip to content

Commit

Permalink
[TUBEMQ-162]Python SDK support in TubeMQ (#303)
Browse files Browse the repository at this point in the history
* [TUBEMQ-260] Init Python client module (#201)

Co-authored-by: dockerzhang(张超) <dockerzhang@tencent.com>

* [TUBEMQ-276] add python client encode/decode protobuf message for TubeMQ RPC Protocol (#203)

Co-authored-by: dockerzhang(张超) <dockerzhang@tencent.com>

* [TUBEMQ-398] reinit project for using pybind11 (#298)

Co-authored-by: dockerzhang <dockerzhang@tencent.com>

* [TUBEMQ-399] expose C++ SDK method by Pybind11 (#299)

Co-authored-by: dockerzhang <dockerzhang@tencent.com>

* [TUBEMQ-400] add example for consume message (#300)

Co-authored-by: dockerzhang <dockerzhang@tencent.com>

* [TUBEMQ-383] add readme for installation and example (#301)

Co-authored-by: dockerzhang <dockerzhang@tencent.com>

* [TUBEMQ-401] fix readme bug (#302)

Co-authored-by: dockerzhang <dockerzhang@tencent.com>

Co-authored-by: dockerzhang <zhchao_beyond@outlook.com>
Co-authored-by: dockerzhang(张超) <dockerzhang@tencent.com>
  • Loading branch information
3 people committed Nov 3, 2020
1 parent b433ceb commit f64863d
Show file tree
Hide file tree
Showing 15 changed files with 565 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ target/
!.gitignore
!.gitkeep
!.travis.yml
tubemq-client-twins/tubemq-client-cpp/build/
venv/
eggs/
wheels/
tubemq-client-twins/tubemq-client-cpp/build/
44 changes: 44 additions & 0 deletions tubemq-client-twins/tubemq-client-python/.appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

version: '{build}'
image: Visual Studio 2015
platform:
- x86
- x64
environment:
global:
DISTUTILS_USE_SDK: 1
PYTHONWARNINGS: ignore:DEPRECATION
MSSdk: 1
matrix:
- PYTHON: 27
- PYTHON: 36
install:
- cmd: '"%VS140COMNTOOLS%\..\..\VC\vcvarsall.bat" %PLATFORM%'
- ps: |
if ($env:PLATFORM -eq "x64") { $env:PYTHON = "$env:PYTHON-x64" }
$env:PATH = "C:\Python$env:PYTHON\;C:\Python$env:PYTHON\Scripts\;$env:PATH"
python -m pip install --disable-pip-version-check --upgrade --no-warn-script-location pip build
build_script:
- ps: |
python -m build -s
cd dist
python -m pip install --verbose tubemq-client-0.0.1.tar.gz
cd ..
30 changes: 30 additions & 0 deletions tubemq-client-twins/tubemq-client-python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
### TubeMQ Python Client
TubeMQ Python Client library is a wrapper over the existing [C++ client library](https://github.com/apache/incubator-tubemq/tree/master/tubemq-client-twins/tubemq-client-cpp/) and exposes all of the same features.

#### Install from source
##### install python-devel
- build and install C++ client SDK

build C++ client SDK from source, and install:
1, copy `tubemq` include directory to `/usr/local/include/`
2, copy `libtubemq_rel.a` to `/usr/local/lib`
&nbsp;

- install python-devel
```
yum install python-devel -y
```
- install required dependency
```
pip install -r requirements.txt
```

- install client
```
pip install ./
```

#### Examples
##### Producer example
##### Consumer example
Consumer example is [available](https://github.com/apache/incubator-tubemq/tree/tubemq-client-python/tubemq-client-twins/tubemq-client-python/src/python/example/consumer).
18 changes: 18 additions & 0 deletions tubemq-client-twins/tubemq-client-python/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
53 changes: 53 additions & 0 deletions tubemq-client-twins/tubemq-client-python/conda.recipe/meta.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

package:
name: tubemq-client
version: 0.0.1

source:
path: ..

build:
number: 0
script: python -m pip install . -vvv

requirements:
build:
- {{ compiler('cxx') }}

host:
- python
- pip
- pybind11 >=2.6.0

run:
- python


test:
imports:
- tubemq-client
source_files:
- tests
commands:
- python tests/test_comsumer.py

about:
summary: TubeMQ Python SDK Client project built with pybind11.
20 changes: 20 additions & 0 deletions tubemq-client-twins/tubemq-client-python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

pybind11==2.6.0
48 changes: 48 additions & 0 deletions tubemq-client-twins/tubemq-client-python/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

from pybind11.setup_helpers import Pybind11Extension, build_ext
from glob import glob
from setuptools import setup

import sys

__version__ = "0.0.1"

ext_modules = [
Pybind11Extension("tubemq_client",
sorted(glob("src/cpp/*.cc")),
cxx_std=11,
define_macros=[('VERSION_INFO', __version__)],
)
]

setup(
name="tubemq-client",
version=__version__,
author="dockerzhang",
author_email="dockerzhang@apache.org",
url="https://github.com/apache/incubator-tubemq/tree/tubemq-client-python",
description="TubeMq Python SDK Client project built with pybind11",
long_description="",
ext_modules=ext_modules,
extras_require={"test": "pytest"},
cmdclass={"build_ext": build_ext},
zip_safe=False,
)
40 changes: 40 additions & 0 deletions tubemq-client-twins/tubemq-client-python/src/cpp/tubemq_client.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#include <pybind11/pybind11.h>
#include "tubemq/tubemq_client.h"

namespace py = pybind11;

using namespace tubemq;
using std::string;

PYBIND11_MODULE(tubemq_client, m) {
m.def("startTubeMQService", &StartTubeMQService, "start TubeMQ Service");
m.def("stopTubeMQService", &StopTubeMQService, "stop TubeMQ Service");

py::class_<TubeMQConsumer>(m, "TubeMQConsumer")
.def(py::init<>())
.def("start", &TubeMQConsumer::Start)
.def("shutDown", &TubeMQConsumer::ShutDown)
.def("getClientId", &TubeMQConsumer::GetClientId)
.def("getMessage", &TubeMQConsumer::GetMessage)
.def("confirm", &TubeMQConsumer::Confirm)
.def("getCurConsumedInfo", &TubeMQConsumer::GetCurConsumedInfo);
}
42 changes: 42 additions & 0 deletions tubemq-client-twins/tubemq-client-python/src/cpp/tubemq_config.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "tubemq/tubemq_config.h"

namespace py = pybind11;

using namespace tubemq;
using std::map;
using std::set;
using std::string;

PYBIND11_MODULE(tubemq_config, m) {
py::class_<ConsumerConfig>(m, "ConsumerConfig")
.def(py::init<>())
.def("setRpcReadTimeoutMs", &ConsumerConfig::SetRpcReadTimeoutMs)
.def("setMasterAddrInfo", &ConsumerConfig::SetMasterAddrInfo)
.def("setGroupConsumeTarget", static_cast<bool (ConsumerConfig::*)\
(string&, const string&, const set<string>&)>(&ConsumerConfig::SetGroupConsumeTarget), "")
.def("setGroupConsumeTarget", static_cast<bool (ConsumerConfig::*)\
(string&, const string&, const map<string, set<string>>&)>(&ConsumerConfig::SetGroupConsumeTarget), "")
.def("getRpcReadTimeoutMs", &ConsumerConfig::GetRpcReadTimeoutMs)
.def("getMasterAddrInfo", &ConsumerConfig::GetMasterAddrInfo);
}
58 changes: 58 additions & 0 deletions tubemq-client-twins/tubemq-client-python/src/cpp/tubemq_errcode.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#include <pybind11/pybind11.h>
#include "tubemq/tubemq_errcode.h"

namespace py = pybind11;

using namespace tubemq;
using namespace tubemq::err_code;
using std::string;

PYBIND11_MODULE(tubemq_errcode, m) {
py::enum_<Result>(m, "Result")
.value("kErrSuccess", Result::kErrSuccess)
.value("kErrNotReady", Result::kErrNotReady)
.value("kErrMoved", Result::kErrMoved)
.value("kErrBadRequest", Result::kErrBadRequest)
.value("kErrUnAuthorized", Result::kErrUnAuthorized)
.value("kErrForbidden", Result::kErrForbidden)
.value("kErrNotFound", Result::kErrNotFound)
.value("kErrNoPartAssigned", Result::kErrNoPartAssigned)
.value("kErrAllPartWaiting", Result::kErrAllPartWaiting)
.value("kErrAllPartInUse", Result::kErrAllPartInUse)
.value("kErrPartitionOccupied", Result::kErrPartitionOccupied)
.value("kErrHbNoNode", Result::kErrHbNoNode)
.value("kErrDuplicatePartition", Result::kErrDuplicatePartition)
.value("kErrCertificateFailure", Result::kErrCertificateFailure)
.value("kErrServerOverflow", Result::kErrServerOverflow)
.value("kErrConsumeGroupForbidden", Result::kErrConsumeGroupForbidden)
.value("kErrConsumeSpeedLimit", Result::kErrConsumeSpeedLimit)
.value("kErrConsumeContentForbidden", Result::kErrConsumeContentForbidden)
.export_values();

py::class_<ErrorCode>(m, "ErrorCode")
.def(py::init<>())
.def(py::init<int, const string&>())
.def("clear", &ErrorCode::Clear)
.def("assign", &ErrorCode::Assign)
.def("value", &ErrorCode::Value)
.def("message", &ErrorCode::Message);
}
Loading

0 comments on commit f64863d

Please sign in to comment.