Skip to content

init_contrib


Script to initialize a new contribution by creating a folder with README.md and metadatas.yml files.

create_contribution(name)

Create a new contribution directory with a README.md, metadatas.yml, init.py, code and tests files.

Parameters:

  • name (str): The name of the contribution.
Source code in scripts/init_contrib.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
def create_contribution(name):
    """
    Create a new contribution directory with a README.md, metadatas.yml, __init__.py, code and tests files.

    Parameters:

    - name (str): The name of the contribution.
    """
    contrib_root = './contrib'
    contrib_path = os.path.join(contrib_root, name)

    # Create the directory if it doesn't exist
    os.makedirs(contrib_path, exist_ok=True)

    # Create README.md
    create_readme(contrib_path=contrib_path, name=name,description=name)

    # Create metadatas.yml
    write_pyproject_file(
         contrib_path=contrib_path,
         name=name,
         metadata={
            "description": name,
         }
    )

    # Create __init__.py
    create_init_py(contrib_path=contrib_path, name=name)

    # Create CONTRIB_NAME.py
    create_main_py(contrib_path=contrib_path, name=name)

    # Create tests folder
    create_tests_directory(contrib_path=contrib_path)

    # Create tests environment config
    create_conftest_file(contrib_path=contrib_path)

    # Create tests/tests_CONTRIB_NAME.py
    create_test_file(contrib_path=contrib_path, name=name)

is_valid_contribution_name(name)

Validate the contribution name.

The name must only contain lowercase letters, digits, and underscores.

Args:

  • name (str): The contribution name to validate.

Returns:

  • bool: True if the name is valid, False otherwise.
Source code in scripts/init_contrib.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def is_valid_contribution_name(name: str):
    """
    Validate the contribution name.

    The name must only contain lowercase letters, digits, and underscores.

    Args:

    - name (str): The contribution name to validate.

    Returns:

    - bool: True if the name is valid, False otherwise.
    """
    return re.fullmatch(r'[a-z0-9_]+', name) is not None

main()

Main entry point of the script.

Accepts the contribution name as a command-line argument, validates it, and creates the necessary files and directory structure in the 'contrib' folder.

Source code in scripts/init_contrib.py
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
def main():
    """
    Main entry point of the script.

    Accepts the contribution name as a command-line argument, validates it, and creates
    the necessary files and directory structure in the 'contrib' folder.
    """
    if len(sys.argv) != 2:
        print("Usage: python3 init.py <contribution_name>")
        sys.exit(1)

    contrib_name = sys.argv[1].strip()

    if not is_valid_contribution_name(contrib_name):
        print(
            "Error: Contribution name must only contain lowercase letters, "
            "digits, and underscores."
        )
        sys.exit(1)

    create_contribution(contrib_name)