[Telepathy-commits] [telepathy-spec/master] Refactor methods/signals/properties into lists, use one global lookup dict in Spec

Davyd Madeley davyd at madeley.id.au
Mon Mar 23 12:29:20 PDT 2009


---
 doc/templates/index.html     |    2 +-
 doc/templates/interface.html |   12 ++++----
 tools/doc-generator.py       |    2 +-
 tools/specparser.py          |   66 +++++++++++++++++++++---------------------
 4 files changed, 41 insertions(+), 41 deletions(-)

diff --git a/doc/templates/index.html b/doc/templates/index.html
index d58d9bc..e400daa 100644
--- a/doc/templates/index.html
+++ b/doc/templates/index.html
@@ -9,7 +9,7 @@
   <h1>FIXME - get this from the XML</h1>
 
   <ul>
-  #for $interface in $interfaces.values()
+  #for $interface in $interfaces
    <li><a href="$interface.get_url()">$interface.name</a></li>
   #end for
   </ul>
diff --git a/doc/templates/interface.html b/doc/templates/interface.html
index 34b69e8..a3e2ccf 100644
--- a/doc/templates/interface.html
+++ b/doc/templates/interface.html
@@ -18,7 +18,7 @@
    #if $interface.methods
    <div id="methods" class="outset method">
     <h1>Methods</h1>
-    #for $method in $interface.methods.values()
+    #for $method in $interface.methods
     <div id="$method.name" class="inset method">
      <span class="permalink">(<a href="$method.get_url()">Permalink</a>)</span>
      <h2>$method.short_name ($method.get_in_args()) &#8594; $method.get_out_args()</h2>
@@ -26,7 +26,7 @@
      <div class="indent">
       <h3>Parameters</h3>
       #for $arg in $method.in_args
-       $arg.name<br/>
+       $arg.short_name<br/>
       #end for
      </div>
      #end if
@@ -34,7 +34,7 @@
      <div class="indent">
       <h3>Returns</h3>
       #for $arg in $method.out_args
-       $arg.name<br/>
+       $arg.short_name<br/>
       #end for
      </div>
      $method.get_docstring()
@@ -47,7 +47,7 @@
    #if $interface.signals
    <div id="signals" class="outset signal">
     <h1>Signals</h1>
-    #for $signal in $interface.signals.values()
+    #for $signal in $interface.signals
     <div id="$signal.name" class="inset signal">
      <span class="permalink">(<a href="$signal.get_url()">Permalink</a>)</span>
      <h2>$signal.short_name ($signal.get_args())</h2>
@@ -55,7 +55,7 @@
      <div class="indent">
       <h3>Parameters</h3>
       #for $arg in $signal.args
-       $arg.name<br/>
+       $arg.short_name<br/>
       #end for
      </div>
      #end if
@@ -68,7 +68,7 @@
    #if $interface.properties
    <div id="properties" class="outset property">
     <h1>Properties</h1>
-    #for $property in $interface.properties.values()
+    #for $property in $interface.properties
     <div id="$property.name" class="inset property">
      <span class="permalink">(<a href="$property.get_url()">Permalink</a>)</span>
      <h2>$property.short_name</h2>
diff --git a/tools/doc-generator.py b/tools/doc-generator.py
index 4a28ae5..bc04815 100755
--- a/tools/doc-generator.py
+++ b/tools/doc-generator.py
@@ -34,7 +34,7 @@ spec = specparser.parse (sys.argv[1])
 namespace = {}
 template_def = load_template ('interface.html')
 t = Template (template_def, namespaces = [namespace])
-for interface in spec.interfaces.values ():
+for interface in spec.interfaces:
     namespace['interface'] = interface
     
     # open the output file
diff --git a/tools/specparser.py b/tools/specparser.py
index 82cc012..81f7f09 100644
--- a/tools/specparser.py
+++ b/tools/specparser.py
@@ -74,11 +74,12 @@ class base (object):
                 n.setAttribute ('class', 'rationale')
 
             # rewrite <tp:member-ref>
+            spec = self.get_spec ()
             interface = self.get_interface ()
             for n in node.getElementsByTagNameNS (XMLNS_TP, 'member-ref'):
                 key = getText (n)
                 try:
