improved python error messages

This commit is contained in:
Frank Skare
2019-09-09 07:09:10 +02:00
parent b59f16a425
commit e0e40b7918
2 changed files with 13 additions and 12 deletions

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.IO;
using System.Reflection; using System.Reflection;
using Microsoft.Scripting; using Microsoft.Scripting;
@@ -15,7 +16,7 @@ namespace mpvnet
ScriptEngine engine; ScriptEngine engine;
ScriptScope scope; ScriptScope scope;
public PythonScript(string code) public PythonScript(string scriptPath)
{ {
try try
{ {
@@ -26,14 +27,14 @@ namespace mpvnet
engine.Execute("clr.AddReference(\"mpvnet\")", scope); engine.Execute("clr.AddReference(\"mpvnet\")", scope);
engine.Execute("import mpvnet", scope); engine.Execute("import mpvnet", scope);
engine.Execute("from mpvnet import *", scope); engine.Execute("from mpvnet import *", scope);
engine.Execute(code, scope); engine.Execute(File.ReadAllText(scriptPath), scope);
} }
catch (Exception ex) catch (Exception ex)
{ {
if (ex is SyntaxErrorException e) if (ex is SyntaxErrorException e)
Msg.ShowError($"{e.Line}, {e.Column}: " + ex.Message); Msg.ShowError(e.GetType().Name,$"{e.Line}, {e.Column}: " + e.Message + "\n\n" + Path.GetFileName(scriptPath));
else else
Msg.ShowException(ex); Msg.ShowError(ex.GetType().Name, ex.Message + "\n\n" + Path.GetFileName(scriptPath));
} }
} }
} }

View File

@@ -252,24 +252,24 @@ namespace mpvnet
{ {
if (Directory.Exists(PathHelp.StartupPath + "Scripts")) if (Directory.Exists(PathHelp.StartupPath + "Scripts"))
{ {
foreach (string path in Directory.GetFiles(PathHelp.StartupPath + "Scripts")) foreach (string scriptPath in Directory.GetFiles(PathHelp.StartupPath + "Scripts"))
{ {
if (KnownScripts.Contains(Path.GetFileName(path))) if (KnownScripts.Contains(Path.GetFileName(scriptPath)))
{ {
if (path.EndsWith(".py")) if (scriptPath.EndsWith(".py"))
Task.Run(() => PythonScripts.Add(new PythonScript(File.ReadAllText(path)))); Task.Run(() => PythonScripts.Add(new PythonScript(scriptPath)));
else if (path.EndsWith(".ps1")) else if (scriptPath.EndsWith(".ps1"))
Task.Run(() => PowerShellScript.Init(path)); Task.Run(() => PowerShellScript.Init(scriptPath));
} }
else else
Msg.ShowError("Failed to load script", path + "\n\nOnly scripts that ship with mpv.net are allowed in <startup>\\scripts\n\nUser scripts have to use <config folder>\\scripts\n\nNever copy or install a new mpv.net version over a old mpv.net version."); Msg.ShowError("Failed to load script", scriptPath + "\n\nOnly scripts that ship with mpv.net are allowed in <startup>\\scripts\n\nUser scripts have to use <config folder>\\scripts\n\nNever copy or install a new mpv.net version over a old mpv.net version.");
} }
} }
if (Directory.Exists(ConfigFolder + "scripts")) if (Directory.Exists(ConfigFolder + "scripts"))
foreach (string scriptPath in Directory.GetFiles(ConfigFolder + "scripts")) foreach (string scriptPath in Directory.GetFiles(ConfigFolder + "scripts"))
if (scriptPath.EndsWith(".py")) if (scriptPath.EndsWith(".py"))
Task.Run(() => PythonScripts.Add(new PythonScript(File.ReadAllText(scriptPath)))); Task.Run(() => PythonScripts.Add(new PythonScript(scriptPath)));
else if (scriptPath.EndsWith(".ps1")) else if (scriptPath.EndsWith(".ps1"))
Task.Run(() => PowerShellScript.Init(scriptPath)); Task.Run(() => PowerShellScript.Init(scriptPath));
} }