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

Commit 49c1193

Browse files
committed
code gardening
1 parent 8d10b9d commit 49c1193

File tree

6 files changed

+76
-50
lines changed

6 files changed

+76
-50
lines changed

cwlgen/__init__.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,15 @@
55
# Import ------------------------------
66

77
# General libraries
8-
import os
9-
import argparse
10-
import sys
118
import logging
129

1310
# External libraries
1411
import ruamel.yaml
1512
import six
1613
from .version import __version__
1714

18-
from .utils import *
19-
from .elements import Parameter, CWL_VERSIONS
15+
from .utils import literal, literal_presenter
16+
from .elements import Parameter, CWL_VERSIONS, DEF_VERSION, CWL_SHEBANG
2017
from .workflow import Workflow, File
2118
from .import_cwl import parse_cwl
2219

@@ -90,8 +87,8 @@ def export(self, outfile=None):
9087
"""
9188
# First add representer (see .utils.py) for multiline writting
9289
ruamel.yaml.add_representer(literal, literal_presenter)
93-
cwl_tool = {k: v for k, v in vars(self).items() if v is not None and\
94-
type(v) is str}
90+
cwl_tool = {k: v for k, v in vars(self).items() if v is not None and
91+
type(v) is str}
9592
cwl_tool['class'] = self.__CLASS__
9693
# Treat doc for multiline writting
9794
if self.doc:
@@ -139,8 +136,6 @@ def export(self, outfile=None):
139136
out_write.close()
140137

141138

142-
143-
144139
class CommandInputParameter(Parameter):
145140
'''
146141
An input parameter for a :class:`cwlgen.CommandLineTool`.
@@ -378,9 +373,7 @@ def _to_dict(self):
378373
tool generated in an export method.
379374
380375
"""
381-
return {p:v for p,v in vars(self).items() if p.startswith('docker') and v is not None}
382-
383-
376+
return {p: v for p, v in vars(self).items() if p.startswith('docker') and v is not None}
384377

385378

386379
class Namespaces(object):

cwlgen/elements.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import logging
12

3+
logging.basicConfig(level=logging.INFO)
4+
_LOGGER = logging.getLogger(__name__)
25

36
# Constant(s) ------------------------------
47

@@ -11,7 +14,6 @@
1114
'Directory', None]
1215

1316

14-
1517
class Parameter(object):
1618
'''
1719
Based class for parameters (common field of Input and Output) for CommandLineTool and Workflow
@@ -38,7 +40,7 @@ def __init__(self, param_id, label=None, secondary_files=None, param_format=None
3840
:type param_type: STRING corresponding to CWLType
3941
'''
4042
if param_type not in CWL_TYPE:
41-
LOGGER.warning("The type is incorrect for the parameter.")
43+
_LOGGER.warning("The type is incorrect for the parameter.")
4244
param_type = None
4345
self.id = param_id
4446
self.label = label
@@ -59,8 +61,8 @@ def get_dict(self):
5961
if dict_param['type'] != 'File':
6062
# Remove what is only for File
6163
for key in ['format', 'secondaryFiles', 'streamable']:
62-
try:
63-
del(dict_param[key])
64-
except KeyError:
65-
pass
64+
try:
65+
del(dict_param[key])
66+
except KeyError:
67+
pass
6668
return dict_param

cwlgen/import_cwl.py

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,27 @@
66

77
# General libraries
88
import os
9-
import argparse
10-
import sys
9+
import six
1110
import logging
1211

1312
# External libraries
1413
import ruamel.yaml as ryaml
15-
import six
1614
import cwlgen
1715
import cwlgen.workflow
1816

1917
logging.basicConfig(level=logging.INFO)
20-
logger = logging.getLogger(__name__)
18+
_LOGGER = logging.getLogger(__name__)
2119

2220
# Class(es) ------------------------------
2321

22+
2423
def parse_cwl(cwl_path):
24+
"""
25+
Method that parse a CWL file.
26+
27+
:param cwl_path: PATH to the CWL file
28+
:type cwl_path: STRING
29+
"""
2530
with open(cwl_path) as yaml_file:
2631
cwl_dict = ryaml.load(yaml_file, Loader=ryaml.Loader)
2732
cl = cwl_dict['class']
@@ -34,6 +39,7 @@ def parse_cwl(cwl_path):
3439
return p.import_cwl(cwl_path)
3540
return None
3641

42+
3743
class CWLToolParser(object):
3844
"""
3945
Class to import content from an existing CWL Tool.
@@ -152,7 +158,7 @@ def _load_class(self, tool, class_el):
152158
:type class_el: STRING
153159
"""
154160
if class_el != 'CommandLineTool':
155-
logger.warning('cwlgen library only handle CommandLineTool for the moment')
161+
_LOGGER.warning('cwlgen library only handle CommandLineTool for the moment')
156162

157163
def _load_inputs(self, tool, inputs_el):
158164
"""
@@ -194,20 +200,26 @@ def import_cwl(self, cwl_path):
194200
try:
195201
getattr(self, '_load_{}'.format(key))(tool, element)
196202
except AttributeError:
197-
logger.warning(key + " content is not processed (yet).")
203+
_LOGGER.warning(key + " content is not processed (yet).")
198204
tool._path = cwl_path
199205
return tool
200206

207+
201208
def dict_or_idlist_items(v):
209+
"""
210+
:param v:
211+
:type v: DICT of [DICT]
212+
"""
202213
if isinstance(v, dict):
203214
return v.items()
204215
o = []
205216
for i in v:
206217
e = dict(i)
207218
del e['id']
208-
o.append( (i['id'], e) )
219+
o.append((i['id'], e))
209220
return o
210221

222+
211223
class InputsParser(object):
212224
"""
213225
Class to parse content of inputs from an existing CWL Tool.
@@ -318,7 +330,7 @@ def load_inputs(self, inputs, inputs_el):
318330
try:
319331
getattr(self, '_load_{}'.format(key))(input_obj, element)
320332
except AttributeError:
321-
logger.warning(key + " content for input is not processed (yet).")
333+
_LOGGER.warning(key + " content for input is not processed (yet).")
322334
inputs.append(input_obj)
323335

