File: auto_doc_mutations.py

package info (click to toggle)
cataclysm-dda 0.H-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 710,808 kB
  • sloc: cpp: 524,019; python: 11,580; sh: 1,228; makefile: 1,169; xml: 507; javascript: 150; sql: 56; exp: 41; perl: 37
file content (300 lines) | stat: -rw-r--r-- 9,881 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import functools
import os
import sys
from collections import defaultdict
from dataclasses import dataclass
from pathlib import Path
from typing import Generator

import click
import orjson


def find_files(
    paths: list[Path], extensions: tuple[str], include_mods=False
) -> Generator[str, None, None]:
    """Recursively search :paths: for files that have :extensions:.

    If :include_mods: is False (the default), ignore all files that are in a
    subdirectory of a directory containing a modinfo.json file. If
    :include_mods: is True, yield modinfo.json before all other files,
    if it exists.
    """
    for path in paths:
        if os.path.isdir(path):
            for dirpath, dirnames, filenames in os.walk(path):
                # Ignore all subdirectories and files inside a mod.
                if not include_mods and "modinfo.json" in filenames:
                    dirnames.clear()
                    filenames.clear()

                # Yield modinfo.json before all other files, if it exists.
                if include_mods and "modinfo.json" in filenames:
                    # More than one modinfo.json is not allowed
                    if filenames.count("modinfo.json") > 1:
                        dirnames.clear()
                        filenames.clear()
                        click.echo(
                            f"More than one modinfo.json found in {dirpath}.",
                            err=True,
                        )
                    else:
                        filenames.remove("modinfo.json")
                        yield os.path.join(dirpath, "modinfo.json")

                for filename in filenames:
                    _, ext = os.path.splitext(filename)
                    if ext in extensions:
                        yield os.path.join(dirpath, filename)
        else:
            if path.suffix in extensions:
                yield path


@functools.total_ordering
@dataclass(init=True, repr=True)
class Mutation:
    id_: str
    name: str
    categories: list[str]
    description: str
    mod: str
    copied: bool = False

    def __lt__(self, other) -> bool:
        if not isinstance(other, Mutation):
            return NotImplemented
        return self.name.lower() < other.name.lower()


def write_mutations(mutations: list[Mutation], filename: Path) -> None:
    categories = defaultdict(list)

    for mut in mutations:
        if mut.categories != "NONE":
            for cat in mut.categories:
                categories[cat].append(mut)
        else:
            categories["NONE"].append(mut)

    # Sort categories alphabetically
    categories = dict(sorted(categories.items()))

    # Sort mutations alphabetically in categories
    for mutations in categories.values():
        mutations.sort()

        # Sort each mutation's categories alphabetically
        for mut in mutations:
            mut.categories.sort()

    with open(filename, "w", encoding="utf-8") as fd:
        # Write table of contents
        fd.write("# Table of Contents\n")
        fd.write("- Categories\n")
        for category in categories.keys():
            fd.write(
                f'  - [{category}](#{category.replace(" ", "-").lower()})\n'
            )
        fd.write("\n\n")

        # Write mutations
        for category, mutations in categories.items():
            fd.write(f"## {category}\n")
            for mut in mutations:
                fd.write(f"### {mut.name}\n")
                fd.write(f"src: `{mut.mod}`\n")
                fd.write(f"id: `{mut.id_}` \n")
                fd.write(f'categories: `{"`, `".join(mut.categories)}`\n')
                fd.write(f"Description: {mut.description}\n")
            fd.write("\n\n")


def extract_mutations(file: Path, mod: str = "dda") -> list[Mutation]:
    """Extract mutations from :file:."""

    with open(file, "rb") as fd:
        jfile = orjson.loads(fd.read())

    if not isinstance(jfile, list):
        return []

    if len(jfile) == 0:
        return []

    mutations = []

    for jo in jfile:
        if not isinstance(jo, dict):
            continue

        if jo.get("type") != "mutation":
            continue

        mutation = Mutation(
            id_=jo.get("id"),
            name="",
            categories=jo.get(
                "category", jo.get("extend", {}).get("category", ["NONE"])
            ),
            description=jo.get("description"),
            mod=mod,
        )

        name = jo.get("name")
        if isinstance(name, dict):
            name = name.get("str")
        elif name is None and jo.get("copy-from"):
            name = "SHOULD_BE_COPIED_FROM"
            mutation.copied = True
        elif not isinstance(name, str):
            click.echo(
                f"Invalid mutation name: {name} in file: {file}", err=True
            )
            sys.exit()

        mutation.name = name
        mutations.append(mutation)

    return mutations


