ctfsolver.managers.manager_functions¶
manager_functions.py
This module provides the ManagerFunction class, which offers utility methods for introspecting and managing functions within a class and Python source files. It includes methods to list class functions, retrieve references to function usages in files, and extract function definitions from Python files using the AST module.
- Classes:
ManagerFunction: Provides methods for function introspection and management.
Example
manager = ManagerFunction() functions = manager.get_self_functions() references = manager.get_function_reference(‘my_func’, ‘my_file.py’) ast_functions = manager.get_functions_from_file(Path(‘my_file.py’))
Classes¶
ManagerFunction provides utility methods for introspecting and managing functions within a class and Python source files. |
Module Contents¶
- class ManagerFunction(*args, **kwargs)[source]¶
ManagerFunction provides utility methods for introspecting and managing functions within a class and Python source files.
- This class includes methods to:
List all callable functions defined in the class.
Retrieve references to function usages in a given file.
Extract function definitions from Python files using the AST module.
- get_self_functions()[source]¶
Lists all callable functions of the class instance, excluding special methods.
- get_function_reference(function, file)[source]¶
Finds and returns all lines in the specified file where the given function name appears.
- Parameters:
- Returns:
Lines from the file containing the function name.
- Return type:
- Raises:
ValueError – If the function is not found in the class.
- get_functions_from_file(file_path, function_name=None)[source]¶
Parses the given Python file and returns function definitions using AST.
- Parameters:
file_path (Path) – Path to the Python file.
function_name (str, optional) – Specific function name to search for.
- Returns:
List of function definitions, a single function definition, or None.
- Return type:
list[ast.FunctionDef] | ast.FunctionDef | None
- find_function_from_file(file_path, function_name)[source]¶
Deprecated. Use get_functions_from_file instead.
- get_self_functions()[source]¶
Retrieves a list of all callable methods of the current instance, excluding special methods. :returns: A list of method names (str) that are callable and do not start with double underscores. :rtype: list
- get_function_reference(function, file)[source]¶
Retrieves all lines from a file that reference a specified function. :param function: The name of the function to search for. :type function: str :param file: The path to the file in which to search for the function reference. :type file: str
- Returns:
A list of strings, each representing a line from the file where the function is referenced.
- Return type:
- Raises:
ValueError – If the specified function is not found in the class.
- function_object(file_path, function_name)[source]¶
- Parameters:
file_path (pathlib.Path)
function_name (str)
- Return type:
ctfsolver.find_usage.function_definition_class.FunctionDefFinder
- get_functions_from_file(file_path, function_name=None)[source]¶
Extracts function definitions from a Python file using AST parsing. :param file_path: The path to the Python file to analyze. :type file_path: Path :param function_name: The name of a specific function to find. If None, all function definitions are returned. :type function_name: str, optional
- Returns:
If function_name is provided and found, returns the corresponding ast.FunctionDef object.
If function_name is not provided, returns a list of all ast.FunctionDef objects found in the file.
Returns None if the specified function_name is not found.
- Return type:
list[ast.FunctionDef] | None | ast.FunctionDef
- Raises:
FileNotFoundError – If the specified file_path does not exist.
SyntaxError – If the file contains invalid Python syntax.
- Parameters:
file_path (pathlib.Path)
function_name (str)