324336

@@ -418,7 +430,7 @@ def load_inbinding(self, input_obj, inbinding_el):
418430
try:
419431
getattr(self, '_load_{}'.format(key))(inbinding_obj, value)
420432
except AttributeError:
421-
logger.warning(key + " content for inputBinding is not processed (yet).")
433+
_LOGGER.warning(key + " content for inputBinding is not processed (yet).")
422434
input_obj.inputBinding = inbinding_obj
423435

424436

@@ -532,7 +544,7 @@ def load_outputs(self, outputs, outputs_el):
532544
try:
533545
getattr(self, '_load_{}'.format(key))(output_obj, element)
534546
except AttributeError:
535-
logger.warning(key + " content for output is not processed (yet).")
547+
_LOGGER.warning(key + " content for output is not processed (yet).")
536548
outputs.append(output_obj)
537549

538550

@@ -588,7 +600,7 @@ def load_outbinding(self, output_obj, outbinding_el):
588600
try:
589601
getattr(self, '_load_{}'.format(key))(outbinding_obj, value)
590602
except AttributeError:
591-
logger.warning(key + " content for outputBinding is not processed (yet).")
603+
_LOGGER.warning(key + " content for outputBinding is not processed (yet).")
592604
output_obj.outputBinding = outbinding_obj
593605

594606

@@ -597,10 +609,22 @@ class StepsParser(object):
597609
Class to parse content of steps of workflow from existing CWL Workflow.
598610
"""
599611
def __init__(self, basedir=None, expand_run=True):
612+
"""
613+
:param basedir:
614+
:type basedir:
615+
:param expand_run:
616+
:type: expand_run:
617+
"""
600618
self.basedir = basedir
601619
self.expand_run = expand_run
602620

603621
def _load_in(self, step_obj, in_el):
622+
"""
623+
:param step_obj:
624+
:type step_obj:
625+
:param in_el:
626+
:type in_el:
627+
"""
604628
for key, val in in_el.items():
605629
o = cwlgen.workflow.WorkflowStepInput(key)
606630
if isinstance(val, dict) and 'default' in val:
@@ -615,14 +639,13 @@ def _load_out(self, step_obj, in_el):
615639
step_obj.outputs.append(o)
616640

617641
def _load_run(self, step_obj, in_el):
618-
if isinstance(in_el, basestring) and self.expand_run:
642+
if isinstance(in_el, six.string_types) and self.expand_run:
619643
path = os.path.join(self.basedir, in_el)
620-
#logger.info("Parsing: %s", path)
644+
# logger.info("Parsing: %s", path)
621645
step_obj.run = parse_cwl(path)
622646
else:
623647
step_obj.run = in_el
624648

625-
626649
def load_steps(self, steps_obj, steps_elm):
627650
"""
628651
Load the content of step into the output object.
@@ -638,7 +661,7 @@ def load_steps(self, steps_obj, steps_elm):
638661
try:
639662
getattr(self, '_load_{}'.format(key))(step_obj, element)
640663
except AttributeError:
641-
logger.warning(key + " content for input is not processed (yet).")
664+
_LOGGER.warning(key + " content for input is not processed (yet).")
642665
steps_obj.append(step_obj)
643666

644667

@@ -674,7 +697,7 @@ def import_cwl(self, cwl_path):
674697
try:
675698
getattr(self, '_load_{}'.format(key))(tool, element)
676699
except AttributeError:
677-
logger.warning(key + " workflow content is not processed (yet).")
700+
_LOGGER.warning(key + " workflow content is not processed (yet).")
678701
tool._path = cwl_path
679702
return tool
680703

@@ -700,7 +723,6 @@ def _load_cwlVersion(self, tool, cwl_version_el):
700723
"""
701724
tool.cwlVersion = cwl_version_el
702725

703-
704726
def _load_class(self, tool, class_el):
705727
"""
706728
Display message to inform that cwlgen only deal with Workflow for the moment.
@@ -711,7 +733,7 @@ def _load_class(self, tool, class_el):
711733
:type class_el: STRING
712734
"""
713735
if class_el != 'Workflow':
714-
logger.warning('cwlgen library only handle Workflow for the moment')
736+
_LOGGER.warning('cwlgen library only handle Workflow for the moment')
715737

716738
def _load_requirements(self, tool, req_el):
717739
pass
@@ -740,7 +762,6 @@ def _load_outputs(self, tool, outputs_el):
740762
inp_parser = OutputsParser()
741763
inp_parser.load_outputs(tool.outputs, outputs_el)
742764

743-
744765
def _load_steps(self, tool, outputs_el):
745766
"""
746767
Load the content of inputs into the tool.

cwlgen/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
Set of util functions and classes
33
"""
44

5+
56
class literal(str): pass
67

8+
79
def literal_presenter(dumper, data):
8-
return dumper.represent_scalar('tag:yaml.org,2002:str', data, style = "|")
10+
return dumper.represent_scalar('tag:yaml.org,2002:str', data, style="|")

cwlgen/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
version_info = (0,2,8)
1+
version_info = (0, 2, 8)
22
__version__ = '.'.join(str(c) for c in version_info)

0 commit comments

Comments
 (0)