Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 49 lines (38 sloc) 1.668 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#-*- coding: utf-8 -*-
"""
This function is snatched from
https://github.com/ojii/django-load/blob/3058ab9d9d4875589638cc45e84b59e7e1d7c9c3/django_load/core.py#L49
local changes:

* added check for basestring to allow values that are already an object
or method.

"""
from django.utils.importlib import import_module


def load_object(import_path):
    """
Loads an object from an 'import_path', like in MIDDLEWARE_CLASSES and the
likes.

Import paths should be: "mypackage.mymodule.MyObject". It then imports the
module up until the last dot and tries to get the attribute after that dot
from the imported module.

If the import path does not contain any dots, a TypeError is raised.

If the module cannot be imported, an ImportError is raised.

If the attribute does not exist in the module, a AttributeError is raised.
"""
    if not isinstance(import_path, basestring):
        return import_path
    if '.' not in import_path:
        raise TypeError(
            "'import_path' argument to 'django_load.core.load_object' " +\
            "must contain at least one dot.")
    module_name, object_name = import_path.rsplit('.', 1)
    module = import_module(module_name)
    return getattr(module, object_name)


def storage_factory(klass, location, base_url):
    """
This factory returns an instance of the storage class provided.
args:

* klass: must be inherit from ``django.core.files.storage.Storage``
* location: is a string representing the PATH similar to MEDIA_ROOT
* base_url: is a string representing the URL similar to MEDIA_URL
"""
    return klass(location=location, base_url=base_url)
Something went wrong with that request. Please try again.