Skip to content

Commit 5ff5ec7

Browse files
committed
Add Directory type
This breaks the project for the v1 spec, and makes you use: - cromwell >= 37 - "version development"
1 parent bb27570 commit 5ff5ec7

File tree

3 files changed

+59
-18
lines changed

3 files changed

+59
-18
lines changed

wdlgen/task.py

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,54 +65,82 @@ def get_string(self):
6565

6666
class CommandInput(CommandArgument):
6767
def __init__(self, name: str, optional: bool=False, prefix: str=None, position: int=None,
68-
separate_value_from_prefix: bool=True, default=None):
68+
separate_value_from_prefix: bool=True, default=None, separator=None, true=None, false=None, separate_arrays=None):
6969
super().__init__(prefix=prefix, value=None, position=position,
7070
separate_value_from_prefix=separate_value_from_prefix)
7171
self.name = name
7272
self.optional = optional
7373
self.default = default
74+
self.separator = separator
75+
self.true = true
76+
self.false = false
77+
self.separate_arrays = separate_arrays
7478

7579
@staticmethod
7680
def from_input(inp: Input, prefix: str=None, position: int=None):
7781
return Task.Command.CommandInput(inp.name, inp.type.optional, prefix, position)
7882

7983
def get_string(self):
80-
pr = self.prefix if self.prefix else ""
81-
sp = " " if self.separate else ""
82-
bc = pr + sp
83-
default = f'default="{self.default}" ' if self.default else ''
84+
name, array_sep, default, true, false = self.name, self.separator, self.default, self.true, self.false
8485

85-
if self.optional and not default:
86-
return '${{"{pre}" + {val}}}'.format(pre=bc, val=self.name, default=default)
86+
pr = self.prefix if self.prefix else ""
87+
bc = pr + (" " if self.separate else "")
88+
89+
if self.separate_arrays:
90+
if array_sep or default or true or false:
91+
print("separate_array take preferences over: separator, default, true, false")
92+
if self.optional:
93+
# Ugly optional workaround: https://github.com/openwdl/wdl/issues/25#issuecomment-315424063
94+
internal_pref = f'if defined({name}) then "--pre " else ""'
95+
return f'${{{internal_pref}}}${{sep="{bc}" {name}}}'
96+
return f'${{sep=" " prefix("{bc}", {name})}}'
97+
98+
options = []
99+
if default:
100+
options.append(f'default="{default}"')
101+
if array_sep:
102+
options.append(f'sep="{array_sep}"')
103+
if true or false:
104+
options.append(f'true="{true if true else ""}"')
105+
options.append(f'false="{false if false else ""}"')
106+
107+
stroptions = "".join(o + " " for o in options)
108+
109+
if self.optional:
110+
prewithquotes = f'"{bc}" + ' if bc.strip() else ''
111+
return f'${{{stroptions}{prewithquotes}{name}}}'
87112
else:
88-
return bc + "${{{default}{val}}}".format(val=self.name, default=default)
113+
return bc + f"${{{stroptions}{name}}}"
89114

90115
def __init__(self, command, inputs: Optional[List[CommandInput]]=None, arguments: Optional[List[CommandArgument]]=None):
91116
self.command = command
92117
self.inputs = inputs if inputs else []
93118
self.arguments = arguments if arguments else []
94119

95120
def get_string(self, indent: int=0):
121+
tb = " "
96122
base_command = self.command if self.command else ""
97123
if not (self.inputs or self.arguments):
98-
return base_command
124+
return indent * tb + base_command
99125

100126
# build up command
101127
args = sorted([*self.inputs, *self.arguments], key=lambda a: a.position if a.position else 0)
102128
command: str = base_command if isinstance(base_command, str) else " ".join(base_command)
103-
tb = " "
104129
tbed_arg_indent = tb * (indent + 1)
105130

106131
return indent * tb + command + "".join([" \\\n" + tbed_arg_indent + a.get_string() for a in args])
107132

