Skip to content

Commit

Permalink
bazel python
Browse files Browse the repository at this point in the history
  • Loading branch information
BYVoid committed Jul 30, 2024
1 parent 1698ae5 commit b2c912b
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/bazel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
- uses: actions/checkout@v4
- uses: bazelbuild/setup-bazelisk@v3
- run: bazel build //:opencc
- run: bazel test --test_output=all //src/... //data/... //test/...
- run: bazel test --test_output=all //src/... //data/... //test/... //python/...
14 changes: 14 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,21 @@ 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 = "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.

10 changes: 10 additions & 0 deletions python/opencc/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
load("@rules_python//python:py_library.bzl", "py_library")

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

py_library(
name = "opencc",
srcs = ["__init__.py"],
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
19 changes: 19 additions & 0 deletions python/tests/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load("@pip//:requirements.bzl", "requirement")
load("@rules_python//python:py_test.bzl", "py_test")

py_test(
name = "test_opencc",
srcs = ["test_opencc.py"],
data = [
"//data/config",
"//data/dictionary:binary_dictionaries",
"//data/dictionary:text_dictionaries",
"//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

0 comments on commit b2c912b

Please sign in to comment.