# sphinx_gallery.py_source_parser

Parser for python source files.

## Functions

sphinx_gallery.py_source_parser.extract_file_config(content: str) → dict[str, Any]

  Pull out the file-specific config specified in the docstring.

sphinx_gallery.py_source_parser.parse_source_file(filename: str | Path) → tuple[Module | None, str]

  Parse source file into AST node.

  Parameters:

    filename (str or Path) – File path

  Returns:

    - node (AST node)

    - content (utf-8 encoded string)

sphinx_gallery.py_source_parser.remove_config_comments(code_block: str) → str

  Return the content of code_block with in-file config comments removed.

  Comment lines of the pattern ‘# sphinx_gallery_[option] = [val]’ are removed, but surrounding empty lines are preserved.

  Parameters:

    code_block (str) – A code segment.

sphinx_gallery.py_source_parser.remove_ignore_blocks(code_block: str) → str

  Return the content of code_block with ignored areas removed.

  An ignore block starts with # sphinx_gallery_start_ignore, and ends with # sphinx_gallery_end_ignore. These lines and anything in between them will be removed, but surrounding empty lines are preserved.

  Parameters:

    code_block (str) – A code segment.

sphinx_gallery.py_source_parser.split_code_and_text_blocks(source_file: str | Path, return_node: Literal[True]) → tuple[dict[str, Any], list, Module | None]
sphinx_gallery.py_source_parser.split_code_and_text_blocks(source_file: str | Path, return_node: Literal[False] = False) → tuple[dict[str, Any], list]

  Return list with source file separated into code and text blocks.

  Parameters:

    - source_file (str or Path) – Path to the source file.

    - return_node (bool) – If True, return the ast node.

  Returns:

    - file_conf (dict) – File-specific settings given in source file comments as: `# sphinx_gallery_<name> = <value>`

    - blocks (list) – (label, content, line_number) List where each element is a tuple with the label (‘text’ or ‘code’), the corresponding content string of block and the leading line number.

    - node (ast.Module) – The parsed ast node.

### Examples using `sphinx_gallery.py_source_parser.split_code_and_text_blocks`

Identifying function names in a script

Identifying function names in a script

## Classes

class sphinx_gallery.py_source_parser.Block(type, content, lineno)
  content

    Alias for field number 1

  lineno

    Alias for field number 2

  type

    Alias for field number 0