##// END OF EJS Templates
allow_new_attr to Struct. _ip.options now disallows attr creation
vivainio -
Show More
@@ -169,6 +169,12 b' class IPApi:'
169 169
170 170 def get_options(self):
171 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 178 return self.IP.rc
173 179
174 180 options = property(get_options,None,None,get_options.__doc__)
@@ -1,7 +1,7 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Mimic C structs with lots of extra functionality.
3 3
4 $Id: ipstruct.py 1005 2006-01-12 08:39:26Z fperez $"""
4 $Id: ipstruct.py 1949 2006-11-28 19:12:15Z vivainio $"""
5 5
6 6 #*****************************************************************************
7 7 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
@@ -105,6 +105,7 b' class Struct:'
105 105 Both can be used, but the dictionary must come first:
106 106 Struct(dict), Struct(k1=v1,k2=v2) or Struct(dict,k1=v1,k2=v2).
107 107 """
108 self.__dict__['__allownew'] = True
108 109 if dict is None:
109 110 dict = {}
110 111 if isinstance(dict,Struct):
@@ -117,11 +118,17 b' class Struct:'
117 118 # safety-checked __setitem__
118 119 for k,v in dict.items():
119 120 self[k] = v
121
120 122
121 123 def __setitem__(self,key,value):
122 124 """Used when struct[key] = val calls are made."""
123 125 if key in Struct.__protected:
124 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 132 self.__dict__[key] = value
126 133
127 134 def __setattr__(self, key, value):
@@ -372,5 +379,15 b' class Struct:'
372 379 if not self.has_key(attr):
373 380 self[attr] = val
374 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 392 # end class Struct
376 393
General Comments 0
You need to be logged in to leave comments. Login now