Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' of github.com:yumike/djangolint

  • Loading branch information...
commit 2a7659998936b60589d2b677b13ae825d5f0bb7f 2 parents 0981f92 + 46da92c
@yumike authored
View
41 project/lint/analyzers/db_backends.py
@@ -0,0 +1,41 @@
+import ast
+
+from .base import BaseAnalyzer, Result
+
+
+class DB_BackendsVisitor(ast.NodeVisitor):
+
+ def __init__(self):
+ self.found = []
+
+ deprecated_items = {
+ 'django.db.backends.postgresql':
+ 'django.db.backends.postgresql_psycopg2',
+
+ }
+
+ def visit_Str(self, node):
+ if node.s in self.deprecated_items.keys():
+ self.found.append((node.s, node))
+
+
+class DB_BackendsAnalyzer(BaseAnalyzer):
+
+ def analyze_file(self, filepath, code):
+ if not isinstance(code, ast.AST):
+ return
+ visitor = DB_BackendsVisitor()
+ visitor.visit(code)
+ for name, node in visitor.found:
+ propose = visitor.deprecated_items[name]
+ result = Result(
+ description = (
+ '%r backend is deprecated, use %r 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
View
1  project/lint/tests/__init__.py
@@ -14,3 +14,4 @@
from .test_formtools_analyzer import FormToolsAnalyzerTests
from .test_context_processors_analyzer import ContextProcessorsAnalyzerTests
+from .test_db_backends_analyzer import DB_BackendsAnalyzerTests
View
7 project/lint/tests/example_project/settings.py
@@ -26,3 +26,10 @@
'django.core.context_processors.PermLookupDict',
'django.contrib.messages.context_processors.messages',
)
+
+DATABASES = {
+ 'default': {
+ 'ENGINE': 'django.db.backends.postgresql',
+ 'NAME': 'project',
+ }
+}
View
34 project/lint/tests/test_db_backends_analyzer.py
@@ -0,0 +1,34 @@
+import os
+from django.test import TestCase
+
+from ..analyzers.db_backends import DB_BackendsAnalyzer
+from ..parsers import Parser
+
+from .base import TESTS_ROOT
+
+
+class DB_BackendsAnalyzerTests(TestCase):
+
+ def setUp(self):
+ self.maxDiff = None
+ self.example_project = os.path.join(TESTS_ROOT, 'example_project')
+ self.code = Parser(self.example_project).parse()
+ self.analyzer = DB_BackendsAnalyzer(self.code, self.example_project)
+
+ def test_analyze(self):
+ results = list(self.analyzer.analyze())
+ self.assertEqual(len(results), 1)
+ self.assertItemsEqual(results[0].source, [
+ (30, False, "DATABASES = {"),
+ (31, False, " 'default': {"),
+ (32, True, " 'ENGINE': 'django.db.backends.postgresql',"),
+ (33, False, " 'NAME': 'project',"),
+ (34, False, " }"),
+ ])
+ self.assertItemsEqual(results[0].solution, [
+ (30, False, "DATABASES = {"),
+ (31, False, " 'default': {"),
+ (32, True, " 'ENGINE': 'django.db.backends.postgresql_psycopg2',"),
+ (33, False, " 'NAME': 'project',"),
+ (34, False, " }"),
+ ])
View
1  project/settings/common.py
@@ -161,6 +161,7 @@
LINT_ANALYZERS = (
'lint.analyzers.context_processors.ContextProcessorsAnalyzer',
+ 'lint.analyzers.db_backends.DB_BackendsAnalyzer',
'lint.analyzers.formtools.FormToolsAnalyzer',
'lint.analyzers.generic_views.GenericViewsAnalyzer',
'lint.analyzers.render_to_response.RenderToResponseAnalyzer',
Please sign in to comment.
Something went wrong with that request. Please try again.