Skip to content

Commit

Permalink
NF: run: Support globbing in --batch-parameter values
Browse files Browse the repository at this point in the history
This is useful for generating batch parameters based on a set of file
names.
  • Loading branch information
kyleam committed Jun 19, 2019
1 parent 6ad7246 commit d24c301
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
12 changes: 10 additions & 2 deletions reproman/interface/run.py
Expand Up @@ -11,6 +11,7 @@

from argparse import REMAINDER
import collections
import glob
import logging
import itertools
import textwrap
Expand Down Expand Up @@ -77,6 +78,9 @@ def _parse_batch_params(params):
-------
A generator that, for each key, yields a list of key-value tuple pairs.
"""
def maybe_glob(x):
return glob.glob(x) if glob.has_magic(x) else [x]

seen_keys = set()
for param in params:
if "=" not in param:
Expand All @@ -86,7 +90,9 @@ def _parse_batch_params(params):
if key in seen_keys:
raise ValueError("Key '{}' was given more than once".format(key))
seen_keys.add(key)
yield [(key, v) for v in value_str.split(",")]
yield [(key, v)
for v_unexpanded in value_str.split(",")
for v in maybe_glob(v_unexpanded)]


def _combine_batch_params(params):
Expand Down Expand Up @@ -172,7 +178,9 @@ def _resolve_batch_parameters(spec_file, params):
"""Define batch parameters with 'KEY=val1,val2,...'. Different keys
can be specified by giving multiple values, in which case the product
of the values are taken. For example, 'subj=mei,satsuki' and 'day=1,2'
would expand to four records, pairing each subj with each day."""),
would expand to four records, pairing each subj with each day. Values
can be a glob pattern to match against the current working
directory."""),
("inputs, outputs",
"""Input and output files (list) to the command."""),
("message",
Expand Down
14 changes: 14 additions & 0 deletions reproman/interface/tests/test_run.py
Expand Up @@ -106,6 +106,20 @@ def test_combine_batch_params(params, expected):
assert actual == expected


def test_combine_batch_params_glob(tmpdir):
tmpdir = str(tmpdir)
create_tree(tmpdir, {"aaa": "a",
"subdir": {"b": "b", "c": "c"}})
with chpwd(tmpdir):
res = sorted(_combine_batch_params(["foo=a*,subdir/*,other"]),
key=lambda d: d["foo"])
assert list(res) == [
{"foo": "aaa"},
{"foo": "other"},
{"foo": "subdir/b"},
{"foo": "subdir/c"}]


def test_combine_batch_params_repeat_key():
with pytest.raises(ValueError):
list(_combine_batch_params(["a=1", "a=2"]))
Expand Down

0 comments on commit d24c301

Please sign in to comment.