1import traceback
2import weakref
3
4import fsui
5from fsbc.util import unused
6from launcher.launcher_config import LauncherConfig
7from launcher.launcher_settings import LauncherSettings
8from launcher.ui.config.expand import expand_config, AbstractExpandFunctions
9from launcher.ui.config.model import ImplicitConfig, normalize
10
11
12class ImplicitConfigHandler:
13    def __init__(self, parent):
14        self.parent = weakref.ref(parent)
15        LauncherConfig.add_listener(self)
16        LauncherSettings.add_listener(self)
17        self.dirty = True
18        self.do_update()
19        parent.destroyed.connect(self.on_parent_destroyed)
20
21    def on_parent_destroyed(self):
22        print("ImplicitConfigHandler.on_parent_destroyed")
23        LauncherConfig.remove_listener(self)
24        LauncherSettings.remove_listener(self)
25
26    def update(self):
27        self.dirty = True
28        fsui.call_after(self.do_update)
29
30    def do_update(self):
31        if not self.dirty:
32            return
33        self.dirty = False
34        # print("ImplicitConfigHandler.do_update")
35        implicit = ImplicitConfig(ConfigProxy(), SettingsProxy())
36        # failed = False
37        try:
38            expand_config(implicit, ExpandFunctions())
39        except Exception:
40            traceback.print_exc()
41            print("expand_config failed")
42            # failed = True
43        implicit_config = {
44            key: ""
45            for key in LauncherConfig.keys()
46            if key.startswith("__implicit_")
47        }
48        for key, value in implicit.items():
49            implicit_config["__implicit_" + key] = value
50        LauncherConfig.set_multiple(list(implicit_config.items()))
51
52        if self.parent().config_browser:
53            self.parent().config_browser.update_from_implicit(implicit)
54
55    def on_config(self, key, value):
56        if key.startswith("__implicit_"):
57            # The implicit key/values are already set by us.
58            return
59        unused(value)
60        self.update()
61
62    def on_setting(self, key, value):
63        unused(key)
64        unused(value)
65        self.update()
66
67
68class SettingsProxy:
69    @staticmethod
70    def get(key):
71        return LauncherSettings.get(key)
72
73    def __getitem__(self, item):
74        return self.get(item)
75
76    def __setitem__(self, key, value):
77        LauncherSettings.set(key, value)
78
79
80class ConfigProxy:
81    @staticmethod
82    def get(key):
83        return LauncherConfig.get(key)
84
85    def __getitem__(self, item):
86        return self.get(item)
87
88    def __setitem__(self, key, value):
89        LauncherConfig.set(key, value)
90
91
92class ExpandFunctions(AbstractExpandFunctions):
93    @staticmethod
94    def matches(a, b):
95        a = normalize(a)
96        if isinstance(b, list):
97            for b_item in b:
98                if a == normalize(b_item):
99                    return True
100            return False
101        return a == normalize(b)
102
103    @staticmethod
104    def fail(message):
105        pass
106
107    @staticmethod
108    def warning(message):
109        # warnings.append(message)
110        pass
111
112    @staticmethod
113    def lower(s):
114        return s.lower()
115