parser module uses Python’s ast module to parse Python source files and extract structured information about classes, functions, arguments, and docstrings.
parse_file()
Parses a single Python file and extracts all classes, functions, and metrics. Location:docugen/core/parser.py:81
Parameters
Absolute or relative path to the Python file to parse. Accepts both string paths and
Path objects.Root directory for computing relative paths. If
None, only the filename is used. If provided, the result will contain the path relative to this root.Returns
Dictionary containing parsed file information with the following structure:
Class Structure
Each class in theclasses list has:
Function/Method Structure
Each function infunctions or class methods has:
Argument Structure
Each argument inargs has:
Error Handling
- File read errors: Returns result with error message in
errorslist - Syntax errors: Returns result with detailed error location in
errorslist - UTF-8 decoding errors: Uses
errors="replace"to handle invalid UTF-8
Example
parse_project()
Parses multiple Python files and returns a mapping of relative paths to parsed data. Location:docugen/core/parser.py:150
Parameters
List of Python file paths to parse. Can be absolute or relative paths.
Root directory for computing relative paths. All files will have paths computed relative to this root.
Returns
Dictionary mapping relative file paths to their parsed data (same structure as
parse_file() returns). Keys are relative POSIX-style paths (using / separator).Behavior
- Files are processed in sorted order by their absolute paths
- Each file is parsed using
parse_file()with the provided root - The result key for each file is the relative path from root
Example
Helper Functions
The module includes several internal helper functions:_safe_unparse()
Location:docugen/core/parser.py:8
None or unparsing fails.
_read_source()
Location:docugen/core/parser.py:17
_extract_arguments()
Location:docugen/core/parser.py:21
- Positional-only arguments
- Regular positional arguments
*args(var_positional)- Keyword-only arguments
**kwargs(var_keyword)
_extract_function()
Location:docugen/core/parser.py:71
Usage Notes
- The parser uses Python’s built-in
astmodule for accurate parsing - Type annotations are preserved as strings (unparsed AST)
- All exceptions during parsing are caught and returned in the
errorsfield - The parser supports both synchronous and asynchronous functions
- Nested functions and classes are not extracted (only top-level items)
