Skip to content

Commit b6eec52

Browse files
committed
Add helper functions to get data paths
1 parent 82fe31e commit b6eec52

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

nemoguardrails/utils.py

+30
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
# limitations under the License.
1515
import asyncio
1616
import dataclasses
17+
import importlib.resources as pkg_resources
1718
import json
19+
import os
1820
import uuid
1921
from collections import namedtuple
2022
from datetime import datetime, timezone
@@ -218,3 +220,31 @@ def get_or_create_event_loop():
218220
asyncio.set_event_loop(loop)
219221

220222
return loop
223+
224+
225+
def get_data_path(package_name: str, file_path: str) -> str:
226+
"""Helper to get the path to the data directory."""
227+
try:
228+
# Try to get the path from the package resources
229+
path = pkg_resources.files(package_name).joinpath(file_path)
230+
if path.exists():
231+
return str(path)
232+
except FileNotFoundError:
233+
pass
234+
235+
# If that fails, try to get the path from the local file system
236+
path = os.path.normpath(os.path.join(os.path.dirname(__file__), "..", file_path))
237+
if os.path.exists(path):
238+
return path
239+
240+
raise FileNotFoundError(f"File not found: {file_path}")
241+
242+
243+
def get_examples_data_path(file_path: str) -> str:
244+
"""Helper to get the path to the examples data directory."""
245+
return get_data_path("nemoguardrails", f"examples/{file_path}")
246+
247+
248+
def get_chat_ui_data_path(file_path: str) -> str:
249+
"""Helper to get the path to the chat-ui data directory."""
250+
return get_data_path("nemoguardrails", f"chat-ui/{file_path}")

0 commit comments

Comments
 (0)