forked from rmadsen-ks/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclr.py
More file actions
171 lines (144 loc) · 5.26 KB
/
clr.py
File metadata and controls
171 lines (144 loc) · 5.26 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
"""
Code in this module gets loaded into the main clr module.
"""
class clrproperty(object):
"""
Property decorator for exposing python properties to .NET.
The property type must be specified as the only argument to clrproperty.
e.g.::
class X(object):
@clrproperty(string)
def test(self):
return "x"
Properties decorated this way can be called from .NET, e.g.::
dynamic x = getX(); // get an instance of X declared in Python
string z = x.test; // calls into python and returns "x"
"""
def __init__(self, type_, fget=None, fset=None, attributes = []):
self.__name__ = getattr(fget, "__name__", None)
self._clr_property_type_ = type_
self.fget = fget
self.fset = fset
self._clr_attributes_ = attributes
def __call__(self, fget):
return self.__class__(self._clr_property_type_,
fget=fget,
fset=self.fset,
attributes = self._clr_attributes_)
def setter(self, fset):
self.fset = fset
return self
def getter(self, fget):
self.fget = fget
return self
def __get__(self, instance, owner):
return self.fget.__get__(instance, owner)()
def __set__(self, instance, value):
if not self.fset:
raise AttributeError("%s is read-only" % self.__name__)
return self.fset.__get__(instance, None)(value)
def add_attribute(self, *args, **kwargs):
lst = []
if len(args) > 0:
if isinstance(args[0], tuple):
lst = args
else:
lst = [(args[0], args[1:], kwargs)]
self._clr_attributes_.extend(lst)
return self
class clrmethod(object):
"""
Method decorator for exposing python methods to .NET.
The argument and return types must be specified as arguments to clrmethod.
e.g.::
class X(object):
@clrmethod(int, [str])
def test(self, x):
return len(x)
Methods decorated this way can be called from .NET, e.g.::
dynamic x = getX(); // get an instance of X declared in Python
int z = x.test("hello"); // calls into python and returns len("hello")
"""
def __init__(self, return_type = None, arg_types = [], clrname=None, func=None, **kwargs):
if return_type == None:
import System
return_type = System.Void
self.__name__ = getattr(func, "__name__", None)
self._clr_return_type_ = return_type
self._clr_arg_types_ = arg_types
self._clr_method_name_ = clrname or self.__name__
self.__func = func
if 'attributes' in kwargs:
self._clr_attributes_ = kwargs["attributes"]
else:
self._clr_attributes_ = []
def __call__(self, func):
self2 = self.__class__(self._clr_return_type_,
self._clr_arg_types_,
clrname=self._clr_method_name_,
func=func)
self2._clr_attributes_ = self._clr_attributes_
return self2
def __get__(self, instance, owner):
return self.__func.__get__(instance, owner)
def add_attribute(self, *args, **kwargs):
lst = []
if len(args) > 0:
if isinstance(args[0], tuple):
lst = args
else:
lst = [(args[0], args[1:], kwargs)]
self._clr_attributes_.extend(lst)
return self
class property(object):
def __init__(self, type, default = None):
import weakref
self._clr_property_type_ = type
self.default = default
self.values = weakref.WeakKeyDictionary()
self._clr_attributes_ = []
self.fget = 1
self.fset = 1
def __get__(self, instance, owner):
if self.fget != 1:
return self.fget(instance)
v = self.values.get(instance, self.default)
return v
def __set__(self, instance, value):
if self.fset != 1:
self.fset(instance,value)
return
self.values[instance] = value
def add_attribute(self, *args, **kwargs):
lst = []
if len(args) > 0:
if isinstance(args[0], tuple):
lst = args
else:
lst = [(args[0], args[1:], kwargs)]
self._clr_attributes_.extend(lst)
return self
def __call__(self, func):
self2 = self.__class__(self._clr_property_type_, None)
self2.fget = func
self2._clr_attributes_ = self._clr_attributes_
return self2
class attribute(object):
def __init__(self, *args, **kwargs):
lst = []
if len(args) > 0:
if isinstance(args[0], tuple):
lst = args
else:
lst = [(args[0], args[1:], kwargs)]
import Python.Runtime
#todo: ensure that attributes only are pushed when @ is used.
self.attr = lst
for item in lst:
Python.Runtime.PythonDerivedType.PushAttribute(item)
def __call__(self, x):
import Python.Runtime
for item in self.attr:
if Python.Runtime.PythonDerivedType.AssocAttribute(item, x):
pass
return x