-                    o = interface.get_ref (key)
+                    o = spec.lookup (key, namespace = interface.name)
                 except KeyError:
                     print >> sys.stderr, \
                         "Key `%s' not known in interface `%s'" % (
@@ -88,27 +89,18 @@ class base (object):
                 n.tagName = 'a'
                 n.namespaceURI = None
                 n.setAttribute ('href', o.get_url ())
-                n.setAttribute ('title', o.name)
+                n.setAttribute ('title', o.get_title ())
 
             # rewrite <tp:dbus-ref>
-            spec = self.get_spec ()
             for n in node.getElementsByTagNameNS (XMLNS_TP, 'dbus-ref'):
-                key = n.getAttribute ('namespace')
-                try:
-                    interface = spec.interfaces[key]
-                except KeyError:
-                    print >> sys.stderr, \
-                        "Interface `%s' not known in spec (%s: %s)" % (
-                            key, self.name, n.toxml ())
-                    continue
-                
+                namespace = n.getAttribute ('namespace')
                 key = getText (n)
                 try:
-                    o = interface.get_ref (key)
+                    o = spec.lookup (key, namespace = namespace)
                 except KeyError:
                     print >> sys.stderr, \
-                        "Key `%s' not known in interface `%s'" % (
-                            key, interface.name)
+                        "Key `%s' not known in namespace `%s'" % (
+                            key, namespace)
                     continue
 
                 n.tagName = 'a'
@@ -128,7 +120,7 @@ class Method (base):
     def __init__ (self, parent, namespace, dom):
         super (Method, self).__init__ (parent, namespace, dom)
 
-        args = map (lambda n: Arg (self, None, n),
+        args = build_list (self, Arg, namespace,
                          dom.getElementsByTagName ('arg'))
 
         # separate arguments as input and output arguments
@@ -194,7 +186,7 @@ class Arg (base):
                                     direction, self.parent))
 
     def spec_name (self):
-        return '%s: %s' % (self.dbus_type, self.name)
+        return '%s: %s' % (self.dbus_type, self.short_name)
 
     def __repr__ (self):
         return '%s(%s:%s)' % (self.__class__.__name__, self.name, self.dbus_type)
@@ -203,7 +195,7 @@ class Signal (base):
     def __init__ (self, parent, namespace, dom):
         super (Signal, self).__init__ (parent, namespace, dom)
 
-        self.args = map (lambda n: Arg (self, None, n),
+        self.args = build_list (self, Arg, namespace,
                          dom.getElementsByTagName ('arg'))
     
     def get_args (self):
@@ -214,13 +206,13 @@ class Interface (base):
         super (Interface, self).__init__ (parent, namespace, dom)
 
         # build a dictionary of methods in this interface
-        self.methods = build_dict (self, Method, self.name,
+        self.methods = build_list (self, Method, self.name,
                                    dom.getElementsByTagName ('method'))
         # build a dictionary of properties in this interface
-        self.properties = build_dict (self, Property, self.name,
+        self.properties = build_list (self, Property, self.name,
                                       dom.getElementsByTagName ('property'))
         # build a dictionary of signals in this interface
-        self.signals = build_dict (self, Signal, self.name,
+        self.signals = build_list (self, Signal, self.name,
                                    dom.getElementsByTagName ('signal'))
 
         # print '-'*78
@@ -234,17 +226,6 @@ class Interface (base):
     def get_url (self):
         return "%s.html" % self.name
 
-    def get_ref (self, name):
-        key = build_name (self.name, name)
-        if key in self.methods:
-            return self.methods[key]
-        elif key in self.signals:
-            return self.signals[key]
-        elif key in self.properties:
-            return self.properties[key]
-        else:
-            raise KeyError (name)
-
 class Error (base): pass
 
 class DBusType (base):
@@ -276,12 +257,28 @@ class Spec (object):
         # types with an Interface
         self.types = parse_types (self, dom)
         # build a dictionary of interfaces in this spec
-        self.interfaces = build_dict (self, Interface, None,
+        self.interfaces = build_list (self, Interface, None,
                                  dom.getElementsByTagName ('interface'))
 
+        # build a giant dictionary of everything
+        self.everything = {}
+        for interface in self.interfaces:
+                self.everything[interface.name] = interface
+
+                for method in interface.methods:
+                    self.everything[method.name] = method
+                for signal in interface.signals:
+                    self.everything[signal.name] = signal
+                for property in interface.properties:
+                    self.everything[property.name] = property
+
     def get_spec (self):
         return self
 
+    def lookup (self, name, namespace = None):
+        key = build_name (namespace, name)
+        return self.everything[key]
+
     def lookup_type (self, type_):
         if type_.endswith ('[]'):
             # FIXME: should this be wrapped in some sort of Array() class?
@@ -310,6 +307,9 @@ def build_dict (parent, type_, namespace, nodes):
 
     return dict (build_tuple (n) for n in nodes)
 
+def build_list (parent, type_, namespace, nodes):
+    return map (lambda node: type_ (parent, namespace, node), nodes)
+
 def parse_types (parent, dom, d = None):
     """Parse all of the types of type nodes mentioned in 't' from the node
        'dom' and insert them into the dictionary 'd'.
-- 
1.5.6.5




More information about the telepathy-commits mailing list