Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPyObjectAnnotator.cs
More file actions
Latest commit
54 lines (47 loc) · 1.99 KB
/
Copy pathPyObjectAnnotator.cs
File metadata and controls
54 lines (47 loc) · 1.99 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
usingSystem.Collections.Generic;
usingPython.Runtime;
namespaceOpenTap.Python;
// PyObject properties can be anything, but
// we have modified PythonNet to add PythonTypeAttribute
// to properties which are python types, so we are able
// to resolve the type and make decisions on how to handle them.
// Currently only enum-types are supported.
publicclassPyObjectAnnotator:IAnnotator
{
publicvoidAnnotate(AnnotationCollectionannotations)
{
varmem=annotations.Get<IMemberAnnotation>();
if(mem==null)return;
if(mem.ReflectionInfo.DescendsTo(typeof(PyObject))==false)return;
varpythonTypeAttr=mem.Member.GetAttribute<PythonTypeAttribute>();
if(pythonTypeAttr==null)return;
using(Py.GIL())
{
PyObjectmod=Py.Import(pythonTypeAttr.Module);
vartype=newPyType(mod.GetAttr(pythonTypeAttr.TypeName));
if(type.HasAttr("_member_map_"))
{
// Handling enum: enums has _member_map_.
varmember_map=type.GetAttr("_member_map_");
try
{
// in some pythons this is not a dict but a OrderedDict.
// so we have to manually pull the values
varlist=newList<PyObject>();
usingvarvalues=newPyIterable(member_map.InvokeMethod("values"));
foreach(varpairinvalues)
list.Add(type.GetAttr(pair.GetAttr("name")));
annotations.Add(newPythonEnumValueProperty(list));
}
catch
{
}
}
// classes that implement describe() can provide a description string for a value.
// see for example EnumUsage.
if(type.HasAttr("describe"))
annotations.Add(newPythonDescribeValueAnnotation(annotations));
}
}
publicdoublePriority=>0.0;
}