def test_mapping_invalid_dataelement_raises_error(self):
     invalid_de = InvalidKindDataElement()
     invalid_de.mapping = self.mapping  # pylint: disable=W0201
     also_provides(invalid_de, IDataElement)
     trv = DataElementTreeTraverser(invalid_de, self.mapping.as_pruning())
     self.assert_raises(ValueError, trv.run, None)
Example #2
0
 def add_resource(self, interface, member, entity,
                  collection=None,
                  collection_root_name=None, collection_title=None,
                  expose=True, repository=None, _info=u''):
     if not IInterface in provided_by(interface):
         raise ValueError('The interface argument must be an Interface.')
     if not (isinstance(member, type)
             and IMemberResource in provided_by(object.__new__(member))):
         raise ValueError('The member argument must be a class that '
                          'implements IMemberResource.')
     if member.relation is None:
         raise ValueError('The member class must have a "relation" '
                          'attribute.')
     if not (isinstance(entity, type)
             and IEntity in provided_by(object.__new__(entity))):
         raise ValueError('The entity argument must be a class that '
                          'implements IEntity.')
     # Configure or create the collection class.
     if collection is None:
         collection = type('%sCollection' % member.__name__,
                           (Collection,), {})
         if collection_title is None:
             collection.title = 'Collection of %s' % member.__name__
     elif not issubclass(collection, Collection):
         raise ValueError('The collection class must be a subclass '
                          'of Collection.')
     # Configure the specified repository.
     repo_mgr = self.get_registered_utility(IRepositoryManager)
     if repository is None:
         repo = repo_mgr.get_default()
     else:
         repo = repo_mgr.get(repository)
         if repo is None:
             # Add a root repository with default configuration on
             # the fly.
             repo_type = getattr(REPOSITORY_TYPES, repository, None)
             if repo_type is None:
                 raise ValueError('Unknown repository type "%s".'
                                  % repository)
             if repo_type == REPOSITORY_TYPES.RDB:
                 self.add_rdb_repository(name=REPOSITORY_TYPES.RDB)
             elif repo_type == REPOSITORY_TYPES.FILE_SYSTEM:
                 self.add_filesystem_repository(
                                         name=REPOSITORY_TYPES.FILE_SYSTEM)
             else:
                 raise NotImplementedError()
             repo = repo_mgr.get(repository)
     # Override the root name and title the collection, if requested.
     if not collection_root_name is None:
         collection.root_name = collection_root_name
     if not collection_title is None:
         collection.title = collection_title
     if collection.relation is None:
         collection.relation = '%s-collection' % member.relation
     if expose and collection.root_name is None:
         # Check that we have a root collection name *before* we register
         # all the adapters and utilities.
         raise ValueError('To expose a collection resource in the '
                          'service (=root), a root name is required.')
     # Register the entity instance -> member instance adapter.
     mb_factory = member.create_from_entity
     self._register_adapter(mb_factory, (interface,), IMemberResource,
                            info=_info)
     # Register adapter object implementing instance -> member class
     self._register_adapter(lambda obj: member,
                            required=(interface,),
                            provided=IMemberResource,
                            name='member-class',
                            info=_info)
     # Register adapter object implementing instance -> collection class
     self._register_adapter(lambda obj: collection,
                            required=(interface,),
                            provided=ICollectionResource,
                            name='collection-class',
                            info=_info)
     # Register adapter object implementing instance -> entity class
     self._register_adapter(lambda obj: entity,
                            required=(interface,),
                            provided=IEntity,
                            name='entity-class',
                            info=_info)
     # Register utility interface -> member class
     self._register_utility(member, interface,
                            name='member-class', info=_info)
     # Register utility interface -> collection class
     self._register_utility(collection, interface,
                            name='collection-class', info=_info)
     # Register utility interface -> entity class
     self._register_utility(entity, interface,
                            name='entity-class', info=_info)
     # Attach the marker interface to the registered resource classes, if
     # necessary, so the instances will provide it.
     if not interface in provided_by(member):
         class_implements(member, interface)
     if not interface in provided_by(collection):
         class_implements(collection, interface)
     if not interface in provided_by(entity):
         class_implements(entity, interface)
     # This enables us to pass a class instead of
     # an interface or instance to the various adapters.
     also_provides(member, interface)
     also_provides(collection, interface)
     also_provides(entity, interface)
     # Register utility member relation -> member class
     self._register_utility(member, IRelation,
                            name=member.relation)
     # Register utility collection relation -> collection class
     self._register_utility(collection, IRelation,
                            name=collection.relation)
     # Register the resource with the repository.
     repo.register_resource(collection)
     # Register adapter implementing interface -> repository.
     self._register_adapter(lambda obj: repo,
                            required=(interface,),
                            provided=IRepository,
                            info=_info)
     # Install an attribute injector in the entity class. This will, on
     # first access, replace the __everest_attributes__ class attribute
     # with an ordered dictionary mapping entity attribute names to
     # resource descriptors.
     entity.__everest_attributes__ = resource_attributes_injector()
     # Expose (=register with the service) if requested.
     if expose:
         srvc = self.query_registered_utilities(IService)
         srvc.register(interface)
 def test_mapping_invalid_dataelement_raises_error(self):
     invalid_de = InvalidKindDataElement()
     invalid_de.mapping = self.mapping  # pylint: disable=W0201
     also_provides(invalid_de, IDataElement)
     trv = DataElementTreeTraverser(invalid_de, self.mapping.as_pruning())
     self.assert_raises(ValueError, trv.run, None)
