[Commits] r483 - website

commits at geoext.org commits at geoext.org
Mon Apr 27 21:19:01 CEST 2009


Author: tschaub
Date: 2009-04-27 21:19:01 +0200 (Mon, 27 Apr 2009)
New Revision: 483

Modified:
   website/jst.py
Log:
support inheritance

Modified: website/jst.py
===================================================================
--- website/jst.py	2009-04-27 18:48:25 UTC (rev 482)
+++ website/jst.py	2009-04-27 19:19:01 UTC (rev 483)
@@ -3,6 +3,7 @@
 import sys
 from ConfigParser import ConfigParser
 from jinja2 import Template
+from jstools import tsort
 
 SUFFIX_JS = ".js"
 SUFFIX_JST = ".jst"
@@ -47,31 +48,52 @@
     
     def run(self):
         sections = self.sections()
-        newfiles = []
+        
         for section in sections:
             cfg = self.make_cfg(section)
             print("Extracting docs for %s" % section)
             sourcedir = cfg['root']
             outdir = cfg['output']
-            # assemble all files in source directory according to config
+            # gather data for each source file
+            files = {}
             for root, dirs, entries in os.walk(sourcedir):
                 for filename in entries:
                     if filename.endswith(SUFFIX_JS) and not filename.startswith("."):
                         filepath = os.path.join(root, filename)[len(sourcedir)+1:]
                         jsfile = SourceFile.from_filename(os.path.join(sourcedir, filepath))
                         if jsfile.data:
-                            template_filename = os.path.join(sourcedir, filepath.split(SUFFIX_JS)[0] + SUFFIX_JST)
-                            if not os.path.exists(template_filename):
-                                # throw something if template not given in config here
-                                template_filename = cfg["template"]
-                            template = Template(open(template_filename, "U").read())
-                            out = template.render(jsfile.data)
-                            output_filename = os.path.join(outdir, filepath.split(SUFFIX_JS)[0] + SUFFIX_RST)
-                            f = open(output_filename, "w")
-                            f.write(out)
-                            f.close()
+                            files[filepath] = jsfile
+        
+            # create list of dependencies
+            dependencies = {}
+            for filepath, jsfile in files.items():
+                dependencies[filepath] = jsfile.extends
+            
+            # get tuple of files ordered by dependency
+            order = tsort.sort(dependencies)
 
+            # extend data with any data from parents
+            #import pdb; pdb.set_trace()
+            for filepath in order:
+                jsfile = files[filepath]
+                if jsfile.extends:
+                    for parentpath in sorted(jsfile.extends, reverse=True):
+                        if files.has_key(parentpath):
+                            jsfile.apply_defaults(files[parentpath].data)
+                    
+                # parse template for each file
+                template_filename = os.path.join(sourcedir, filepath.split(SUFFIX_JS)[0] + SUFFIX_JST)
+                if not os.path.exists(template_filename):
+                    # throw something if template not given in config here
+                    template_filename = cfg["template"]
+                template = Template(open(template_filename, "U").read())
+                out = template.render(jsfile.data)
+                output_filename = os.path.join(outdir, filepath.split(SUFFIX_JS)[0] + SUFFIX_RST)
+                f = open(output_filename, "w")
+                f.write(out)
+                f.close()
 
+
 class SourceFile(object):
     """
     Represents a JavaScript source code file.
@@ -81,6 +103,7 @@
         self._data = _marker
         self._comments = _marker
         self._context = _marker
+        self.extends = []
     
     @classmethod
     def from_filename(cls, filename):
@@ -136,6 +159,8 @@
                             m = DEF_RE.match(defline)
                             if m:
                                 data[m.group(1)] = m.group(2)
+                    elif label == "(extends)":
+                        self.extends = [path.strip() for path in block if path.strip()]
                 else:
                     block = "".join(block)
                     m = BRACKET_RE.match(label)
@@ -160,6 +185,30 @@
                 self._data = None
         return self._data
     
+    def apply_defaults(self, data):
+        if self._data.has_key("_parents"):
+            self.data["_parents"].insert(0, data)
+        else:
+            self.data["_parents"] = [data]
+        for key, value in data.items():
+            if isinstance(value, list):
+                # concatenate with any existing list
+                if self._data.has_key(key):
+                    self._data[key] += value
+                else:
+                    self._data[key] = value[:]
+            elif isinstance(value, dict):
+                # acquire values for keys we don't have already
+                if self._data.has_key(key):
+                    for k, v in value.items():
+                        if not self._data[key].has_key(k):
+                            self._data[key][k] = v
+                else:
+                    self._data[key] = value.copy()
+            else:
+                if not self._data.has_key(key):
+                    self._data[key] = value
+    
 
 
 



More information about the Commits mailing list