@dataclass(init=True, repr=True)
class Mod:
    id_: str
    mutations: list[Mutation]
    dependencies: list[str]


@click.command()
@click.argument(
    "paths",
    type=click.Path(exists=True, resolve_path=True, path_type=Path),
    nargs=-1,
)
@click.option(
    "-o",
    "--outfile",
    type=click.Path(resolve_path=True, path_type=Path, dir_okay=False),
    default="mutations.md",
    help="""Specify the filepath to write the documentation for DDA to.
    If mods are included, their documentation will be written to the same
    directory.""",
    show_default=True,
)
@click.option(
    "--include-mods / --exclude-mods",
    default=False,
    help="""Include (or exclude) mutations defined in mods.
    A mod is a file which is contained in a subdirectory of a
    directory including a modinfo.json file.""",
    show_default=True,
)
def cli(paths, outfile, include_mods) -> None:
    """Search PATHS for JSON files and generate documentation for mutations."""
    # The entrypoint to the CLI.

    if not include_mods:
        mutations = []

        for file in find_files(paths, (".json",), include_mods=False):
            mutations.extend(extract_mutations(file))

        write_mutations(mutations, outfile)
        click.echo(f"Mutations written to {outfile}")
    else:
        dda = Mod("dda", [], [])
        mod_stack: list[tuple[Mod, str]] = [(dda, "PLACEHOLDER")]
        mods: dict[str, Mod] = {"dda": dda}

        def move_stack(
            file, mod_stack: list[tuple[Mod, str]]
        ) -> list[tuple[Mod, str]]:
            if len(mod_stack) == 1:
                return mod_stack
            if mod_stack[-1][1] in Path(file).parents:
                return mod_stack
            else:
                return move_stack(file, mod_stack[:-1])

        for file in find_files(paths, (".json",), include_mods=True):
            if os.path.basename(file) != "modinfo.json":
                # Move mods who we have recursed out of off the stack.
                mod_stack = move_stack(file, mod_stack)
                mod_stack[-1][0].mutations.extend(
                    extract_mutations(file, mod_stack[-1][0].id_)
                )

            elif os.path.split(os.path.split(file)[0])[1] == "dda":
                # Skip DDA
                continue
            else:
                # Add a new mod to the stack.
                with open(file, "rb") as fd:
                    jfile = orjson.loads(fd.read())

                # modinfo.json supports two formats:
                # - An array containing at least one object, the first of which
                # contains info about the mod.
                # - A single top-level object.
                if isinstance(jfile, list) and len(jfile) > 0:
                    mod_info = jfile[0]
                else:
                    mod_info = jfile

                if isinstance(mod_info, dict) and all(
                    (mod_info.get("id"), mod_info.get("dependencies"))
                ):
                    new_mod = Mod(mod_info["id"], [], mod_info["dependencies"])
                    mod_stack.append((new_mod, Path(os.path.dirname(file))))
                    mods[new_mod.id_] = new_mod
                else:
                    click.echo(f"{file} is an invalid modinfo.json file.")
                    sys.exit()

        # Resolve mutations which use copy-from.
        for id_, mod in mods.items():
            if not mod.dependencies:
                continue

            for mut in (mut for mut in mod.mutations if mut.copied is True):
                for dep in mod.dependencies:
                    if not mods.get(dep):
                        click.echo(
                            f"Mod {mod.id_} is missing dependency {dep}"
                        )
                        continue

                    for m in mods[dep].mutations:
                        if m.id_ == mut.id_:
                            mut.description = m.description
                            mut.name = m.name
                            mut.mod = m.mod

                            if mut.categories != ["NONE"] and m.categories != [
                                "NONE"
                            ]:
                                mut.categories += m.categories
                            elif m.categories != ["NONE"]:
                                mut.categories = m.categories

        for _, mod in mods.items():
            try:
                if not mod.mutations:
                    continue
            except AttributeError:
                breakpoint()

            if mod.id_ == "dda":
                writepath = outfile
            else:
                writepath = outfile.parent.joinpath(mod.id_ + ".md")

            write_mutations(mod.mutations, writepath)
            click.echo(
                f"Mutations for mod [ {mod.id_} ] written to {writepath}"
            )


if __name__ == "__main__":
    cli()