SimObject.py (12805:3c900ca6be0a) SimObject.py (13356:913658aa619c)
1# Copyright (c) 2017 ARM Limited
1# Copyright (c) 2017-2018 ARM Limited
2# All rights reserved.
3#
4# The license below extends only to copyright in the software and shall
5# not be construed as granting a license to any other intellectual
6# property including but not limited to intellectual property relating
7# to a hardware implementation of the functionality of the software
8# licensed hereunder. You may use the software subject to the license
9# terms below provided that you ensure that this notice is replicated

--- 919 unchanged lines hidden (view full) ---

929 self.created = True
930
931 # Make it so we can only set attributes at initialization time
932 # and effectively make this a const object.
933 def __setattr__(self, name, value):
934 if not "created" in self.__dict__:
935 self.__dict__[name] = value
936
2# All rights reserved.
3#
4# The license below extends only to copyright in the software and shall
5# not be construed as granting a license to any other intellectual
6# property including but not limited to intellectual property relating
7# to a hardware implementation of the functionality of the software
8# licensed hereunder. You may use the software subject to the license
9# terms below provided that you ensure that this notice is replicated

--- 919 unchanged lines hidden (view full) ---

929 self.created = True
930
931 # Make it so we can only set attributes at initialization time
932 # and effectively make this a const object.
933 def __setattr__(self, name, value):
934 if not "created" in self.__dict__:
935 self.__dict__[name] = value
936
937class SimObjectCliWrapperException(Exception):
938 def __init__(self, message):
939 super(Exception, self).__init__(message)
940
941class SimObjectCliWrapper(object):
942 """
943 Wrapper class to restrict operations that may be done
944 from the command line on SimObjects.
945
946 Only parameters may be set, and only children may be accessed.
947
948 Slicing allows for multiple simultaneous assignment of items in
949 one statement.
950 """
951
952 def __init__(self, sim_objects):
953 self.__dict__['_sim_objects'] = list(sim_objects)
954
955 def __getattr__(self, key):
956 return SimObjectCliWrapper(sim_object._children[key]
957 for sim_object in self._sim_objects)
958
959 def __setattr__(self, key, val):
960 for sim_object in self._sim_objects:
961 if key in sim_object._params:
962 if sim_object._params[key].isCmdLineSettable():
963 setattr(sim_object, key, val)
964 else:
965 raise SimObjectCliWrapperException(
966 'tried to set or unsettable' \
967 'object parameter: ' + key)
968 else:
969 raise SimObjectCliWrapperException(
970 'tried to set or access non-existent' \
971 'object parameter: ' + key)
972
973 def __getitem__(self, idx):
974 """
975 Extends the list() semantics to also allow tuples,
976 for example object[1, 3] selects items 1 and 3.
977 """
978 out = []
979 if isinstance(idx, tuple):
980 for t in idx:
981 out.extend(self[t]._sim_objects)
982 else:
983 if isinstance(idx, int):
984 _range = range(idx, idx + 1)
985 elif not isinstance(idx, slice):
986 raise SimObjectCliWrapperException( \
987 'invalid index type: ' + repr(idx))
988 for sim_object in self._sim_objects:
989 if isinstance(idx, slice):
990 _range = range(*idx.indices(len(sim_object)))
991 out.extend(sim_object[i] for i in _range)
992 return SimObjectCliWrapper(out)
993
937# The SimObject class is the root of the special hierarchy. Most of
938# the code in this class deals with the configuration hierarchy itself
939# (parent/child node relationships).
940class SimObject(object):
941 # Specify metaclass. Any class inheriting from SimObject will
942 # get this metaclass.
943 __metaclass__ = MetaSimObject
944 type = 'SimObject'

--- 575 unchanged lines hidden (view full) ---

1520 yield # make this function a (null) generator
1521
1522 def recurseDeviceTree(self, state):
1523 for child in self._children.itervalues():
1524 for item in child: # For looping over SimObjectVectors
1525 for dt in item.generateDeviceTree(state):
1526 yield dt
1527
994# The SimObject class is the root of the special hierarchy. Most of
995# the code in this class deals with the configuration hierarchy itself
996# (parent/child node relationships).
997class SimObject(object):
998 # Specify metaclass. Any class inheriting from SimObject will
999 # get this metaclass.
1000 __metaclass__ = MetaSimObject
1001 type = 'SimObject'

--- 575 unchanged lines hidden (view full) ---

1577 yield # make this function a (null) generator
1578
1579 def recurseDeviceTree(self, state):
1580 for child in self._children.itervalues():
1581 for item in child: # For looping over SimObjectVectors
1582 for dt in item.generateDeviceTree(state):
1583 yield dt
1584
1585 # On a separate method otherwise certain buggy Python versions
1586 # would fail with: SyntaxError: unqualified exec is not allowed
1587 # in function 'apply_config'
1588 def _apply_config_get_dict(self):
1589 return {
1590 child_name: SimObjectCliWrapper(
1591 iter(self._children[child_name]))
1592 for child_name in self._children
1593 }
1594
1595 def apply_config(self, params):
1596 """
1597 exec a list of Python code strings contained in params.
1598
1599 The only exposed globals to those strings are the child
1600 SimObjects of this node.
1601
1602 This function is intended to allow users to modify SimObject
1603 parameters from the command line with Python statements.
1604 """
1605 d = self._apply_config_get_dict()
1606 for param in params:
1607 exec(param, d)
1608
1528# Function to provide to C++ so it can look up instances based on paths
1529def resolveSimObject(name):
1530 obj = instanceDict[name]
1531 return obj.getCCObject()
1532
1533def isSimObject(value):
1534 return isinstance(value, SimObject)
1535

--- 58 unchanged lines hidden ---
1609# Function to provide to C++ so it can look up instances based on paths
1610def resolveSimObject(name):
1611 obj = instanceDict[name]
1612 return obj.getCCObject()
1613
1614def isSimObject(value):
1615 return isinstance(value, SimObject)
1616

--- 58 unchanged lines hidden ---