Skip to content

update_codemeta

Shallow wrapper around codemetapy to use it with pre-commit.

update_codemeta(target=trg_arg, sources=src_arg)

Entry point of CLI application.

Runs codemetapy on the passed sources, compares resulting graph with target file (if it exists).

Only writes to the output if the metadata is not equivalent. The equivalence is checked on graph level using rdflib.

Parameters:

Name Type Description Default
target Path

Output file (usually codemeta.json)

trg_arg
sources List[str]

Metadata input files (such as pyproject.toml)

src_arg
Source code in src/dev_utils/update_codemeta.py
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
@app.command(
    help="""
Create or update the target codemeta file (first argument)
by running codemetapy with all the other passed arguments.
If the output is the same as before, will keep file unchanged.
"""
)
def update_codemeta(
    target: Path = trg_arg,
    sources: List[str] = src_arg,
):
    """Entry point of CLI application.

    Runs codemetapy on the passed sources,
    compares resulting graph with target file (if it exists).

    Only writes to the output if the metadata is not equivalent.
    The equivalence is checked on graph level using `rdflib`.

    Args:
        target: Output file (usually `codemeta.json`)
        sources: Metadata input files (such as `pyproject.toml`)
    """
    # load old codemeta graph (if any)
    old_metadata = rdflib.Graph()
    if target.is_file():
        with open(target, "r") as f:
            dat = json.dumps(_localize_codemeta_context(json.load(f)))
        old_metadata.parse(data=dat, format="json-ld")

    # generate new codemeta graph
    cm = _gen_codemeta(sources)
    original = _serialize_codemeta(cm)

    # only write result to file if the graph changed
    expanded = _serialize_codemeta(_localize_codemeta_context(cm))
    new_metadata = rdflib.Graph()
    new_metadata.parse(data=expanded, format="json-ld")
    if not rdflib.compare.isomorphic(old_metadata, new_metadata):
        typer.echo(f"Project metadata changed, writing {target} ...")
        with open(target, "w") as f:
            f.write(original)