xml: Convert GirType to type name automatically

Makes some of the emit_xml code a bit simpler.
This commit is contained in:
James Westman 2022-04-30 13:54:32 -05:00
parent 4e1398f238
commit 0da0c9399f
No known key found for this signature in database
GPG key ID: CE2DBA0ADB654EA6
3 changed files with 15 additions and 10 deletions

View file

@ -109,7 +109,7 @@ class Object(AstNode):
from .gtkbuilder_child import Child from .gtkbuilder_child import Child
xml.start_tag("object", **{ xml.start_tag("object", **{
"class": self.gir_class.glib_type_name if self.gir_class else self.tokens["class_name"], "class": self.gir_class or self.tokens["class_name"],
"id": self.tokens["id"], "id": self.tokens["id"],
}) })
for child in self.children: for child in self.children:

View file

@ -38,13 +38,11 @@ class Template(Object):
pass # does not apply to templates pass # does not apply to templates
def emit_xml(self, xml: XmlEmitter): def emit_xml(self, xml: XmlEmitter):
if self.gir_class: xml.start_tag(
parent = self.gir_class.glib_type_name "template",
elif self.tokens["class_name"]: **{"class": self.tokens["name"]},
parent = self.tokens["class_name"] parent=self.gir_class or self.tokens["class_name"]
else: )
parent = None
xml.start_tag("template", **{"class": self.tokens["name"]}, parent=parent)
for child in self.children: for child in self.children:
child.emit_xml(xml) child.emit_xml(xml)
xml.end_tag() xml.end_tag()

View file

@ -19,6 +19,7 @@
from xml.sax import saxutils from xml.sax import saxutils
from . import gir
class XmlEmitter: class XmlEmitter:
@ -33,7 +34,7 @@ class XmlEmitter:
self.result += f"<{tag}" self.result += f"<{tag}"
for key, val in attrs.items(): for key, val in attrs.items():
if val is not None: if val is not None:
self.result += f' {key.replace("_", "-")}="{saxutils.escape(str(val))}"' self.result += f' {key.replace("_", "-")}="{saxutils.escape(self._to_string(val))}"'
self.result += ">" self.result += ">"
self._tag_stack.append(tag) self._tag_stack.append(tag)
self._needs_newline = False self._needs_newline = False
@ -43,7 +44,7 @@ class XmlEmitter:
self.result += f"<{tag}" self.result += f"<{tag}"
for key, val in attrs.items(): for key, val in attrs.items():
if val is not None: if val is not None:
self.result += f' {key}="{saxutils.escape(str(val))}"' self.result += f' {key.replace("_", "-")}="{saxutils.escape(self._to_string(val))}"'
self.result += "/>" self.result += "/>"
self._needs_newline = True self._needs_newline = True
@ -61,3 +62,9 @@ class XmlEmitter:
def _indent(self): def _indent(self):
if self.indent is not None: if self.indent is not None:
self.result += "\n" + " " * (self.indent * len(self._tag_stack)) self.result += "\n" + " " * (self.indent * len(self._tag_stack))
def _to_string(self, val):
if isinstance(val, gir.GirType):
return val.glib_type_name
else:
return str(val)