-
Notifications
You must be signed in to change notification settings - Fork 4
/
IronPythonCommandsMefExport.cs
219 lines (195 loc) · 8.11 KB
/
IronPythonCommandsMefExport.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;
using System.IO;
using System.Reflection;
using System.Threading;
using Core;
using Core.API;
using Core.Abstractions;
using IronPython.Hosting;
using System.Linq;
using System.Threading.Tasks;
using IronPython.Runtime;
using IronPythonMef;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using Plugins.Commands;
namespace Plugins.IronPython
{
public class IronPythonCommandsMefExport : IStartupTask
{
private const string IronPythonScriptExtension = ".py";
private readonly CompositionContainer _mefContainer;
private readonly ILog _log;
private ScriptEngine _engine;
private Dictionary<string, IronPythonFile> _files = new Dictionary<string, IronPythonFile>();
private Dictionary<string,FileSystemWatcher> _watchers = new Dictionary<string, FileSystemWatcher>();
public EventHandler RefreshedFiles;
public static IList<Type> InitialScopeTypes = new[]
{
typeof (IItem), typeof (IConverter<>), typeof (BaseActOnTypedItem<>),
typeof (BaseActOnTypedItemAndReturnTypedItem<,>),
typeof (IItemSource),
typeof (BaseItemSource),
typeof (IConverter),
typeof (ICommand),
typeof (IRequest),
typeof (IActOnItem),
typeof (IActOnItemWithArguments),
typeof (IActOnTypedItemWithArguments<>),
typeof (IActOnTypedItemWithAutoCompletedArguments<>),
typeof (ICanActOnTypedItem<>),
typeof (IActOnTypedItemAndReturnItem<>),
typeof (IActOnTypedItemAndReturnTypedItem<,>),
typeof (IActOnTypedItemWithArgumentsAndReturnTypedItem<,>),
typeof (ArgumentAutoCompletionResult),
typeof (BasePythonItemSource),
typeof (IronPythonImportDefinition)
};
[ImportConfiguration]
public CoreConfiguration CoreConfiguration { get; set; }
[ImportConfiguration]
public Configuration Configuration { get; set; }
public IronPythonCommandsMefExport(CompositionContainer mefContainer, ILog log)
{
_mefContainer = mefContainer;
_log = log;
RefreshedFiles += (e, s) => { };
}
public bool Executed { get; private set; }
public Task Execute()
{
try
{
Thread.Sleep(TimeSpan.FromSeconds(5));
_engine = Python.CreateEngine();
foreach (var directory in GetIronPythonPluginsDirectories())
{
var pythonFiles =
directory.GetFiles().Where(f => f.Extension.ToLowerInvariant() == IronPythonScriptExtension);
foreach (var pythonFile in pythonFiles)
{
AddIronPythonFile(pythonFile);
}
var watcher = new FileSystemWatcher(directory.FullName, "*"+IronPythonScriptExtension);
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Created += new FileSystemEventHandler(_watcher_Created);
watcher.Deleted += new FileSystemEventHandler(_watcher_Deleted);
watcher.Changed += new FileSystemEventHandler(_watcher_Changed);
watcher.Renamed += new RenamedEventHandler(_watcher_Renamed);
watcher.InternalBufferSize = pythonFiles.Count() + 10; // TODO: make this a bit smaller, or cleverer..
_watchers[directory.FullName] = watcher;
}
foreach (var watcher in _watchers.Values)
{
watcher.EnableRaisingEvents = true;
}
}
finally
{
Executed = true;
}
return Task.CompletedTask;
}
void _watcher_Renamed(object sender, RenamedEventArgs e)
{
if (_files.ContainsKey(e.OldFullPath))
{
_log.Info("Removing and decomposing file {0}", e.OldFullPath);
_files.Remove(e.OldFullPath);
_files[e.OldFullPath].Decompose();
}
else
{
_log.Warn("File {0} not found in the ironpython cache but got notified it was renamed");
}
if (_files.ContainsKey(e.FullPath))
{
_log.Info("Removing and decomposing file {0}", e.FullPath);
_files[e.FullPath].Decompose();
_files.Remove(e.FullPath);
}
AddIronPythonFile(new FileInfo(e.FullPath));
RefreshedFiles(this, new EventArgs());
}
void _watcher_Changed(object sender, FileSystemEventArgs e)
{
if(!_files.ContainsKey(e.FullPath))
{
_log.Warn("File {0} not found in the ironpython cache but got notified it changed");
return;
}
_log.Info("Reloading file {0}", e.FullPath);
try
{
_files[e.FullPath].Compose();
}
catch (IronPythonFile.PythonException ex)
{
_log.Error(ex, "Error executing file {0}:{1}", e.FullPath, ex.Message);
}
catch (Exception ex)
{
_log.Error(ex, "Error executing file {0}:{1}", e.FullPath, ex.Message);
}
RefreshedFiles(this, new EventArgs());
}
void _watcher_Deleted(object sender, FileSystemEventArgs e)
{
if(!_files.ContainsKey(e.FullPath))
{
_log.Warn("File {0} not found in the ironpython cache but got notified it was deleted");
return;
}
var f = _files[e.FullPath];
_log.Info("Removing and decomposing file {0}", e.FullPath);
_files.Remove(e.FullPath);
f.Decompose();
RefreshedFiles(this, new EventArgs());
}
void _watcher_Created(object sender, FileSystemEventArgs e)
{
AddIronPythonFile(new FileInfo(e.FullPath));
RefreshedFiles(this, new EventArgs());
}
private void AddIronPythonFile(FileInfo pythonFile)
{
_log.Debug("Loading ironpython file {0}", pythonFile.FullName);
var file = new IronPythonFile(pythonFile, _engine, _mefContainer, new ExtractTypesFromScript(_engine));
_files[pythonFile.FullName] = file;
try
{
file.Compose();
}
catch (IronPythonFile.PythonException e)
{
_log.Error(e, "Error executing file {0}:{1}", pythonFile.FullName, e.Message);
}
catch (Exception e)
{
_log.Error(e, "Error executing file {0}:{1}", pythonFile.FullName, e.Message);
}
}
private IEnumerable<DirectoryInfo> GetIronPythonPluginsDirectories()
{
IEnumerable<DirectoryInfo> directoryInfos = CoreConfiguration
.ExpandPaths(Configuration.ScriptDirectories)
.Select(p => Path.GetFullPath(p))
.Distinct()
.Select(p => new DirectoryInfo(p));
foreach (var directory in directoryInfos)
{
if(!directory.Exists)
{
_log.Warn("Directory {0} doesn't exist", directory.FullName);
continue;
}
yield return directory;
}
}
}
}