decompiler: Use single quotes

This commit is contained in:
James Westman 2023-09-28 14:51:16 -05:00
parent ea92838cf3
commit 3d5a5521aa
15 changed files with 49 additions and 41 deletions

View file

@ -138,7 +138,7 @@ class DecompileCtx:
return value.replace("-", "_") return value.replace("-", "_")
if type is None: if type is None:
self.print(f'{name}: "{escape_quote(value)}";') self.print(f"{name}: {escape_quote(value)};")
elif type.assignable_to(FloatType()): elif type.assignable_to(FloatType()):
self.print(f"{name}: {value};") self.print(f"{name}: {value};")
elif type.assignable_to(BoolType()): elif type.assignable_to(BoolType()):
@ -157,7 +157,7 @@ class DecompileCtx:
self.gir.namespaces["Gtk"].lookup_type("Gtk.ShortcutTrigger") 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: elif value == self.template_class:
self.print(f"{name}: template;") self.print(f"{name}: template;")
elif type.assignable_to( elif type.assignable_to(
@ -170,7 +170,7 @@ class DecompileCtx:
elif isinstance(type, Enumeration): elif isinstance(type, Enumeration):
self.print(f"{name}: {get_enum_name(value)};") self.print(f"{name}: {get_enum_name(value)};")
else: else:
self.print(f'{name}: "{escape_quote(value)}";') self.print(f"{name}: {escape_quote(value)};")
def _decompile_element( def _decompile_element(
@ -280,11 +280,11 @@ def decompile_translatable(
comments = f"/* Translators: {comments} */" comments = f"/* Translators: {comments} */"
if context is not None: 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: else:
return comments, f'_("{escape_quote(string)}")' return comments, f"_({escape_quote(string)})"
else: else:
return comments, f'"{escape_quote(string)}"' return comments, f"{escape_quote(string)}"
@decompiler("property", cdata=True) @decompiler("property", cdata=True)
@ -325,7 +325,7 @@ def decompile_property(
ctx.print(comments) ctx.print(comments)
ctx.print(f"{name}: {translatable};") ctx.print(f"{name}: {translatable};")
elif gir is None or gir.properties.get(name) is None: 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: else:
ctx.print_attribute(name, cdata, gir.properties.get(name).type) ctx.print_attribute(name, cdata, gir.properties.get(name).type)
return gir return gir

View file

@ -231,7 +231,7 @@ def decompile_relation(ctx, gir, name, cdata):
@decompiler("state", cdata=True) @decompiler("state", cdata=True)
def decompile_state(ctx, gir, name, cdata, translatable="false"): def decompile_state(ctx, gir, name, cdata, translatable="false"):
if decompile.truthy(translatable): if decompile.truthy(translatable):
ctx.print(f'{name}: _("{escape_quote(cdata)}");') ctx.print(f"{name}: _({escape_quote(cdata)});")
else: else:
ctx.print_attribute(name, cdata, get_types(ctx.gir).get(name)) ctx.print_attribute(name, cdata, get_types(ctx.gir).get(name))

View file

@ -119,7 +119,7 @@ def decompile_mime_types(ctx, gir):
@decompiler("mime-type", cdata=True) @decompiler("mime-type", cdata=True)
def decompile_mime_type(ctx, gir, cdata): def decompile_mime_type(ctx, gir, cdata):
ctx.print(f'"{cdata}",') ctx.print(f"{escape_quote(cdata)},")
@decompiler("patterns") @decompiler("patterns")
@ -129,7 +129,7 @@ def decompile_patterns(ctx, gir):
@decompiler("pattern", cdata=True) @decompiler("pattern", cdata=True)
def decompile_pattern(ctx, gir, cdata): def decompile_pattern(ctx, gir, cdata):
ctx.print(f'"{cdata}",') ctx.print(f"{escape_quote(cdata)},")
@decompiler("suffixes") @decompiler("suffixes")
@ -139,4 +139,4 @@ def decompile_suffixes(ctx, gir):
@decompiler("suffix", cdata=True) @decompiler("suffix", cdata=True)
def decompile_suffix(ctx, gir, cdata): def decompile_suffix(ctx, gir, cdata):
ctx.print(f'"{cdata}",') ctx.print(f"{escape_quote(cdata)},")

View file

@ -109,12 +109,15 @@ class UnescapeError(Exception):
def escape_quote(string: str) -> str: def escape_quote(string: str) -> str:
return ( return (
"'"
+ (
string.replace("\\", "\\\\") string.replace("\\", "\\\\")
.replace("'", "\\'") .replace("'", "\\'")
.replace('"', '\\"')
.replace("\n", "\\n") .replace("\n", "\\n")
.replace("\t", "\\t") .replace("\t", "\\t")
) )
+ "'"
)
def unescape_quote(string: str) -> str: def unescape_quote(string: str) -> str:

View file

@ -2,7 +2,7 @@ using Gtk 4.0;
Box { Box {
accessibility { accessibility {
label: _("Hello, world!"); label: _('Hello, world!');
labelled-by: my_label; labelled-by: my_label;
checked: true; checked: true;
} }

View file

@ -1,18 +1,18 @@
using Gtk 4.0; using Gtk 4.0;
FileFilter { FileFilter {
name: "File Filter Name"; name: 'File Filter Name';
mime-types [ mime-types [
"text/plain", 'text/plain',
"image/ *", 'image/ *',
] ]
patterns [ patterns [
"*.txt", '*.txt',
] ]
suffixes [ suffixes [
"png", 'png',
] ]
} }

View file

@ -3,8 +3,8 @@ using Gtk 4.0;
Grid { Grid {
Label { Label {
layout { layout {
column: "0"; column: '0';
row: "1"; row: '1';
} }
} }
} }

View file

@ -3,26 +3,26 @@ using Gtk 4.0;
menu my-menu { menu my-menu {
submenu { submenu {
section { section {
label: "test section"; label: 'test section';
} }
item { item {
label: C_("context", "test translated item"); label: C_('context', 'test translated item');
} }
item { item {
label: "test item shorthand 1"; label: 'test item shorthand 1';
} }
item { item {
label: "test item shorthand 2"; label: 'test item shorthand 2';
action: "app.test-action"; action: 'app.test-action';
} }
item { item {
label: "test item shorthand 3"; label: 'test item shorthand 3';
action: "app.test-action"; action: 'app.test-action';
icon: "test-symbolic"; icon: 'test-symbolic';
} }
} }
} }

View file

@ -3,8 +3,8 @@ using Adw 1;
Adw.MessageDialog { Adw.MessageDialog {
responses [ responses [
cancel: _("Cancel"), cancel: _('Cancel'),
discard: _("Discard") destructive, discard: _('Discard') destructive,
save: "Save" suggested disabled, save: 'Save' suggested disabled,
] ]
} }

View file

@ -3,7 +3,7 @@ using Gtk 4.0;
Scale { Scale {
marks [ marks [
mark (-1, bottom), mark (-1, bottom),
mark (0, top, _("Hello, world!")), mark (0, top, _('Hello, world!')),
mark (2), mark (2),
] ]
} }

View file

@ -0,0 +1,5 @@
using Gtk 4.0;
Label {
label: '\\\\\'Test 1 2 3\n & 4 "5\' 6 \t';
}

View file

@ -1,7 +1,7 @@
using Gtk 4.0; using Gtk 4.0;
template $TestTemplate : ApplicationWindow { template $TestTemplate : ApplicationWindow {
test-property: "Hello, world"; test-property: 'Hello, world';
test-signal => $on_test_signal(); test-signal => $on_test_signal();
} }

View file

@ -1,9 +1,9 @@
using Gtk 4.0; using Gtk 4.0;
Label { Label {
label: _("Hello, world!"); label: _('Hello, world!');
} }
Label { Label {
label: C_("translation context", "Hello"); label: C_('translation context', 'Hello');
} }

View file

@ -2,6 +2,6 @@ using Gtk 4.0;
$MyComponent component { $MyComponent component {
$MyComponent2 { $MyComponent2 {
flags-value: "a|b"; flags-value: 'a|b';
} }
} }

View file

@ -229,7 +229,7 @@ class TestSamples(unittest.TestCase):
self.assert_decompile("responses") self.assert_decompile("responses")
self.assert_decompile("scale_marks") self.assert_decompile("scale_marks")
self.assert_decompile("signal") self.assert_decompile("signal")
self.assert_decompile("strings") self.assert_decompile("strings_dec")
self.assert_decompile("style_dec") self.assert_decompile("style_dec")
self.assert_decompile("template") self.assert_decompile("template")
self.assert_decompile("translated") self.assert_decompile("translated")