Skip to content

CLI

docugenius.cli.main

RunArgs

Bases: BaseModel

A class to hold the arguments for running the docstring generation.

Attributes:

Name Type Description
input_path str

The path to the input file or directory.

model str

The model to use for generating docstrings. Must be one of ["openai:gpt-4o", "openai:gpt-4o-mini"].

docstring_format str

The format of the generated docstrings. Must be one of ["google", "numpy", "sprinx"].

skip_raises bool

Whether to include information about exceptions raised by the code.

skip_returns bool

Whether to include information about the return value of the code.

skip_examples bool

Whether to include examples of how to use the code.

output_path str

The path to the output file/destination. If not provided, the output will overwrite the input file. If a directory is passed as input, this should be a directory.

Raises: ValueError: If the model or docstring_format is not one of the specified literals.

is_recursive()

Check if the input path is a directory or a file.

Returns:

Name Type Description
bool bool

True if the input path is a directory, False if it is a file.

Source code in docugenius/cli/main.py
def is_recursive(self) -> bool:
    """
    Check if the input path is a directory or a file.

    Returns:
        bool: True if the input path is a directory, False if it is a file.
    """
    return Path(self.input_path).is_dir()

run(args)

Run the docstring generation process based on the provided arguments.

Parameters:

Name Type Description Default
args RunArgs

The arguments for running the docstring generation.

required

Raises:

Type Description
FileNotFoundError

If the input file/directory does not exist.

Exception

If there is an error during docstring generation.

Returns:

Name Type Description
None

This function does not return a value.

Example

run(RunArgs(input_path='example.py', model='openai:gpt-4o'))

Source code in docugenius/cli/main.py
def run(args: RunArgs):
    """
    Run the docstring generation process based on the provided arguments.

    Args:
        args (RunArgs): The arguments for running the docstring generation.

    Raises:
        FileNotFoundError: If the input file/directory does not exist.
        Exception: If there is an error during docstring generation.

    Returns:
        None: This function does not return a value.

    Example:
        >>> run(RunArgs(input_path='example.py', model='openai:gpt-4o'))
    """

    input_path = args.input_path
    model = args.model
    output_path = args.output_path

    if args.is_recursive():
        for file in find_python_files(input_path):
            run_on_file(file, model, output_path)
    else:
        run_on_file(input_path, model, output_path)

run_on_file(input_file, model, output_file=None)

Generate docstrings for the code in the specified input file.

Parameters:

Name Type Description Default
input_file str

The path to the input file.

required
model str

The model to use for generating docstrings.

required
output_file str

The path to the output file. Defaults to None.

None

Raises:

Type Description
FileNotFoundError

If the input file does not exist.

Exception

If there is an error during docstring generation.

Returns:

Name Type Description
None

This function does not return a value.

Example

run_on_file('example.py', 'openai:gpt-4o')

Source code in docugenius/cli/main.py
def run_on_file(input_file: str, model: str, output_file: str = None):
    """
    Generate docstrings for the code in the specified input file.

    Args:
        input_file (str): The path to the input file.
        model (str): The model to use for generating docstrings.
        output_file (str, optional): The path to the output file. Defaults to None.

    Raises:
        FileNotFoundError: If the input file does not exist.
        Exception: If there is an error during docstring generation.

    Returns:
        None: This function does not return a value.

    Example:
        >>> run_on_file('example.py', 'openai:gpt-4o')
    """

    with open(input_file) as f:
        code = f.read()

    genius = genius_factory(model)

    docstring = genius.generate(code)

    output_file = output_file or input_file

    with open(output_file, "w") as f:
        f.write(docstring)