From 9bd8b8b594f6ca2dbc89c895b579b849b19ebe89 Mon Sep 17 00:00:00 2001 From: Yax <1949284+kianby@users.noreply.github.com> Date: Mon, 9 Dec 2024 17:18:23 +0100 Subject: [PATCH] Package resources --- Makefile | 2 ++ pyproject.toml | 13 +++++++++++++ src/stacosys/i18n/messages.py | 11 +++++++---- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 386eb06..9da87cf 100644 --- a/Makefile +++ b/Makefile @@ -30,6 +30,8 @@ test: # build build: + # https://stackoverflow.com/questions/24347450/how-do-you-add-additional-files-to-a-wheel + rm -rf build/* dist/* *.egg-info uv build --wheel --out-dir dist docker build -t kianby/stacosys . diff --git a/pyproject.toml b/pyproject.toml index 590c7f6..dd0af66 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,3 +26,16 @@ dev = [ "pytest>=8.3.4", "black>=24.10.0", ] + +[tool.setuptools] +package-dir = { "" = "src" } # Specify the root directory for packages +packages = ["stacosys"] + +[tool.setuptools.package-data] +# Include `.properties` and `.html` files in the specified directories +"stacosys.i18n" = ["*.properties"] +"stacosys.interface.templates" = ["*.html"] + +[build-system] +requires = ["setuptools>=61.0"] +build-backend = "setuptools.build_meta" diff --git a/src/stacosys/i18n/messages.py b/src/stacosys/i18n/messages.py index a92113a..375506f 100644 --- a/src/stacosys/i18n/messages.py +++ b/src/stacosys/i18n/messages.py @@ -1,6 +1,6 @@ import configparser import os - +import importlib.resources class Messages: def __init__(self): @@ -8,9 +8,12 @@ class Messages: def load_messages(self, lang): config = configparser.ConfigParser() - config.read( - os.path.join(os.path.dirname(__file__), "messages_" + lang + ".properties") - ) + + # Access the resource file within the package + with importlib.resources.open_text( + __package__, f"messages_{lang}.properties" + ) as file: + config.read_file(file) for key, value in config.items("messages"): self.property_dict[key] = value