summaryrefslogtreecommitdiff
path: root/giscanner/xmlwriter.py
diff options
context:
space:
mode:
authorChristoph Reiter <reiter.christoph@gmail.com>2018-07-27 14:58:14 +0200
committerChristoph Reiter <reiter.christoph@gmail.com>2018-07-27 15:03:52 +0200
commit90e98240c854fb8ee491919794d65fa431e78c01 (patch)
treede85ed3679218b655137d7e87674b19349926432 /giscanner/xmlwriter.py
parentdc4c2f9d2db5563ad482a09d04aeeab6972c53ac (diff)
downloadgobject-introspection-90e98240c854fb8ee491919794d65fa431e78c01.tar.gz
xmlwriter: move collect_attributes() back to a Python implementation
This reverts f345916405d94829696985 and related. The commit states that both versions are about the same in performance, but the C version is more code and harder to maintain. It also states that the behaviour re invalid control characters is better with the C version which produces entities. But those make any Python xml parser fail, which given that most of our tooling is Python, doesn't seem better to me, see #135.
Diffstat (limited to 'giscanner/xmlwriter.py')
-rwxr-xr-xgiscanner/xmlwriter.py44
1 files changed, 37 insertions, 7 deletions
diff --git a/giscanner/xmlwriter.py b/giscanner/xmlwriter.py
index f79362a1..a65fc40c 100755
--- a/giscanner/xmlwriter.py
+++ b/giscanner/xmlwriter.py
@@ -28,9 +28,7 @@ import os
import sys
from contextlib import contextmanager
-from xml.sax.saxutils import escape
-
-from .libtoolimporter import LibtoolImporter
+from xml.sax.saxutils import escape, quoteattr
if sys.version_info.major < 3:
from StringIO import StringIO
@@ -39,11 +37,43 @@ else:
unicode = str
-with LibtoolImporter(None, None):
- if 'UNINSTALLED_INTROSPECTION_SRCDIR' in os.environ:
- from _giscanner import collect_attributes
+def _calc_attrs_length(attributes, indent, self_indent):
+ if indent == -1:
+ return -1
+ attr_length = 0
+ for attr, value in attributes:
+ # FIXME: actually, if we have attributes with None as value this
+ # should be considered a bug and raise an error. We are just
+ # ignoring them here while we fix GIRParser to create the right
+ # ast with the correct attributes.
+ if value is None:
+ continue
+ attr_length += 2 + len(attr) + len(quoteattr(value))
+ return attr_length + indent + self_indent
+
+
+def collect_attributes(tag_name, attributes, self_indent, self_indent_char, indent=-1):
+ if not attributes:
+ return ''
+ if _calc_attrs_length(attributes, indent, self_indent) > 79:
+ indent_len = self_indent + len(tag_name) + 1
else:
- from giscanner._giscanner import collect_attributes
+ indent_len = 0
+ first = True
+ attr_value = ''
+ for attr, value in attributes:
+ # FIXME: actually, if we have attributes with None as value this
+ # should be considered a bug and raise an error. We are just
+ # ignoring them here while we fix GIRParser to create the right
+ # ast with the correct attributes.
+ if value is None:
+ continue
+ if indent_len and not first:
+ attr_value += '\n%s' % (self_indent_char * indent_len)
+ attr_value += ' %s=%s' % (attr, quoteattr(value))
+ if first:
+ first = False
+ return attr_value
def build_xml_tag(tag_name, attributes=None, data=None, self_indent=0,