Show More
@@ -169,6 +169,12 b' class IPApi:' | |||||
169 |
|
169 | |||
170 | def get_options(self): |
|
170 | def get_options(self): | |
171 | """All configurable variables.""" |
|
171 | """All configurable variables.""" | |
|
172 | ||||
|
173 | # catch typos by disabling new attribute creation. If new attr creation | |||
|
174 | # is in fact wanted (e.g. when exposing new options), do allow_new_attr(True) | |||
|
175 | # for the received rc struct. | |||
|
176 | ||||
|
177 | self.IP.rc.allow_new_attr(False) | |||
172 | return self.IP.rc |
|
178 | return self.IP.rc | |
173 |
|
179 | |||
174 | options = property(get_options,None,None,get_options.__doc__) |
|
180 | options = property(get_options,None,None,get_options.__doc__) |
@@ -1,7 +1,7 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """Mimic C structs with lots of extra functionality. |
|
2 | """Mimic C structs with lots of extra functionality. | |
3 |
|
3 | |||
4 |
$Id: ipstruct.py 1 |
|
4 | $Id: ipstruct.py 1949 2006-11-28 19:12:15Z vivainio $""" | |
5 |
|
5 | |||
6 | #***************************************************************************** |
|
6 | #***************************************************************************** | |
7 | # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu> |
|
7 | # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu> | |
@@ -105,6 +105,7 b' class Struct:' | |||||
105 | Both can be used, but the dictionary must come first: |
|
105 | Both can be used, but the dictionary must come first: | |
106 | Struct(dict), Struct(k1=v1,k2=v2) or Struct(dict,k1=v1,k2=v2). |
|
106 | Struct(dict), Struct(k1=v1,k2=v2) or Struct(dict,k1=v1,k2=v2). | |
107 | """ |
|
107 | """ | |
|
108 | self.__dict__['__allownew'] = True | |||
108 | if dict is None: |
|
109 | if dict is None: | |
109 | dict = {} |
|
110 | dict = {} | |
110 | if isinstance(dict,Struct): |
|
111 | if isinstance(dict,Struct): | |
@@ -117,11 +118,17 b' class Struct:' | |||||
117 | # safety-checked __setitem__ |
|
118 | # safety-checked __setitem__ | |
118 | for k,v in dict.items(): |
|
119 | for k,v in dict.items(): | |
119 | self[k] = v |
|
120 | self[k] = v | |
|
121 | ||||
120 |
|
122 | |||
121 | def __setitem__(self,key,value): |
|
123 | def __setitem__(self,key,value): | |
122 | """Used when struct[key] = val calls are made.""" |
|
124 | """Used when struct[key] = val calls are made.""" | |
123 | if key in Struct.__protected: |
|
125 | if key in Struct.__protected: | |
124 | raise KeyError,'Key '+`key`+' is a protected key of class Struct.' |
|
126 | raise KeyError,'Key '+`key`+' is a protected key of class Struct.' | |
|
127 | if not self['__allownew'] and key not in self.__dict__: | |||
|
128 | raise KeyError( | |||
|
129 | "Can't create unknown attribute %s - Check for typos, or use allow_new_attr to create new attributes!" % | |||
|
130 | key) | |||
|
131 | ||||
125 | self.__dict__[key] = value |
|
132 | self.__dict__[key] = value | |
126 |
|
133 | |||
127 | def __setattr__(self, key, value): |
|
134 | def __setattr__(self, key, value): | |
@@ -372,5 +379,15 b' class Struct:' | |||||
372 | if not self.has_key(attr): |
|
379 | if not self.has_key(attr): | |
373 | self[attr] = val |
|
380 | self[attr] = val | |
374 | return self.get(attr,val) |
|
381 | return self.get(attr,val) | |
|
382 | ||||
|
383 | def allow_new_attr(self, allow = True): | |||
|
384 | """ Set whether new attributes can be created inside struct | |||
|
385 | ||||
|
386 | This can be used to catch typos by verifying that the attribute user tries to | |||
|
387 | change already exists in this Struct. | |||
|
388 | """ | |||
|
389 | self['__allownew'] = allow | |||
|
390 | ||||
|
391 | ||||
375 | # end class Struct |
|
392 | # end class Struct | |
376 |
|
393 |
General Comments 0
You need to be logged in to leave comments.
Login now