From 3c1941a17e550debb6c220c5c1a4b3ce05ae2d44 Mon Sep 17 00:00:00 2001 From: James Westman Date: Sat, 29 Apr 2023 21:48:02 -0500 Subject: [PATCH] Simplify Translated Remove the TranslatedWithContext and TranslatedWithoutContext rules and just use Translated. --- blueprintcompiler/language/__init__.py | 2 - blueprintcompiler/language/values.py | 51 +++++++++-------------- blueprintcompiler/outputs/xml/__init__.py | 12 ++---- 3 files changed, 22 insertions(+), 43 deletions(-) diff --git a/blueprintcompiler/language/__init__.py b/blueprintcompiler/language/__init__.py index 571f09b..c48ae35 100644 --- a/blueprintcompiler/language/__init__.py +++ b/blueprintcompiler/language/__init__.py @@ -39,8 +39,6 @@ from .values import ( QuotedLiteral, StringValue, Translated, - TranslatedWithContext, - TranslatedWithoutContext, TypeLiteral, Value, ) diff --git a/blueprintcompiler/language/values.py b/blueprintcompiler/language/values.py index b9eb877..f0cc068 100644 --- a/blueprintcompiler/language/values.py +++ b/blueprintcompiler/language/values.py @@ -27,40 +27,27 @@ from .gobject_object import Object from .contexts import ScopeCtx, ValueTypeCtx -class TranslatedWithoutContext(AstNode): - grammar = ["_", "(", UseQuoted("string"), Optional(","), ")"] - - @property - def string(self) -> str: - return self.tokens["string"] - - -class TranslatedWithContext(AstNode): - grammar = [ - "C_", - "(", - UseQuoted("context"), - ",", - UseQuoted("string"), - Optional(","), - ")", - ] - - @property - def string(self) -> str: - return self.tokens["string"] - - @property - def translate_context(self) -> str: - return self.tokens["context"] - - class Translated(AstNode): - grammar = AnyOf(TranslatedWithoutContext, TranslatedWithContext) + grammar = AnyOf( + ["_", "(", UseQuoted("string"), Optional(","), ")"], + [ + "C_", + "(", + UseQuoted("context"), + ",", + UseQuoted("string"), + Optional(","), + ")", + ], + ) @property - def child(self) -> T.Union[TranslatedWithContext, TranslatedWithoutContext]: - return self.children[0] + def string(self) -> str: + return self.tokens["string"] + + @property + def translate_context(self) -> T.Optional[str]: + return self.tokens["context"] @validate() def validate_for_type(self) -> None: @@ -382,7 +369,7 @@ class StringValue(AstNode): @property def string(self) -> str: if isinstance(self.child, Translated): - return self.child.child.string + return self.child.string else: return self.child.value diff --git a/blueprintcompiler/outputs/xml/__init__.py b/blueprintcompiler/outputs/xml/__init__.py index 8332cee..ed13610 100644 --- a/blueprintcompiler/outputs/xml/__init__.py +++ b/blueprintcompiler/outputs/xml/__init__.py @@ -103,7 +103,7 @@ class XmlOutput(OutputFormat): xml.start_tag( "property", **props, **self._translated_string_attrs(child) ) - xml.put_text(child.child.string) + xml.put_text(child.string) xml.end_tag() else: xml.start_tag("property", **props) @@ -149,12 +149,7 @@ class XmlOutput(OutputFormat): if isinstance(translated, QuotedLiteral): return {} else: - return { - "translatable": "true", - "context": translated.child.translate_context - if isinstance(translated.child, TranslatedWithContext) - else None, - } + return {"translatable": "true", "context": translated.translate_context} def _emit_signal(self, signal: Signal, xml: XmlEmitter): name = signal.name @@ -267,7 +262,7 @@ class XmlOutput(OutputFormat): if isinstance(value.child, Translated): xml.start_tag(tag, **attrs, **self._translated_string_attrs(value.child)) - xml.put_text(value.child.child.string) + xml.put_text(value.child.string) xml.end_tag() elif isinstance(value.child, QuotedLiteral): xml.start_tag(tag, **attrs) @@ -308,7 +303,6 @@ class XmlOutput(OutputFormat): elif isinstance(extension, Responses): xml.start_tag("responses") for response in extension.responses: - # todo: translated xml.start_tag( "response", id=response.id,