Skip to content

Commit f7dccad

Browse files
authored
Adding the option for force overwriting in cobrawap init (#110)
1 parent 540204d commit f7dccad

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

cobrawap/__main__.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,21 @@ def get_parser():
9191
type=Path,
9292
default=None,
9393
help="directory where the analysis output is stored "
94-
"[default: '~/cobrawap_output/']",
94+
"[default: '~/cobrawap_output/']",
9595
)
9696
CLI_init.add_argument(
9797
"--config_path",
9898
type=Path,
9999
default=None,
100100
help="directory where the analysis config files are "
101-
"stored [default: '~/cobrawap_config/']",
101+
"stored [default: '~/cobrawap_config/']",
102+
)
103+
CLI_init.add_argument(
104+
"-F",
105+
"--force_overwrite",
106+
action="store_true",
107+
help="force the initialization, overwriting previous settings"
108+
"when already present",
102109
)
103110

104111
# Show Settings
@@ -128,23 +135,23 @@ def get_parser():
128135
type=Path,
129136
default=None,
130137
help="name of the data specific loading script "
131-
"(in <config_path>/stage01_data_entry/scripts/)",
138+
"(in <config_path>/stage01_data_entry/scripts/)",
132139
)
133140
CLI_create.add_argument(
134141
"--profile",
135142
type=str,
136143
nargs="?",
137144
default=None,
138145
help="profile name of this dataset/application "
139-
"(see profile name conventions in documentation)",
146+
"(see profile name conventions in documentation)",
140147
)
141148
CLI_create.add_argument(
142149
"--parent_profile",
143150
type=str,
144151
nargs="?",
145152
default=None,
146153
help="optional parent profile name "
147-
"(see profile name conventions in documentation)",
154+
"(see profile name conventions in documentation)",
148155
)
149156

150157
# Additional configurations
@@ -159,7 +166,7 @@ def get_parser():
159166
nargs="?",
160167
default=None,
161168
help="profile name of this dataset/application "
162-
"(see profile name conventions in documentation)",
169+
"(see profile name conventions in documentation)",
163170
)
164171
CLI_profile.add_argument(
165172
"--stages",
@@ -175,15 +182,15 @@ def get_parser():
175182
nargs="?",
176183
default=None,
177184
help="optional parent profile name from which to "
178-
"initialize the new config "
179-
"[default: basic template]",
185+
"initialize the new config "
186+
"[default: basic template]",
180187
)
181188

182189
# Run
183190
CLI_run = subparsers.add_parser(
184191
"run",
185192
help="run the analysis pipeline on the selected "
186-
"input and with the specified configurations",
193+
"input and with the specified configurations",
187194
)
188195
CLI_run.set_defaults(command="run")
189196
CLI_run.add_argument(
@@ -292,7 +299,7 @@ def main():
292299
return None
293300

294301

295-
def initialize(output_path=None, config_path=None, **kwargs):
302+
def initialize(output_path=None, config_path=None, force_overwrite=False, **kwargs):
296303
# set output_path
297304
if output_path is None:
298305
output_path = (
@@ -307,7 +314,8 @@ def initialize(output_path=None, config_path=None, **kwargs):
307314
if not output_path.is_dir():
308315
raise ValueError(f"{output_path} is not a valid directory!")
309316

310-
set_setting(dict(output_path=str(output_path)))
317+
set_setting(dict(output_path=str(output_path)),
318+
force_overwrite=force_overwrite)
311319

312320
# set config_path
313321
if config_path is None:
@@ -323,18 +331,21 @@ def initialize(output_path=None, config_path=None, **kwargs):
323331
if not config_path.is_dir():
324332
raise ValueError(f"{config_path} is not a valid directory!")
325333

326-
set_setting(dict(config_path=str(config_path)))
334+
set_setting(dict(config_path=str(config_path)),
335+
force_overwrite=force_overwrite)
327336

328337
# set pipeline path
329338
pipeline_path = Path(inspect.getfile(lambda: None)).parent / "pipeline"
330-
set_setting(dict(pipeline_path=str(pipeline_path.resolve())))
339+
set_setting(dict(pipeline_path=str(pipeline_path.resolve())),
340+
force_overwrite=force_overwrite)
331341

332342
# set available stages
333-
set_setting(dict(stages=get_initial_available_stages()))
343+
set_setting(dict(stages=get_initial_available_stages()),
344+
force_overwrite=force_overwrite)
334345
stages = get_setting("stages")
335346

336347
# populate config_path with template config files
337-
if any(config_path.iterdir()):
348+
if any(config_path.iterdir()) and not force_overwrite:
338349
overwrite = (
339350
input(
340351
f"The config directory {config_path} already exists "

cobrawap/pipeline/utils/snakefile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def get_setting(key: str = None):
3030
return settings[key]
3131

3232

33-
def set_setting(setting: dict) -> None:
33+
def set_setting(setting: dict, force_overwrite=False) -> None:
3434
if type(setting) != dict:
3535
raise TypeError('Function expects a dictionary!')
3636

@@ -48,7 +48,7 @@ def set_setting(setting: dict) -> None:
4848
settings = {}
4949

5050
key_overlap = [k for k in setting.keys() if k in settings.keys()]
51-
if key_overlap:
51+
if key_overlap and not force_overwrite:
5252
overwrite = (input(f"There are already settings for {key_overlap}! "\
5353
"Overwrite? [y/N]").lower() == 'y'
5454
or False)

0 commit comments

Comments
 (0)