Docstring Parsers

RestructuredText and Parameter parsers

class Options[source]

A dictionary with attribute access.

class Directive[source]

A simple Class which corresponds to reStructureText directive.

Parameters

argument (Optional[str]) – The argument of the directive.

property line_string: str

Converts the lines of the directive into a single string.

Returns

The string of the lines.

Return type

str

to_string()[source]

Converts the directive into a string represenation meant for usage in Discord Embeds.

Parameters

ext (Optional[str]) – The extension for markdown.

Returns

The string representation of the directive.

Return type

str

class Param[source]

A simple Class which corresponds to function parameters.

Parameters
  • name (str) – The name of the parameter.

  • description (str) – The description of the parameter.

parse()[source]

Resolves the parameter type into attributes using regular expressions.

Returns

The resolved parameters.

Return type

Dict[str, Any]

class CustomRstParser[source]
A barebones reStructuredText parser using Regex.
Can also be used in Context Manager mode.

Note

Might be switched to an AST based one in the future.

Example

>>> from scripts.utils.parsers import CustomRstParser
>>> with open('README.rst') as f:
...     data = f.read()
>>> # As an object.
>>> parser = CustomRstParser()
>>> parser.parse(data)
>>> print(
...     parser.sections[0].to_string()
... )
>>> # As a context manager.
>>> with CustomRstParser() as parser:
...     parser.parse(data)
...     print(
...         parser.sections[0].to_string()
...     )
>>> assert parser.sections == []
meta

Meta Directive

Tip

There’s usually only one Meta directive per docstring.

directives

All the parsed Directive.

params

All the parsed Param.

sections

Rubric sections containing child Directive.

info

Additional lines before Params.

property parsed_params

Returns a list of parsed parameters.

Returns

The parsed params.

Return type

List[Dict[str, Any]]

property param_names: List[str]

Returns a list of parsed parameter names.

Returns

The parsed params.

Return type

List[str]

parse()[source]

Parses the given text and updates the internal sections, directives and parameters.

Parameters

text (str) – The reST text (docstrings) to parse.