def test_import_and_get(): """Test the behavior of the import and get utility function. """ assert (import_and_get('exopy.utils.declarator', 'Declarator', {}, '') is Declarator) tb = {} import_and_get('___exopy', 'r', tb, 'test') if sys.version_info < (3, 6): assert 'ImportError' in tb['test'] else: assert 'ModuleNotFoundError' in tb['test'] import_and_get('exopy.testing.broken_enaml', 'r', tb, 'test') assert 'AttributeError' in tb['test'] or 'NameError' in tb['test'] import_and_get('exopy.utils.declarator', '___D', tb, 'test') assert 'AttributeError' in tb['test']
def register(self, collector, traceback): """Collect shape and view and add infos to the DeclaratorCollector contributions member. The group declared by a parent if any is taken into account. All Interface children are also registered. """ # Build the shape id by assembling the package name and the class # name. shape_id = self.id # If the shape only specifies a name update the matching infos. if ':' not in self.shape: if self.shape not in collector.contributions: collector._delayed.append(self) return infos = collector.contributions[shape_id] infos.metadata.update(self.metadata) self.is_registered = True return # Determine the path of shape and view path = self.get_path() try: s_path, shape = (path + '.' + self.shape if path else self.shape).split(':') v_path, view = (path + '.' + self.view if path else self.view).split(':') except ValueError: msg = 'Incorrect %s (%s), path must be of the form a.b.c:Class' err_id = s_path.split('.', 1)[0] + '.' + shape msg = msg % ('view', self.view) traceback[err_id] = msg return # Check that the shape does not already exist. if shape_id in collector.contributions or shape_id in traceback: i = 1 while True: err_id = '%s_duplicate%d' % (shape_id, i) if err_id not in traceback: break msg = 'Duplicate definition of {}, found in {}' traceback[err_id] = msg.format(shape, s_path) return infos = ShapeInfos(metadata=self.metadata) # Get the sequence class. s_cls = import_and_get(s_path, shape, traceback, shape_id) if s_cls is None: return try: infos.cls = s_cls except TypeError: msg = '{} should be a subclass of AbstractShape. \n{}' traceback[shape_id] = msg.format(s_cls, format_exc()) return # Get the shape view. s_view = import_and_get(v_path, view, traceback, shape_id) if s_view is None: return try: infos.view = s_view except TypeError: msg = '{} should be a subclass of AbstractShapeView,.\n{}' traceback[shape_id] = msg.format(s_view, format_exc()) return # Add group and add to collector infos.metadata['group'] = self.get_group() collector.contributions[shape_id] = infos self.is_registered = True
def register(self, collector, traceback): """Collect config and view and add infos to the DeclaratorCollector contributions member under the supported task name. """ # Determine the path to the config and view. path = self.get_path() try: c_path, config = (path + '.' + self.config if path else self.config).split(':') v_path, view = (path + '.' + self.view if path else self.view).split(':') except ValueError: msg = 'Incorrect %s (%s), path must be of the form a.b.c:Class' if ':' in self.config: msg = msg % ('view', self.view) else: msg = msg % ('config', self.config) traceback[self.id] = msg return try: s_cls = self.get_sequence_class() except Exception: msg = 'Failed to get supported sequence : %s' traceback[self.id] = msg % format_exc() return # Check that the configurer does not already exist. if self.id in traceback: i = 1 while True: err_id = '%s_duplicate%d' % (self.id, i) if err_id not in traceback: break msg = 'Duplicate definition of {}, found in {}' traceback[err_id] = msg.format(s_cls, c_path) return if s_cls in collector.contributions: msg = 'Duplicate definition for {}, found in {}' traceback[self.id] = msg.format(s_cls, c_path) return infos = ConfigInfos() # Get the config class. c_cls = import_and_get(c_path, config, traceback, self.id) if c_cls is None: return try: infos.cls = c_cls except TypeError: msg = '{} should a subclass of AbstractConfig.\n{}' traceback[self.id] = msg.format(c_cls, format_exc()) return # Get the config view. view = import_and_get(v_path, view, traceback, self.id) if view is None: return try: infos.view = view except TypeError: msg = '{} should a subclass of AbstractConfigView.\n{}' traceback[self.id] = msg.format(c_cls, format_exc()) return collector.contributions[s_cls] = infos self.is_registered = True
def register(self, collector, traceback): """Collect task and view and add infos to the DeclaratorCollector contributions member. The group declared by a parent if any is taken into account. All Interface children are also registered. """ # Build the task id by assembling the package name and the class name task_id = self.id # If the task only specifies a name update the matching infos. if ':' not in self.task: if self.task not in collector.contributions: collector._delayed.append(self) return infos = collector.contributions[task_id] infos.instruments.update(self.instruments) infos.dependencies.update(self.dependencies) infos.metadata.update(self.metadata) check = check_children(self) if check: traceback[task_id] = check return for i in self.children: i.register(collector, traceback) self.is_registered = True return # Determine the path to the task and view. path = self.get_path() try: t_path, task = (path + '.' + self.task if path else self.task).split(':') v_path, view = (path + '.' + self.view if path else self.view).split(':') except ValueError: msg = 'Incorrect %s (%s), path must be of the form a.b.c:Class' err_id = t_path.split('.', 1)[0] + '.' + task msg = msg % ('view', self.view) traceback[err_id] = msg return # Check that the task does not already exist. if task_id in collector.contributions or task_id in traceback: i = 1 while True: err_id = '%s_duplicate%d' % (task_id, i) if err_id not in traceback: break msg = 'Duplicate definition of {}, found in {}' traceback[err_id] = msg.format(task, t_path) return infos = TaskInfos(metadata=self.metadata, dependencies=self.dependencies, instruments=self.instruments) # Get the task class. t_cls = import_and_get(t_path, task, traceback, task_id) if t_cls is None: return try: infos.cls = t_cls except TypeError: msg = '{} should a subclass of BaseTask.\n{}' traceback[task_id] = msg.format(t_cls, format_exc()) return # Get the task view. t_view = import_and_get(v_path, view, traceback, task_id) if t_view is None: return try: infos.view = t_view except TypeError: msg = '{} should a subclass of BaseTaskView.\n{}' traceback[task_id] = msg.format(t_view, format_exc()) return # Check children type. check = check_children(self) if check: traceback[task_id] = check return # Add group and add to collector infos.metadata['group'] = self.get_group() collector.contributions[task_id] = infos # Register children. for i in self.children: i.register(collector, traceback) self.is_registered = True