betagouv/beta.gouv.fr

[beta.gouv.fr] Ajouter les startups sur les fiches membres

LucasCharrier opened this issue · 5 comments

[beta.gouv.fr] Ajouter les startups sur les fiches membres

j'ai un script pour faire ça, j'attends peut être qu'on ai mergé ta PR @revolunet pour faire les modifs

import os
import yaml
import copy

# Define the path to the authors directory
authors_dir = 'content/_authors'

class CustomDumper(yaml.SafeDumper):
    def increase_indent(self, flow=False, indentless=False):
        return super(CustomDumper, self).increase_indent(flow, False)

# Iterate over every file in the authors directory
for filename in os.listdir(authors_dir)[0:10]:
    # Construct the full path to the file
    file_path = os.path.join(authors_dir, filename)
    
    # Initialize a list to store updated documents
    updated_documents = []

    try:
        with open(file_path, 'r') as file:
            documents = yaml.safe_load_all(file)
            for data in documents:
                # Ensure data is a dictionary before proceeding
                if isinstance(data, dict):
                    # Check if the document is not empty
                    if data:
                        # Check if there's only one mission and if startups key exists
                        if 'missions' in data and len(data['missions']) == 1 and 'startups' in data:
                            # Copy the startups information into the mission
                            data['missions'][0]['startups'] = copy.deepcopy(data['startups'])
                        # Append the potentially modified document to the list
                print(data)
                updated_documents.append(data)
    except yaml.YAMLError as e:
        print(f"Error processing {filename}: {e}")
        continue  # Skip to the next file

    # Write the updated documents back to the YAML file, if any documents were successfully processed
    if updated_documents:
        newstr = ''
        for i, document in enumerate(updated_documents):
            newstr += '---\n'
            if document:
                yaml_str = yaml.dump(document, Dumper=CustomDumper, allow_unicode=True, sort_keys=False, explicit_end=None)
                newstr += yaml_str
                if newstr.endswith('...\n'):
                    newstr=newstr[:-4]
            with open(file_path, 'w', encoding='utf-8') as file:
                file.write(newstr)

print("Processing complete.")

Je viens de voir que c'est mergé. Je vais faire mes changements.

J'avais un début de script ici au cas ou :

// fix author files missions (WIP)

j'ai refait une PR plus propre

fixed by Lucas !