晋太元中,武陵人捕鱼为业。缘溪行,忘路之远近。忽逢桃花林,夹岸数百步,中无杂树,芳草鲜美,落英缤纷。渔人甚异之,复前行,欲穷其林。 林尽水源,便得一山,山有小口,仿佛若有光。便舍船,从口入。初极狭,才通人。复行数十步,豁然开朗。土地平旷,屋舍俨然,有良田、美池、桑竹之属。阡陌交通,鸡犬相闻。其中往来种作,男女衣着,悉如外人。黄发垂髫,并怡然自乐。 见渔人,乃大惊,问所从来。具答之。便要还家,设酒杀鸡作食。村中闻有此人,咸来问讯。自云先世避秦时乱,率妻子邑人来此绝境,不复出焉,遂与外人间隔。问今是何世,乃不知有汉,无论魏晋。此人一一为具言所闻,皆叹惋。余人各复延至其家,皆出酒食。停数日,辞去。此中人语云:“不足为外人道也。”(间隔 一作:隔绝) 既出,得其船,便扶向路,处处志之。及郡下,诣太守,说如此。太守即遣人随其往,寻向所志,遂迷,不复得路。 南阳刘子骥,高尚士也,闻之,欣然规往。未果,寻病终。后遂无问津者。
| DIR:/opt/cloudlinux/venv/lib/python3.11/site-packages/pylint_django/checkers/ |
| Current File : //opt/cloudlinux/venv/lib/python3.11/site-packages/pylint_django/checkers/foreign_key_strings.py |
from __future__ import absolute_import
import astroid
from pylint.checkers import BaseChecker
from pylint.checkers.utils import check_messages
from pylint.interfaces import IAstroidChecker
from pylint_django.__pkginfo__ import BASE_ID
from pylint_django.transforms import foreignkey
class ForeignKeyStringsChecker(BaseChecker):
"""
Adds transforms to be able to do type inference for model ForeignKeyField
properties which use a string to name the foreign relationship. This uses
Django's model name resolution and this checker wraps the setup to ensure
Django is able to configure itself before attempting to use the lookups.
"""
_LONG_MESSAGE = """Finding foreign-key relationships from strings in pylint-django requires configuring Django.
This can be done via the DJANGO_SETTINGS_MODULE environment variable or the pylint option django-settings-module, eg:
`pylint --load-plugins=pylint_django --django-settings-module=myproject.settings`
. This can also be set as an option in a .pylintrc configuration file.
Some basic default settings were used, however this will lead to less accurate linting.
Consider passing in an explicit Django configuration file to match your project to improve accuracy."""
__implements__ = (IAstroidChecker,)
name = "Django foreign keys referenced by strings"
options = (
(
"django-settings-module",
{
"default": None,
"type": "string",
"metavar": "<django settings module>",
"help": "A module containing Django settings to be used while linting.",
},
),
)
msgs = {
# pylint: disable=implicit-str-concat
f"E{BASE_ID}10": (
"Django was not configured. For more information run "
"pylint --load-plugins=pylint_django --help-msg=django-not-configured",
"django-not-configured",
_LONG_MESSAGE,
),
f"F{BASE_ID}10": (
"Provided Django settings %s could not be loaded",
"django-settings-module-not-found",
"The provided Django settings module %s was not found on the path",
),
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._raise_warning = False
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.
try:
from django.core.exceptions import ( # pylint: disable=import-outside-toplevel
ImproperlyConfigured,
)
except ModuleNotFoundError:
return
try:
import django # pylint: disable=import-outside-toplevel
django.setup()
from django.apps import ( # noqa pylint: disable=import-outside-toplevel,unused-import
apps,
)
# flake8: noqa=F401, F403
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)
# TODO: this is a bit messy having so many inline imports but in order to avoid
# duplicating the django_installed checker, it'll do for now. In the future, merging
# those two checkers together might make sense.
@check_messages("django-not-configured")
def visit_module(self, node):
if self._raise_warning:
# just add it to the first node we see... which isn't nice but not sure what else to do
self.add_message("django-not-configured", node=node)
self._raise_warning = False # only raise it once...
|