Skip to content

Commit

Permalink
python version agnostic method to get package files
Browse files Browse the repository at this point in the history
  • Loading branch information
daler committed Apr 19, 2024
1 parent 2f3a545 commit a6fe73e
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions trackhub/trackhub_from_excel
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env python

import sys
import os
import importlib.resources
import trackhub
from collections import defaultdict
Expand Down Expand Up @@ -585,10 +587,13 @@ def create_example(filename):
This uses the example file that is shipped with the trackhub package, and
adds two new sheets to include absolute paths to the example data.
"""
# Find the package's data directory
data = importlib.resources.files("trackhub") / "test" / "data"

wb = load_workbook(data / "example.xlsx", data_only=True)
# In Python >=3.9, importlib.resources.files would work, but the method
# used here is agnostic to Python versions.
example_file = os.path.join(
os.path.dirname(sys.modules['trackhub'].__file),
"example.xlsx"
)
wb = load_workbook(example_file, data_only=True)

bigbeds = wb.create_sheet("bigbeds")
bigbeds.append(
Expand Down

0 comments on commit a6fe73e

Please sign in to comment.