<commit_before>import ast

from .base import BaseAnalyzer, Result


class TemplateLoadersVisitor(ast.NodeVisitor):

    def __init__(self):
        self.found = []

    deprecated_items = {
        'django.template.loaders.app_directories.load_template_source':
            'django.template.loaders.app_directories.Loader',
        'django.template.loaders.eggs.load_template_source':
            'django.template.loaders.eggs.Loader',
        'django.template.loaders.filesystem.load_template_source':
            'django.template.loaders.filesystem.Loader',
    }

    def visit_Str(self, node):
        if node.s in self.deprecated_items.keys():
            self.found.append((node.s, node))


class TemplateLoadersAnalyzer(BaseAnalyzer):

    def analyze_file(self, filepath, code):
        if not isinstance(code, ast.AST):
            return
        visitor = TemplateLoadersVisitor()
        visitor.visit(code)
        for name, node in visitor.found:
            propose = visitor.deprecated_items[name]
            result = Result(
                description = (
                    '%r function is deprecated, use %r class instead' % (name, propose)
                ),
                path = filepath,
                line = node.lineno)
            lines = self.get_file_lines(filepath, node.lineno, node.lineno)
            for lineno, important, text in lines:
                result.source.add_line(lineno, text, important)
                result.solution.add_line(lineno, text.replace(name, propose), important)
            yield result<commit_msg>Update template loaders analyzer to target Django 1.5<commit_after>import ast

from .base import BaseAnalyzer, Result


class TemplateLoadersVisitor(ast.NodeVisitor):

    def __init__(self):
        self.found = []

    removed_items = {
        'django.template.loaders.app_directories.load_template_source':
            'django.template.loaders.app_directories.Loader',
        'django.template.loaders.eggs.load_template_source':
            'django.template.loaders.eggs.Loader',
        'django.template.loaders.filesystem.load_template_source':
            'django.template.loaders.filesystem.Loader',
    }

    def visit_Str(self, node):
        if node.s in self.removed_items.keys():
            self.found.append((node.s, node))


class TemplateLoadersAnalyzer(BaseAnalyzer):

    def analyze_file(self, filepath, code):
        if not isinstance(code, ast.AST):
            return
        visitor = TemplateLoadersVisitor()
        visitor.visit(code)
        for name, node in visitor.found:
            propose = visitor.removed_items[name]
            result = Result(
                description = (
                    '%r function has been deprecated in Django 1.2 and '
                    'removed in 1.4. Use %r class instead.' % (name, propose)
                ),
                path = filepath,
                line = node.lineno)
            lines = self.get_file_lines(filepath, node.lineno, node.lineno)
            for lineno, important, text in lines:
                result.source.add_line(lineno, text, important)
                result.solution.add_line(lineno, text.replace(name, propose), important)
            yield result
{'fraction_non_alphanumeric': 0.09615384615384616, 'fraction_numerical': 0.0018610421836228288, 'mean_word_length': 2.2057654075546718, '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': '6421519', 'n_tokens_mistral': 903, 'n_tokens_neox': 877, 'n_words': 184}