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

Add Bazel Python library and test. #882

Merged
merged 5 commits into from
Jul 30, 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
6 changes: 5 additions & 1 deletion .github/workflows/bazel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: bazelbuild/setup-bazelisk@v3
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- run: bazel build //:opencc
- run: bazel test --test_output=all //src/... //data/... //test/...
- run: bazel test --test_output=all //src/... //data/... //test/... //python/...
10 changes: 10 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
load("@rules_cc//cc:defs.bzl", "cc_library")
load("@rules_python//python:py_library.bzl", "py_library")

package(default_visibility = ["//visibility:public"])

cc_library(
Expand All @@ -17,3 +20,10 @@ cc_library(
"//src:opencc",
],
)

py_library(
name = "py_opencc",
deps = [
"//python/opencc",
],
)
15 changes: 15 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,22 @@ module(
bazel_dep(name = "darts-clone", version = "0.32")
bazel_dep(name = "googletest", version = "1.15.0", dev_dependency = True)
bazel_dep(name = "marisa-trie", version = "0.2.6")
bazel_dep(name = "platforms", version = "0.0.10")
bazel_dep(name = "pybind11_bazel", version = "2.12.0")
bazel_dep(name = "rapidjson", version = "1.1.0")
bazel_dep(name = "rules_cc", version = "0.0.9")
bazel_dep(name = "rules_python", version = "0.34.0")
bazel_dep(name = "tclap", version = "1.2.5")


python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(
python_version = "3.12",
)
pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
pip.parse(
hub_name = "pip",
python_version = "3.12",
requirements_lock = "//python/tests:requirements_lock.txt",
)
use_repo(pip, "pip")
31 changes: 31 additions & 0 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion data/scripts/find_target.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
Expand Down
2 changes: 1 addition & 1 deletion data/scripts/merge.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import codecs
Expand Down
2 changes: 1 addition & 1 deletion data/scripts/reverse.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
Expand Down
2 changes: 1 addition & 1 deletion data/scripts/sort.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys

Expand Down
2 changes: 1 addition & 1 deletion data/scripts/sort_all.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import glob
import sys
Expand Down
15 changes: 15 additions & 0 deletions python/opencc/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load("@rules_python//python:py_library.bzl", "py_library")

package(default_visibility = ["//visibility:public"])

py_library(
name = "opencc",
srcs = ["__init__.py"],
data = [
"//data/config",
"//data/dictionary:binary_dictionaries",
"//data/dictionary:text_dictionaries",
],
imports = [".."],
deps = ["//src:py_opencc"],
)
17 changes: 13 additions & 4 deletions python/opencc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
import os
import sys

from opencc.clib import opencc_clib
try:
import opencc_clib
except ImportError:
from opencc.clib import opencc_clib

__all__ = ['OpenCC', 'CONFIGS', '__version__']

__version__ = opencc_clib.__version__
_thisdir = os.path.dirname(os.path.abspath(__file__))
_opencc_share_dir = os.path.join(_thisdir, 'clib', 'share', 'opencc')
_this_dir = os.path.dirname(os.path.abspath(__file__))
_opencc_share_dir = os.path.join(_this_dir, 'clib', 'share', 'opencc')
_opencc_rootdir = os.path.abspath(os.path.join(_this_dir, '..', '..'))
_opencc_configdir = os.path.join(_opencc_rootdir, 'data', 'config')

if sys.version_info.major == 2:
text_type = unicode # noqa
Expand All @@ -18,6 +23,8 @@

if os.path.isdir(_opencc_share_dir):
CONFIGS = [f for f in os.listdir(_opencc_share_dir) if f.endswith('.json')]
elif os.path.isdir(_opencc_configdir):
CONFIGS = [f for f in os.listdir(_opencc_configdir) if f.endswith('.json')]
else:
CONFIGS = []

Expand All @@ -39,7 +46,9 @@ def __init__(self, config='t2s'):
if not config.endswith('.json'):
config += '.json'
if not os.path.isfile(config):
config = os.path.join(_opencc_share_dir, config)
config_under_share_dir = os.path.join(_opencc_share_dir, config)
if os.path.isfile(config_under_share_dir):
config = config_under_share_dir
super(OpenCC, self).__init__(config)
self.config = config

Expand Down
16 changes: 16 additions & 0 deletions python/tests/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
load("@pip//:requirements.bzl", "requirement")
load("@rules_python//python:py_test.bzl", "py_test")

py_test(
name = "test_opencc",
srcs = ["test_opencc.py"],
data = [
"//test/testcases",
],
imports = [".."],
deps = [
"//python/opencc",
requirement("pytest"),
requirement("exceptiongroup"),
],
)
5 changes: 5 additions & 0 deletions python/tests/requirements_lock.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exceptiongroup==1.2.2
iniconfig==2.0.0
packaging==24.1
pluggy==1.5.0
pytest==8.3.2
7 changes: 7 additions & 0 deletions python/tests/test_opencc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from __future__ import unicode_literals

import os
import pytest
import sys

from glob import glob

_this_dir = os.path.dirname(os.path.abspath(__file__))
Expand Down Expand Up @@ -39,3 +42,7 @@ def test_conversion():
for text, ans in zip(intexts, anstexts):
assert converter.convert(text) == ans, \
'Failed to convert {} for {} -> {}'.format(pref, text, ans)


if __name__ == "__main__":
sys.exit(pytest.main(sys.argv[1:]))
14 changes: 14 additions & 0 deletions src/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
load("@pybind11_bazel//:build_defs.bzl", "pybind_extension")
load("@rules_cc//cc:defs.bzl", "cc_library")
load("@rules_python//python:py_library.bzl", "py_library")

package(default_visibility = ["//visibility:public"])

Expand Down Expand Up @@ -328,6 +330,18 @@ cc_library(
],
)

pybind_extension(
name = "opencc_clib",
srcs = ["py_opencc.cpp"],
deps = [":opencc"],
)

py_library(
name = "py_opencc",
data = [":opencc_clib"],
imports = ["."],
)

cc_library(
name = "segmentation",
srcs = ["Segmentation.cpp"],
Expand Down