Skip to content

Commit a4793fe

Browse files
undutetron
authored andcommitted
Generate nameroot and nameext from basename (#420)
* [PATCH 1/3] generating nameroot when not provided explicitely from basename Issue#268 * [PATCH 2/3] Calculate correctly nameroot from basename Additionally only generate it for Files, not Directories (following specification). And also generate nameext. * [PATCH 3/3] Fix existing test to conform to generated fields * fix: generate nameext when nameroot is not missing * new: test behaviour of nameroot and nameext generation * fix: make nameext to conform to specs * fix: generate values from basename unconditionally
1 parent 794b317 commit a4793fe

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

cwltool/pathmapper.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ def addLocation(d):
7777
if "basename" not in d:
7878
d["basename"] = os.path.basename(urllib.request.url2pathname(path))
7979

80+
if d["class"] == "File":
81+
d["nameroot"], d["nameext"] = os.path.splitext(d["basename"])
82+
8083
visit_class(job, ("File", "Directory"), addLocation)
8184

8285

tests/test_examples.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,16 @@ def loadref(base, p):
209209

210210
self.assertEquals([{
211211
"basename": "bar.cwl",
212+
"nameroot": "bar",
212213
"class": "File",
214+
"nameext": ".cwl",
213215
"location": "file:///example/bar.cwl"
214216
},
215217
{
216218
"basename": "data.txt",
219+
"nameroot": "data",
217220
"class": "File",
221+
"nameext": ".txt",
218222
"location": "file:///example/data.txt"
219223
},
220224
{
@@ -223,17 +227,23 @@ def loadref(base, p):
223227
"location": "file:///example/data2",
224228
"listing": [{
225229
"basename": "data3.txt",
230+
"nameroot": "data3",
226231
"class": "File",
232+
"nameext": ".txt",
227233
"location": "file:///example/data3.txt",
228234
"secondaryFiles": [{
229235
"class": "File",
230236
"basename": "data5.txt",
231-
"location": "file:///example/data5.txt"
237+
"location": "file:///example/data5.txt",
238+
"nameext": ".txt",
239+
"nameroot": "data5"
232240
}]
233241
}]
234242
}, {
235243
"basename": "data4.txt",
244+
"nameroot": "data4",
236245
"class": "File",
246+
"nameext": ".txt",
237247
"location": "file:///example/data4.txt"
238248
}], sc)
239249

@@ -245,7 +255,9 @@ def loadref(base, p):
245255

246256
self.assertEquals([{
247257
"basename": "bar.cwl",
258+
"nameroot": "bar",
248259
"class": "File",
260+
"nameext": ".cwl",
249261
"location": "file:///example/bar.cwl"
250262
}], sc)
251263

tests/test_pathmapper.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,30 @@ def test_strip_trailing(self):
2626
"basename": "bar"
2727
},
2828
d)
29+
30+
def test_basename_field_generation(self):
31+
base_file = {
32+
"class": "File",
33+
"location": "/foo/"
34+
}
35+
# (filename, expected: (nameroot, nameext))
36+
testdata = [
37+
("foo.bar", ("foo", ".bar")),
38+
("foo", ("foo", '')),
39+
(".foo", (".foo", '')),
40+
("foo.", ("foo", '.')),
41+
("foo.bar.baz", ("foo.bar", ".baz"))
42+
]
43+
44+
for filename, (nameroot, nameext) in testdata:
45+
file = dict(base_file)
46+
file["location"] = file["location"] + filename
47+
48+
expected = dict(file)
49+
expected["basename"] = filename
50+
expected["nameroot"] = nameroot
51+
expected["nameext"] = nameext
52+
53+
normalizeFilesDirs(file)
54+
self.assertEqual(file, expected)
55+

0 commit comments

Comments
 (0)