1 from __future__
import generators
7 #####################################################################
9 # M5 Python Configuration Utility
11 # The basic idea is to write simple Python programs that build Python
12 # objects corresponding to M5 SimObjects for the deisred simulation
13 # configuration. For now, the Python emits a .ini file that can be
14 # parsed by M5. In the future, some tighter integration between M5
15 # and the Python interpreter may allow bypassing the .ini file.
17 # Each SimObject class in M5 is represented by a Python class with the
18 # same name. The Python inheritance tree mirrors the M5 C++ tree
19 # (e.g., SimpleCPU derives from BaseCPU in both cases, and all
20 # SimObjects inherit from a single SimObject base class). To specify
21 # an instance of an M5 SimObject in a configuration, the user simply
22 # instantiates the corresponding Python object. The parameters for
23 # that SimObject are given by assigning to attributes of the Python
24 # object, either using keyword assignment in the constructor or in
25 # separate assignment statements. For example:
27 # cache = BaseCache('my_cache', root, size=64*K)
28 # cache.hit_latency = 3
31 # (The first two constructor arguments specify the name of the created
32 # cache and its parent node in the hierarchy.)
34 # The magic lies in the mapping of the Python attributes for SimObject
35 # classes to the actual SimObject parameter specifications. This
36 # allows parameter validity checking in the Python code. Continuing
37 # the example above, the statements "cache.blurfl=3" or
38 # "cache.assoc='hello'" would both result in runtime errors in Python,
39 # since the BaseCache object has no 'blurfl' parameter and the 'assoc'
40 # parameter requires an integer, respectively. This magic is done
41 # primarily by overriding the special __setattr__ method that controls
42 # assignment to object attributes.
44 # The Python module provides another class, ConfigNode, which is a
45 # superclass of SimObject. ConfigNode implements the parent/child
46 # relationship for building the configuration hierarchy tree.
47 # Concrete instances of ConfigNode can be used to group objects in the
48 # hierarchy, but do not correspond to SimObjects themselves (like a
49 # .ini section with "children=" but no "type=".
51 # Once a set of Python objects have been instantiated in a hierarchy,
52 # calling 'instantiate(obj)' (where obj is the root of the hierarchy)
53 # will generate a .ini file. See simple-4cpu.py for an example
54 # (corresponding to m5-test/simple-4cpu.ini).
56 #####################################################################
58 #####################################################################
60 # ConfigNode/SimObject classes
62 # The Python class hierarchy rooted by ConfigNode (which is the base
63 # class of SimObject, which in turn is the base class of all other M5
64 # SimObject classes) has special attribute behavior. In general, an
65 # object in this hierarchy has three categories of attribute-like
68 # 1. Regular Python methods and variables. These must start with an
69 # underscore to be treated normally.
71 # 2. SimObject parameters. These values are stored as normal Python
72 # attributes, but all assignments to these attributes are checked
73 # against the pre-defined set of parameters stored in the class's
74 # _param_dict dictionary. Assignments to attributes that do not
75 # correspond to predefined parameters, or that are not of the correct
76 # type, incur runtime errors.
78 # 3. Hierarchy children. The child nodes of a ConfigNode are stored
79 # in the node's _children dictionary, but can be accessed using the
80 # Python attribute dot-notation (just as they are printed out by the
81 # simulator). Children cannot be created using attribute assigment;
82 # they must be added by specifying the parent node in the child's
83 # constructor or using the '+=' operator.
85 # The SimObject parameters are the most complex, for a few reasons.
86 # First, both parameter descriptions and parameter values are
87 # inherited. Thus parameter description lookup must go up the
88 # inheritance chain like normal attribute lookup, but this behavior
89 # must be explicitly coded since the lookup occurs in each class's
90 # _param_dict attribute. Second, because parameter values can be set
91 # on SimObject classes (to implement default values), the parameter
92 # checking behavior must be enforced on class attribute assignments as
93 # well as instance attribute assignments. Finally, because we allow
94 # class specialization via inheritance (e.g., see the L1Cache class in
95 # the simple-4cpu.py example), we must do parameter checking even on
96 # class instantiation. To provide all these features, we use a
97 # metaclass to define most of the SimObject parameter behavior for
98 # this class hierarchy.
100 #####################################################################
102 # The metaclass for ConfigNode (and thus for everything that derives
103 # from ConfigNode, including SimObject). This class controls how new
104 # classes that derive from ConfigNode are instantiated, and provides
105 # inherited class behavior (just like a class controls how instances
106 # of that class are instantiated, and provides inherited instance
108 class MetaConfigNode(type):
110 # __new__ is called before __init__, and is where the statements
111 # in the body of the class definition get loaded into the class's
112 # __dict__. We intercept this to filter out parameter assignments
113 # and only allow "private" attributes to be passed to the base
114 # __new__ (starting with underscore).
115 def __new__(cls
, name
, bases
, dict):
116 priv_keys
= [k
for k
in dict.iterkeys() if k
.startswith('_')]
118 for k
in priv_keys
: priv_dict
[k
] = dict[k
]; del dict[k
]
119 # entries left in dict will get passed to __init__, where we'll
120 # deal with them as params.
121 return super(MetaConfigNode
, cls
).__new
__(cls
, name
, bases
, priv_dict
)
123 # initialization: start out with an empty param dict (makes life
124 # simpler if we can assume _param_dict is always valid). Also
125 # build inheritance list to simplify searching for inherited
126 # params. Finally set parameters specified in class definition
128 def __init__(cls
, name
, bases
, dict):
129 super(MetaConfigNode
, cls
).__init
__(cls
, name
, bases
, {})
130 # initialize _param_dict to empty
132 # __mro__ is the ordered list of classes Python uses for
133 # method resolution. We want to pick out the ones that have a
134 # _param_dict attribute for doing parameter lookups.
136 [c
for c
in cls
.__mro
__ if hasattr(c
, '_param_dict')]
137 # initialize attributes with values from class definition
138 for (pname
, value
) in dict.items():
140 setattr(cls
, pname
, value
)
141 except Exception, exc
:
142 print "Error setting '%s' to '%s' on class '%s'\n" \
143 % (pname
, value
, cls
.__name
__), exc
145 # set the class's parameter dictionary (called when loading
146 # class descriptions)
147 def set_param_dict(cls
, param_dict
):
148 # should only be called once (current one should be empty one
150 assert not cls
._param
_dict
151 cls
._param
_dict
= param_dict
152 # initialize attributes with default values
153 for (pname
, param
) in param_dict
.items():
155 setattr(cls
, pname
, param
.default
)
156 except Exception, exc
:
157 print "Error setting '%s' default on class '%s'\n" \
158 % (pname
, cls
.__name
__), exc
160 # Lookup a parameter description by name in the given class. Use
161 # the _param_bases list defined in __init__ to go up the
162 # inheritance hierarchy if necessary.
163 def lookup_param(cls
, param_name
):
164 for c
in cls
._param
_bases
:
165 param
= c
._param
_dict
.get(param_name
)
166 if param
: return param
169 # Set attribute (called on foo.attr_name = value when foo is an
170 # instance of class cls).
171 def __setattr__(cls
, attr_name
, value
):
172 # normal processing for private attributes
173 if attr_name
.startswith('_'):
174 object.__setattr
__(cls
, attr_name
, value
)
176 # no '_': must be SimObject param
177 param
= cls
.lookup_param(attr_name
)
179 raise AttributeError, \
180 "Class %s has no parameter %s" % (cls
.__name
__, attr_name
)
181 # It's ok: set attribute by delegating to 'object' class.
182 # Note the use of param.make_value() to verify/canonicalize
184 object.__setattr
__(cls
, attr_name
, param
.make_value(value
))
186 # generator that iterates across all parameters for this class and
187 # all classes it inherits from
188 def all_param_names(cls
):
189 for c
in cls
._param
_bases
:
190 for p
in c
._param
_dict
.iterkeys():
193 # The ConfigNode class is the root of the special hierarchy. Most of
194 # the code in this class deals with the configuration hierarchy itself
195 # (parent/child node relationships).
196 class ConfigNode(object):
197 # Specify metaclass. Any class inheriting from ConfigNode will
198 # get this metaclass.
199 __metaclass__
= MetaConfigNode
201 # Constructor. Since bare ConfigNodes don't have parameters, just
202 # worry about the name and the parent/child stuff.
203 def __init__(self
, _name
, _parent
=None):
205 if type(_name
) != str:
206 if isinstance(_name
, ConfigNode
):
207 # special case message for common error of trying to
208 # coerce a SimObject to the wrong type
210 "Attempt to coerce %s to %s" \
211 % (_name
.__class
__.__name
__, self
.__class
__.__name
__)
214 "%s name must be string (was %s, %s)" \
215 % (self
.__class
__.__name
__, _name
, type(_name
))
216 # if specified, parent must be a subclass of ConfigNode
217 if _parent
!= None and not isinstance(_parent
, ConfigNode
):
219 "%s parent must be ConfigNode subclass (was %s, %s)" \
220 % (self
.__class
__.__name
__, _name
, type(_name
))
222 self
._parent
= _parent
225 _parent
.__addChild
(self
)
226 # Set up absolute path from root.
227 if (_parent
and _parent
._path
!= 'Universe'):
228 self
._path
= _parent
._path
+ '.' + self
._name
230 self
._path
= self
._name
232 # When printing (e.g. to .ini file), just give the name.
236 # Catch attribute accesses that could be requesting children, and
237 # satisfy them. Note that __getattr__ is called only if the
238 # regular attribute lookup fails, so private and parameter lookups
239 # will already be satisfied before we ever get here.
240 def __getattr__(self
, name
):
242 return self
._children
[name
]
244 raise AttributeError, \
245 "Node '%s' has no attribute or child '%s'" \
248 # Set attribute. All attribute assignments go through here. Must
249 # be private attribute (starts with '_') or valid parameter entry.
250 # Basically identical to MetaConfigClass.__setattr__(), except
251 # this handles instances rather than class attributes.
252 def __setattr__(self
, attr_name
, value
):
253 if attr_name
.startswith('_'):
254 object.__setattr
__(self
, attr_name
, value
)
256 # not private; look up as param
257 param
= self
.__class
__.lookup_param(attr_name
)
259 raise AttributeError, \
260 "Class %s has no parameter %s" \
261 % (self
.__class
__.__name
__, attr_name
)
262 # It's ok: set attribute by delegating to 'object' class.
263 # Note the use of param.make_value() to verify/canonicalize
265 object.__setattr
__(self
, attr_name
, param
.make_value(value
))
267 # Add a child to this node.
268 def __addChild(self
, new_child
):
269 # set child's parent before calling this function
270 assert new_child
._parent
== self
271 if not isinstance(new_child
, ConfigNode
):
273 "ConfigNode child must also be of class ConfigNode"
274 if new_child
._name
in self
._children
:
275 raise AttributeError, \
276 "Node '%s' already has a child '%s'" \
277 % (self
._name
, new_child
._name
)
278 self
._children
[new_child
._name
] = new_child
280 # operator overload for '+='. You can say "node += child" to add
281 # a child that was created with parent=None. An early attempt
282 # at playing with syntax; turns out not to be that useful.
283 def __iadd__(self
, new_child
):
284 if new_child
._parent
!= None:
285 raise AttributeError, \
286 "Node '%s' already has a parent" % new_child
._name
287 new_child
._parent
= self
288 self
.__addChild
(new_child
)
291 # Print instance info to .ini file.
292 def _instantiate(self
):
293 print '[' + self
._path
+ ']' # .ini section header
295 # instantiate children in sorted order for backward
296 # compatibility (else we can end up with cpu1 before cpu0).
297 child_names
= self
._children
.keys()
300 for child_name
in child_names
:
303 self
._instantiateParams
()
305 # recursively dump out children
307 for child_name
in child_names
:
308 self
._children
[child_name
]._instantiate
()
310 # ConfigNodes have no parameters. Overridden by SimObject.
311 def _instantiateParams(self
):
314 # SimObject is a minimal extension of ConfigNode, implementing a
315 # hierarchy node that corresponds to an M5 SimObject. It prints out a
316 # "type=" line to indicate its SimObject class, prints out the
317 # assigned parameters corresponding to its class, and allows
318 # parameters to be set by keyword in the constructor. Note that most
319 # of the heavy lifting for the SimObject param handling is done in the
320 # MetaConfigNode metaclass.
322 class SimObject(ConfigNode
):
323 # initialization: like ConfigNode, but handle keyword-based
324 # parameter initializers.
325 def __init__(self
, _name
, _parent
=None, **params
):
326 ConfigNode
.__init
__(self
, _name
, _parent
)
327 for param
, value
in params
.items():
328 setattr(self
, param
, value
)
330 # print type and parameter values to .ini file
331 def _instantiateParams(self
):
332 print "type =", self
.__class
__._name
333 for pname
in self
.__class
__.all_param_names():
334 value
= getattr(self
, pname
)
336 print pname
, '=', value
340 param_names
= cls
._param
_dict
.keys()
342 code
= "BEGIN_DECLARE_SIM_OBJECT_PARAMS(%s)\n" % name
343 decls
= [" " + cls
._param
_dict
[pname
].sim_decl(pname
) \
344 for pname
in param_names
]
345 code
+= "\n".join(decls
) + "\n"
346 code
+= "END_DECLARE_SIM_OBJECT_PARAMS(%s)\n\n" % name
347 code
+= "BEGIN_INIT_SIM_OBJECT_PARAMS(%s)\n" % name
348 inits
= [" " + cls
._param
_dict
[pname
].sim_init(pname
) \
349 for pname
in param_names
]
350 code
+= ",\n".join(inits
) + "\n"
351 code
+= "END_INIT_SIM_OBJECT_PARAMS(%s)\n\n" % name
353 _sim_code
= classmethod(_sim_code
)
355 #####################################################################
357 # Parameter description classes
359 # The _param_dict dictionary in each class maps parameter names to
360 # either a Param or a VectorParam object. These objects contain the
361 # parameter description string, the parameter type, and the default
362 # value (loaded from the PARAM section of the .odesc files). The
363 # make_value() method on these objects is used to force whatever value
364 # is assigned to the parameter to the appropriate type.
366 # Note that the default values are loaded into the class's attribute
367 # space when the parameter dictionary is initialized (in
368 # MetaConfigNode.set_param_dict()); after that point they aren't
371 #####################################################################
373 def isNullPointer(value
):
374 return isinstance(value
, NullSimObject
)
376 def isSimObjectType(ptype
):
377 return issubclass(ptype
, SimObject
)
381 # Constructor. E.g., Param(Int, "number of widgets", 5)
382 def __init__(self
, ptype
, desc
, default
=None):
384 self
.ptype_name
= self
.ptype
.__name
__
386 self
.default
= default
388 # Convert assigned value to appropriate type. Force parameter
389 # value (rhs of '=') to ptype (or None, which means not set).
390 def make_value(self
, value
):
391 # nothing to do if None or already correct type. Also allow NULL
392 # pointer to be assigned where a SimObject is expected.
393 if value
== None or isinstance(value
, self
.ptype
) or \
394 isNullPointer(value
) and isSimObjectType(self
.ptype
):
396 # this type conversion will raise an exception if it's illegal
397 return self
.ptype(value
)
399 def sim_decl(self
, name
):
400 return 'Param<%s> %s;' % (self
.ptype_name
, name
)
402 def sim_init(self
, name
):
403 if self
.default
== None:
404 return 'INIT_PARAM(%s, "%s")' % (name
, self
.desc
)
406 return 'INIT_PARAM_DFLT(%s, "%s", %s)' % \
407 (name
, self
.desc
, str(self
.default
))
409 # The _VectorParamValue class is a wrapper for vector-valued
410 # parameters. The leading underscore indicates that users shouldn't
411 # see this class; it's magically generated by VectorParam. The
412 # parameter values are stored in the 'value' field as a Python list of
413 # whatever type the parameter is supposed to be. The only purpose of
414 # storing these instead of a raw Python list is that we can override
415 # the __str__() method to not print out '[' and ']' in the .ini file.
416 class _VectorParamValue(object):
417 def __init__(self
, list):
421 return ' '.join(map(str, self
.value
))
423 # Vector-valued parameter description. Just like Param, except that
424 # the value is a vector (list) of the specified type instead of a
426 class VectorParam(Param
):
428 # Inherit Param constructor. However, the resulting parameter
429 # will be a list of ptype rather than a single element of ptype.
430 def __init__(self
, ptype
, desc
, default
=None):
431 Param
.__init
__(self
, ptype
, desc
, default
)
433 # Convert assigned value to appropriate type. If the RHS is not a
434 # list or tuple, it generates a single-element list.
435 def make_value(self
, value
):
436 if value
== None: return value
437 if isinstance(value
, list) or isinstance(value
, tuple):
438 # list: coerce each element into new list
439 val_list
= [Param
.make_value(self
, v
) for v
in iter(value
)]
441 # singleton: coerce & wrap in a list
442 val_list
= [Param
.make_value(self
, value
)]
443 # wrap list in _VectorParamValue (see above)
444 return _VectorParamValue(val_list
)
446 def sim_decl(self
, name
):
447 return 'VectorParam<%s> %s;' % (self
.ptype_name
, name
)
449 # sim_init inherited from Param
451 #####################################################################
455 # Though native Python types could be used to specify parameter types
456 # (the 'ptype' field of the Param and VectorParam classes), it's more
457 # flexible to define our own set of types. This gives us more control
458 # over how Python expressions are converted to values (via the
459 # __init__() constructor) and how these values are printed out (via
460 # the __str__() conversion method). Eventually we'll need these types
461 # to correspond to distinct C++ types as well.
463 #####################################################################
465 # Integer parameter type.
467 # Constructor. Value must be Python int or long (long integer).
468 def __init__(self
, value
):
470 if t
== int or t
== long:
473 raise TypeError, "Int param got value %s %s" % (repr(value
), t
)
475 # Use Python string conversion. Note that this puts an 'L' on the
476 # end of long integers; we can strip that off here if it gives us
479 return str(self
.value
)
481 # Counter, Addr, and Tick are just aliases for Int for now.
491 # Boolean parameter type.
494 # Constructor. Typically the value will be one of the Python bool
495 # constants True or False (or the aliases true and false below).
496 # Also need to take integer 0 or 1 values since bool was not a
497 # distinct type in Python 2.2. Parse a bunch of boolean-sounding
498 # strings too just for kicks.
499 def __init__(self
, value
):
503 elif t
== int or t
== long:
510 if v
== "true" or v
== "t" or v
== "yes" or v
== "y":
512 elif v
== "false" or v
== "f" or v
== "no" or v
== "n":
514 # if we didn't set it yet, it must not be something we understand
515 if not hasattr(self
, 'value'):
516 raise TypeError, "Bool param got value %s %s" % (repr(value
), t
)
518 # Generate printable string version.
520 if self
.value
: return "true"
523 # String-valued parameter.
524 class String(object):
525 # Constructor. Value must be Python string.
526 def __init__(self
, value
):
531 raise TypeError, "String param got value %s %s" % (repr(value
), t
)
533 # Generate printable string version. Not too tricky.
537 # Special class for NULL pointers. Note the special check in
538 # make_param_value() above that lets these be assigned where a
539 # SimObject is required.
540 class NullSimObject(object):
541 # Constructor. No parameters, nothing to do.
548 # The only instance you'll ever need...
549 NULL
= NullSimObject()
551 # Enumerated types are a little more complex. The user specifies the
552 # type as Enum(foo) where foo is either a list or dictionary of
553 # alternatives (typically strings, but not necessarily so). (In the
554 # long run, the integer value of the parameter will be the list index
555 # or the corresponding dictionary value. For now, since we only check
556 # that the alternative is valid and then spit it into a .ini file,
557 # there's not much point in using the dictionary.)
559 # What Enum() must do is generate a new type encapsulating the
560 # provided list/dictionary so that specific values of the parameter
561 # can be instances of that type. We define two hidden internal
562 # classes (_ListEnum and _DictEnum) to serve as base classes, then
563 # derive the new type from the appropriate base class on the fly.
566 # Base class for list-based Enum types.
567 class _ListEnum(object):
568 # Constructor. Value must be a member of the type's map list.
569 def __init__(self
, value
):
570 if value
in self
.map:
572 self
.index
= self
.map.index(value
)
574 raise TypeError, "Enum param got bad value '%s' (not in %s)" \
577 # Generate printable string version of value.
579 return str(self
.value
)
581 class _DictEnum(object):
582 # Constructor. Value must be a key in the type's map dictionary.
583 def __init__(self
, value
):
584 if value
in self
.map:
586 self
.index
= self
.map[value
]
588 raise TypeError, "Enum param got bad value '%s' (not in %s)" \
589 % (value
, self
.map.keys())
591 # Generate printable string version of value.
593 return str(self
.value
)
595 # Enum metaclass... calling Enum(foo) generates a new type (class)
596 # that derives from _ListEnum or _DictEnum as appropriate.
598 # counter to generate unique names for generated classes
601 def __new__(cls
, map):
602 if isinstance(map, dict):
605 elif isinstance(map, list):
609 raise TypeError, "Enum map must be list or dict (got %s)" % map
610 classname
= "Enum%04d" % Enum
.counter
612 # New class derives from selected base, and gets a 'map'
613 # attribute containing the specified list or dict.
614 return type.__new
__(cls
, classname
, (base
,), { 'map': map })
618 # "Constants"... handy aliases for various values.
621 # For compatibility with C++ bool constants.
625 # Some memory range specifications use this as a default upper bound.
628 # For power-of-two sizing, e.g. 64*K gives an integer value 65536.
633 #####################################################################
635 # Object description loading.
637 # The final step is to define the classes corresponding to M5 objects
638 # and their parameters. These classes are described in .odesc files
639 # in the source tree. This code walks the tree to find those files
640 # and loads up the descriptions (by evaluating them in pieces as
644 # Because SimObject classes inherit from other SimObject classes, and
645 # can use arbitrary other SimObject classes as parameter types, we
646 # have to do this in three steps:
648 # 1. Walk the tree to find all the .odesc files. Note that the base
649 # of the filename *must* match the class name. This step builds a
650 # mapping from class names to file paths.
652 # 2. Start generating empty class definitions (via def_class()) using
653 # the OBJECT field of the .odesc files to determine inheritance.
654 # def_class() recurses on demand to define needed base classes before
657 # 3. Now that all of the classes are defined, go through the .odesc
658 # files one more time loading the parameter descriptions.
660 #####################################################################
662 # dictionary: maps object names to file paths
665 # dictionary: maps object names to boolean flag indicating whether
666 # class definition was loaded yet. Since SimObject is defined in
667 # m5.config.py, count it as loaded.
668 odesc_loaded
= { 'SimObject': True }
670 # Find odesc files in namelist and initialize odesc_file and
671 # odesc_loaded dictionaries. Called via os.path.walk() (see below).
672 def find_odescs(process
, dirpath
, namelist
):
673 # Prune out SCCS directories so we don't process s.*.odesc files.
675 while i
< len(namelist
):
676 if namelist
[i
] == "SCCS":
680 # Find .odesc files and record them.
681 for name
in namelist
:
682 if name
.endswith('.odesc'):
683 objname
= name
[:name
.rindex('.odesc')]
684 path
= os
.path
.join(dirpath
, name
)
685 if odesc_file
.has_key(objname
):
686 print "Warning: duplicate object names:", \
687 odesc_file
[objname
], path
688 odesc_file
[objname
] = path
689 odesc_loaded
[objname
] = False
692 # Regular expression string for parsing .odesc files.
693 file_re_string
= r
'''
694 ^OBJECT: \s* (\w+) \s* \( \s* (\w+) \s* \)
696 ^PARAMS: \s*\n ( (\s+.*\n)* )
699 # Compiled regular expression object.
700 file_re
= re
.compile(file_re_string
, re
.MULTILINE | re
.VERBOSE
)
702 # .odesc file parsing function. Takes a filename and returns tuple of
703 # object name, object base, and parameter description section.
704 def parse_file(path
):
705 f
= open(path
, 'r').read()
706 m
= file_re
.search(f
)
708 print "Can't parse", path
710 return (m
.group(1), m
.group(2), m
.group(3))
712 # Define SimObject class based on description in specified filename.
713 # Class itself is empty except for _name attribute; parameter
714 # descriptions will be loaded later. Will recurse to define base
715 # classes as needed before defining specified class.
718 (obj
, parent
, params
) = parse_file(path
)
719 # check to see if base class is defined yet; define it if not
720 if not odesc_loaded
.has_key(parent
):
721 print "No .odesc file found for", parent
723 if not odesc_loaded
[parent
]:
724 def_class(odesc_file
[parent
])
725 # define the class. The _name attribute of the class lets us
726 # track the actual SimObject class name even when we derive new
727 # subclasses in scripts (to provide new parameter value settings).
728 s
= "class %s(%s): _name = '%s'" % (obj
, parent
, obj
)
730 # execute in global namespace, so new class will be globally
733 except Exception, exc
:
734 print "Object error in %s:" % path
, exc
735 # mark this file as loaded
736 odesc_loaded
[obj
] = True
738 # Munge an arbitrary Python code string to get it to execute (mostly
739 # dealing with indentation). Stolen from isa_parser.py... see
740 # comments there for a more detailed description.
741 def fixPythonIndentation(s
):
742 # get rid of blank lines first
743 s
= re
.sub(r
'(?m)^\s*\n', '', s
);
744 if (s
!= '' and re
.match(r
'[ \t]', s
[0])):
748 # Load parameter descriptions from .odesc file. Object class must
749 # already be defined.
750 def def_params(path
):
752 (obj_name
, parent_name
, param_code
) = parse_file(path
)
753 # initialize param dict
755 # execute parameter descriptions.
757 # "in globals(), param_dict" makes exec use the current
758 # globals as the global namespace (so all of the Param
759 # etc. objects are visible) and param_dict as the local
760 # namespace (so the newly defined parameter variables will be
761 # entered into param_dict).
762 exec fixPythonIndentation(param_code
) in globals(), param_dict
763 except Exception, exc
:
764 print "Param error in %s:" % path
, exc
766 # Convert object name string to Python class object
768 # Set the object's parameter description dictionary (see MetaConfigNode).
769 obj
.set_param_dict(param_dict
)
772 # Walk directory tree to find .odesc files.
773 # Someday we'll have to make the root path an argument instead of
774 # hard-coding it. For now the assumption is you're running this in
777 os
.path
.walk(root
, find_odescs
, None)
779 # Iterate through file dictionary and define classes.
780 for objname
, path
in odesc_file
.iteritems():
781 if not odesc_loaded
[objname
]:
784 sim_object_list
= odesc_loaded
.keys()
785 sim_object_list
.sort()
787 # Iterate through files again and load parameters.
788 for path
in odesc_file
.itervalues():
791 #####################################################################
793 # Hook to generate C++ parameter code.
794 def gen_sim_code(file):
795 for objname
in sim_object_list
:
796 print >> file, eval("%s._sim_code()" % objname
)
798 # The final hook to generate .ini files. Called from configuration
799 # script once config is built.
800 def instantiate(*objs
):