Esempio n. 1
0
    def export_as_script(self, filename):

        data_filename = os.path.relpath(filename) + '.data'

        save(data_filename, self.session.data_collection)

        imports = ['from glue.core.state import load']

        imports_header, header = self._script_header()
        imports.extend(imports_header)

        layers = ""
        for ilayer, layer in enumerate(self.layers):
            if layer.layer.label:
                layers += '## Layer {0}: {1}\n\n'.format(
                    ilayer + 1, layer.layer.label)
            else:
                layers += '## Layer {0}\n\n'.format(ilayer + 1)
            if layer.visible and layer.enabled:
                if isinstance(layer.layer, BaseData):
                    index = self.session.data_collection.index(layer.layer)
                    layers += "layer_data = data_collection[{0}]\n\n".format(
                        index)
                else:
                    dindex = self.session.data_collection.index(
                        layer.layer.data)
                    sindex = layer.layer.data.subsets.index(layer.layer)
                    layers += (
                        "layer_data = data_collection[{0}].subsets[{1}]\n\n".
                        format(dindex, sindex))
            imports_layer, layer_script = layer._python_exporter(layer)
            if layer_script is None:
                continue
            imports.extend(imports_layer)
            layers += layer_script.strip() + "\n"

        imports_legend, legend = self._script_legend()
        imports.extend(imports_legend)
        imports_footer, footer = self._script_footer()
        imports.extend(imports_footer)

        imports = os.linesep.join(
            sorted(set(imports), key=lambda s: s.strip('# ')))
        # The sorting key is added keep together similar but commented imports
        # Typical ex:
        #    matplotlib.use('Agg')
        #    # matplotlib.use('qt5Agg')

        script = TEMPLATE_SCRIPT.format(data=os.path.basename(data_filename),
                                        imports=imports.strip(),
                                        header=header.strip(),
                                        layers=layers.strip(),
                                        legend=legend.strip(),
                                        footer=footer.strip())

        with open(filename, 'w') as f:
            f.write(script)
Esempio n. 2
0
    def export_as_script(self, filename):

        data_filename = os.path.relpath(filename) + '.data'

        save(data_filename, self.session.data_collection)

        imports = ['from glue.core.state import load']

        imports_header, header = self._script_header()
        imports.extend(imports_header)

        layers = ""
        for ilayer, layer in enumerate(self.layers):
            if layer.layer.label:
                layers += '## Layer {0}: {1}\n\n'.format(
                    ilayer + 1, layer.layer.label)
            else:
                layers += '## Layer {0}\n\n'.format(ilayer + 1)
            if layer.visible and layer.enabled:
                if isinstance(layer.layer, BaseData):
                    index = self.session.data_collection.index(layer.layer)
                    layers += "layer_data = data_collection[{0}]\n\n".format(
                        index)
                else:
                    dindex = self.session.data_collection.index(
                        layer.layer.data)
                    sindex = layer.layer.data.subsets.index(layer.layer)
                    layers += (
                        "layer_data = data_collection[{0}].subsets[{1}]\n\n".
                        format(dindex, sindex))
            imports_layer, layer_script = layer._python_exporter(layer)
            if layer_script is None:
                continue
            imports.extend(imports_layer)
            layers += layer_script.strip() + "\n"

        imports_footer, footer = self._script_footer()
        imports.extend(imports_footer)

        imports = os.linesep.join(sorted(set(imports)))

        script = TEMPLATE_SCRIPT.format(data=data_filename,
                                        imports=imports.strip(),
                                        header=header.strip(),
                                        layers=layers.strip(),
                                        footer=footer.strip())

        with open(filename, 'w') as f:
            f.write(script)
Esempio n. 3
0
    def export_as_script(self, filename):

        data_filename = os.path.relpath(filename) + '.data'

        save(data_filename, self.session.data_collection)

        imports = ['from glue.core.state import load']

        imports_header, header = self._script_header()
        imports.extend(imports_header)

        layers = ""
        for ilayer, layer in enumerate(self.layers):
            if layer.layer.label:
                layers += '## Layer {0}: {1}\n\n'.format(ilayer + 1, layer.layer.label)
            else:
                layers += '## Layer {0}\n\n'.format(ilayer + 1)
            if layer.visible and layer.enabled:
                if isinstance(layer.layer, BaseData):
                    index = self.session.data_collection.index(layer.layer)
                    layers += "layer_data = data_collection[{0}]\n\n".format(index)
                else:
                    dindex = self.session.data_collection.index(layer.layer.data)
                    sindex = layer.layer.data.subsets.index(layer.layer)
                    layers += ("layer_data = data_collection[{0}].subsets[{1}]\n\n"
                               .format(dindex, sindex))
            imports_layer, layer_script = layer._python_exporter(layer)
            if layer_script is None:
                continue
            imports.extend(imports_layer)
            layers += layer_script.strip() + "\n"

        imports_footer, footer = self._script_footer()
        imports.extend(imports_footer)

        imports = os.linesep.join(sorted(set(imports)))

        script = TEMPLATE_SCRIPT.format(data=data_filename,
                                        imports=imports.strip(),
                                        header=header.strip(),
                                        layers=layers.strip(),
                                        footer=footer.strip())

        with open(filename, 'w') as f:
            f.write(script)