Skip to content

Commit 4eaef84

Browse files
committed
More robust and efficient handling of files in data_loader
1 parent 091ccac commit 4eaef84

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

data_loader.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@
88
from httpx import Client
99

1010

11-
def load_data(data_dir: str, filename: str) -> dict[str, Any]:
11+
def load_data(filepath: str) -> dict[str, Any]:
1212
"""Load json data from a file within the specified data directory."""
13-
filepath = os.path.join(data_dir, filename)
14-
if not os.path.exists(filepath):
13+
try:
14+
with open(filepath, "rb") as file:
15+
return orjson.loads(file.read())
16+
except FileNotFoundError as e:
1517
click.secho(f"File not found: {filepath}", fg="red", err=True)
16-
raise click.Abort()
17-
with open(filepath, "rb") as file:
18-
return orjson.loads(file.read())
18+
raise click.Abort() from e
1919

2020

2121
def load_collection(client: Client, collection_id: str, data_dir: str) -> None:
2222
"""Load a STAC collection into the database."""
23-
collection = load_data(data_dir, "collection.json")
23+
collection = load_data(os.path.join(data_dir, "collection.json"))
2424
collection["id"] = collection_id
2525
resp = client.post("/collections", json=collection)
2626
if resp.status_code == 200 or resp.status_code == 201:
@@ -38,22 +38,29 @@ def load_items(
3838
client: Client, collection_id: str, use_bulk: bool, data_dir: str
3939
) -> None:
4040
"""Load STAC items into the database based on the method selected."""
41-
# Attempt to dynamically find a suitable feature collection file
42-
feature_files = [
43-
file
44-
for file in os.listdir(data_dir)
45-
if file.endswith(".json") and file != "collection.json"
46-
]
47-
if not feature_files:
41+
with os.scandir(data_dir) as entries:
42+
# Attempt to dynamically find a suitable feature collection file
43+
# Use the first found feature collection file
44+
feature_file = next(
45+
(
46+
entry.path
47+
for entry in entries
48+
if entry.is_file()
49+
and entry.name.endswith(".json")
50+
and entry.name != "collection.json"
51+
),
52+
None,
53+
)
54+
55+
if feature_file is None:
4856
click.secho(
4957
"No feature collection files found in the specified directory.",
5058
fg="red",
5159
err=True,
5260
)
5361
raise click.Abort()
5462

55-
# Use the first found feature collection file
56-
feature_collection = load_data(data_dir, feature_files[0])
63+
feature_collection = load_data(feature_file)
5764

5865
load_collection(client, collection_id, data_dir)
5966
if use_bulk:

0 commit comments

Comments
 (0)