Repository: raspberrypi/pico-sdk
File: tools/extract_cmake_functions.py (Python)

Code before:
                for match in CMAKE_FUNCTION_RE.finditer(text):
                    name = match.group(4)
                    signature = match.group(1).strip()
                    if signature.startswith(name):
                        description, brief, params = process_commands(match.group(2).replace('#', ''), name, group, signature)
                        all_functions[name] = {
                            'name': name,
                            'group': group,
                            'signature': signature,
                            'brief': brief,
                            'description': description,
                            'params': params
                        }

                for match in CMAKE_PICO_FUNCTIONS_RE.finditer(text):
                    name = match.group(1)
                    if name not in all_functions and name not in allowed_missing_functions:
                        logger.warning("{} function has no description in {}".format(name, file_path))


with open(outfile, 'w', newline='') as csvfile:
    fieldnames = ('name', 'group', 'signature', 'brief', 'description', 'params')
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames, extrasaction='ignore', dialect='excel-tab')

    writer.writeheader()
    for name, row in sorted(all_functions.items(), key=sort_functions):
        writer.writerow(row)


Review comment (refactor):
`name` isn't used here, so I guess you could even simplify this to `for row in sorted(all_functions.values(), key=sort_functions):` ?

Code after:


def sort_functions(item):
    group = item['group']
    name = item['name']

    precedence = 5
    if name.startswith('_desc_'):
        precedence = 0
    elif group == 'other':
        if re.match(r'^pico_add_.*_output$', name):
            precedence = 1
        elif name == 'pico_add_extra_outputs':
            precedence = 2
        elif re.match(r'^pico_.*_binary$', name):
            precedence = 3
    return group + str(precedence) + name


# Scan all CMakeLists.txt and .cmake files in the specific path, recursively.

for dirpath, dirnames, filenames in os.walk(scandir):
    for filename in filenames:
        if filename in skip_files:
            continue
        # Default group is the directory name - can be overridden by the \ingroup\ command
        group = os.path.basename(dirpath)
        if group in skip_groups:
            continue
        if group in ['tools', 'cmake']:
            group = 'other'
        file_ext = os.path.splitext(filename)[1]
        if filename == 'CMakeLists.txt' or file_ext == '.cmake':
            file_path = os.path.join(dirpath, filename)

            with open(file_path, encoding="ISO-8859-1") as fh:
                text = fh.read()
                for match in CMAKE_FUNCTION_RE.finditer(text):
                    name = match.group(4)
                    signature = match.group(1).strip()
                    if signature.startswith(name):
                        description, brief, params, processed_group = process_commands(match.group(2).replace('#', ''), name, group, signature)
                        new_dict = {
                            'name': name,
                            'group': processed_group,
                            'signature': signature,
                            'brief': brief,
                            'description': description,
                            'params': params
                        }
                        if all_functions.get(name):