108-
def __init__(self, name: str, inputs: List[Input]=None, outputs: List[Output]=None, command: Command=None, runtime: Runtime=None):
133+
def __init__(self, name: str, inputs: List[Input]=None, outputs: List[Output]=None, command: Command=None, runtime: Runtime=None, version="draft-2"):
109134
self.name = name
110135
self.inputs = inputs if inputs else []
111136
self.outputs = outputs if outputs else []
112137
self.command = command
113138
self.runtime = runtime
139+
self.version = version
114140

115141
self.format = """
142+
version {version}
143+
116144
task {name} {{
117145
{inputs_block}
118146
{command_block}
@@ -128,7 +156,7 @@ def get_string(self):
128156
inputs_block, command_block, runtime_block, output_block = "", "", "", ""
129157

130158
if self.inputs:
131-
inputs_block = "\n".join(tb + i.get_string() for i in self.inputs)
159+
inputs_block = f"{tb}input {{\n" + "\n".join(2*tb + i.get_string() for i in self.inputs) + f"\n{tb}}}"
132160

133161
if self.outputs:
134162
output_block = "{tb}output {{\n{outs}\n{tb}}}".format(
@@ -137,9 +165,14 @@ def get_string(self):
137165
)
138166

139167
if self.command:
168+
169+
if isinstance(self.command, list):
170+
com = "\n".join(c.get_string(indent=2) for c in self.command)
171+
else:
172+
com = self.command.get_string(indent=2)
140173
command_block = "{tb}command {{\n{args}\n{tb}}}".format(
141174
tb=tb,
142-
args=self.command.get_string(indent=2)
175+
args=com
143176
)
144177

145178
if self.runtime:
@@ -153,5 +186,6 @@ def get_string(self):
153186
inputs_block=inputs_block,
154187
command_block=command_block,
155188
runtime_block=runtime_block,
156-
output_block=output_block
189+
output_block=output_block,
190+
version=self.version
157191
)

wdlgen/types.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ class PrimitiveType:
1414
kString = "String"
1515
kFile = "File"
1616

17+
kDirectory = "Directory" # development branch
18+
1719
DEF_TYPE = kString
1820

19-
types = [kBoolean, kInt, kFloat, kFile, kString]
21+
types = [kBoolean, kInt, kFloat, kFile, kString, kDirectory]
2022

2123
def __init__(self, prim_type):
2224
if prim_type not in self.types:
@@ -166,3 +168,4 @@ def check_quantifiers(t: str):
166168
Float = WdlType(PrimitiveType(PrimitiveType.kFloat))
167169
File = WdlType(PrimitiveType(PrimitiveType.kFile))
168170
String = WdlType(PrimitiveType(PrimitiveType.kString))
171+
Directory = WdlType(PrimitiveType(PrimitiveType.kDirectory))

wdlgen/workflow.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class Workflow(WdlBase):
1010

1111
def __init__(self, name, inputs: List[Input]=None, outputs: List[str]=None, calls: List[WorkflowCallBase]=None,
12-
imports: List[Any]=None):
12+
imports: List[Any]=None, version="draft-2"):
1313
"""
1414
1515
:param name:
@@ -27,8 +27,11 @@ def __init__(self, name, inputs: List[Input]=None, outputs: List[str]=None, call
2727
self.outputs = outputs if outputs else []
2828
self.calls = calls if calls else []
2929
self.imports = imports if imports else []
30+
self.version = version
3031

3132
self.format = """
33+
version {version}
34+
3235
{imports_block}
3336
3437
workflow {name} {{
@@ -51,7 +54,7 @@ def get_string(self):
5154
ins.extend(tb + ii for ii in wd)
5255
else:
5356
ins.append(tb + wd)
54-
inputs_block = "\n".join(ins)
57+
inputs_block = f"{tb}input {{\n" + "\n".join(ins) + f"\n{tb}}}"
5558

5659
if self.outputs:
5760
outs = []
@@ -84,7 +87,8 @@ def get_string(self):
8487
inputs_block=inputs_block,
8588
output_block=output_block,
8689
call_block=call_block,
87-
imports_block=imports_block
90+
imports_block=imports_block,
91+
version=self.version
8892
)
8993

9094
class WorkflowImport(WdlBase):

0 commit comments

Comments
 (0)