Skip to content

Commit 8cb8b57

Browse files
authored
refactor: extract INI creation into shared ParameterManager.create_ini() (#327)
- Add create_ini() method to ParameterManager for creating TOPP tool ini files - Update save_parameters() to use create_ini() instead of skipping missing ini files - Update input_TOPP() in StreamlitUI to use the shared method - Removes code duplication and enables ini creation during parameter save
1 parent ae20b2c commit 8cb8b57

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

src/workflow/ParameterManager.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pyopenms as poms
22
import json
33
import shutil
4+
import subprocess
45
import streamlit as st
56
from pathlib import Path
67

@@ -25,6 +26,25 @@ def __init__(self, workflow_dir: Path):
2526
self.param_prefix = f"{workflow_dir.stem}-param-"
2627
self.topp_param_prefix = f"{workflow_dir.stem}-TOPP-"
2728

29+
def create_ini(self, tool: str) -> bool:
30+
"""
31+
Create an ini file for a TOPP tool if it doesn't exist.
32+
33+
Args:
34+
tool: Name of the TOPP tool (e.g., "CometAdapter")
35+
36+
Returns:
37+
True if ini file exists (created or already existed), False if creation failed
38+
"""
39+
ini_path = Path(self.ini_dir, tool + ".ini")
40+
if ini_path.exists():
41+
return True
42+
try:
43+
subprocess.call([tool, "-write_ini", str(ini_path)])
44+
except FileNotFoundError:
45+
return False
46+
return ini_path.exists()
47+
2848
def save_parameters(self) -> None:
2949
"""
3050
Saves the current parameters from Streamlit's session state to a JSON file.
@@ -54,11 +74,15 @@ def save_parameters(self) -> None:
5474
)
5575
# for each TOPP tool, open the ini file
5676
for tool in current_topp_tools:
77+
if not self.create_ini(tool):
78+
# Could not create ini file - skip this tool
79+
continue
80+
ini_path = Path(self.ini_dir, f"{tool}.ini")
5781
if tool not in json_params:
5882
json_params[tool] = {}
5983
# load the param object
6084
param = poms.Param()
61-
poms.ParamXMLFile().load(str(Path(self.ini_dir, f"{tool}.ini")), param)
85+
poms.ParamXMLFile().load(str(ini_path), param)
6286
# get all session state param keys and values for this tool
6387
for key, value in st.session_state.items():
6488
if key.startswith(f"{self.topp_param_prefix}{tool}:1:"):

src/workflow/StreamlitUI.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -641,12 +641,11 @@ def input_TOPP(
641641

642642
# write defaults ini files
643643
ini_file_path = Path(self.parameter_manager.ini_dir, f"{topp_tool_name}.ini")
644-
if not ini_file_path.exists():
645-
try:
646-
subprocess.call([topp_tool_name, "-write_ini", str(ini_file_path)])
647-
except FileNotFoundError:
648-
st.error(f"TOPP tool **'{topp_tool_name}'** not found.")
649-
return
644+
ini_existed = ini_file_path.exists()
645+
if not self.parameter_manager.create_ini(topp_tool_name):
646+
st.error(f"TOPP tool **'{topp_tool_name}'** not found.")
647+
return
648+
if not ini_existed:
650649
# update custom defaults if necessary
651650
if custom_defaults:
652651
param = poms.Param()

0 commit comments

Comments
 (0)