Example #1
0
def docstring(filename, write, verbose):
    """Manage the docstrings for the project.

    If verbose is passed in, print out the diffs.
    If a filename is passed in, run only on that file. Otherwise run on all files.
    If write is passed in, and a filename is passed. Then overwrite the changes and run black.

    """

    # Only overwrite if looking at a single file and write flag is passed in.
    if filename and write:
        pycom = PyComment(filename, output_style="google")
        pycom.proceed()
        list_from, list_to = pycom.compute_before_after()
        if list_from != list_to:
            click.echo(
                f"Overwriting file: { filename } with changes and calling tool black"
            )
            pycom.overwrite_source_file(list_to)
            call(["black", filename])
        else:
            click.echo(f"No changes needed for file: { filename }")

        return

    file_list = []
    if filename:
        file_list = [filename]
    else:
        files_and_directories = get_root_files_and_directories()
        for item in files_and_directories:
            file_list.extend(get_files_from_dir(item))

    files_with_changes = 0
    files_without_changes = 0
    total_files = len(file_list)

    for file in file_list:
        pycom = PyComment(file, output_style="google")
        pycom.proceed()

        # calculate the difference and track stats
        diff = pycom.diff()
        if len(diff) > 0:
            files_with_changes = files_with_changes + 1
            if verbose:
                click.echo(f"File: { file } has changes")
                for line in diff:
                    click.echo(line, nl=False)
        else:
            files_without_changes = files_without_changes + 1
            if verbose:
                click.echo(f"File: { file } does not have changes")

    click.echo((
        f"{total_files} files scanned, {files_with_changes} files with changes and "
        f"{files_without_changes} files without changes."))
Example #2
0
def run(source, files=[], input_style='auto', output_style='reST', first_line=True, quotes='"""',
        init2class=False, convert=False, config_file=None, ignore_private=False, overwrite=False):
    if input_style == 'auto':
        input_style = None

    config = get_config(config_file)
    if 'init2class' in config:
        init2class = config.pop('init2class')
    if 'convert_only' in config:
        convert = config.pop('convert_only')
    if 'quotes' in config:
        quotes = config.pop('quotes')
    if 'input_style' in config:
        input_style = config.pop('input_style')
    if 'output_style' in config:
        output_style = config.pop('output_style')
    if 'first_line' in config:
        first_line = config.pop('first_line')
    for f in files:
        if os.path.isdir(source):
            path = source + os.sep + os.path.relpath(os.path.abspath(f), os.path.abspath(source))
            path = path[:-len(os.path.basename(f))]
        else:
            path = ''
        c = PyComment(f, quotes=quotes,
                      input_style=input_style,
                      output_style=output_style,
                      first_line=first_line,
                      ignore_private=ignore_private,
                      convert_only=convert,
                      **config)
        c.proceed()
        if init2class:
            c.docs_init_to_class()

        if overwrite:
            list_from, list_to = c.compute_before_after()
            lines_to_write = list_to
        else:
            lines_to_write = c.get_patch_lines(path, path)

        if f == '-':
            sys.stdout.writelines(lines_to_write)
        else:
            if overwrite:
                if list_from != list_to:
                    c.overwrite_source_file(lines_to_write)
            else:
                c.write_patch_file(os.path.basename(f) + ".patch", lines_to_write)
Example #3
0
def run(source,
        files=[],
        input_style='auto',
        output_style='reST',
        first_line=True,
        quotes='"""',
        init2class=False,
        convert=False,
        config_file=None,
        ignore_private=False,
        overwrite=False):
    if input_style == 'auto':
        input_style = None

    config = get_config(config_file)
    if 'init2class' in config:
        init2class = config.pop('init2class')
    if 'convert_only' in config:
        convert = config.pop('convert_only')
    if 'quotes' in config:
        quotes = config.pop('quotes')
    if 'input_style' in config:
        input_style = config.pop('input_style')
    if 'output_style' in config:
        output_style = config.pop('output_style')
    if 'first_line' in config:
        first_line = config.pop('first_line')
    for f in files:
        if os.path.isdir(source):
            path = source + os.sep + os.path.relpath(os.path.abspath(f),
                                                     os.path.abspath(source))
            path = path[:-len(os.path.basename(f))]
        else:
            path = ''
        c = PyComment(f,
                      quotes=quotes,
                      input_style=input_style,
                      output_style=output_style,
                      first_line=first_line,
                      ignore_private=ignore_private,
                      convert_only=convert,
                      **config)
        c.proceed()
        if init2class:
            c.docs_init_to_class()

        if overwrite:
            list_from, list_to = c.compute_before_after()
            lines_to_write = list_to
        else:
            lines_to_write = c.get_patch_lines(path, path)

        if f == '-':
            sys.stdout.writelines(lines_to_write)
        else:
            if overwrite:
                if list_from != list_to:
                    c.overwrite_source_file(lines_to_write)
            else:
                c.write_patch_file(
                    os.path.basename(f) + ".patch", lines_to_write)