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.IO;
using System.Reflection;
using Microsoft.Scripting;
@@ -15,7 +16,7 @@ namespace mpvnet
ScriptEngine engine;
ScriptScope scope;
public PythonScript(string code)
public PythonScript(string scriptPath)
{
try
{
@@ -26,14 +27,14 @@ namespace mpvnet
engine.Execute("clr.AddReference(\"mpvnet\")", scope);
engine.Execute("import mpvnet", scope);
engine.Execute("from mpvnet import *", scope);
engine.Execute(code, scope);
engine.Execute(File.ReadAllText(scriptPath), scope);
}
catch (Exception ex)
{
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
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"))
{
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"))
Task.Run(() => PythonScripts.Add(new PythonScript(File.ReadAllText(path))));
else if (path.EndsWith(".ps1"))
Task.Run(() => PowerShellScript.Init(path));
if (scriptPath.EndsWith(".py"))
Task.Run(() => PythonScripts.Add(new PythonScript(scriptPath)));
else if (scriptPath.EndsWith(".ps1"))
Task.Run(() => PowerShellScript.Init(scriptPath));
}
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"))
foreach (string scriptPath in Directory.GetFiles(ConfigFolder + "scripts"))
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"))
Task.Run(() => PowerShellScript.Init(scriptPath));
}