Repository navigation
Expand file tree
/
Copy pathPythonPluginProvider.cs
More file actions
177 lines (141 loc) · 6.9 KB
/
Copy pathPythonPluginProvider.cs
File metadata and controls
177 lines (141 loc) · 6.9 KB
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
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using OpenTap.Package;
using Python.Runtime;
namespace OpenTap.Python
{
[PluginOrder(typeof(DotNetTypeDataSearcher))]
public class PythonPluginProvider : ITypeDataSearcher, ITypeDataProvider, ITypeDataSourceProvider
{
class PythonTypeDataSource : ITypeDataSource
{
public string Name { get; }
public string Location { get; }
public IEnumerable<ITypeData> Types => DiscoveredTypes.Values;
public IEnumerable<object> Attributes => Array.Empty<object>();
public IEnumerable<ITypeDataSource> References => new ITypeDataSource[]
{TypeData.GetTypeDataSource(TypeData.FromType(typeof(PythonTypeDataSource)))};
public string Version { get; }
public Dictionary<string, TypeData> DiscoveredTypes { get; } = new Dictionary<string, TypeData>();
public PythonTypeDataSource(string name, string location)
{
Name = name;
Location = location;
Version = Installation.Current.FindPackageContainingFile(Location)?.Version?.ToString() ?? "0.1";
}
}
public PythonPluginProvider()
{
if (!PythonEngine.IsInitialized) TapThread.Start(() => PythonInitializer.LoadPython());
}
static readonly TraceSource log = Log.CreateSource("Python");
public void Search()
{
// ensure that it's loaded.
PythonInitializer.LoadPython();
if (!PythonEngine.IsInitialized) return; //something went wrong and python is not initialized.
using (Py.GIL())
{
var types = new List<TypeData>();
var modules = new List<string>();
foreach (var sp in PythonSettings.Current.GetSearchList())
{
if (Directory.Exists(sp) == false) continue;
var mod2 = Directory.EnumerateDirectories(sp, "*", SearchOption.TopDirectoryOnly)
.Where(dir => Directory.EnumerateFiles(dir, "*.py").Any()).ToList();
modules.AddRange(mod2);
}
if(modules.Any())
Py.Import("opentap");
var sources = new Dictionary<string, PythonTypeDataSource>();
var visitedTypes2 = new HashSet<PyType>();
foreach (var file in modules)
{
var baseModule = Path.GetFileNameWithoutExtension(file);
try
{
var pyFiles = Directory.EnumerateFiles(file, "*.py");
foreach (var py in pyFiles)
{
var subName = Path.GetFileNameWithoutExtension(py);
var moduleName = baseModule;
if (subName != "__init__")
{
moduleName = baseModule + "." + subName;
}
if (moduleName == "Python.opentap")
continue;
log.Debug("Loading: {0}", moduleName);
PyObject module;
try
{
module = Py.Import(moduleName);
}
catch(Exception e){
log.Error("Caught exception loading {0}: {1}", moduleName, e.Message);
log.Debug(e);
continue;
}
var files = module.GetAttr("__dict__");
var objValues = new PyDict(files);
var values = objValues.Values().ToArray();
for (int i = 0; i < values.Length; i++)
{
var _item = values[i];
var name = _item.GetPythonType().Name;
if (name != "CLRMetatype") continue;
var pyType = new PyType(_item);
var type = (Type) _item.AsManagedObject(typeof(Type));
if (!type.Assembly.IsDynamic)
continue;
if (!pyType.HasAttr("__module__")) continue;
var mod = pyType.GetAttr("__module__").As<string>();
if (!sources.TryGetValue(mod, out var asm))
{
asm = sources[mod] = new PythonTypeDataSource(mod, py);
}
var td = TypeData.FromType(type);
if (visitedTypes2.Add(pyType))
types.Add(td);
asm.DiscoveredTypes[td.Name] = td;
}
}
}
catch (Exception e)
{
log.Error("Caught exception loading {0}: {1}", file, e.Message);
log.Debug(e);
}
}
PythonPluginProvider.types =
types.ToImmutableDictionary(x => x.Name, x => new PythonTypeDataWrapper(x));
PythonPluginProvider.typeDataSources = sources.Values
.SelectMany(x => x.DiscoveredTypes.Select(y => (y.Value, x)))
.ToImmutableDictionary(x => new PythonTypeDataWrapper(x.Value), x => x.x);
}
}
static ImmutableDictionary<PythonTypeDataWrapper, PythonTypeDataSource> typeDataSources =
ImmutableDictionary<PythonTypeDataWrapper, PythonTypeDataSource>.Empty;
static ImmutableDictionary<string, PythonTypeDataWrapper> types =
ImmutableDictionary<string, PythonTypeDataWrapper>.Empty;
public IEnumerable<ITypeData> Types => types.Values;
public ITypeDataSource GetSource(ITypeData typeData)
{
while (!(typeData is TypeData) && typeData != null)
typeData = typeData.BaseType;
if (typeData is TypeData td && td.Type.Assembly.IsDynamic &&
typeDataSources.TryGetValue(new PythonTypeDataWrapper(td), out var src))
return src;
return null;
}
public ITypeData GetTypeData(string identifier) => types.GetValueOrDefault(identifier);
public ITypeData GetTypeData(object obj)
{
return null;
}
public double Priority => 1.0;
}
}