File: model_methods.py

package info (click to toggle)
python-django-lint 0.13-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 176 kB
  • sloc: python: 638; makefile: 5
file content (136 lines) | stat: -rw-r--r-- 4,291 bytes parent folder | download | duplicates (2)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# -*- coding: utf-8 -*-

# django-lint -- Static analysis tool for Django projects and applications
# Copyright (C) 2008-2009 Chris Lamb <chris@chris-lamb.co.uk>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os.path

from logilab import astng

from pylint.interfaces import IASTNGChecker
from pylint.checkers import BaseChecker
from pylint.checkers.utils import safe_infer

from utils import is_model

try:
    from itertools import combinations
except ImportError:
    # Python <= 2.5 fallback
    def combinations(iterable, r):
        if r:
            for i, cur in enumerate(iterable):
                for xs in combinations(iterable[:i] + iterable[i + 1:], r - 1):
                    yield [cur] + xs
        else:
            yield []

class ModelMethodsChecker(BaseChecker):
    __implements__ = IASTNGChecker

    name = 'django_model_models'
    msgs = {
        'W8010': (
            'Too many models (%d/%d); consider splitting application',
        '',),
        'W8011': ('Use __unicode__ instead of __str__', '',),
        'W8012': ('Method should come after standard model methods', '',),
        'W8013': ('Standard model method should come before %r', '',),
        'W8015': (
            '%d models have common prefix (%r) - rename or split application',
        '',),
    }

    options = (
        ('max-models', {
            'default': 10,
            'type': 'int',
            'metavar': '<int>',
            'help': 'Maximum number of models per module',
        }),
    )

    def visit_module(self, node):
        self.model_names = []

    def leave_module(self, node):
        if len(self.model_names) >= self.config.max_models:
            self.add_message('W8010', node=node.root(),
                args=(len(self.model_names), self.config.max_models))

        for names in combinations(self.model_names, 4):
            common = os.path.commonprefix(names)
            if len(common) >= 4:
                # Whitelist a few common names
                if common.lower() in ('abstract',):
                    continue

                # How many actually have this prefix?
                xs = filter(lambda x: x.startswith(common), self.model_names)

                self.add_message('W8015', node=node.root(),
                    args=(len(xs), common,))
                break

    def visit_function(self, node):
        if not is_model(node.parent.frame()):
            return

        if node.name == '__str__':
            self.add_message('W8011', node=node)

        try:
            idx = [
                '__unicode__',
                '__str__',
                'save',
                'delete',
                'get_absolute_url',
            ].index(node.name)

            if self.prev_idx == -1:
                self.add_message('W8012', node=self.prev_node)

            elif idx < self.prev_idx:
                self.add_message('W8013', node=node, args=self.prev_node.name)

        except ValueError:
            idx = -1

        self.prev_idx = idx
        self.prev_node = node

    def visit_class(self, node):
        if not is_model(node):
            return

        self.model_names.append(node.name)
        self.prev_idx = None
        self.prev_name = None

    def leave_class(self, node):
        if node.name == 'Meta' and is_model(node.parent.parent):
            # Annotate the model with information from the Meta class
            try:
                val = safe_infer(node.locals['abstract'][-1]).value
                if val is True:
                    node.parent.parent._django_abstract = True
            except KeyError:
                pass
            return

        if not is_model(node):
            return