Skip to content

Commit

Permalink
Adds import Python script option, WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
remram44 committed Apr 23, 2015
1 parent 2953684 commit 27c9977
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 3 deletions.
10 changes: 10 additions & 0 deletions vistrails/core/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,16 @@ def open_workflow(self, locator):
controller.set_changed(True)
return controller

def import_python(self, filename, controller=None):
if controller is None:
controller = self.get_current_controller()
if controller is None:
new_locator = UntitledLocator()
controller = self.open_vistrail(new_locator)
if controller.import_python_script(filename):
return controller
return None

def save_vistrail(self, locator=None, controller=None, export=False):
if controller is None:
controller = self.get_current_controller()
Expand Down
4 changes: 3 additions & 1 deletion vistrails/core/scripting/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
from __future__ import division

from vistrails.core.scripting.export import write_workflow_to_python
from vistrails.core.scripting.import_ import read_workflow_from_python
from vistrails.core.scripting.scripts import Prelude, Script


__all__ = ['Prelude', 'Script', 'write_workflow_to_python']
__all__ = ['Prelude', 'Script',
'read_workflow_from_python', 'write_workflow_to_python']
35 changes: 35 additions & 0 deletions vistrails/core/scripting/import_.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

import ast
import redbaron
import urllib2

from vistrails.core.db.action import create_action
from vistrails.core.modules.module_registry import get_module_registry


NUMBER_LITERALS = (redbaron.IntNode, redbaron.FloatNode, redbaron.LongNode,
Expand All @@ -27,6 +31,37 @@ def eval_int_literal(node):
return ast.literal_eval(node.value)


def read_workflow_from_python(controller, filename):
"""Imports a Python script as a workflow on the given controller.
"""
# FIXME: Unicode support?
with open(filename, 'rb') as f:
script = redbaron.RedBaron(f.read())
print ("Got %d-line Python script" % len(script))

# Remove previous modules
ops = [('delete', conn)
for conn in controller.current_pipeline.connection_list]
ops.extend(('delete', mod)
for mod in controller.current_pipeline.module_list)

# Create PythonSource
md = get_module_registry().get_descriptor_by_name(
'org.vistrails.vistrails.basic',
'PythonSource')
module = controller.create_module_from_descriptor(md)
source = urllib2.quote(script.dumps())
function = controller.create_function(module, 'source', [source])
module.add_function(function)
ops.append(('add', module))

action = create_action(ops)
controller.add_new_action(action, "Imported Python script")
controller.perform_action(action)
controller.change_selected_version(action.id)
return True


###############################################################################

import unittest
Expand Down
6 changes: 5 additions & 1 deletion vistrails/core/vistrail/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@
from vistrails.db.services.io import create_temp_folder, remove_temp_folder
from vistrails.db.services.io import SaveBundle, open_vt_log_from_db
from vistrails.db.services.vistrail import getSharedRoot
from vistrails.core.scripting import write_workflow_to_python
from vistrails.core.scripting import write_workflow_to_python, \
read_workflow_from_python
from vistrails.core.utils import any


Expand Down Expand Up @@ -4082,6 +4083,9 @@ def write_workflow_to_python(self, filename):
return
write_workflow_to_python(self.current_pipeline, filename)

def import_python_script(self, filename):
return read_workflow_from_python(self, filename)

def write_log(self, locator):
if self.log:
if self.vistrail.db_log_filename is not None:
Expand Down
27 changes: 26 additions & 1 deletion vistrails/gui/vistrails_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,11 @@ def init_action_list(self):
('importWorkflowDB', "Workflow from DB...",
{'statusTip': "Import a workflow from a database",
'enabled': True,
'callback': _app.import_workflow_from_db})]),
'callback': _app.import_workflow_from_db}),
('importPython', "Python Script as Workflow...",
{'statusTip': "Translates a Python script to a workflow",
'enabled': True,
'callback': _app.import_python})]),
("export", "Export",
[('exportFile', "To DB...",
{'statusTip': "Export the current vistrail to a " \
Expand Down Expand Up @@ -1823,6 +1827,27 @@ def import_workflow_from_db(self):
"""
self.import_workflow(DBLocator)

def import_python(self, controller=None):
""" translate a Python script back into a workflow """
filename = QtGui.QFileDialog.getOpenFileName(
self, "Choose a file",
".", "Python script (*.py)")
if not filename:
return False

self.close_first_vistrail_if_necessary()
controller = get_vistrails_application().import_python(filename,
controller)
if controller is not None:
self.qactions['pipeline'].trigger()
view = self.get_current_view()
view.controller.recompute_terse_graph()
view.controller.invalidate_version_tree()
from vistrails.gui.collection.workspace import QWorkspaceWindow
QWorkspaceWindow.instance().add_vt_window(view)
return True
return False

def open_workflow(self, locator):
get_vistrails_application().open_workflow(locator)
self.close_first_vistrail_if_necessary()
Expand Down

0 comments on commit 27c9977

Please sign in to comment.