Skip to content

Commit 27ea84f

Browse files
author
Uri Neri
committed
Add support for environmental variables in rpconfig.json
1 parent b4e37bb commit 27ea84f

File tree

3 files changed

+62
-7
lines changed

3 files changed

+62
-7
lines changed

src/rolypoly/rolypoly.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,35 @@
1919
# load rolypoly config
2020
with resources.files("rolypoly").joinpath("rpconfig.json").open("r") as conff:
2121
config = load(conff)
22-
data_dir = config["ROLYPOLY_DATA"]
22+
23+
24+
def resolve_data_dir(configured_path: str) -> str:
25+
"""Resolve data dir only when path starts with a leading $ENV_VAR token."""
26+
if not configured_path.startswith("$"):
27+
return configured_path
28+
29+
if configured_path.startswith("${"):
30+
closing = configured_path.find("}")
31+
if closing == -1:
32+
return configured_path
33+
var_name = configured_path[2:closing]
34+
suffix = configured_path[closing + 1 :]
35+
else:
36+
slash_index = configured_path.find("/")
37+
if slash_index == -1:
38+
var_name = configured_path[1:]
39+
suffix = ""
40+
else:
41+
var_name = configured_path[1:slash_index]
42+
suffix = configured_path[slash_index:]
43+
44+
env_value = os.environ.get(var_name)
45+
if not env_value:
46+
return configured_path
47+
return f"{env_value}{suffix}"
48+
49+
50+
data_dir = resolve_data_dir(config["ROLYPOLY_DATA"])
2351
os.environ["ROLYPOLY_DATA"] = data_dir # export to env just in case
2452
os.environ["ROLYPOLY_DATA_DIR"] = data_dir # export to env just in case x2
2553
ROLYPOLY_REMIND_CITATIONS = config["ROLYPOLY_REMIND_CITATIONS"]
@@ -82,9 +110,8 @@
82110
"fastx-stats": "rolypoly.commands.misc.fastx_stats.fastx_stats",
83111
"fastx-calc": "rolypoly.commands.misc.fastx_calc.fastx_calc",
84112
"rename-seqs": "rolypoly.commands.misc.rename_seqs.rename_seqs",
85-
# "visualize": "rolypoly.commands.virotype.visualize.visualize",
86113
"quick-taxonomy": "rolypoly.commands.misc.quick_taxonomy.quick_taxonomy",
87-
# "test": "tests.test_cli_commands.test",
114+
"help": "rolypoly.rolypoly.rolypoly",
88115
},
89116
},
90117
"bining": {

src/rolypoly/rpconfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
2-
"ROLYPOLY_DATA": "<REPO_PATH>/data/",
3-
"ROLYPOLY_TEST_DIR": "/REDACTED_HPC_PATH/tests/rp_tests/",
2+
"ROLYPOLY_DATA": "$RP_DIR/data/",
43
"ROLYPOLY_REMIND_CITATIONS": "False"
54
}

src/rolypoly/utils/logging/loggit.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,34 @@
1010
ROLYPOLY_HANDLER_ATTR = "rolypoly_handler_type"
1111

1212

13+
def resolve_config_path(raw_path: str) -> Path:
14+
"""Resolve config paths only when they start with a leading $ENV_VAR token."""
15+
import os
16+
17+
if not raw_path.startswith("$"):
18+
return Path(raw_path)
19+
20+
if raw_path.startswith("${"):
21+
closing = raw_path.find("}")
22+
if closing == -1:
23+
return Path(raw_path)
24+
var_name = raw_path[2:closing]
25+
suffix = raw_path[closing + 1 :]
26+
else:
27+
slash_index = raw_path.find("/")
28+
if slash_index == -1:
29+
var_name = raw_path[1:]
30+
suffix = ""
31+
else:
32+
var_name = raw_path[1:slash_index]
33+
suffix = raw_path[slash_index:]
34+
35+
env_value = os.environ.get(var_name)
36+
if not env_value:
37+
return Path(raw_path)
38+
return Path(f"{env_value}{suffix}")
39+
40+
1341
def get_version_info() -> dict[str, str]:
1442
"""Get the current version of RolyPoly (code and data).
1543
Returns a dictionary with the following keys:
@@ -53,7 +81,7 @@ def get_version_info() -> dict[str, str]:
5381
if config_path.exists():
5482
with config_path.open("r") as f:
5583
config = json.load(f)
56-
data_dir_path = Path(config["ROLYPOLY_DATA"])
84+
data_dir_path = resolve_config_path(config["ROLYPOLY_DATA"])
5785
if data_dir_path.exists():
5886
with open(data_dir_path / "README.md", "r") as f:
5987
for line in f:
@@ -227,7 +255,7 @@ def resolve_datadir() -> Path:
227255
if cfg_path.exists():
228256
with cfg_path.open() as fh:
229257
cfg = json.load(fh)
230-
data_dir = Path(cfg.get("ROLYPOLY_DATA", ""))
258+
data_dir = resolve_config_path(cfg.get("ROLYPOLY_DATA", ""))
231259
if data_dir and data_dir.exists():
232260
return data_dir
233261
except Exception:
@@ -242,3 +270,4 @@ def resolve_datadir() -> Path:
242270

243271
# final fallback to cwd --- Probably in ipython / creating a data release?
244272
return Path.cwd()
273+

0 commit comments

Comments
 (0)