@@ -21,16 +21,18 @@ def test(self):
21
21
string z = x.test; // calls into python and returns "x"
22
22
"""
23
23
24
- def __init__ (self , type_ , fget = None , fset = None ):
24
+ def __init__ (self , type_ , fget = None , fset = None , attributes = [] ):
25
25
self .__name__ = getattr (fget , "__name__" , None )
26
26
self ._clr_property_type_ = type_
27
27
self .fget = fget
28
28
self .fset = fset
29
-
29
+ self . _clr_attributes_ = attributes
30
30
def __call__ (self , fget ):
31
- return self .__class__ (self ._clr_property_type_ ,
31
+ self .__class__ (self ._clr_property_type_ ,
32
32
fget = fget ,
33
- fset = self .fset )
33
+ fset = self .fset ,
34
+ attributes = self ._clr_attributes_ )
35
+
34
36
35
37
def setter (self , fset ):
36
38
self .fset = fset
@@ -47,8 +49,32 @@ def __set__(self, instance, value):
47
49
if not self .fset :
48
50
raise AttributeError ("%s is read-only" % self .__name__ )
49
51
return self .fset .__get__ (instance , None )(value )
52
+ def add_attribute (self , attribute ):
53
+ self ._clr_attributes_ .append (attribute )
54
+ return self
50
55
56
+ class property (object ):
51
57
58
+ def __init__ (self , type , default ):
59
+ import weakref
60
+ self ._clr_property_type_ = type
61
+ self .default = default
62
+ self .values = weakref .WeakKeyDictionary ()
63
+ self ._clr_attributes_ = []
64
+ self .fget = 1
65
+ self .fset = 1
66
+ def __get__ (self , instance , owner ):
67
+ v = self .values .get (instance , self .default )
68
+ return v
69
+ def __set__ (self , instance , value ):
70
+ self .values [instance ] = value
71
+ def add_attribute (self , attribute ):
72
+ self ._clr_attributes_ .append (attribute )
73
+ return self
74
+ def __call__ (self , type , default ):
75
+ self2 = self .__class__ (self ._clr_property_type_ , type , default )
76
+ self2 ._clr_attributes_ = self ._clr_attributes_
77
+ return self2
52
78
class clrmethod (object ):
53
79
"""
54
80
Method decorator for exposing python methods to .NET.
@@ -67,18 +93,47 @@ def test(self, x):
67
93
int z = x.test("hello"); // calls into python and returns len("hello")
68
94
"""
69
95
70
- def __init__ (self , return_type , arg_types , clrname = None , func = None ):
96
+ def __init__ (self , return_type = None , arg_types = [], clrname = None , func = None , ** kwargs ):
97
+ if return_type == None :
98
+ import System
99
+ return_type = System .Void
71
100
self .__name__ = getattr (func , "__name__" , None )
72
101
self ._clr_return_type_ = return_type
73
102
self ._clr_arg_types_ = arg_types
74
103
self ._clr_method_name_ = clrname or self .__name__
75
104
self .__func = func
105
+ if 'attributes' in kwargs :
106
+ self ._clr_attributes_ = kwargs ["attributes" ]
107
+ else :
108
+ self ._clr_attributes_ = []
76
109
77
110
def __call__ (self , func ):
78
- return self .__class__ (self ._clr_return_type_ ,
111
+ self2 = self .__class__ (self ._clr_return_type_ ,
79
112
self ._clr_arg_types_ ,
80
113
clrname = self ._clr_method_name_ ,
81
114
func = func )
115
+ self2 ._clr_attributes_ = self ._clr_attributes_
116
+ return self2
82
117
83
118
def __get__ (self , instance , owner ):
84
119
return self .__func .__get__ (instance , owner )
120
+
121
+ def clr_attribute (self , attribute ):
122
+ self ._clr_attributes_ .append (attribute )
123
+ return self
124
+
125
+ class attribute (object ):
126
+
127
+ def __init__ (self , attr , * args , ** kwargs ):
128
+ self .attr = attr
129
+ import Python .Runtime
130
+ #todo: ensure that attributes only are pushed when @ is used.
131
+ #import inspect
132
+ #Python.Runtime.PythonDerivedType.Test(inspect.stack()[1].code_context)
133
+
134
+ Python .Runtime .PythonDerivedType .PushAttribute (attr )
135
+ def __call__ (self , x ):
136
+ import Python .Runtime
137
+ if Python .Runtime .PythonDerivedType .AssocAttribute (self .attr , x ):
138
+ pass
139
+ return x
0 commit comments