[Commits] r455 - website

commits at geoext.org commits at geoext.org
Fri Apr 24 01:59:05 CEST 2009


Author: tschaub
Date: 2009-04-24 01:59:05 +0200 (Fri, 24 Apr 2009)
New Revision: 455

Modified:
   website/jst.py
Log:
Code shuffle.  Deciding that label marker should always be jst:.  Syntax of the label itself determine semantics.

Modified: website/jst.py
===================================================================
--- website/jst.py	2009-04-23 22:21:07 UTC (rev 454)
+++ website/jst.py	2009-04-23 23:59:05 UTC (rev 455)
@@ -12,7 +12,10 @@
 COMMENT_RE = re.compile(r'^\s*\*?')
 ENDCOMMENT_RE = re.compile(r'^\s*\*/')
 DEF_RE = re.compile(r"\s*([\w\.]+)\s*=\s*(.*)$")
+INDENTED_RE = re.compile(r"^\s+\S")
 
+_marker = object()
+
 class DocParser(ConfigParser):
 
     def __init__(self, defaults=None):
@@ -21,12 +24,12 @@
     @classmethod
     def from_fn(cls, fn):
         """Load up config files in our parser."""
-        worker = cls()
+        parser = cls()
         if isinstance(fn, basestring):
             fn = fn,
-        fns = worker.read(fn)
+        fns = parser.read(fn)
         assert fns, ValueError("No valid config files: %s" % fns)
-        return worker
+        return parser
     
     key_list = () 
     keys = 'root', 
@@ -54,77 +57,94 @@
                 for filename in entries:
                     if filename.endswith(SUFFIX_JS) and not filename.startswith("."):
                         filepath = os.path.join(root, filename)[len(sourcedir)+1:]
-                        source = SourceFile(sourcedir, filepath)
-                        if source.data:
+                        jsfile = SourceFile(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(source.data)
+                            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.
     """
     def __init__(self, sourcedir, filepath):
-        source = open(os.path.join(sourcedir, filepath), "U").readlines()
-        self.data = {}
-        inblock = False
-        defs = False
-        name = None
-        block = None
-        got_indent = False
-        got_data = False
-        for line in source:
-            if not inblock:
-                m = JST_RE.match(line)
-                if m:
-                    inblock = True
-                    name = m.group(1)
-                    defs = (name == "defs")
-                    block = []
-                    spaces = 0
-                    got_indent = False
-            else:
-                m = ENDCOMMENT_RE.match(line)
-                if m:
-                    inblock = False
-                    got_data = True
-                    if defs:
+        self.source = open(os.path.join(sourcedir, filepath), "U").readlines()
+        self._data = _marker
+        self._jst_comments = _marker
+    
+    @property
+    def jst_comments(self):
+        if self._jst_comments == _marker:
+            comments = ()
+            inblock = False
+            label = None
+            block = None
+            got_indent = False
+            for line in self.source:
+                if not inblock:
+                    m = JST_RE.match(line)
+                    if m:
+                        inblock = True
+                        label = m.group(1)
+                        block = ()
+                        spaces = 0
+                        got_indent = False
+                else:
+                    m = ENDCOMMENT_RE.match(line)
+                    if m:
+                        inblock = False
+                        comments += (dict(label=label, block=block),)
+                    else:
+                        line = COMMENT_RE.sub("", line)
+                        if not got_indent:
+                            if INDENTED_RE.match(line):
+                                spaces = len(line) - len(line.lstrip())
+                                got_indent = True
+                        if len(line) > spaces:
+                            line = line[spaces:]
+                        block += (line,)
+            self._jst_comments = comments
+        return self._jst_comments
+    
+    @property
+    def data(self):
+        if self._data == _marker:
+            data = {}
+            for comment in self.jst_comments:
+                label = comment["label"]
+                block = comment["block"]
+                if label.startswith("("):
+                    if label == "(defs)":
                         for defline in block:
                             m = DEF_RE.match(defline)
                             if m:
-                                self.data[m.group(1)] = m.group(2)
-                    else:
-                        block = "".join(block)
-                        if name[-2:] == "[]":
-                            short = name[:-2]
-                            if self.data.has_key(short):
-                                if type(self.data[short]) is type([]):
-                                    self.data[short] += [block]
-                                else:
-                                    self.data[short] = [self.data[short], block]
+                                data[m.group(1)] = m.group(2)
+                else:
+                    block = "".join(block)
+                    if label[-2:] == "[]":
+                        short = label[:-2]
+                        if data.has_key(short):
+                            if type(data[short]) is type([]):
+                                data[short] += [block]
                             else:
-                                self.data[short] = [block]
+                                data[short] = [data[short], block]
                         else:
-                            self.data[name] = block
-                else:
-                    line = COMMENT_RE.sub("", line)
-                    if not got_indent:
-                        if re.compile(r'.*\S').match(line):
-                            spaces = len(line) - len(line.lstrip())
-                            got_indent = True
-                    if len(line) > spaces:
-                        line = line[spaces:]
-                    block += (line,)
-                        
-        if not got_data:
-            self.data = None
+                            data[short] = [block]
+                    else:
+                        data[label] = block
+            if len(self.jst_comments) > 0:
+                self._data = data
+            else:
+                self._data = None
+        return self._data
 
 
 



More information about the Commits mailing list