| 1 |
# xml_emitter.py |
| 2 |
# |
| 3 |
# Copyright 2021 James Westman <[email protected]> |
| 4 |
# |
| 5 |
# This file is free software; you can redistribute it and/or modify it |
| 6 |
# under the terms of the GNU Lesser General Public License as |
| 7 |
# published by the Free Software Foundation; either version 3 of the |
| 8 |
# License, or (at your option) any later version. |
| 9 |
# |
| 10 |
# This file is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
# Lesser General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU Lesser General Public |
| 16 |
# License along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
# |
| 18 |
# SPDX-License-Identifier: LGPL-3.0-or-later |
| 19 |
|
| 20 |
import typing as T |
| 21 |
from xml.sax import saxutils |
| 22 |
|
| 23 |
from blueprintcompiler.gir import GirType |
| 24 |
from blueprintcompiler.language.types import ClassName |
| 25 |
|
| 26 |
|
| 27 |
class XmlEmitter: |
| 28 |
def __init__(self, indent=2, generated_notice=True): |
| 29 |
self.indent = indent |
| 30 |
self.result = '<?xml version="1.0" encoding="UTF-8"?>' |
| 31 |
if generated_notice: |
| 32 |
self.result += ( |
| 33 |
"\n" |
| 34 |
"<!--\n" |
| 35 |
"DO NOT EDIT!\n" |
| 36 |
"This file was @generated by blueprint-compiler. Instead, edit the\n" |
| 37 |
"corresponding .blp file and regenerate this file with blueprint-compiler.\n" |
| 38 |
"-->" |
| 39 |
) |
| 40 |
self._tag_stack = [] |
| 41 |
self._needs_newline = False |
| 42 |
|
| 43 |
def start_tag( |
| 44 |
self, tag, **attrs: T.Union[str, GirType, ClassName, bool, None, float] |
| 45 |
): |
| 46 |
self._indent() |
| 47 |
self.result += f"<{tag}" |
| 48 |
for key, val in attrs.items(): |
| 49 |
if val is not None: |
| 50 |
self.result += f' {key.replace("_", "-")}="{saxutils.escape(self._to_string(val))}"' |
| 51 |
self.result += ">" |
| 52 |
self._tag_stack.append(tag) |
| 53 |
self._needs_newline = False |
| 54 |
|
| 55 |
def put_self_closing(self, tag, **attrs): |
| 56 |
self._indent() |
| 57 |
self.result += f"<{tag}" |
| 58 |
for key, val in attrs.items(): |
| 59 |
if val is not None: |
| 60 |
self.result += f' {key.replace("_", "-")}="{saxutils.escape(self._to_string(val))}"' |
| 61 |
self.result += "/>" |
| 62 |
self._needs_newline = True |
| 63 |
|
| 64 |
def end_tag(self): |
| 65 |
tag = self._tag_stack.pop() |
| 66 |
if self._needs_newline: |
| 67 |
self._indent() |
| 68 |
self.result += f"</{tag}>" |
| 69 |
self._needs_newline = True |
| 70 |
|
| 71 |
def put_text(self, text: T.Union[str, int, float]): |
| 72 |
self.result += saxutils.escape(str(text)) |
| 73 |
self._needs_newline = False |
| 74 |
|
| 75 |
def put_cdata(self, text: str): |
| 76 |
text = text.replace("]]>", "]]]]><![CDATA[>") |
| 77 |
self.result += f"<![CDATA[{text}]]>" |
| 78 |
self._needs_newline = False |
| 79 |
|
| 80 |
def _indent(self): |
| 81 |
if self.indent is not None: |
| 82 |
self.result += "\n" + " " * (self.indent * len(self._tag_stack)) |
| 83 |
|
| 84 |
def _to_string(self, val): |
| 85 |
if isinstance(val, GirType): |
| 86 |
return val.glib_type_name |
| 87 |
elif isinstance(val, ClassName): |
| 88 |
return val.glib_type_name |
| 89 |
else: |
| 90 |
return str(val) |