Skip to content

Instantly share code, notes, and snippets.

@ialex32x
Last active December 20, 2015 06:19
Show Gist options
  • Save ialex32x/b33d5ac087c4a2c59bdd to your computer and use it in GitHub Desktop.
Save ialex32x/b33d5ac087c4a2c59bdd to your computer and use it in GitHub Desktop.
IronPython basic usage for hosting.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
namespace NewWorld
{
public partial class Form1 : Form
{
public class Entry
{
public string name { get; set; }
public Entry(string s)
{
name = s;
}
public void Speak(IronPython.Runtime.List o)
{
System.Diagnostics.Debug.WriteLine("List: " + o.Count);
}
public void Speak(IronPython.Runtime.PythonDictionary o)
{
System.Diagnostics.Debug.WriteLine("PythonDictionary: " + o.Count);
}
public void Speak(IronPython.Runtime.PythonTuple o)
{
System.Diagnostics.Debug.WriteLine("PythonTuple: " + o.Count);
}
public virtual string GetName()
{
return "stub";
}
public static void Speak(Entry e)
{
Debug.WriteLine(e.GetName());
}
}
public Microsoft.Scripting.Hosting.ScriptEngine engine;
public Microsoft.Scripting.Hosting.ScriptScope scope;
public FileSystemWatcher fsw;
public DateTime lastWriteTime;
private Action _timerFunc;
public void SetTimerCallback(int time, Action f)
{
_timerFunc = f;
timer1.Interval = time;
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
engine = IronPython.Hosting.Python.CreateEngine();
engine.Runtime.LoadAssembly(System.Reflection.Assembly.GetExecutingAssembly());
scope = engine.CreateScope();
scope.SetVariable("windows", this);
timer1.Enabled = true;
try
{
engine.ExecuteFile("../../scripts/main.py", scope);
}
catch (Exception err)
{
Debug.WriteLine(err);
}
fsw = new FileSystemWatcher("../../scripts");
fsw.Changed += fsw_Changed;
fsw.EnableRaisingEvents = true;
//Debug.WriteLine("ctor.Thread:" + System.Threading.Thread.CurrentThread.ManagedThreadId);
}
void fsw_Changed(object sender, FileSystemEventArgs e)
{
Action f = () =>
{
try
{
var fi = new System.IO.FileInfo(e.FullPath);
if (lastWriteTime != fi.LastWriteTime)
{
using (var fs = fi.OpenRead())
{
using (var rd = new System.IO.StreamReader(fs))
{
var text = rd.ReadToEnd();
//Debug.WriteLine("fsw.Thread:" + System.Threading.Thread.CurrentThread.ManagedThreadId);
lastWriteTime = fi.LastWriteTime;
engine.Execute(text, scope);
rd.Close();
}
fs.Close();
}
}
}
catch (Exception err)
{
System.Diagnostics.Debug.WriteLine(err);
}
};
BeginInvoke(f);
}
private void timer1_Tick(object sender, EventArgs e)
{
if (_timerFunc != null)
_timerFunc();
//System.Diagnostics.Debug.WriteLine("_timerFunc");
//Debug.WriteLine("timer.Thread:" + System.Threading.Thread.CurrentThread.ManagedThreadId);
}
}
}
from NewWorld import *
def wrap(f):
def _(*args):
print("wrap")
return f(*args)
return _
class X(Form1.Entry):
@wrap
def GetName(self):
return Form1.Entry.GetName(self) + ".script"
e = X("USD")
e.Speak({"a":1,})
e.Speak([1,2])
e.Speak((2,3,4))
Form1.Entry.Speak(e)
"""
@wrap
def Sample():
print("sample2")
windows.SetTimerCallback(2000, Sample)
"""
@ialex32x
Copy link
Author

LoadAssembly 所在类如果是非public好像会导致脚本访问不到assembly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment