aboutsummaryrefslogtreecommitdiff
path: root/src/riscv_opcodes/resources.py
diff options
context:
space:
mode:
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")