Skip to content
This repository was archived by the owner on Jun 9, 2020. It is now read-only.

Commit eb2f74c

Browse files
committed
Clean up exporting and README example
1 parent 2308973 commit eb2f74c

File tree

7 files changed

+81
-62
lines changed

7 files changed

+81
-62
lines changed

README.md

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,68 +14,60 @@ The library works for both Python 2.7.12+ and 3.6.0.
1414
------------------------
1515

1616

17-
18-
I've forked this repository to try and get my use of this repository working quickly without
19-
worrying too much about proper code etiquette. I have and will continue to submit merge requests
20-
back to the [original repository](https://github.com/common-workflow-language/python-cwlgen).
21-
22-
[![Build Status](https://travis-ci.org/illusional/python-cwlgen.svg?branch=master)](https://travis-ci.org/common-workflow-language/python-cwlgen)
23-
[![codecov](https://codecov.io/gh/illusional/python-cwlgen/branch/master/graph/badge.svg)](https://codecov.io/gh/illusional/python-cwlgen)
24-
[![PyPI version](https://badge.fury.io/py/illusional.cwlgen.svg)](https://badge.fury.io/py/illusional.cwlgen)
25-
2617
# Common Workflow Language
2718

28-
[Common Workflow Language (CWL)](https://www.commonwl.org/v1.0/index.html) is a method to describe workflows,
29-
and any tools (software) that it may use. The [user guide](http://www.commonwl.org/user_guide/01-introduction/index.html)
30-
gives a gentle (and better) explanation of what its goals are, and how they are achieved, but broadly:
19+
[Common Workflow Language (CWL)](https://www.commonwl.org/v1.0/index.html) is a language to describe workflows.
20+
The [user guide](http://www.commonwl.org/user_guide/01-introduction/index.html)
21+
gives a gentle explanation of what its goals are, but broadly:
3122

3223
1. Stop writing bash scripts for long complex jobs.
3324
2. Take pipelines anywhere (portability).
34-
3. Try to enforce reproducibility guidelines.
25+
3. Enforce reproducibility guidelines.
3526

36-
This python repository is simply a python wrapper for _most_ of the classes (work in progress),
27+
This python repository is a python wrapper for _most_ of the classes (work in progress),
3728
allowing you to build the structure of the workflow in Python and have this module generate and export CWL for you.
3829

3930
**Nb:** This isn't going to sanity or quality check Workflows or CommandLineTools for you, use
4031
[CWLTool](https://github.com/common-workflow-language/cwltool) or [WOMTool](https://cromwell.readthedocs.io/en/develop/WOMtool/) for that.
4132

4233
## Quick-start guide
4334

44-
This is available through PIP!
35+
You can install python-cwlgen through pip with the following command:
4536

4637
```
47-
pip install illusional.cwlgen
38+
pip install cwlgen
4839
```
4940

5041
## How it works ?
5142

5243
There's a pretty close copy of the cwl specifications ([Workflow](https://www.commonwl.org/v1.0/Workflow.html)|
5344
[CommandLineTool](https://www.commonwl.org/v1.0/CommandLineTool.html)), where the Python classes mirror the CWL spec.
54-
This repository also includes some of the docstrings to give you context of classes and their properties.
55-
56-
I've tried to include direct links to a classes documentation, however this isn't always possible.
45+
This repository also includes docstrings to give you context of classes and their properties.
5746

58-
There are some small examples in the `examples/` folder, however for whatever class you need, you simply just init
59-
that class, for example:
47+
The `examples/` folder contains some simple examples, however ,
48+
you can simply initialise that class, for example:
6049

6150
_Creating a CommandLineTool_
6251
```python
63-
# if using gitsubmodules, you can use the following import statement
64-
import cwlgen as cwl
52+
import cwlgen
6553

66-
tool_object = cwl.CommandLineTool(cwltool_id="echo-tool", base_command="echo", label=None, doc=None,
54+
tool_object = cwlgen.CommandLineTool(tool_id="echo-tool", base_command="echo", label=None, doc=None,
6755
cwl_version="v1.0", stdin=None, stderr=None, stdout=None, path=None)
68-
tool_object.inputs.append(cwl.CommandInputParameter("myParamId", label=None, secondary_files=None, param_format=None,
69-
streamable=None, doc=None, input_binding=None, default=None, param_type=None))
70-
71-
# fill in the fields as required
56+
tool_object.inputs.append(
57+
cwlgen.CommandInputParameter("myParamId", param_type="string", label=None, secondary_files=None, param_format=None,
58+
streamable=None, doc=None, input_binding=None, default=None)
59+
)
7260

7361
# to get the dictionary representation:
7462
dict_to_export = tool_object.get_dict()
7563

76-
# dump using a yaml exporter
77-
yaml.dump(dict_to_export)
78-
```
64+
# to get the string representation (YAML)
65+
yaml_export = tool_object.export_string()
66+
67+
# print to console
68+
tool_object.export()
7969

80-
All of the classes should work in a similar way. I've removed the `literal` representation from my fork as I
81-
didn't want to use _ruamel_ at the moment. Otherwise file an issue and I'll have a look into it.
70+
# print to file
71+
with open("echotool.cwl", "w") as f:
72+
tool_object.export(f)
73+
```

cwlgen/commandlinetool.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,25 @@ def get_dict(self):
9292
d['requirements'] = {r.get_class(): r.get_dict() for r in self.requirements}
9393
return d
9494

95+
def export_string(self):
96+
ruamel.yaml.add_representer(literal, literal_presenter)
97+
cwl_tool = self.get_dict()
98+
return ruamel.yaml.dump(cwl_tool, default_flow_style=False)
99+
95100
def export(self, outfile=None):
96101
"""
97102
Export the tool in CWL either on STDOUT or in outfile.
98103
"""
99-
# First add representer (see .utils.py) for multiline writting
100-
ruamel.yaml.add_representer(literal, literal_presenter)
101-
102-
cwl_tool = self.get_dict()
104+
rep = self.export_string()
103105

104106
# Write CWL file in YAML
105107
if outfile is None:
106108
six.print_(CWL_SHEBANG, "\n", sep='')
107-
six.print_(ruamel.yaml.dump(cwl_tool))
109+
six.print_(rep)
108110
else:
109111
out_write = open(outfile, 'w')
110112
out_write.write(CWL_SHEBANG + '\n\n')
111-
out_write.write(ruamel.yaml.dump(cwl_tool))
113+
out_write.write(rep)
112114
out_write.close()
113115

114116

cwlgen/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def __init__(self):
126126
self.s = "http://schema.org/"
127127

128128

129-
class Metadata(object):
129+
class Metadata(Serializable):
130130
"""
131131
Represent metadata described by http://schema.org.
132132
"""

cwlgen/workflow.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,23 +110,25 @@ def get_dict(self):
110110

111111
return cwl_workflow
112112

113+
def export_string(self):
114+
ruamel.yaml.add_representer(literal, literal_presenter)
115+
cwl_tool = self.get_dict()
116+
return ruamel.yaml.dump(cwl_tool, default_flow_style=False)
117+
113118
def export(self, outfile=None):
114119
"""
115120
Export the workflow in CWL either on STDOUT or in outfile.
116121
"""
117-
# First add representer (see .utils.py) for multiline writting
118-
ruamel.yaml.add_representer(literal, literal_presenter)
119-
120-
cwl_workflow = self.get_dict()
122+
rep = self.export_string()
121123

122124
# Write CWL file in YAML
123125
if outfile is None:
124126
six.print_(CWL_SHEBANG, "\n", sep='')
125-
six.print_(ruamel.yaml.dump(cwl_workflow))
127+
six.print_(rep)
126128
else:
127129
out_write = open(outfile, 'w')
128130
out_write.write(CWL_SHEBANG + '\n\n')
129-
out_write.write(ruamel.yaml.dump(cwl_workflow))
131+
out_write.write(rep)
130132
out_write.close()
131133

132134
# def add(self, step_id, tool, params):

examples/grep.cwl

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
#!/usr/bin/env cwl-runner
22

3-
$namespaces: {s: http://schema.org/}
4-
arguments: []
3+
$namespaces:
4+
s: http://schema.org/
55
baseCommand: grep
66
class: CommandLineTool
77
cwlVersion: v1.0
8-
doc: |-
9-
grep searches for a pattern in a file.
8+
doc: grep searches for a pattern in a file.
109
id: grep
1110
inputs:
12-
input_file:
13-
doc: input file from which you want to look for the pattern
14-
inputBinding: {loadContents: false, position: 2, separate: true, shellQuote: true}
15-
type: File
16-
pattern:
17-
doc: pattern to find in the input file
18-
inputBinding: {loadContents: false, position: 1, separate: true, shellQuote: true}
19-
type: string
11+
- doc: input file from which you want to look for the pattern
12+
id: input_file
13+
inputBinding:
14+
position: 2
15+
type: File
16+
- doc: pattern to find in the input file
17+
id: pattern
18+
inputBinding:
19+
position: 1
20+
type: string
2021
label: print lines matching a pattern
22+
metadata:
23+
about: grep searches for a pattern in a file.
24+
name: grep
2125
outputs:
22-
output: {doc: lines found with the pattern, type: stdout}
26+
- doc: lines found with the pattern
27+
id: output
28+
type: stdout
2329
s:about: grep searches for a pattern in a file.
2430
s:name: grep
2531
stdout: grep.txt

examples/grep_example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@
4646

4747
# Add Metadata
4848
metadata = {'name': 'grep',
49-
'about' : 'grep searches for a pattern in a file.'}
49+
'about': 'grep searches for a pattern in a file.'}
5050
cwl_tool.metadata = cwlgen.Metadata(**metadata)
5151
cwl_tool.metadata = cwlgen.Metadata(**metadata)
5252

5353
# Write in an output file
54-
#cwl_tool.export()
55-
cwl_tool.export("grep.cwl")
54+
cwl_tool.export()
55+
# cwl_tool.export("grep.cwl")

examples/readme_example.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import cwlgen
2+
3+
tool_object = cwlgen.CommandLineTool(tool_id="echo-tool", base_command="echo", label=None, doc=None,
4+
cwl_version="v1.0", stdin=None, stderr=None, stdout=None, path=None)
5+
tool_object.inputs.append(
6+
cwlgen.CommandInputParameter("myParamId", label=None, secondary_files=None, param_format=None,
7+
streamable=None, doc=None, input_binding=None, default=None, param_type="string")
8+
)
9+
10+
# to get the dictionary representation:
11+
dict_to_export = tool_object.get_dict()
12+
13+
# to get the string representation (YAML)
14+
yaml_export = tool_object.export_string()
15+
16+
# print to console
17+
tool_object.export()

0 commit comments

Comments
 (0)