]> git.cworth.org Git - apitrace/blobdiff - specs/stdapi.py
specs: Allow an API to spread across multiple modules.
[apitrace] / specs / stdapi.py
index 846227ef3468fd2f06956476dc29d10bfd27c2b2..1097347f38ce3a97a26b9312c7de183c469e8c0f 100644 (file)
@@ -837,11 +837,8 @@ class Collector(Traverser):
 
 
 
-class API:
-    '''API abstraction.
-
-    Essentially, a collection of types, functions, and interfaces.
-    '''
+class Module:
+    '''A collection of functions.'''
 
     def __init__(self, name = None):
         self.name = name
@@ -849,50 +846,73 @@ class API:
         self.functions = []
         self.interfaces = []
 
+    def addFunctions(self, functions):
+        self.functions.extend(functions)
+
+    def addInterfaces(self, interfaces):
+        self.interfaces.extend(interfaces)
+
+    def mergeModule(self, module):
+        self.headers.extend(module.headers)
+        self.functions.extend(module.functions)
+        self.interfaces.extend(module.interfaces)
+
+    def getFunctionByName(self, name):
+        for function in self.functions:
+            if function.name == name:
+                return function
+        return None
+
+
+class API:
+    '''API abstraction.
+
+    Essentially, a collection of types, functions, and interfaces.
+    '''
+
+    def __init__(self, modules = None):
+        self.modules = []
+        if modules is not None:
+            self.modules.extend(modules)
+
     def getAllTypes(self):
         collector = Collector()
-        for function in self.functions:
-            for arg in function.args:
-                collector.visit(arg.type)
-            collector.visit(function.type)
-        for interface in self.interfaces:
-            collector.visit(interface)
-            for method in interface.iterMethods():
-                for arg in method.args:
+        for module in self.modules:
+            for function in module.functions:
+                for arg in function.args:
                     collector.visit(arg.type)
-                collector.visit(method.type)
+                collector.visit(function.type)
+            for interface in module.interfaces:
+                collector.visit(interface)
+                for method in interface.iterMethods():
+                    for arg in method.args:
+                        collector.visit(arg.type)
+                    collector.visit(method.type)
         return collector.types
 
+    def getAllFunctions(self):
+        functions = []
+        for module in self.modules:
+            functions.extend(module.functions)
+        return functions
+
     def getAllInterfaces(self):
         types = self.getAllTypes()
         interfaces = [type for type in types if isinstance(type, Interface)]
-        for interface in self.interfaces:
-            if interface not in interfaces:
-                interfaces.append(interface)
+        for module in self.modules:
+            for interface in module.interfaces:
+                if interface not in interfaces:
+                    interfaces.append(interface)
         return interfaces
 
-    def addFunction(self, function):
-        self.functions.append(function)
-
-    def addFunctions(self, functions):
-        for function in functions:
-            self.addFunction(function)
-
-    def addInterface(self, interface):
-        self.interfaces.append(interface)
-
-    def addInterfaces(self, interfaces):
-        self.interfaces.extend(interfaces)
-
-    def addApi(self, api):
-        self.headers.extend(api.headers)
-        self.addFunctions(api.functions)
-        self.addInterfaces(api.interfaces)
+    def addModule(self, module):
+        self.modules.append(module)
 
     def getFunctionByName(self, name):
-        for function in self.functions:
-            if function.name == name:
-                return function
+        for module in self.modules:
+            for function in module.functions:
+                if function.name == name:
+                    return function
         return None