<commit_before>import inspect
import logging
import os
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from haystack.sites import site
try:
    from django.utils import importlib
except ImportError:
    from haystack.utils import importlib


__author__ = '<NAME>'
__version__ = (1, 1, 0, 'alpha')
__all__ = ['backend']


# Setup default logging.
log = logging.getLogger('haystack')
stream = logging.StreamHandler()
stream.setLevel(logging.INFO)
log.addHandler(stream)


if not hasattr(settings, "HAYSTACK_SITECONF"):
    raise ImproperlyConfigured("You must define the HAYSTACK_SITECONF setting before using the search framework.")
if not hasattr(settings, "HAYSTACK_SEARCH_ENGINE"):
    raise ImproperlyConfigured("You must define the HAYSTACK_SEARCH_ENGINE setting before using the search framework.")


# Load the search backend.
def load_backend(backend_name=None):
    """
    Loads a backend for interacting with the search engine.
    
    Optionally accepts a ``backend_name``. If provided, it should be a string
    of one of the following (built-in) options::
    
      * solr
      * xapian
      * whoosh
      * simple
      * dummy
    
    If you've implemented a custom backend, you can provide the "short" portion
    of the name (before the ``_backend``) and Haystack will attempt to load
    that backend instead.
    
    If not provided, the ``HAYSTACK_SEARCH_ENGINE`` setting is used.
    """
    if not backend_name:
        backend_name = settings.HAYSTACK_SEARCH_ENGINE
    
    try:
        # Most of the time, the search backend will be one of the
        # backends that ships with haystack, so look there first.
        return importlib.import_module('haystack.backends.%s_backend' % backend_name)
    except ImportError, e:
        # If the import failed, we might be looking for a search backend
        # distributed external to haystack. So we'll try that next.
        try:
            return importlib.import_module('%s_backend' % backend_name)
        except ImportError, e_user:
            # The search backend wasn't found. Display a helpful error message
            # listing all possible (built-in) database backends.
            backend_dir = os.path.join(__path__[0], 'backends')
            available_backends = [
                os.path.splitext(f)[0].split("_backend")[0] for f in os.listdir(backend_dir)
                if f != "base.py"
                and not f.startswith('_') 
                and not f.startswith('.') 
                and not f.endswith('.pyc')
            ]
            available_backends.sort()
            if backend_name not in available_backends:
                raise ImproperlyConfigured, "%r isn't an available search backend. Available options are: %s" % \
                    (backend_name, ", ".join(map(repr, available_backends)))
            else:
                raise # If there's some other error, this must be an error in Django itself.


backend = load_backend(settings.HAYSTACK_SEARCH_ENGINE)


def autodiscover():
    """
    Automatically build the site index.
    <commit_msg>Ignore ``.pyo`` files when listing out the backend options. Thanks to kylemacfarlane for the report.<commit_after>import inspect
import logging
import os
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from haystack.sites import site
try:
    from django.utils import importlib
except ImportError:
    from haystack.utils import importlib


__author__ = '<NAME>'
__version__ = (1, 1, 0, 'alpha')
__all__ = ['backend']


# Setup default logging.
log = logging.getLogger('haystack')
stream = logging.StreamHandler()
stream.setLevel(logging.INFO)
log.addHandler(stream)


if not hasattr(settings, "HAYSTACK_SITECONF"):
    raise ImproperlyConfigured("You must define the HAYSTACK_SITECONF setting before using the search framework.")
if not hasattr(settings, "HAYSTACK_SEARCH_ENGINE"):
    raise ImproperlyConfigured("You must define the HAYSTACK_SEARCH_ENGINE setting before using the search framework.")


# Load the search backend.
def load_backend(backend_name=None):
    """
    Loads a backend for interacting with the search engine.
    
    Optionally accepts a ``backend_name``. If provided, it should be a string
    of one of the following (built-in) options::
    
      * solr
      * xapian
      * whoosh
      * simple
      * dummy
    
    If you've implemented a custom backend, you can provide the "short" portion
    of the name (before the ``_backend``) and Haystack will attempt to load
    that backend instead.
    
    If not provided, the ``HAYSTACK_SEARCH_ENGINE`` setting is used.
    """
    if not backend_name:
        backend_name = settings.HAYSTACK_SEARCH_ENGINE
    
    try:
        # Most of the time, the search backend will be one of the
        # backends that ships with haystack, so look there first.
        return importlib.import_module('haystack.backends.%s_backend' % backend_name)
    except ImportError, e:
        # If the import failed, we might be looking for a search backend
        # distributed external to haystack. So we'll try that next.
        try:
            return importlib.import_module('%s_backend' % backend_name)
        except ImportError, e_user:
            # The search backend wasn't found. Display a helpful error message
            # listing all possible (built-in) database backends.
            backend_dir = os.path.join(__path__[0], 'backends')
            available_backends = [
                os.path.splitext(f)[0].split("_backend")[0] for f in os.listdir(backend_dir)
                if f != "base.py"
                and not f.startswith('_') 
                and not f.startswith('.') 
                and not f.endswith('.pyc')
                and not f.endswith('.pyo')
            ]
            available_backends.sort()
            if backend_name not in available_backends:
                raise ImproperlyConfigured, "%r isn't an available search backend. Available options are: %s" % \
                    (backend_name, ", ".join(map(repr, available_backends)))
            else:
                raise # If there's some other error, this must be an error in Django itself.


backend = load_backend(settings.HAYSTACK_SEARCH_ENGINE)


def autodiscover():
    """
    Automatically build the site index.
    
{'fraction_non_alphanumeric': 0.08173228346456693, 'fraction_numerical': 0.001889763779527559, 'mean_word_length': 2.746902654867257, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5768603', 'n_tokens_mistral': 1825, 'n_tokens_neox': 1766, 'n_words': 607}