class PySP_Annotation(object): def __init__(self): self.data = ComponentMap() try: self.declare(None) assert None in self.data self.default = self.data[None] del self.data[None] except TypeError: self.default = None def declare(self, component, *args, **kwds): raise NotImplementedError("This method is abstract") def pprint(self, *args, **kwds): self.data.pprint(*args, **kwds)
class PySP_Annotation(object): _ctypes = () _ctypes_data = () def __init__(self): assert len(self._ctypes) > 0 assert len(self._ctypes) == len(self._ctypes_data) self._data = ComponentMap() self._default = None @property def default(self): return self._default def has_declarations(self): return bool(len(self._data) > 0) def declare(self, component, *args, **kwds): if ( isinstance(component, self._ctypes_data) or isinstance(component, self._ctypes) or isinstance(component, (Block, _BlockData)) ): self._declare_impl(component, *args, **kwds) else: raise TypeError( "Declarations in annotation type %s must be of types " "%s or Block. Invalid type: %s" % (self.__class__.__name__, (",".join(ctype.__name__ for ctype in self._ctypes)), type(component)) ) def pprint(self, *args, **kwds): self._data.pprint(*args, **kwds) def expand_entries(self, expand_containers=True): """ Translates the annotation into a flattened list of (component, annotation_value) pairs. The ctypes argument can be a single component type are a tuple of component types. If any components are found in the annotation not matching those types, an exception will be raised. If 'expand_containers' is set to False, then component containers will not be flattened into the set of components they contain. """ items = [] component_ids = set() def _append(component, val): items.append((component, val)) if id(component) in component_ids: raise RuntimeError( "Component %s was assigned multiple declarations " "in annotation type %s. To correct this issue, ensure that " "multiple container components under which the component might " "be stored (such as a Block and an indexed Constraint) are not " "simultaneously set in this annotation." % (component.name, self.__class__.__name__) ) component_ids.add(id(component)) for component in self._data: component_annotation_value = self._data[component] if not getattr(component, "active", True): continue if isinstance(component, self._ctypes_data): _append(component, component_annotation_value) elif isinstance(component, self._ctypes): if expand_containers: for index in component: obj = component[index] if getattr(obj, "active", True): _append(obj, component_annotation_value) else: _append(component, component_annotation_value) elif isinstance(component, _BlockData): for ctype in self._ctypes: if expand_containers: for obj in component.component_data_objects(ctype, active=True, descend_into=True): _append(obj, component_annotation_value) else: for obj in component.component_data_objects(ctype, active=True, descend_into=True): _append(obj, component_annotation_value) elif isinstance(component, Block): for index in component: block = component[index] if block.active: for ctype in self._ctypes: if expand_containers: for obj in block.component_data_objects(ctype, active=True, descend_into=True): _append(obj, component_annotation_value) else: for obj in block.component_objects(ctype, active=True, descend_into=True): _append(obj, component_annotation_value) else: raise TypeError( "Declarations in annotation type %s must be of types " "%s or Block. Invalid type: %s" % (self.__class__.__name__, (",".join(ctype.__name__ for ctype in self._ctypes)), type(component)) ) return items
class PySP_Annotation(object): _ctypes = () _ctypes_data = () def __init__(self): assert len(self._ctypes) > 0 assert len(self._ctypes) == len(self._ctypes_data) self._data = ComponentMap() self._default = None @property def default(self): return self._default def has_declarations(self): return bool(len(self._data) > 0) def declare(self, component, *args, **kwds): if isinstance(component, self._ctypes_data) or \ isinstance(component, self._ctypes) or \ isinstance(component, (Block, _BlockData)): self._declare_impl(component, *args, **kwds) else: raise TypeError( "Declarations in annotation type %s must be of types " "%s or Block. Invalid type: %s" % (self.__class__.__name__, (",".join(ctype.__name__ for ctype in self._ctypes)), type(component))) def pprint(self, *args, **kwds): self._data.pprint(*args, **kwds) def expand_entries(self, expand_containers=True): """ Translates the annotation into a flattened list of (component, annotation_value) pairs. The ctypes argument can be a single component type are a tuple of component types. If any components are found in the annotation not matching those types, an exception will be raised. If 'expand_containers' is set to False, then component containers will not be flattened into the set of components they contain. """ items = [] component_ids = set() def _append(component, val): items.append((component, val)) if id(component) in component_ids: raise RuntimeError( "Component %s was assigned multiple declarations " "in annotation type %s. To correct this issue, ensure that " "multiple container components under which the component might " "be stored (such as a Block and an indexed Constraint) are not " "simultaneously set in this annotation." % (component.name, self.__class__.__name__)) component_ids.add(id(component)) for component in self._data: component_annotation_value = self._data[component] if not getattr(component, "active", True): continue if isinstance(component, self._ctypes_data): _append(component, component_annotation_value) elif isinstance(component, self._ctypes): if expand_containers: for index in component: obj = component[index] if getattr(obj, "active", True): _append(obj, component_annotation_value) else: _append(component, component_annotation_value) elif isinstance(component, _BlockData): for ctype in self._ctypes: if expand_containers: for obj in component.component_data_objects( ctype, active=True, descend_into=True): _append(obj, component_annotation_value) else: for obj in component.component_data_objects( ctype, active=True, descend_into=True): _append(obj, component_annotation_value) elif isinstance(component, Block): for index in component: block = component[index] if block.active: for ctype in self._ctypes: if expand_containers: for obj in block.component_data_objects( ctype, active=True, descend_into=True): _append(obj, component_annotation_value) else: for obj in block.component_objects( ctype, active=True, descend_into=True): _append(obj, component_annotation_value) else: raise TypeError( "Declarations in annotation type %s must be of types " "%s or Block. Invalid type: %s" % (self.__class__.__name__, (",".join(ctype.__name__ for ctype in self._ctypes)), type(component))) return items