Skip to content

Commit eae1794

Browse files
committed
Refactor config and index URL management
- Modify config.py to create default config with minimal structure - Remove hardcoded planetarypy_config.toml from data directory - Create new planetarypy_index_urls.toml with comprehensive mission index URLs - Update index_config.py to handle default config creation more robustly - Improve error handling and logging for config file generation
1 parent 381e8bf commit eae1794

File tree

4 files changed

+123
-131
lines changed

4 files changed

+123
-131
lines changed

src/planetarypy/config.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88

99
import json
1010
import os
11-
import shutil
1211
from functools import reduce
13-
from importlib.resources import files
1412
from pathlib import Path
1513
from typing import Union
1614

@@ -36,17 +34,26 @@ def __init__(self, config_path: str = None): # str or pathlib.Path
3634
if config_path is not None:
3735
self.path = Path(config_path)
3836
if not self.path.exists():
39-
p = files("planetarypy.data").joinpath(self.fname)
40-
shutil.copy(p, self.path)
37+
self._create_default_config()
4138
self._read_config()
4239

40+
def _create_default_config(self):
41+
"""Create a minimal default config file with just storage_root."""
42+
doc = tomlkit.document()
43+
doc.add(tomlkit.comment("PlanetaryPy Configuration"))
44+
doc.add(tomlkit.nl())
45+
doc.add(tomlkit.comment("Root directory for storing all planetarypy data"))
46+
doc.add(tomlkit.nl())
47+
doc["storage_root"] = "" # Empty string will be updated during _read_config
48+
self.path.write_text(tomlkit.dumps(doc))
49+
4350
def _read_config(self):
4451
"""Read the configfile and store config dict.
4552
4653
`storage_root` will be stored as attribute.
4754
"""
4855
self.tomldoc = tomlkit.loads(self.path.read_text())
49-
if not self.tomldoc["storage_root"]:
56+
if not self.tomldoc.get("storage_root"):
5057
path = Path.home() / "planetarypy_data"
5158
path.mkdir(exist_ok=True)
5259
self.tomldoc["storage_root"] = str(path)

src/planetarypy/data/planetarypy_config.toml

Lines changed: 0 additions & 109 deletions
This file was deleted.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# PlanetaryPy Index URLs Configuration
2+
3+
# This file contains URLs for PDS indices, organized by mission and instrument
4+
5+
6+
[missions]
7+
# # Mro Mission
8+
9+
[missions.mro]
10+
# # CTX Instrument
11+
12+
[missions.mro.ctx]
13+
edr = "https://planetarydata.jpl.nasa.gov/img/data/mro/ctx/mrox_5010//index/cumindex.lbl"
14+
15+
# # Latest URL discovered on 2025-03-07
16+
17+
# # HIRISE Instrument
18+
19+
[missions.mro.hirise]
20+
dtm = "https://hirise-pds.lpl.arizona.edu/PDS/INDEX/DTMCUMINDEX.LBL"
21+
edr = "https://hirise-pds.lpl.arizona.edu/PDS/INDEX/EDRCUMINDEX.LBL"
22+
rdr = "https://hirise-pds.lpl.arizona.edu/PDS/INDEX/RDRCUMINDEX.LBL"
23+
# # CRISM Instrument
24+
25+
[missions.mro.crism]
26+
mtrdr = "https://pds-geosciences.wustl.edu/mro/mro-m-crism-5-rdr-mptargeted-v1/mrocr_4001/index/mtrdr0705_index.lbl"
27+
28+
# # Lro Mission
29+
30+
[missions.lro]
31+
# # LROC Instrument
32+
33+
[missions.lro.lroc]
34+
edr = "https://pds.lroc.asu.edu/data/LRO-L-LROC-2-EDR-V1.0/LROLRC_0061C//INDEX/CUMINDEX.LBL"
35+
36+
# # Latest URL discovered on 2025-03-07
37+
38+
# # Latest URL discovered on 2025-03-07
39+
# # DIVINER Instrument
40+
41+
[missions.lro.diviner]
42+
edr1 = "https://pds-geosciences.wustl.edu/lro/lro-l-dlre-2-edr-v1/lrodlr_0001/index/index.lbl"
43+
edr2 = "https://pds-geosciences.wustl.edu/lro/lro-l-dlre-2-edr-v1/lrodlr_0002/index/index.lbl"
44+
rdr1 = "https://pds-geosciences.wustl.edu/lro/lro-l-dlre-4-rdr-v1/lrodlr_1001/index/rdrindex.lbl"
45+
rdr2 = "https://pds-geosciences.wustl.edu/lro/lro-l-dlre-4-rdr-v1/lrodlr_1002/index/rdrindex.lbl"
46+
# # LOLA Instrument
47+
48+
[missions.lro.lola]
49+
edr = "http://pds-geosciences.wustl.edu/lro/lro-l-lola-2-edr-v1/lrolol_0xxx/index/edrindex.lbl"
50+
rdr = "http://pds-geosciences.wustl.edu/lro/lro-l-lola-3-rdr-v1/lrolol_1xxx/index/rdrindex.lbl"
51+
52+
# # Cassini Mission
53+
54+
[missions.cassini]
55+
# # ISS Instrument
56+
57+
[missions.cassini.iss]
58+
index = "https://pds-rings.seti.org/holdings/metadata/COISS_2xxx/COISS_2999/COISS_2999_index.lbl"
59+
inventory = "https://pds-rings.seti.org/holdings/metadata/COISS_2xxx/COISS_2999/COISS_2999_inventory.lbl"
60+
moon_summary = "https://pds-rings.seti.org/holdings/metadata/COISS_2xxx/COISS_2999/COISS_2999_moon_summary.lbl"
61+
ring_summary = "https://pds-rings.seti.org/holdings/metadata/COISS_2xxx/COISS_2999/COISS_2999_ring_summary.lbl"
62+
saturn_summary = "https://pds-rings.seti.org/holdings/metadata/COISS_2xxx/COISS_2999/COISS_2999_saturn_summary.lbl"
63+
# # UVIS Instrument
64+
65+
[missions.cassini.uvis]
66+
index = "https://pds-rings.seti.org/holdings/metadata/COUVIS_0xxx/COUVIS_0999/COUVIS_0999_index.lbl"
67+
moon_summary = "https://pds-rings.seti.org/holdings/metadata/COUVIS_0xxx/COUVIS_0999/COUVIS_0999_moon_summary.lbl"
68+
ring_summary = "https://pds-rings.seti.org/holdings/metadata/COUVIS_0xxx/COUVIS_0999/COUVIS_0999_ring_summary.lbl"
69+
saturn_summary = "https://pds-rings.seti.org/holdings/metadata/COUVIS_0xxx/COUVIS_0999/COUVIS_0999_saturn_summary.lbl"
70+
supplemental_index = "https://pds-rings.seti.org/holdings/metadata/COUVIS_0xxx/COUVIS_0999/COUVIS_0999_supplemental_index.lbl"
71+
versions = "https://pds-rings.seti.org/holdings/metadata/COUVIS_0xxx/COUVIS_0999/COUVIS_0999_versions.lbl"
72+
73+
# # Go Mission
74+
75+
[missions.go]
76+
# # SSI Instrument
77+
78+
[missions.go.ssi]
79+
edr = "https://pds-rings.seti.org/holdings/metadata/GO_0xxx/GO_0999/GO_0999_index.lbl"

