Esempio n. 1
0
    def initialize(self, request, connectors):
        # Install the WSGI interception layer.
        install()

        # Unload django.
        for module in list(sys.modules.keys()):
            if module and 'django' in module:
                del sys.modules[module]

        # Reset and clear all global cache in armet.
        from armet import decorators

        decorators._resources = {}
        # decorators._handlers = {}
        armet.use.config = {}

        # Re-initialize the configuration.
        armet.use(connectors=connectors, debug=True)

        prefix = 'tests.armet.connectors.'
        callback = None
        if 'model' in connectors:
            # Initialize the database access layer.
            model = import_module(prefix + connectors['model'])
            callback = model.model_setup

            # Add the models module so that it can be generically imported.
            sys.modules[prefix + 'models'] = model

        # Initialize the http access layer.
        http = import_module(prefix + connectors['http'])
        http.http_setup(connectors, self.host, self.port, callback=callback)

        # Add a finalizer to teardown the http layer.
        request.addfinalizer(lambda: http.http_teardown(self.host, self.port))
Esempio n. 2
0
def model_setup():
    # Initialize the database and create all models.
    Base.metadata.drop_all(engine)
    Base.metadata.create_all(engine)

    # Load the data fixture.
    _load_fixture(os.path.join(os.path.dirname(__file__), 'data.json'))

    # Configure armet and provide the session factory.
    armet.use(Session=Session)
Esempio n. 3
0
    def __new__(cls, name, bases, attrs):
        if not cls._is_resource(name, bases):
            # This is not an actual resource.
            return super(ResourceBase, cls).__new__(cls, name, bases, attrs)

        # Gather the attributes of all options classes.
        # Start with the base configuration.
        metadata = armet.use().copy()
        values = lambda x: {n: getattr(x, n) for n in dir(x)}

        # Expand the options class with the gathered metadata.
        base_meta = []
        cls._gather_metadata(base_meta, bases)

        # Apply the configuration from each class in the chain.
        for meta in base_meta:
            metadata.update(**values(meta))

        cur_meta = {}
        if attrs.get('Meta'):
            # Apply the configuration from the current class.
            cur_meta = values(attrs['Meta'])
            metadata.update(**cur_meta)

        # Gather and construct the options object.
        meta = attrs['meta'] = cls.options(metadata, name, cur_meta, base_meta)

        # Apply the found connectors.
        bases = apply_connectors(meta, name, bases, metadata, base_meta)

        # Construct the class object.
        self = super(ResourceBase, cls).__new__(cls, name, bases, attrs)

        # Generate a serializer map that maps media ranges to serializer
        # names.
        self._serializer_map = smap = {}
        for key in self.meta.allowed_serializers:
            serializer = self.meta.serializers[key]
            for media_type in serializer.media_types:
                smap[media_type] = key

        # Generate a deserializer map that maps media ranges to deserializer
        # names.
        self._deserializer_map = dmap = {}
        for key in self.meta.allowed_deserializers:
            deserializer = self.meta.deserializers[key]
            for media_type in deserializer.media_types:
                dmap[media_type] = key

        # Filter the available connectors according to the
        # metaclass restriction set.
        for key in list(meta.connectors.keys()):
            if key not in cls.connectors:
                del meta.connectors[key]

        # Return the constructed instance.
        return self
Esempio n. 4
0
def configure(application):
    # Gather configuration from the application config.
    store = {}
    get = partial(_get_config, store, application)
    get('debug', default=False)
    connector = 'alchemist_armet'
    get('connectors', default={'http': connector, 'model': connector})
    get('trailing_slash', default=True)
    get('http_exposed_headers')
    get('http_allowed_origins')
    get('legacy_redirect')
    get('serializers')
    get('allowed_serializers')
    get('default_serializer')
    get('deserializers')
    get('allowed_deserializers')
    get('authentication')
    get('authorization')

    # Apply configuration.
    armet.use(**store)
Esempio n. 5
0
    def initialize(self, request, connectors):
        # Install the WSGI interception layer.
        install()

        # Unload django.
        for module in list(sys.modules.keys()):
            if module and 'django' in module:
                del sys.modules[module]

        # Reset and clear all global cache in armet.
        from armet import decorators

        decorators._resources = {}
        # decorators._handlers = {}
        armet.use.config = {}

        # Re-initialize the configuration.
        armet.use(connectors=connectors, debug=True)

        prefix = 'tests.connectors.'
        callback = None
        if 'model' in connectors:
            # Initialize the database access layer.
            model = import_module(prefix + connectors['model'])
            callback = model.model_setup

            # Add the models module so that it can be generically imported.
            sys.modules[prefix + 'models'] = model
            request.cls.models = model

        # Initialize the http access layer.
        http = import_module(prefix + connectors['http'])
        http.http_setup(connectors, self.host, self.port, callback=callback)

        # Add a finalizer to teardown the http layer.
        request.addfinalizer(lambda: http.http_teardown(self.host, self.port))
Esempio n. 6
0
    def __new__(cls, name, bases, attrs):
        if not cls._is_resource(name, bases):
            # This is not an actual resource.
            return super(ResourceBase, cls).__new__(cls, name, bases, attrs)

        # Gather the attributes of all options classes.
        # Start with the base configuration.
        metadata = armet.use().copy()
        values = lambda x: {n: getattr(x, n) for n in dir(x)}

        # Expand the options class with the gathered metadata.
        base_meta = []
        cls._gather_metadata(base_meta, bases)

        # Apply the configuration from each class in the chain.
        for meta in base_meta:
            metadata.update(**values(meta))

        cur_meta = {}
        if attrs.get('Meta'):
            # Apply the configuration from the current class.
            cur_meta = values(attrs['Meta'])
            metadata.update(**cur_meta)

        # Gather and construct the options object.
        meta = attrs['meta'] = cls.options(metadata, name, cur_meta, base_meta)

        # Construct the class object.
        self = super(ResourceBase, cls).__new__(cls, name, bases, attrs)

        # Generate a serializer map that maps media ranges to serializer
        # names.
        self._serializer_map = smap = {}
        for key in self.meta.allowed_serializers:
            serializer = self.meta.serializers[key]
            for media_type in serializer.media_types:
                smap[media_type] = key

        # Generate a deserializer map that maps media ranges to deserializer
        # names.
        self._deserializer_map = dmap = {}
        for key in self.meta.allowed_deserializers:
            deserializer = self.meta.deserializers[key]
            for media_type in deserializer.media_types:
                dmap[media_type] = key

        # Filter the available connectors according to the
        # metaclass restriction set.
        for key in list(meta.connectors.keys()):
            if key not in cls.connectors:
                del meta.connectors[key]

        # Iterate through the available connectors.
        iterator = six.iteritems(meta.connectors)
        self.connectors = connectors = []
        cmap = CONNECTORS
        for key, ref in iterator:
            options = utils.import_module(cmap[key]['options'][0].format(ref))
            if options:
                options = getattr(options, cmap[key]['options'][1], None)
                if options:
                    # Available options to parse for this connector;
                    # instantiate the options class and apply all
                    # available options.
                    options_instance = options(metadata, name, base_meta)
                    meta.__dict__.update(**options_instance.__dict__)

            module = utils.import_module(cmap[key]['resource'][0].format(ref))
            if module:
                klass = getattr(module, cmap[key]['resource'][1], None)
                if klass:
                    # Found a connector class for this connector
                    connectors.append(klass)

        # Return the constructed instance.
        return self