aboutsummaryrefslogtreecommitdiff
path: root/src/riscv_opcodes/resources.py
diff options
context:
space:
mode:
authorAndrew Waterman <andrew@sifive.com>2025-10-13 16:24:22 -0700
committerGitHub <noreply@github.com>2025-10-13 16:24:22 -0700
commit26e2c04c913a67ac51bf0a354f21f2d7d5c07c40 (patch)
tree7db4f4637f71f1777f86702a2228ccf3b9217a03 /src/riscv_opcodes/resources.py
parent433081c02368eb72e6dea5413e404a8ef231658e (diff)
downloadriscv-opcodes-master.zip
riscv-opcodes-master.tar.gz
riscv-opcodes-master.tar.bz2
Reorganise Python code using pyproject.toml (#378)HEADmaster
Add pyproject.toml (the modern alternative to requirements.txt), making this a proper Python package that can be installed via pip and potentially uploaded to PyPI. This also loads the files using `importlib.resources` and installs them into the wheel. This means that when you create a wheel using `uv build` it will still be able to load all the opcodes and CSV files. To avoid moving those resource files in the source repo, the Python build backend (hatchling) is instructed to move them to the right place when building a wheel, and the `resource_root()` function checks in both places so it always works. This is a little hacky but it works. CI builds source and binary wheels (not actually binary) that can be uploaded to PyPI. If we do upload them then using this project is as simple as ``` uvx riscv_opcodes -c 'rv*' ``` Co-authored-by: Tim Hutt <timothy.hutt@codasip.com>
Diffstat (limited to 'src/riscv_opcodes/resources.py')
-rw-r--r--src/riscv_opcodes/resources.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/riscv_opcodes/resources.py b/src/riscv_opcodes/resources.py
new file mode 100644
index 0000000..e9398ec
--- /dev/null
+++ b/src/riscv_opcodes/resources.py
@@ -0,0 +1,39 @@
+import sys
+from importlib.resources import files
+from typing import IO
+
+if sys.version_info < (3, 12):
+ # This was deprecated in Python 3.12.
+ from importlib.abc import Traversable
+else:
+ from importlib.resources.abc import Traversable
+
+
+def resource_root() -> Traversable:
+ """
+ Return the root directory as a traversable that can
+ be used to load the `extensions`, `*.csv` and `encoding.h`
+ files. For historical reasons these are not stored inside
+ the `src/riscv_opcodes` directory in the source distribution
+ but they are moved there when generating the binary wheel.
+ This means we need to check in both places.
+ """
+ assert __package__ is not None
+ package_root = files(__package__)
+ if (package_root / "extensions").is_dir():
+ return package_root
+ return package_root / ".." / ".."
+
+
+def read_text_resource(path_relative_to_root: str) -> str:
+ """
+ Read a text file relative to the root of this repo.
+ """
+ return resource_root().joinpath(path_relative_to_root).read_text(encoding="utf-8")
+
+
+def open_text_resource(path_relative_to_root: str) -> IO[str]:
+ """
+ Open a text file relative to the root of this repo.
+ """
+ return resource_root().joinpath(path_relative_to_root).open("r", encoding="utf-8")