aboutsummaryrefslogtreecommitdiff
path: root/libs/basic_games/games/baldursgate3/plugins/convert_jsons_to_yaml_plugin.py
blob: fde343156b568225e240132b31d0739f6ca9f08c (plain)
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
import json
import os
from pathlib import Path

from PyQt6.QtCore import qInfo, qWarning
from PyQt6.QtWidgets import QApplication

from .bg3_tool_plugin import BG3ToolPlugin
from .icons import exchange


class BG3ToolConvertJsonsToYaml(BG3ToolPlugin):
    icon_bytes = exchange
    sub_name = "Convert JSONS to YAML"
    desc = "Convert all jsons in active mods to yaml immediately."

    def display(self):
        from ...game_baldursgate3 import BG3Game

        game_plugin = self._organizer.managedGame()
        if not isinstance(game_plugin, BG3Game):
            return
        utils = game_plugin.utils
        qInfo("converting all json files to yaml")
        active_mods = utils.active_mods()
        progress = utils.create_progress_window(
            "Converting all json files to yaml", len(active_mods) + 1
        )
        for mod in active_mods:
            _convert_jsons_in_dir_to_yaml(Path(mod.absolutePath()))
            progress.setValue(progress.value() + 1)
            QApplication.processEvents()
            if progress.wasCanceled():
                qWarning("conversion canceled by user")
                return
        _convert_jsons_in_dir_to_yaml(utils.overwrite_path)
        progress.setValue(len(active_mods) + 1)
        QApplication.processEvents()
        progress.close()


def _convert_jsons_in_dir_to_yaml(path: Path):
    for file in list(path.rglob("*.json")):
        converted_path = file.parent / file.name.replace(".json", ".yaml")
        try:
            if not converted_path.exists() or os.path.getmtime(file) > os.path.getmtime(
                converted_path
            ):
                import yaml

                with open(file, "r") as json_file:
                    with open(converted_path, "w") as yaml_file:
                        yaml.dump(
                            json.load(json_file), yaml_file, indent=2, sort_keys=False
                        )
                qInfo(f"Converted {file} to YAML")
        except OSError as e:
            qWarning(f"Error accessing file {converted_path}: {e}")