class Analyzer::Python::Django

Defined in:

analyzer/analyzers/python/django.cr

Constant Summary

DECORATOR_METHOD_NAME_REGEXES = HTTP_METHODS_EXCLUDING_QUERY.to_h do |m| {m, /[^a-zA-Z0-9](#{m})[^a-zA-Z0-9]/} end

HTTP_METHODS is a fixed table (from PythonEngine), so the decorator-stack scan and the request.method == "..." scan below (each an .each loop run per decorator line / per function-body line) can look up a precompiled regex per method name instead of interpolating and recompiling one on every iteration. Purely a lookup-table hoist — the matched text/capture groups are unchanged, so it doesn't affect the line/offset values computed elsewhere in this file.

DECORATOR_METHOD_NAME_REGEXES also uses HTTP_METHODS_EXCLUDING_QUERY, for the OTHER reason that constant documents: it bare-word-matches a method name anywhere in a decorator's text (this scan is not CBV- specific — it runs over the decorator stack above a plain function too), and real decorators (e.g. DRF-spectacular's OpenApiParameter("query", ...)) commonly carry query as a parameter name rather than a verb restriction. REQUEST_METHOD_NAME_REGEXES keeps the full list: it only fires on an explicit, quoted request.method == "QUERY" comparison, which is unambiguous developer-written evidence, not a name collision.

REGEX_CBV_METHOD_DEF = /\s+(?:async\s+)?def\s+(#{HTTP_METHODS_EXCLUDING_QUERY.join("|")})\s*\(/

def get(...) / async def post(...) method heads in class-based views. Precompiled — an interpolated literal would be recompiled on every line of every CBV class body. Uses HTTP_METHODS_EXCLUDING_QUERY (see PythonEngine): Django's CBV dispatch (View.dispatch) looks up request.method.lower() against the class's http_method_names allowlist, which does not include query upstream — unlike Flask's duck-typed getattr(self, request.method.lower()) dispatch, a def query(self): method is genuinely inert unless a view manually widens that allowlist, so matching it here would report a phantom route.

REGEX_INCLUDE_URLS = /\binclude\s*\(\s*r?['"]([^'"\\]*)['"]/
REGEX_ROOT_URLCONF = /\s*ROOT_URLCONF\s*=\s*r?['"]([^'"\\]*)['"]/

Regular expressions for extracting Django URL configurations

REQUEST_METHOD_NAME_REGEXES = HTTP_METHODS.to_h do |m| {m, /['"](#{m})['"]/} end
REQUEST_PARAM_FIELD_MAP = {"GET" => {["GET"], "query"}, "POST" => {["POST"], "form"}, "COOKIES" => {nil, "cookie"}, "META" => {nil, "header"}, "data" => {["POST", "PUT", "PATCH"], "form"}, "query_params" => {nil, "query"}}

Map request parameters to their respective fields

REQUEST_PARAM_FIELD_PATTERNS = REQUEST_PARAM_FIELD_MAP.map do |field_name, tuple| {field_name, tuple[0], tuple[1], Regex.new("request\\.#{field_name}\\[[rf]?['\"]([^'\"]*)['\"]\\]"), Regex.new("request\\.#{field_name}\\.get(?:list)?\\([rf]?['\"]([^'\"]*)['\"]")} end

Precompiled per-field access patterns so #extract_params_from_line never rebuilds a PCRE2 regex per request field. Compiled once from REQUEST_PARAM_FIELD_MAP. {field_name, field_methods, param_type, bracket_re, get_re}

REQUEST_PARAM_TYPE_MAP = {"query" => nil, "form" => ["POST", "PUT", "PATCH"], "cookie" => nil, "header" => nil}

Map request parameter types to HTTP methods

Class Method Summary

Instance Method Summary

Instance methods inherited from class Analyzer::Python::PythonEngine

build_callees_from(body : String, body_start_line : Int32, path : String, *, definition_base_path : String | Nil = nil, source : String | Nil = nil) : Array(Callee) build_callees_from, find_def_line(lines : Array(String), decorator_line : Int32) : Int32 | Nil find_def_line, find_imported_modules(app_base_path : String, file_path : String, content : String | Nil = nil) : Hash(String, Tuple(String, Int32)) find_imported_modules, find_imported_package(package_path : String, dotted_as_names : String) : Array(Tuple(String, String, Int32)) find_imported_package, find_json_params(codeblock_lines : Array(String), json_var_names : Array(String)) : Array(Param) find_json_params, join_until_python_call_closes(lines : Array(String), index : Int32, line : String) : String join_until_python_call_closes, parse_code_block(data : String | Array(String), after : Regex | Nil = nil) : String | Nil parse_code_block, parse_function_def(source_lines : Array(String), start_index : Int32) : FunctionDefinition | Nil parse_function_def, push_callees_from(endpoint : Endpoint, body : String, body_start_line : Int32, path : String, *, definition_base_path : String | Nil = nil, source : String | Nil = nil) : Nil push_callees_from, python_bracket_delta(line : String) : Int32 python_bracket_delta, python_paren_delta(line : String) : Int32 python_paren_delta, python_signature_line_span(lines : Array(String)) : Int32 python_signature_line_span, return_literal_value(data : String) : String return_literal_value

Class methods inherited from class Analyzer::Python::PythonEngine

python_test_path?(path : String, base_path : String | Nil = nil) : Bool python_test_path?

Instance methods inherited from class Analyzer

analyze analyze, base_path : String base_path, base_paths : Array(String) base_paths, base_relative_path(path : String) : String base_relative_path, callees_needed? : Bool callees_needed?, content_matches?(content : String, markers : Regex) : Bool content_matches?, http_header_name(name : String) : String | Nil http_header_name, line_number_for_index(content : String, char_index : Int32) : Int32 line_number_for_index, logger : NoirLogger logger, parallel_analyze(files : Array(String), &block : String -> Nil) parallel_analyze, read_file_content(path : String) : String read_file_content, result : Array(Endpoint) result, tech : String tech, unique_params(params : Array(Param)) : Array(Param) unique_params, url : String url, web_root_path(path : String, markers : Array(String)) : String web_root_path

Constructor methods inherited from class Analyzer

new(options : Hash(String, YAML::Any)) new

Macros inherited from class Analyzer

analyzer_for(tech) analyzer_for

Instance methods inherited from module FileHelper

all_files : Array(String) all_files, get_files_by_basename(basename : String) : Array(String) get_files_by_basename, get_files_by_extension(extension : String) : Array(String) get_files_by_extension, get_files_by_extensions(extensions : Array(String)) : Array(String) get_files_by_extensions, get_files_by_prefix(prefix : String) : Array(String) get_files_by_prefix, get_files_by_prefix_and_extension(prefix : String, extension : String) : Array(String) get_files_by_prefix_and_extension, get_files_by_relative_path(relative_path : String, root : String = "") : Array(String) get_files_by_relative_path, get_public_dir_files(base_path : String, folder : String) : Array(String) get_public_dir_files, get_public_files(base_path : String, anchors : Array(String) = ["shard.yml", "Gemfile"]) : Array(String) get_public_files, walked_path(expanded : String) : String walked_path

Class Method Detail

def self.tech_name : String #

[View source]

Instance Method Detail

def analyze #

[View source]
def extract_endpoints(django_urls : DjangoUrls) : Array(Endpoint) #

Extract endpoints from a Django URL configuration file


[View source]
def extract_endpoints_from_file(url : String, filepath : String, function_or_class_name : String) #

Extract endpoints from a given file


[View source]
def extract_params_from_line(line : String, endpoint_methods : Array(String)) #

Extract parameters from a line of code


[View source]
def filter_params(method : String, params : Array(Param)) #

Filter parameters based on HTTP method


[View source]
def find_root_django_urls : Array(DjangoUrls) #

Find all root Django URLs


[View source]
def tech : String #

Instance-side view of the same declaration. The per-file rescues live on this base class, which has no way to name the analyzer that is running inside them, so a skipped file could not be attributed to a tech. Deriving it from analyzer_for keeps the name written exactly once.


[View source]