src/planetarypy/pds/index_config.py

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import json
55
import logging
66
import os
7+
import shutil
8+
from importlib.resources import files
79
from pathlib import Path
810
from typing import Optional, Union
911

@@ -41,26 +43,39 @@ def __init__(self, config_path: str = None):
4143
self.discover_dynamic_urls()
4244

4345
def _create_default_config(self):
44-
"""Create a default configuration file with empty structure."""
45-
doc = tomlkit.document()
46-
47-
# Add a comment at the top
48-
doc.add(tomlkit.comment("PlanetaryPy Index URLs Configuration"))
49-
doc.add(tomlkit.nl())
50-
doc.add(
51-
tomlkit.comment(
52-
"This file contains URLs for PDS indices, organized by mission and instrument"
46+
"""Create a default configuration file by copying the template from package data."""
47+
try:
48+
# Try to copy the template file from the package's data directory
49+
template_path = files("planetarypy.data").joinpath(self.fname)
50+
shutil.copy(template_path, self.path)
51+
logger.info(
52+
f"Created default index URLs config at {self.path} from template"
53+
)
54+
except (FileNotFoundError, ImportError, shutil.Error) as e:
55+
# If the template file doesn't exist or can't be copied, create a basic structure
56+
logger.warning(
57+
f"Could not copy template file: {e}. Creating basic structure."
58+
)
59+
doc = tomlkit.document()
60+
61+
# Add a comment at the top
62+
doc.add(tomlkit.comment("PlanetaryPy Index URLs Configuration"))
63+
doc.add(tomlkit.nl())
64+
doc.add(
65+
tomlkit.comment(
66+
"This file contains URLs for PDS indices, organized by mission and instrument"
67+
)
5368
)
54-
)
55-
doc.add(tomlkit.nl())
56-
doc.add(tomlkit.nl())
69+
doc.add(tomlkit.nl())
70+
doc.add(tomlkit.nl())
5771

58-
# Add missions table
59-
missions_table = tomlkit.table()
60-
doc["missions"] = missions_table
72+
# Add missions table
73+
missions_table = tomlkit.table()
74+
doc["missions"] = missions_table
6175

62-
# Write the document
63-
self.path.write_text(tomlkit.dumps(doc))
76+
# Write the document
77+
self.path.write_text(tomlkit.dumps(doc))
78+
logger.info(f"Created basic index URLs config at {self.path}")
6479

6580
def _read_config(self):
6681
"""Read the configuration file."""

0 commit comments

Comments
 (0)