config_unprettify_json
This commit is contained in:
parent
e4986c7784
commit
bc67268d82
@ -1301,6 +1301,7 @@ class ConfigEditor(QDialog):
|
||||
|
||||
def accept(self):
|
||||
txt = self.form.editor.toPlainText()
|
||||
txt = gui_hooks.addon_config_editor_will_save_json(txt)
|
||||
try:
|
||||
new_conf = json.loads(txt)
|
||||
except Exception as e:
|
||||
|
@ -106,6 +106,35 @@ class _AddonConfigEditorWillDisplayJsonFilter:
|
||||
addon_config_editor_will_display_json = _AddonConfigEditorWillDisplayJsonFilter()
|
||||
|
||||
|
||||
class _AddonConfigEditorWillSaveJsonFilter:
|
||||
"""Allows changing the text of the json configuration that was
|
||||
received from the user before actually reading it. For
|
||||
example, you can replace new line in strings by some "\n"."""
|
||||
|
||||
_hooks: List[Callable[[str], str]] = []
|
||||
|
||||
def append(self, cb: Callable[[str], str]) -> None:
|
||||
"""(text: str)"""
|
||||
self._hooks.append(cb)
|
||||
|
||||
def remove(self, cb: Callable[[str], str]) -> None:
|
||||
if cb in self._hooks:
|
||||
self._hooks.remove(cb)
|
||||
|
||||
def __call__(self, text: str) -> str:
|
||||
for filter in self._hooks:
|
||||
try:
|
||||
text = filter(text)
|
||||
except:
|
||||
# if the hook fails, remove it
|
||||
self._hooks.remove(filter)
|
||||
raise
|
||||
return text
|
||||
|
||||
|
||||
addon_config_editor_will_save_json = _AddonConfigEditorWillSaveJsonFilter()
|
||||
|
||||
|
||||
class _AvPlayerDidBeginPlayingHook:
|
||||
_hooks: List[Callable[["aqt.sound.Player", "anki.sound.AVTag"], None]] = []
|
||||
|
||||
|
@ -464,6 +464,14 @@ def emptyNewCard():
|
||||
while reading the file and let the user uses real new line in
|
||||
string instead of its encoding.""",
|
||||
),
|
||||
Hook(
|
||||
name="addon_config_editor_will_save_json",
|
||||
args=["text: str"],
|
||||
return_type="str",
|
||||
doc="""Allows changing the text of the json configuration that was
|
||||
received from the user before actually reading it. For
|
||||
example, you can replace new line in strings by some "\\\\n".""",
|
||||
),
|
||||
# Other
|
||||
###################
|
||||
Hook(
|
||||
|
Loading…
Reference in New Issue
Block a user