示例#1
0
"""Transforms."""
import os
import re
from pylint_django.transforms import foreignkey, fields
from astroid import MANAGER
from astroid.builder import AstroidBuilder
from astroid import nodes


foreignkey.add_transform(MANAGER)
fields.add_transforms(MANAGER)


def _add_transform(package_name, *class_names):
    """Transform package's classes."""
    transforms_dir = os.path.join(os.path.dirname(__file__), 'transforms')
    fake_module_path = os.path.join(transforms_dir, '%s.py' % re.sub(r'\.', '_', package_name))

    with open(fake_module_path) as modulefile:
        fake_module = modulefile.read()

    fake = AstroidBuilder(MANAGER).string_build(fake_module)

    def set_fake_locals(module):
        """Set fake locals for package."""
        if module.name != package_name:
            return
        for class_name in class_names:
            module.locals[class_name] = fake.locals[class_name]

    MANAGER.register_transform(nodes.Module, set_fake_locals)
    def open(self):
        # This is a bit of a hacky workaround. pylint-django does not *require* that
        # Django is configured explicitly, and will use some basic defaults in that
        # case. However, as this is a WARNING not a FATAL, the error must be raised
        # with an AST node - only F and R messages are scope exempt (see
        # https://github.com/PyCQA/pylint/blob/master/pylint/constants.py#L24)

        # However, testing to see if Django is configured happens in `open()`
        # before any modules are inspected, as Django needs to be configured with
        # defaults before the foreignkey checker can work properly. At this point,
        # there are no nodes.

        # Therefore, during the initialisation in `open()`, if django was configured
        # using defaults by pylint-django, it cannot raise the warning yet and
        # must wait until some module is inspected to be able to raise... so that
        # state is stashed in this property.

        from django.core.exceptions import (  # pylint: disable=import-outside-toplevel
            ImproperlyConfigured,
        )

        try:
            import django  # pylint: disable=import-outside-toplevel

            django.setup()
            from django.apps import apps  # noqa pylint: disable=import-outside-toplevel,unused-import
        except ImproperlyConfigured:
            # this means that Django wasn't able to configure itself using some defaults
            # provided (likely in a DJANGO_SETTINGS_MODULE environment variable)
            # so see if the user has specified a pylint option
            if self.config.django_settings_module is None:
                # we will warn the user that they haven't actually configured Django themselves
                self._raise_warning = True
                # but use django defaults then...
                from django.conf import (  # pylint: disable=import-outside-toplevel
                    settings,
                )
                settings.configure()
                django.setup()
            else:
                # see if we can load the provided settings module
                try:
                    from django.conf import (  # pylint: disable=import-outside-toplevel
                        settings,
                        Settings,
                    )

                    settings.configure(Settings(self.config.django_settings_module))
                    django.setup()
                except ImportError:
                    # we could not find the provided settings module...
                    # at least here it is a fatal error so we can just raise this immediately
                    self.add_message(
                        "django-settings-module-not-found",
                        args=self.config.django_settings_module,
                    )
                    # however we'll trundle on with basic settings
                    from django.conf import (  # pylint: disable=import-outside-toplevel
                        settings,
                    )

                    settings.configure()
                    django.setup()

        # Now we can add the transforms specific to this checker
        foreignkey.add_transform(astroid.MANAGER)
示例#3
0
"""Transforms."""
import os
import re
from pylint_django.transforms import foreignkey, fields
from astroid import MANAGER
from astroid.builder import AstroidBuilder
from astroid import nodes

foreignkey.add_transform(MANAGER)
fields.add_transforms(MANAGER)


def _add_transform(package_name, *class_names):
    """Transform package's classes."""
    transforms_dir = os.path.join(os.path.dirname(__file__), 'transforms')
    fake_module_path = os.path.join(transforms_dir,
                                    '%s.py' % re.sub(r'\.', '_', package_name))

    with open(fake_module_path) as modulefile:
        fake_module = modulefile.read()

    fake = AstroidBuilder(MANAGER).string_build(fake_module)

    def set_fake_locals(module):
        """Set fake locals for package."""
        if module.name != package_name:
            return
        for class_name in class_names:
            module._locals[class_name] = fake._locals[class_name]  # pylint: disable=protected-access

    MANAGER.register_transform(nodes.Module, set_fake_locals)
示例#4
0
"""Transforms."""
import os
import re

import astroid

from pylint_django.transforms import foreignkey, fields

foreignkey.add_transform(astroid.MANAGER)
fields.add_transforms(astroid.MANAGER)


def _add_transform(package_name):
    def fake_module_builder():
        """
            Build a fake module to use within transformations.
            @package_name is a parameter from the outher scope b/c according to
            the docs this can't receive any parameters.
            http://pylint.pycqa.org/projects/astroid/en/latest/extending.html?highlight=MANAGER#module-extender-transforms
        """
        transforms_dir = os.path.join(os.path.dirname(__file__), 'transforms')
        fake_module_path = os.path.join(
            transforms_dir, '%s.py' % re.sub(r'\.', '_', package_name))

        with open(fake_module_path) as modulefile:
            fake_module = modulefile.read()

        return astroid.builder.AstroidBuilder(
            astroid.MANAGER).string_build(fake_module)

    astroid.register_module_extender(astroid.MANAGER, package_name,