From 3d5a5521aac5a8d727d736f9d9e8de924b2fd119 Mon Sep 17 00:00:00 2001 From: James Westman Date: Thu, 28 Sep 2023 14:51:16 -0500 Subject: [PATCH] decompiler: Use single quotes --- blueprintcompiler/decompiler.py | 14 +++++++------- blueprintcompiler/language/gtk_a11y.py | 2 +- blueprintcompiler/language/gtk_file_filter.py | 6 +++--- blueprintcompiler/utils.py | 13 ++++++++----- tests/samples/accessibility_dec.blp | 2 +- tests/samples/file_filter.blp | 10 +++++----- tests/samples/layout_dec.blp | 4 ++-- tests/samples/menu_dec.blp | 16 ++++++++-------- tests/samples/responses.blp | 6 +++--- tests/samples/scale_marks.blp | 2 +- tests/samples/strings_dec.blp | 5 +++++ tests/samples/template.blp | 2 +- tests/samples/translated.blp | 4 ++-- tests/samples/unchecked_class_dec.blp | 2 +- tests/test_samples.py | 2 +- 15 files changed, 49 insertions(+), 41 deletions(-) create mode 100644 tests/samples/strings_dec.blp diff --git a/blueprintcompiler/decompiler.py b/blueprintcompiler/decompiler.py index 550f60b..2663a09 100644 --- a/blueprintcompiler/decompiler.py +++ b/blueprintcompiler/decompiler.py @@ -138,7 +138,7 @@ class DecompileCtx: return value.replace("-", "_") if type is None: - self.print(f'{name}: "{escape_quote(value)}";') + self.print(f"{name}: {escape_quote(value)};") elif type.assignable_to(FloatType()): self.print(f"{name}: {value};") elif type.assignable_to(BoolType()): @@ -157,7 +157,7 @@ class DecompileCtx: self.gir.namespaces["Gtk"].lookup_type("Gtk.ShortcutTrigger") ) ): - self.print(f'{name}: "{escape_quote(value)}";') + self.print(f"{name}: {escape_quote(value)};") elif value == self.template_class: self.print(f"{name}: template;") elif type.assignable_to( @@ -170,7 +170,7 @@ class DecompileCtx: elif isinstance(type, Enumeration): self.print(f"{name}: {get_enum_name(value)};") else: - self.print(f'{name}: "{escape_quote(value)}";') + self.print(f"{name}: {escape_quote(value)};") def _decompile_element( @@ -280,11 +280,11 @@ def decompile_translatable( comments = f"/* Translators: {comments} */" if context is not None: - return comments, f'C_("{escape_quote(context)}", "{escape_quote(string)}")' + return comments, f"C_({escape_quote(context)}, {escape_quote(string)})" else: - return comments, f'_("{escape_quote(string)}")' + return comments, f"_({escape_quote(string)})" else: - return comments, f'"{escape_quote(string)}"' + return comments, f"{escape_quote(string)}" @decompiler("property", cdata=True) @@ -325,7 +325,7 @@ def decompile_property( ctx.print(comments) ctx.print(f"{name}: {translatable};") elif gir is None or gir.properties.get(name) is None: - ctx.print(f'{name}: "{escape_quote(cdata)}";') + ctx.print(f"{name}: {escape_quote(cdata)};") else: ctx.print_attribute(name, cdata, gir.properties.get(name).type) return gir diff --git a/blueprintcompiler/language/gtk_a11y.py b/blueprintcompiler/language/gtk_a11y.py index 2ad8097..a4c3415 100644 --- a/blueprintcompiler/language/gtk_a11y.py +++ b/blueprintcompiler/language/gtk_a11y.py @@ -231,7 +231,7 @@ def decompile_relation(ctx, gir, name, cdata): @decompiler("state", cdata=True) def decompile_state(ctx, gir, name, cdata, translatable="false"): if decompile.truthy(translatable): - ctx.print(f'{name}: _("{escape_quote(cdata)}");') + ctx.print(f"{name}: _({escape_quote(cdata)});") else: ctx.print_attribute(name, cdata, get_types(ctx.gir).get(name)) diff --git a/blueprintcompiler/language/gtk_file_filter.py b/blueprintcompiler/language/gtk_file_filter.py index 8dcbcb8..482d6e1 100644 --- a/blueprintcompiler/language/gtk_file_filter.py +++ b/blueprintcompiler/language/gtk_file_filter.py @@ -119,7 +119,7 @@ def decompile_mime_types(ctx, gir): @decompiler("mime-type", cdata=True) def decompile_mime_type(ctx, gir, cdata): - ctx.print(f'"{cdata}",') + ctx.print(f"{escape_quote(cdata)},") @decompiler("patterns") @@ -129,7 +129,7 @@ def decompile_patterns(ctx, gir): @decompiler("pattern", cdata=True) def decompile_pattern(ctx, gir, cdata): - ctx.print(f'"{cdata}",') + ctx.print(f"{escape_quote(cdata)},") @decompiler("suffixes") @@ -139,4 +139,4 @@ def decompile_suffixes(ctx, gir): @decompiler("suffix", cdata=True) def decompile_suffix(ctx, gir, cdata): - ctx.print(f'"{cdata}",') + ctx.print(f"{escape_quote(cdata)},") diff --git a/blueprintcompiler/utils.py b/blueprintcompiler/utils.py index 6d03a9e..25803d7 100644 --- a/blueprintcompiler/utils.py +++ b/blueprintcompiler/utils.py @@ -109,11 +109,14 @@ class UnescapeError(Exception): def escape_quote(string: str) -> str: return ( - string.replace("\\", "\\\\") - .replace("'", "\\'") - .replace('"', '\\"') - .replace("\n", "\\n") - .replace("\t", "\\t") + "'" + + ( + string.replace("\\", "\\\\") + .replace("'", "\\'") + .replace("\n", "\\n") + .replace("\t", "\\t") + ) + + "'" ) diff --git a/tests/samples/accessibility_dec.blp b/tests/samples/accessibility_dec.blp index a603e25..eedb6cd 100644 --- a/tests/samples/accessibility_dec.blp +++ b/tests/samples/accessibility_dec.blp @@ -2,7 +2,7 @@ using Gtk 4.0; Box { accessibility { - label: _("Hello, world!"); + label: _('Hello, world!'); labelled-by: my_label; checked: true; } diff --git a/tests/samples/file_filter.blp b/tests/samples/file_filter.blp index 3a52c3f..e9866d9 100644 --- a/tests/samples/file_filter.blp +++ b/tests/samples/file_filter.blp @@ -1,18 +1,18 @@ using Gtk 4.0; FileFilter { - name: "File Filter Name"; + name: 'File Filter Name'; mime-types [ - "text/plain", - "image/ *", + 'text/plain', + 'image/ *', ] patterns [ - "*.txt", + '*.txt', ] suffixes [ - "png", + 'png', ] } diff --git a/tests/samples/layout_dec.blp b/tests/samples/layout_dec.blp index b0e66e0..3754fe1 100644 --- a/tests/samples/layout_dec.blp +++ b/tests/samples/layout_dec.blp @@ -3,8 +3,8 @@ using Gtk 4.0; Grid { Label { layout { - column: "0"; - row: "1"; + column: '0'; + row: '1'; } } } diff --git a/tests/samples/menu_dec.blp b/tests/samples/menu_dec.blp index 14b4df3..44c244f 100644 --- a/tests/samples/menu_dec.blp +++ b/tests/samples/menu_dec.blp @@ -3,26 +3,26 @@ using Gtk 4.0; menu my-menu { submenu { section { - label: "test section"; + label: 'test section'; } item { - label: C_("context", "test translated item"); + label: C_('context', 'test translated item'); } item { - label: "test item shorthand 1"; + label: 'test item shorthand 1'; } item { - label: "test item shorthand 2"; - action: "app.test-action"; + label: 'test item shorthand 2'; + action: 'app.test-action'; } item { - label: "test item shorthand 3"; - action: "app.test-action"; - icon: "test-symbolic"; + label: 'test item shorthand 3'; + action: 'app.test-action'; + icon: 'test-symbolic'; } } } diff --git a/tests/samples/responses.blp b/tests/samples/responses.blp index d7032a7..be8c03b 100644 --- a/tests/samples/responses.blp +++ b/tests/samples/responses.blp @@ -3,8 +3,8 @@ using Adw 1; Adw.MessageDialog { responses [ - cancel: _("Cancel"), - discard: _("Discard") destructive, - save: "Save" suggested disabled, + cancel: _('Cancel'), + discard: _('Discard') destructive, + save: 'Save' suggested disabled, ] } \ No newline at end of file diff --git a/tests/samples/scale_marks.blp b/tests/samples/scale_marks.blp index a766cfa..f75930c 100644 --- a/tests/samples/scale_marks.blp +++ b/tests/samples/scale_marks.blp @@ -3,7 +3,7 @@ using Gtk 4.0; Scale { marks [ mark (-1, bottom), - mark (0, top, _("Hello, world!")), + mark (0, top, _('Hello, world!')), mark (2), ] } diff --git a/tests/samples/strings_dec.blp b/tests/samples/strings_dec.blp new file mode 100644 index 0000000..576ddb4 --- /dev/null +++ b/tests/samples/strings_dec.blp @@ -0,0 +1,5 @@ +using Gtk 4.0; + +Label { + label: '\\\\\'Test 1 2 3\n & 4 "5\' 6 \t'; +} diff --git a/tests/samples/template.blp b/tests/samples/template.blp index fa2f041..fb019d3 100644 --- a/tests/samples/template.blp +++ b/tests/samples/template.blp @@ -1,7 +1,7 @@ using Gtk 4.0; template $TestTemplate : ApplicationWindow { - test-property: "Hello, world"; + test-property: 'Hello, world'; test-signal => $on_test_signal(); } diff --git a/tests/samples/translated.blp b/tests/samples/translated.blp index c7d4cd7..8c837d7 100644 --- a/tests/samples/translated.blp +++ b/tests/samples/translated.blp @@ -1,9 +1,9 @@ using Gtk 4.0; Label { - label: _("Hello, world!"); + label: _('Hello, world!'); } Label { - label: C_("translation context", "Hello"); + label: C_('translation context', 'Hello'); } diff --git a/tests/samples/unchecked_class_dec.blp b/tests/samples/unchecked_class_dec.blp index dbbfe14..d9df875 100644 --- a/tests/samples/unchecked_class_dec.blp +++ b/tests/samples/unchecked_class_dec.blp @@ -2,6 +2,6 @@ using Gtk 4.0; $MyComponent component { $MyComponent2 { - flags-value: "a|b"; + flags-value: 'a|b'; } } diff --git a/tests/test_samples.py b/tests/test_samples.py index 226e762..a91a594 100644 --- a/tests/test_samples.py +++ b/tests/test_samples.py @@ -229,7 +229,7 @@ class TestSamples(unittest.TestCase): self.assert_decompile("responses") self.assert_decompile("scale_marks") self.assert_decompile("signal") - self.assert_decompile("strings") + self.assert_decompile("strings_dec") self.assert_decompile("style_dec") self.assert_decompile("template") self.assert_decompile("translated")