23
23
import sys
24
24
import subprocess
25
25
import keyword
26
- from inspect import getargspec
26
+ from inspect import getcallargs
27
27
from functools import wraps
28
28
29
29
# Notes for v3
@@ -141,12 +141,16 @@ def fastflatten(inarray, dtype):
141
141
142
142
def set_passed_properties (property_names = {}):
143
143
"""
144
- This decorator is intended to wrap the __init__ functions and to
145
- write any selected kwargs into the properties.
146
-
147
- names is a dictionary {key:val}, where each val
144
+ Decorator for device __init__ methods that saves the listed arguments/keyword
145
+ arguments as properties. Argument values as passed to __init__ will be saved, with
146
+ the exception that if an instance attribute exists after __init__ has run that has
147
+ the same name as an argument, the instance attribute will be saved instead of the
148
+ argument value. This allows code within __init__ to process default arguments
149
+ before they are saved.
150
+
151
+ property_names is a dictionary {key:val}, where each val
148
152
is a list [var1, var2, ...] of variables to be pulled from
149
- properties_dict and added to the property with name key (it's location)
153
+ properties_dict and added to the property with name key (its location)
150
154
151
155
internally they are all accessed by calling self.get_property()
152
156
"""
@@ -156,21 +160,24 @@ def new_function(inst, *args, **kwargs):
156
160
157
161
return_value = func (inst , * args , ** kwargs )
158
162
159
- # Introspect arguments and named arguments functions. in python 3 this is
160
- # a pair of func.__something__ calls and no import from argspec is needed
161
- a = getargspec (func )
162
-
163
- if a .defaults is not None :
164
- args_dict = {key :val for key ,val in zip (a .args [- len (a .defaults ):],a .defaults )}
165
- else :
166
- args_dict = {}
167
-
168
- # Update this list with the values from the passed keywords
169
- args_dict .update (kwargs )
163
+ # Get a dict of the call arguments/keyword arguments by name:
164
+ call_values = getcallargs (func , inst , * args , ** kwargs )
165
+
166
+ all_property_names = set ()
167
+ for names in property_names .values ():
168
+ all_property_names .update (names )
169
+
170
+ property_values = {}
171
+ for name in all_property_names :
172
+ # If there is an instance attribute with that name, use that, otherwise
173
+ # use the call value:
174
+ if hasattr (inst , name ):
175
+ property_values [name ] = getattr (inst , name )
176
+ else :
177
+ property_values [name ] = call_values [name ]
170
178
171
- # print args_dict
172
- # print property_names
173
- inst .set_properties (args_dict , property_names )
179
+ # Save them:
180
+ inst .set_properties (property_values , property_names )
174
181
175
182
return return_value
176
183
0 commit comments