<commit_before>import os
import unittest
import logging

import mock
import requests
from django.conf import settings
from django.test import TestCase
from django.core.cache.backends.base import BaseCache
from django.contrib.staticfiles.finders import find as find_static_file

from . import forms, models, factories, typeahead

VALID_DATE_STRING = "2014-05-12 12:18"


def intercept_requests_to_statics(url, *a, **k):
    """
    Utility function to use with mock.path.
    Blocks all requests through `requests.get` except for those to local static files.
    In that case makes `requests.get` return file content without going through `httplib`
    """
    logging.info("intercept: %s", url)
    if url.startswith(settings.STATIC_URL):
        r = requests.Response()
        try:
            path = url[len(settings.STATIC_URL):]
            file_path = find_static_file(path)
            logging.info("path:%r", path)
            logging.info("file+path:%r", file_path)
            if file_path and os.path.isfile(file_path):
                with open(file_path) as f:
                    r._content = f.read()
                    r.status_code = 200
                    logging.info("response: %s", r._content)
                    logging.info("response: %s", r.content)
            else:
                r.status_code = 404
        except Exception, e:<commit_msg>fix for cornercase for handling static files in tests<commit_after>import os
import unittest
import logging

import mock
import requests
from django.conf import settings
from django.test import TestCase
from django.core.cache.backends.base import BaseCache
from django.contrib.staticfiles.finders import find as find_static_file

from . import forms, models, factories, typeahead

VALID_DATE_STRING = "2014-05-12 12:18"


def intercept_requests_to_statics(url, *a, **k):
    """
    Utility function to use with mock.path.
    Blocks all requests through `requests.get` except for those to local static files.
    In that case makes `requests.get` return file content without going through `httplib`
    """
    logging.info("intercept: %s", url)
    if url.startswith(settings.STATIC_URL):
        r = requests.Response()
        try:
            path = url[len(settings.STATIC_URL):]
            path = path.split('?')[0]  # FIXME use urlparse
            file_path = find_static_file(path)
            logging.info("path:%r", path)
            logging.info("file+path:%r", file_path)
            if file_path and os.path.isfile(file_path):
                with open(file_path) as f:
                    r._content = f.read()
                    r.status_code = 200
                    logging.info("response: %s", r._content)
                    logging.info("response: %s", r.content)
            else:
                r.status_code = 404
        except Exception, e:
{'fraction_non_alphanumeric': 0.09500351864883885, 'fraction_numerical': 0.013019000703729768, 'mean_word_length': 2.4670731707317075, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1420642', 'n_tokens_mistral': 843, 'n_tokens_neox': 789, 'n_words': 234}