|
15 | 15 | from nbformat.notebooknode import NotebookNode # type: ignore |
16 | 16 |
|
17 | 17 | from .iotypes import CWLFilePathInput, CWLBooleanInput, CWLIntInput, CWLStringInput, CWLFilePathOutput, \ |
18 | | - CWLDumpableFile, CWLDumpableBinaryFile, CWLDumpable, CWLPNGPlot |
| 18 | + CWLDumpableFile, CWLDumpableBinaryFile, CWLDumpable, CWLPNGPlot, CWLPNGFigure |
19 | 19 | from .requirements_manager import RequirementsManager |
20 | 20 |
|
21 | 21 | with open(os.sep.join([os.path.abspath(os.path.dirname(__file__)), 'templates', 'template.dockerfile'])) as f: |
@@ -62,14 +62,19 @@ class AnnotatedVariablesExtractor(ast.NodeTransformer): |
62 | 62 |
|
63 | 63 | dumpable_mapper = { |
64 | 64 | (CWLDumpableFile.__name__,): ( |
65 | | - "with open('{var_name}', 'w') as f:\n\tf.write({var_name})", lambda node: node.target.id |
| 65 | + (None, "with open('{var_name}', 'w') as f:\n\tf.write({var_name})",), |
| 66 | + lambda node: node.target.id |
66 | 67 | ), |
67 | 68 | (CWLDumpableBinaryFile.__name__,): ( |
68 | | - "with open('{var_name}', 'wb') as f:\n\tf.write({var_name})", lambda node: node.target.id |
| 69 | + (None, "with open('{var_name}', 'wb') as f:\n\tf.write({var_name})"), |
| 70 | + lambda node: node.target.id |
69 | 71 | ), |
70 | 72 | (CWLDumpable.__name__, CWLDumpable.dump.__name__): None, |
71 | 73 | (CWLPNGPlot.__name__,): ( |
72 | | - 'import matplotlib.pyplot as plt\nplt.savefig("{var_name}.png")', |
| 74 | + (None, '{var_name}[-1].figure.savefig("{var_name}.png")'), |
| 75 | + lambda node: str(node.target.id) + '.png'), |
| 76 | + (CWLPNGFigure.__name__,): ( |
| 77 | + ('import matplotlib.pyplot as plt\nplt.figure()', '{var_name}[-1].figure.savefig("{var_name}.png")'), |
73 | 78 | lambda node: str(node.target.id) + '.png'), |
74 | 79 | } |
75 | 80 |
|
@@ -110,11 +115,18 @@ def _visit_input_ann_assign(self, node, annotation): |
110 | 115 | return None |
111 | 116 |
|
112 | 117 | def _visit_default_dumper(self, node, dumper): |
113 | | - dump_tree = ast.parse(dumper[0].format(var_name=node.target.id)) |
| 118 | + if dumper[0][0] is None: |
| 119 | + pre_code_body = [] |
| 120 | + else: |
| 121 | + pre_code_body = ast.parse(dumper[0][0].format(var_name=node.target.id)).body |
| 122 | + if dumper[0][1] is None: |
| 123 | + post_code_body = [] |
| 124 | + else: |
| 125 | + post_code_body = ast.parse(dumper[0][1].format(var_name=node.target.id)).body |
114 | 126 | self.extracted_variables.append(_VariableNameTypePair( |
115 | 127 | node.target.id, None, None, None, False, True, dumper[1](node)) |
116 | 128 | ) |
117 | | - return [self.conv_AnnAssign_to_Assign(node), *dump_tree.body] |
| 129 | + return [*pre_code_body, self.conv_AnnAssign_to_Assign(node), *post_code_body] |
118 | 130 |
|
119 | 131 | def _visit_user_defined_dumper(self, node): |
120 | 132 | load_ctx = ast.Load() |
|
0 commit comments