Skip to content

Commit

Permalink
T1.2 accept/reject code changes proposed by the MigrationAssistant (D…
Browse files Browse the repository at this point in the history
…ynamoDS#10915)

* T1.1 Code Migration (#4)

* add ScriptMigrator

* Add visual difference viewer

* Update PythonMigrationViewExtension.cs

* Add tooltip description to migration assistant

* updates

* comment updates

* comment updates

* Update PythonMigrationViewExtension.csproj

* T1.2 accept/reject code changes (#5)

* add ScriptMigrator

* Add visual difference viewer

* Added methods for updating the ScriptEditors with the migrated code

* Update PythonMigrationViewExtension.cs

* Add tooltip description to migration assistant

* updates

* comment updates

* comment updates

* Update ScriptEditorWindow.xaml.cs

* Update PythonMigrationViewExtension.csproj

* Update PythonMigrationViewExtension.csproj

* comment updates

* Revert "comment updates"

This reverts commit 2d2332a.

* comment updates

* Update PythonMigrationAssistantViewModel.cs

* comment updates 2

* update engine version when migrating code

* extension and test updates

* add 2to3 icon

* comment updates

* GraphPythonDependencies changes

* make event internal

* fix build failure

* update license files, MigrationAssitantClicked event and PythonMigrationAssistantTests

* clean up

* update licence files

* make MigrateCode internal

* comments update

* unsubscribe from ScriptEditorWindow closed
  • Loading branch information
SHKnudsen authored Jul 23, 2020
1 parent 26450a9 commit 61d3273
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 10 deletions.
38 changes: 38 additions & 0 deletions src/Libraries/PythonNodeModels/PythonNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@

namespace PythonNodeModels
{
/// <summary>
/// Event arguments used to send the original and migrated code to the ScriptEditor
/// </summary>
internal class PythonCodeMigrationEventArgs : EventArgs
{
public string OldCode { get; private set; }
public string NewCode { get; private set; }
public PythonCodeMigrationEventArgs(string oldCode, string newCode)
{
OldCode = oldCode;
NewCode = newCode;
}
}

public abstract class PythonNodeBase : VariableInputNode
{
private PythonEngineVersion engine = PythonEngineVersion.IronPython2;
Expand Down Expand Up @@ -202,6 +216,30 @@ protected override bool UpdateValueCore(UpdateValueParams updateValueParams)
return base.UpdateValueCore(updateValueParams);
}

/// <summary>
/// Updates the Script property of the node.
/// NOTE: This is a temporary method used during the Python 2 to Python 3 transistion period,
/// it will be removed when the transistion period is over.
/// </summary>
/// <param name="newCode">The new migrated code</param>
internal void MigrateCode(string newCode)
{
var e = new PythonCodeMigrationEventArgs(Script, newCode);
Script = newCode;
OnCodeMigrated(e);
}

/// <summary>
/// Fires when the Script content is migrated to Python 3.
/// NOTE: This is a temporary event used during the Python 2 to Python 3 transistion period,
/// it will be removed when the transistion period is over.
/// </summary>
internal event EventHandler<PythonCodeMigrationEventArgs> CodeMigrated;
private void OnCodeMigrated(PythonCodeMigrationEventArgs e)
{
CodeMigrated?.Invoke(this, e);
}

#region SerializeCore/DeserializeCore

[Obsolete]
Expand Down
16 changes: 16 additions & 0 deletions src/Libraries/PythonNodeModelsWpf/ScriptEditorWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ public ScriptEditorWindow(
ref ModelessChildWindow.WindowRect windowRect
) : base(nodeView, ref windowRect)
{
this.Closed += OnScriptEditorWindowClosed;
this.dynamoViewModel = dynamoViewModel;
this.nodeModel = nodeModel;

completionProvider = new SharedCompletionProvider(nodeModel.Engine,dynamoViewModel.Model.PathManager.DynamoCoreDirectory);
completionProvider.MessageLogged += dynamoViewModel.Model.Logger.Log;
nodeModel.CodeMigrated += OnNodeModelCodeMigrated;

InitializeComponent();

Expand Down Expand Up @@ -131,6 +133,14 @@ private void OnTextAreaTextEntered(object sender, TextCompositionEventArgs e)

#region Private Event Handlers

private void OnNodeModelCodeMigrated(object sender, PythonCodeMigrationEventArgs e)
{
originalScript = e.OldCode;
editText.Text = e.NewCode;
if (nodeModel.Engine != PythonEngineVersion.CPython3)
nodeModel.Engine = PythonEngineVersion.CPython3;
}

private void OnSaveClicked(object sender, RoutedEventArgs e)
{
UpdateScript(editText.Text);
Expand Down Expand Up @@ -193,5 +203,11 @@ private void OnEngineChanged(object sender, System.Windows.Controls.SelectionCha
UpdateScript(editText.Text);
}
}

private void OnScriptEditorWindowClosed(object sender, EventArgs e)
{
nodeModel.CodeMigrated -= OnNodeModelCodeMigrated;
this.Closed -= OnScriptEditorWindowClosed;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
HorizontalAlignment="Right"
Grid.Column="1">
<Button x:Name="AcceptButton"
Click="OnAcceptButtonClicked"
HorizontalAlignment="Right"
ToolTip="{x:Static p:Resources.AcceptButtonTooltip}"
Height="44">
Expand All @@ -100,6 +101,7 @@
</Button.Resources>
</Button>
<Button x:Name="RejectButton"
Click="OnRejectButtonClicked"
HorizontalAlignment="Right"
ToolTip="{x:Static p:Resources.RejectButtonTooltip}"
Height="44">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ namespace Dynamo.PythonMigration.Controls
public partial class VisualDifferenceViewer : Window
{
private PythonMigrationAssistantViewModel ViewModel { get; set; }
internal VisualDifferenceViewer(PythonMigrationAssistantViewModel viewModel, Window ownerWindow)

internal VisualDifferenceViewer(PythonMigrationAssistantViewModel viewModel)
{
ViewModel = viewModel;
Owner = ownerWindow;
InitializeComponent();
LoadData();
DiffView.ViewModeChanged += OnViewModeChanged;
Expand Down Expand Up @@ -55,6 +55,17 @@ private void SizeWindowToContent()
SizeToContent = SizeToContent.Manual;
}

private void OnAcceptButtonClicked(object sender, RoutedEventArgs e)
{
ViewModel.ChangeCode();
this.Close();
}

private void OnRejectButtonClicked(object sender, RoutedEventArgs e)
{
this.Close();
}

private void OnVisualDifferenceViewerClosed(object sender, System.EventArgs e)
{
DiffView.ViewModeChanged -= OnViewModeChanged;
Expand Down
4 changes: 2 additions & 2 deletions src/PythonMigrationViewExtension/GraphPythonDependencies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal enum CNPythonDependencyType

internal GraphPythonDependencies(ViewLoadedParams viewLoadedParams)
{
this.ViewLoaded = viewLoadedParams;
ViewLoaded = viewLoadedParams;
}

private static bool IsIronPythonPackageLoaded()
Expand Down Expand Up @@ -78,7 +78,7 @@ internal IEnumerable<INodeLibraryDependencyInfo> AddPythonPackageDependency()
? PackageDependencyState.Loaded
: PackageDependencyState.Missing;

return new[] {packageDependencyInfo};
return new[] { packageDependencyInfo };
}

// This function returns true, if any of the custom nodes in the input list has an IronPython dependency.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Reflection;
using Python.Included;
using Python.Runtime;

namespace Dynamo.PythonMigration.MigrationAssistant
Expand All @@ -17,7 +18,7 @@ internal static class ScriptMigrator
/// <returns></returns>
internal static string MigrateCode(string code)
{
Python.Included.Installer.SetupPython().Wait();
Installer.SetupPython().Wait();

if (!PythonEngine.IsInitialized)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ internal class PythonMigrationAssistantViewModel
{
public string OldCode { get; set; }
public string NewCode { get; set; }
private PythonNode PythonNode { get; set; }

public PythonMigrationAssistantViewModel(PythonNode pythonNode)
{
PythonNode = pythonNode;
OldCode = pythonNode.Script;
MigrateCode();
}
Expand All @@ -18,5 +20,10 @@ private void MigrateCode()
{
NewCode = ScriptMigrator.MigrateCode(OldCode);
}

public void ChangeCode()
{
PythonNode.MigrateCode(NewCode);
}
}
}
12 changes: 7 additions & 5 deletions src/PythonMigrationViewExtension/PythonMigrationViewExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Linq;
using System.Windows;
using System.Windows.Threading;
using Dynamo.Controls;
using Dynamo.Core;
using Dynamo.Graph.Workspaces;
using Dynamo.Logging;
Expand Down Expand Up @@ -63,7 +62,7 @@ public void Loaded(ViewLoadedParams p)
PythonDependencies = new GraphPythonDependencies(LoadedParams);
DynamoViewModel = LoadedParams.DynamoWindow.DataContext as DynamoViewModel;
CurrentWorkspace = LoadedParams.CurrentWorkspaceModel as WorkspaceModel;
CustomNodeManager = (CustomNodeManager) LoadedParams.StartupParams.CustomNodeManager;
CustomNodeManager = (CustomNodeManager)LoadedParams.StartupParams.CustomNodeManager;
CurrentWorkspace.RequestPackageDependencies += PythonDependencies.AddPythonPackageDependency;
Dispatcher = Dispatcher.CurrentDispatcher;

Expand Down Expand Up @@ -118,10 +117,13 @@ private void OnMigrationAssistantRequested(object sender, EventArgs e)

var node = sender as PythonNode;
var viewModel = new PythonMigrationAssistantViewModel(node);
var assistantWindow = new VisualDifferenceViewer(viewModel, parentWindow);
var assistantWindow = new VisualDifferenceViewer(viewModel)
{
Owner = parentWindow
};

// show modal window so user cant interact with dynamo while migration assistant is open
assistantWindow.ShowDialog();

}

private void OnNotificationLogged(NotificationMessage obj)
Expand Down Expand Up @@ -198,7 +200,7 @@ private void OnCurrentWorkspaceChanged(IWorkspaceModel workspace)
if (PythonDependencies.ContainsIronPythonDependencyInCurrentWS())
{
CurrentWorkspace.Nodes
.Where(x=>x is PythonNodeBase)
.Where(x => x is PythonNodeBase)
.ToList()
.ForEach(x => SubscribeToPythonNodeEvents(x as PythonNodeBase));
}
Expand Down

0 comments on commit 61d3273

Please sign in to comment.