Example #4
0
 def add_resource(self,
                  interface,
                  member,
                  entity,
                  collection=None,
                  collection_root_name=None,
                  collection_title=None,
                  expose=True,
                  repository=None,
                  _info=u''):
     if not IInterface in provided_by(interface):
         raise ValueError('The interface argument must be an Interface.')
     if not (isinstance(member, type)
             and IMemberResource in provided_by(object.__new__(member))):
         raise ValueError('The member argument must be a class that '
                          'implements IMemberResource.')
     if member.relation is None:
         raise ValueError('The member class must have a "relation" '
                          'attribute.')
     if not (isinstance(entity, type)
             and IEntity in provided_by(object.__new__(entity))):
         raise ValueError('The entity argument must be a class that '
                          'implements IEntity.')
     # Configure or create the collection class.
     if collection is None:
         collection = type('%sCollection' % member.__name__, (Collection, ),
                           {})
         if collection_title is None:
             collection.title = 'Collection of %s' % member.__name__
     elif not issubclass(collection, Collection):
         raise ValueError('The collection class must be a subclass '
                          'of Collection.')
     # Configure the specified repository.
     repo_mgr = self.get_registered_utility(IRepositoryManager)
     if repository is None:
         repo = repo_mgr.get_default()
     else:
         repo = repo_mgr.get(repository)
         if repo is None:
             # Add a root repository with default configuration on
             # the fly.
             repo_type = getattr(REPOSITORY_TYPES, repository, None)
             if repo_type is None:
                 raise ValueError('Unknown repository type "%s".' %
                                  repository)
             if repo_type == REPOSITORY_TYPES.RDB:
                 self.add_rdb_repository(name=REPOSITORY_TYPES.RDB)
             elif repo_type == REPOSITORY_TYPES.FILE_SYSTEM:
                 self.add_filesystem_repository(
                     name=REPOSITORY_TYPES.FILE_SYSTEM)
             else:
                 raise NotImplementedError()
             repo = repo_mgr.get(repository)
     # Override the root name and title the collection, if requested.
     if not collection_root_name is None:
         collection.root_name = collection_root_name
     if not collection_title is None:
         collection.title = collection_title
     if collection.relation is None:
         collection.relation = '%s-collection' % member.relation
     if expose and collection.root_name is None:
         # Check that we have a root collection name *before* we register
         # all the adapters and utilities.
         raise ValueError('To expose a collection resource in the '
                          'service (=root), a root name is required.')
     # Register the entity instance -> member instance adapter.
     mb_factory = member.create_from_entity
     self._register_adapter(mb_factory, (interface, ),
                            IMemberResource,
                            info=_info)
     # Register adapter object implementing instance -> member class
     self._register_adapter(lambda obj: member,
                            required=(interface, ),
                            provided=IMemberResource,
                            name='member-class',
                            info=_info)
     # Register adapter object implementing instance -> collection class
     self._register_adapter(lambda obj: collection,
                            required=(interface, ),
                            provided=ICollectionResource,
                            name='collection-class',
                            info=_info)
     # Register adapter object implementing instance -> entity class
     self._register_adapter(lambda obj: entity,
                            required=(interface, ),
                            provided=IEntity,
                            name='entity-class',
                            info=_info)
     # Register utility interface -> member class
     self._register_utility(member,
                            interface,
                            name='member-class',
                            info=_info)
     # Register utility interface -> collection class
     self._register_utility(collection,
                            interface,
                            name='collection-class',
                            info=_info)
     # Register utility interface -> entity class
     self._register_utility(entity,
                            interface,
                            name='entity-class',
                            info=_info)
     # Attach the marker interface to the registered resource classes, if
     # necessary, so the instances will provide it.
     if not interface in provided_by(member):
         class_implements(member, interface)
     if not interface in provided_by(collection):
         class_implements(collection, interface)
     if not interface in provided_by(entity):
         class_implements(entity, interface)
     # This enables us to pass a class instead of
     # an interface or instance to the various adapters.
     also_provides(member, interface)
     also_provides(collection, interface)
     also_provides(entity, interface)
     # Register utility member relation -> member class
     self._register_utility(member, IRelation, name=member.relation)
     # Register utility collection relation -> collection class
     self._register_utility(collection, IRelation, name=collection.relation)
     # Register the resource with the repository.
     repo.register_resource(interface)
     # Register adapter implementing interface -> repository.
     self._register_adapter(lambda obj: repo,
                            required=(interface, ),
                            provided=IRepository,
                            info=_info)
     # Expose (=register with the service) if requested.
     if expose:
         srvc = self.query_registered_utilities(IService)
         srvc.register(interface)