gobject-introspection r846 - in trunk: . giscanner



Author: johan
Date: Thu Oct 30 20:42:30 2008
New Revision: 846
URL: http://svn.gnome.org/viewvc/gobject-introspection?rev=846&view=rev

Log:
2008-10-30  Johan Dahlin  <jdahlin async com br>

    * giscanner/giscannermodule.c (calc_attrs_length),
    (pygi_collect_attributes):
    * giscanner/xmlwriter.py:
    Write a C version of the xml namespace formatter.
    Saves 15% of the time it takes to create a gtk gir.



Modified:
   trunk/ChangeLog
   trunk/giscanner/giscannermodule.c
   trunk/giscanner/xmlwriter.py

Modified: trunk/giscanner/giscannermodule.c
==============================================================================
--- trunk/giscanner/giscannermodule.c	(original)
+++ trunk/giscanner/giscannermodule.c	Thu Oct 30 20:42:30 2008
@@ -592,9 +592,101 @@
 };
 
 
+static int calc_attrs_length(PyObject *attributes, int indent,
+			     int self_indent)
+{
+  int attr_length = 0;
+  int i;
+  
+  if (indent == -1)
+    return -1;
+
+  for (i = 0; i < PyList_Size (attributes); ++i)
+    {
+      PyObject *tuple;
+      char *attr, *value;
+      char *escaped;
+      
+      tuple = PyList_GetItem (attributes, i);
+      if (PyTuple_GetItem(tuple, 1) == Py_None)
+	continue;
+
+      g_assert(PyArg_ParseTuple(tuple, "ss", &attr, &value));
+      
+      escaped = g_markup_escape_text (value, -1);
+      attr_length += 2 + strlen(attr) + strlen(escaped) + 2;
+      g_free(escaped);
+    }
+
+  return attr_length + indent + self_indent;
+}
+
+static PyObject *
+pygi_collect_attributes (PyObject *self,
+			 PyObject *args)
+{
+  char *tag_name;
+  PyObject *attributes;
+  int indent, indent_len, i, j, self_indent;
+  char *indent_char;
+  gboolean first;
+  GString *attr_value;
+  
+  if (!PyArg_ParseTuple(args, "sOisi",
+			&tag_name, &attributes,
+			&self_indent, &indent_char,
+			&indent))
+    return NULL;
+
+  if (attributes == Py_None || !PyList_Size(attributes))
+    return PyString_FromString("");
+
+  if (calc_attrs_length(attributes, indent, self_indent) > 79)
+    indent_len = self_indent + strlen(tag_name) + 1;
+  else
+    indent_len = 0;
+
+  first = TRUE;
+  attr_value = g_string_new ("");
+
+  for (i = 0; i < PyList_Size (attributes); ++i)
+    {
+      PyObject *tuple;
+      char *attr, *value, *escaped;
+      
+      tuple = PyList_GetItem (attributes, i);
+      g_assert(tuple != NULL);
+      g_assert(PyTuple_Size(tuple) == 2);
+      if (PyTuple_GetItem(tuple, 1) == Py_None)
+	continue;
+
+      g_assert(PyArg_ParseTuple(tuple, "ss", &attr, &value));
+
+      if (indent_len && !first)
+	{
+	  g_string_append_c (attr_value, '\n');
+	  for (j = 0; j < indent_len; j++)
+	    g_string_append_c (attr_value, ' ');
+	}
+      g_string_append_c (attr_value, ' ');
+      g_string_append (attr_value, attr);
+      g_string_append_c (attr_value, '=');
+      g_string_append_c (attr_value, '\"');
+      escaped = g_markup_escape_text (value, -1);
+      g_string_append (attr_value, escaped);
+      g_string_append_c (attr_value, '\"');
+      if (first)
+	first = FALSE;
+  }
+
+  return PyString_FromString (g_string_free (attr_value, FALSE));
+}
+
 /* Module */
 
 static const PyMethodDef pyscanner_functions[] = {
+  { "collect_attributes",
+    (PyCFunction) pygi_collect_attributes, METH_VARARGS },
   { NULL, NULL, 0, NULL }
 };
 

Modified: trunk/giscanner/xmlwriter.py
==============================================================================
--- trunk/giscanner/xmlwriter.py	(original)
+++ trunk/giscanner/xmlwriter.py	Thu Oct 30 20:42:30 2008
@@ -18,10 +18,14 @@
 # 02110-1301, USA.
 #
 
+from __future__ import with_statement
+
 from contextlib import contextmanager
 from cStringIO import StringIO
 from xml.sax.saxutils import quoteattr
 
+from .libtoolimporter import LibtoolImporter
+
 
 def _calc_attrs_length(attributes, indent, self_indent):
     if indent == -1:
@@ -38,8 +42,8 @@
     return attr_length + indent + self_indent
 
 
-def _collect_attributes(tag_name, attributes, self_indent,
-                        self_indent_char, indent=-1):
+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:
@@ -63,6 +67,10 @@
     return attr_value
 
 
+with LibtoolImporter:
+    from giscanner._giscanner import collect_attributes
+
+
 class XMLWriter(object):
 
     def __init__(self):
@@ -76,7 +84,7 @@
     # Private
 
     def _open_tag(self, tag_name, attributes=None):
-        attrs = _collect_attributes(
+        attrs = collect_attributes(
             tag_name, attributes, self._indent,
             self._indent_char,
             len(tag_name) + 2)
@@ -101,7 +109,7 @@
             suffix = '>%s</%s>' % (data, tag_name)
         else:
             suffix = '/>'
-        attrs = _collect_attributes(
+        attrs = collect_attributes(
             tag_name, attributes,
             self._indent,
             self._indent_char,



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]